var mooPopUp = {
	
	start: function(){
		this.mLinks = $$('a.moopopup');
		this.mPops = [];
		//this.overlay = new Element('div', {'id': 'mpOverlay'}).injectInside(document.body);
		this.container = new Element('div', {'id': 'mpContainer'}).injectInside(document.body);
		
		$each(this.mLinks, function(el, i){

			el.addEvent('click', function(e){
				e = new Event(e);
				
				p = new Pop('mpCenter' + i, el);
				
				e.stop();
			});
		});
	}
};

var Pop = new Class ({
	initialize: function(id, el, options)
	{
		//this.open = false;
		this.id = id;
		this.rel=el.rel;
		this.options = $extend({
			initialWidth: 600,
			initialHeight: 400,
			showTop:true,
			activeOver:true 
		}, options || {});
		
		var aDim = this.rel.match(/[0-9]+/g);
		this.options.initialWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.initialWidth;
		this.options.initialHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.initialHeight;
		
		// still open		
		if($(this.id)) {
			this.center = $(id);
			this.center.setStyle('height', '0');
							
			slidefx = new Fx.Style(this.center, 'height', {
				duration: 800,
				transition: Fx.Transitions.backOut,
				wait: false
			}).start(this.options.initialHeight) ;
		}
		else {
				
			//this.center = new Element('div', {'id': id, 'class': 'mpCenter',
			//'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
			//new Element('div', {'id': id + 'Inside','class': 'mpInside'}).injectInside(center);	
			this.center = new Element('div', {'id': id, 'class': 'mpCenter',
			'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight,
			'left': (window.getWidth() - this.options.initialWidth)/2 ,
			'top': (window.getHeight() - this.options.initialHeight)/2 , 'display': 'none'}}).injectInside(document.body);
			
			if(this.options.showTop)
			{
				var top = new Element('div', {'id': id + 'Top','class': 'mpTop'}).injectInside(this.center);
				new Element('a', {'id': id + 'Close', 'href': '#', 'class': 'mpClose'}).injectInside(top).onclick = this.close.bind(this);
				new Element('a', {'id': id + 'Hide', 'href': '#', 'class': 'mpHide'}).injectInside(top).onclick = this.hide.bind(this);
				if(el.title) new Element('p', {'class': 'mpTitle'}).injectInside(top).setHTML(el.title);
			
			}
			else document.body.onmousedown = this.close.bind(this.center);
			/*
			var scroll = new Fx.Scroll(id, {
			wait: false,
			duration: 2500,
			transition: Fx.Transitions.Quad.easeInOut
			});
			new Element('a', {'id': id + 'Down', 'href': '#', 'class': 'mpDown'}).injectInside(this.center).setHTML("en bas").onclick = function () { scroll.toBottom; return false};
			*/
			new Element('div', {'id': id + 'Inside','class': 'mpInside'}).injectInside(this.center);	
						
			
			new Ajax(el.href, {
			method: 'get',
			update: $(id + 'Inside'),
			onComplete: function() {
				this.center.setStyle('opacity', '0.2');
				this.center.setStyle('display', 'block');
						//alert ("Lien " + i + "\n" + url);
					
				slidefx = new Fx.Style(this.center, 'opacity', {
					duration: 2000,
					transition: Fx.Transitions.backOut,
					wait: false
				}).start(1) ;
				new Drag.Move(id);
				
			}.bind(this)
			
			}).request();
			
			//alert(this.center);
			this.center.addEvent(this.activeOver ? 'mouseenter' : 'mousedown', function()
			{
				//alert(this + "\n" + this.id);
				$each(mooPopUp.mLinks, function(el, i) {
					var openPop = $('mpCenter' + i);
					if(openPop) {
						//alert("Open : " + openPop.id + "\n"
						//+ "Call : " + id );
						if(openPop.id == id) {
							//this.id = id ;
							openPop.setStyle('zIndex', '15');
							this.center = $(id);
							//this.options
						}
						else openPop.setStyle('zIndex', '10');			
					}
				}.bind(this)); 
			}.bind(this));
			/*
			this.center.addEvent('onmouseenter', function ()
			{
				$each(mooPopUp.mLinks, function(el, i) {
					var openPop = $('mpCenter' + i);
					if(openPop) {
						alert("Open : " + openPop.id + "\n"
						+ "Call : " + id );
						if(openPop.id == id) openPop.setStyle('zIndex', '110');
						else openPop.setStyle('zIndex', '100');			
					}
				});
			});
			*/
			
			//this.open = true;
			return false;
		}
	},
	
	/*
	these functions are working with this as Element Pop.center
	*/
	hide: function(){
		// this refer to idClose injected inside Pop.center.id so...
		//this.getParent().remove();
		var top = $(this.id + 'Top');		
	
		var toH=(this.center.getStyle('height') == top.getStyle('height'))
		?parseInt(this.options.initialHeight)
		:parseInt(top.getStyle('height'));
					
		slidefx = new Fx.Style(this.center, 'height', {
			duration: 400,
			transition: Fx.Transitions.linear,
			wait: false
		}).start(toH) ;		
				
		//this.remove();
		return false;
	},
	
	close: function(){
		// this refer to idClose injected inside Pop.center.id so...
		//this.getParent().remove();
		//alert(this.id);
		this.center.remove();
		this.empty;
		return false;
	}

});

window.addEvent('domready', mooPopUp.start.bind(mooPopUp));
