var Overlay = new Class({
	
	getOptions: function(){
		return {
			//opacity: 0.7,
			zIndex: 1,
			container: document.body,
			background:'#000000',
			onClick: Class.empty
		};
	},

	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		
		this.options.container = $(this.options.container);
		
		this.overlay = new Element('div').setProperty('id', 'Overlay').setStyles({
			position: 'absolute',
			left: '0px',
			top: '66px',
			width: '100%',
			background:'transparent url(http://front.creasenso.fr/layout/img/transparent.png) repeat scroll left bottom',
			zIndex: this.options.zIndex
		}).injectInside(this.options.container);
		
		this.overlay.addEvent('click', function(){
			this.options.onClick();
		}.bind(this));
		
		//this.fade = new Fx.Style(this.overlay, 'opacity').set(0);
		this.position();
		
		window.addEvent('resize', this.position.bind(this));
	},
	
	position: function(){ 
		if(this.options.container == document.body){ 
			var h = window.getScrollHeight()-66+'px'; 
			this.overlay.setStyles({top: '66px', height: h}); 
			//$('screencast').setStyles({top: '66px', height: h}); 
			
		}else{ 
			var myCoords = this.options.container.getCoordinates(); 
			this.overlay.setStyles({
				top: myCoords.top+'px', 
				height: myCoords.height+'px', 
				left: myCoords.left+'px', 
				width: myCoords.width+'px'
			}); 
		} 
	},
	
	show: function(){
		//this.fade.start(0,this.options.opacity);
		$('Overlay').setStyle('visibility','visible');
		$('talents-box').setStyle('display','none');
		var scroll =new  Fx.Scroll(window,{duration:500,wait:'false'});
		scroll.scrollTo(0,0);
	},
	
	hide: function(){
		if($('stfpop')){$('stfpop').remove();}
		$('screencast').remove();
		$('Overlay').remove();
		$('talents-box').setStyle('display','block');
		scr = new Element('div' ,{id:'screencast'}).injectAfter('size');
		//this.fade.start(this.options.opacity,0);
	},
	
	sendtofriend:function(b){
		
		popw=400;
		poph=200;
		
		if($('stfpop')){
			var fxc = new Fx.Styles(stf, {duration: 1000, wait: false, onComplete: function(){stf.remove();}});
			fxc.start({'top':-poph+'px'});
		}
		else{
		w = $('sect-talents').getStyle('width');
		w=(parseInt(w) - popw) / 2;
		w=w.round();
		h= window.getHeight();
		h=parseInt(h) - 66;
		h=(h - poph) / 2;
		h=h.round();
		stf= new Element('div').setProperty('id', 'stfpop').setStyles({
			position: 'absolute',
			left: w+'px',
			top: -poph+'px',
			width: popw+'px',
			height:poph+'px',
			backgroundColor:'#141414',
			padding:'15px',
			zIndex: '99999'
		}).injectInside(this.options.container);
		
		clx = new Element('div',{'styles':{'position':'absolute','top':'0px','left':'419px','width':'9px','height':'12px','font-size':'12px','display':'block','background-color':'#272727','padding':'1px','cursor':'pointer'}});
		clx.setHTML('X');
		clx.addEvent('click',function(){
			var fxc = new Fx.Styles(stf, {duration: 1000, wait: false, onComplete: function(){stf.remove();}});
			fxc.start({'top':-poph+'px'});
		});
		clx.injectInside(stf);
		form = new Element('form',{'action':'/trt/sendfriend.php'});
		
		h1=new Element('h2',{'styles':{'font-size':'12px','margin-bottom':'7px'},'class':'mail-title'});
		p1 = new Element('p',{'class':'mail-field'});
		p2 = p1.clone();
		p3 = p1.clone();
		your_email = new Element('input',{'id':'your_email','name':'your_email','class':'fld_text','value':'Votre e-mail'});
		your_email.addEvents({
			'focus':function(){if(this.getProperty('value')=='Votre e-mail'){this.setProperty('value','');}},
			'blur':function(){if(this.getProperty('value')==''){this.setProperty('value','Votre e-mail');}}			
		});
		friends_email = new Element('input',{'id':'friends_email','name':'friends_email','class':'fld_text','value':'Son e-mail'});
		friends_email.addEvents({
			'focus':function(){if(this.getProperty('value')=='Son e-mail'){this.setProperty('value','');}},
			'blur':function(){if(this.getProperty('value')==''){this.setProperty('value','Son e-mail');}}
			
		});
		comment = new Element('textarea',{'styles':{'margin-right':'40px','height':'100px','width':'250px','font-family':'arial,sans-serif','font-size':'1em'},'id':'comment','name':'comment','class':'fld_text','value':'Commentaire'});
		comment.addEvents({
			'focus':function(){if(this.getProperty('value')=='Commentaire'){this.setProperty('value','');}},
			'blur':function(){if(this.getProperty('value')==''){this.setProperty('value','Commentaire');}}	
		});
		// Hidden with url
		var url = '/trt/url.php';
		var param='id_book='+b;
		
		button = new Element('input',{'styles':{'font-size':'12px'},'id':'submit','name':'submit','type':'submit','class':'btn','value':'ENVOYER'});
		form.injectInside(stf);
		h1.setHTML('ENVOYER CE BOOK A UN AMI');
		h1.injectInside(form);
		p1.injectInside(form);
		p2.injectInside(form);
		p3.injectInside(form);
		your_email.injectInside(p1);
		friends_email.injectInside(p2);
		comment.injectInside(p3);
		button.injectInside(p3);
		
		new Ajax(url,{method: 'post',data: param, onComplete :function(e){
			var sendfriend_url  = 'http://front.creasenso.fr/talents/'+e;
			hidden = new Element('input',{'id':'url','name':'url','type':'hidden','value':sendfriend_url});
			hidden.injectInside(p3);
		}}).request();	
		//
		
		var fx = new Fx.Styles(stf, {duration: 1000, wait: false});
		var fxc = new Fx.Styles(stf, {duration: 1000, wait: false, onComplete: function(){stf.remove();}});
		var fe_valid = 0;
		var ye_valid = 0;
		button.addEvent('click',function(){
			 at = friends_email.getProperty('value').indexOf('@'); dot = friends_email.getProperty('value').indexOf('.'); 
			 yat = your_email.getProperty('value').indexOf('@'); ydot = your_email.getProperty('value').indexOf('.'); 
			 if(at == -1 ||  dot == -1 || dot <= at + 1 || dot == 0 || dot == friends_email.getProperty('value').length - 1) {fe_valid=0;}
			 else{fe_valid=1;}
			 
			 if(yat == -1 ||  ydot == -1 || ydot <= at + 1 || ydot == 0 || ydot == your_email.getProperty('value').length - 1) {ye_valid=0;}
			 else{ye_valid=1;}
		});
	
		form.addEvent('submit', function(e) {
			new Event(e).stop();
			if((fe_valid==1)&&(ye_valid==1)){
			this.send({
			method: 'post',
			onComplete: function() {
				stf.empty();
				stf.setHTML('<p class="email_confirmation">Votre e-mail a bien été envoyé.<br/>Merci.</p>');
				dly = (function(){fxc.start({'top':-poph+'px'});}).delay(1500);
				}
			  });}
			  else{alert('Votre e-mail ou l\'e-mail de votre ami est invalide.');}
		});
		
		fx.start({'top': h+'px'});
	    }
	},
	
	bookmark:function(b){
		var url = '/trt/url.php';
		var param='id_book='+b;
		new Ajax(url,{method: 'post',data: param, onComplete :function(e){
			var addthis_url  = 'http://front.creasenso.fr/talents/'+e;
			alert(addthis_url);
			var addthis_title = document.title;
			var addthis_pub = 'zend';
			width=640; height=605;
			left   = (window.getWidth()  - width)/2;
 			top = (window.getHeight() - height)/2;
			newwindow=window.open('http://www.addthis.com/bookmark.php','name','height='+height+',width='+width+',left='+left+',top='+top+',scrollbars=1,resizable=yes,status=yes');
			newwindow.focus();		
		}}).request();	
	},
	recrut:function(b){
		newwindow=window.open('mailto:hello@creasenso.com','sendmail');
		newwindow.focus();
		newwindow.close();
	},
	download:function(b){
		newwindow=window.open('http://back.creasenso.fr/export/pdfbook.php?id_book='+b,'download');
		newwindow.focus();
		
	}
	
});
Overlay.implement(new Options);