var clrz_lightbox = new Class({

	options: {
		closeText:"Fermer",
		containerW:540,
		containerH:415,
		link: '',
		rel: '',
		url: 'wp-content/themes/silences/libraries/js/',
		destruct: false
	},
	
	initialize :function(options){
		
		this.setOptions(options);
			
		this.centerW = (window.getHeight()/2)+window.getScrollTop();
		this.containerCenter = (window.getWidth().toInt()/2)-(this.options.containerW/2);
		
		if(this.options.destruct == true)
		{
			this.preDestruct();
			this.divover = $('divover');
			
		}
		else
		{
			this.divover = new Element('div', {'id':'divover','styles': {'cursor':'pointer','display': 'block', 'background-color': 'black','position':'absolute','top':'0px','left':'0px','z-index':1000,'width':window.getWidth(),'margin-top':this.centerW,'opacity':0 }}).inject(document.body,'top');
		}
			

		this.divover_fx = new Fx.Morph(this.divover, {duration: 600, wait: false,transition: Fx.Transitions.Quart.easeInOut});
		
		this.divcontainer = new Element('div', {'id': 'container'+this.options.rel,'class':'clrz_divover','styles': {'z-index':1000,'background':'white','overflow':'hidden','display': 'block', 'position':'absolute','top':(window.getScrollTop()+80),'left':this.containerCenter,'width':this.options.containerW,'height':this.options.containerH,'opacity':0 }});
			
		this.divcontainer_fx = new Fx.Morph(this.divcontainer, {duration: 600, wait: false,transition: Fx.Transitions.Quart.easeInOut});
		
		this.divcontainer.inject(this.divover,'after');

		this.setup();
	},
	
	preDestruct: function(){
	
		var divovers = $$('.clrz_divover');
		
		divovers.each(function(el,i){
		
			el.setStyle('display','none');
		
		});
	
	},
	
	initClosing: function(){
	
		var myclass = this;
			
		this.divover.addEvent("click",function(e){	
			e = new Event(e).stop();
			myclass.destruct();	
		});
		
		$('close').addEvent("click",function(e){
			e = new Event(e).stop();	
			myclass.destruct();	
		});
	},
	
	setup:function(){
	
		var myclass = this;
		
		myclass.letsmoove();

		var myHTMLRequest = new Request.HTML({url : this.options.link+'&ajax=true',update:myclass.divcontainer,onComplete: function(){
			
			var url = '/'+myclass.options.url+'asset.'+myclass.options.rel+'.js';
			myAsset = Asset.javascript(url);
			
			myclass.initClosing();
		
			if($('user_email') && myclass.options.rel == 'connexion')
				$('user_email').addEvents({
				'focus': function(e){ if(this.get('value')=='Login') this.setProperty('value','') },
				'blur': function(e){ if(this.get('value')=='') this.setProperty('value','Login') }
				});
			
			if($('motdepasse') && myclass.options.rel == 'connexion')	
				$('motdepasse').addEvents({
				'focus': function(e){ if(this.get('value')=='pass') this.setProperty('value','') },
				'blur': function(e){ if(this.get('value')=='') this.setProperty('value','pass') }
			});

		}}).get();
				
	},
		
	letsmoove : function(){
	
		var myclass = this;
		
		this.divover.setStyle('display','block');
		
		if(myclass.options.destruct == false)
			this.divcontainer.setStyle('display','block');
				
		if(this.options.destruct != true)
		{	
			this.divover_fx.set({'height':window.getScrollHeight(),'margin-top':0,'margin-left':0});	
		

			this.divover_fx.start({'opacity':[0,.8],'width':[0,window.getWidth().toInt()]}).chain(function()
			{

				myclass.divcontainer_fx.start({'opacity':1,'left':[0,myclass.containerCenter],'margin-top':0}).chain(function(){
				myclass.fireEvent('onMoveComplete'); 
			
				});
			
			});
		}
		else
		{
				myclass.divcontainer_fx.start({'opacity':[0,1],'left':[0,myclass.containerCenter],'margin-top':0}).chain(function(){
				myclass.fireEvent('onMoveComplete'); 
			
				});
		}

	
	},
	
	destruct : function(){
		var myclass = this;
		
		var btnClose = $('close');
		 
		new Fx.Morph(btnClose, {duration: 600, wait: false,transition: Fx.Transitions.Quart.easeInOut}).start().chain(function()
		{
			myclass.divcontainer_fx.start({'opacity':0,'left':window.getWidth().toInt()}).chain(function()
			{
				myclass.divover_fx.start({'width':0,'opacity':0,'margin-left':window.getWidth().toInt()}).chain(function(){ myclass.elreset(); });
			});
		});	
	
	},
	
	elreset : function(){
		
		this.divover.setStyle('display','none');
		this.divcontainer.setStyle('display','none');
		this.divcontainer.set('html','');
	}

});
clrz_lightbox.implement(new Events, new Options);