var FormContact = new Class({
	initialize : function(){
		this.divFullContact = $('fairepart-publier');
		this.divContact = $('contactsAuto');
		this.divList = $('contacts').getElement('div.list');
		this.divWait = $('contacts').getElement('div.waiting');
		this.options = $('options');
		this.divWait.addClass('dNone');
		this.nobs = $('nobs');

		
		$('email_publieur').addEvent('keyup', function(){
			this.simpleMailParse();
		}.bind(this));
		$('email_publieur').addEvent('blur', function(){
			this.simpleMailParse();
		}.bind(this));
		
		this.reposition();
		
		this.form = $('formContact');
		this.form.set('send', {
			onRequest : function(){
				this.divContact.addClass('dNone');
				this.divList.addClass('dNone');
				this.divWait.removeClass('dNone');
				this.form.getElements('p.error').each(function(p){
					p.innerHTML = "";
				});
			}.bind(this),
			onComplete : function(response){
				this.divList.removeClass('dNone');
				
				this.divWait.addClass('dNone');
				
			}.bind(this),
			onFailure : function(response){
				this.divContact.removeClass('dNone');
				var errors = JSON.decode(response.responseText);
				// fonction de traitement
				$each(errors, function(item, index){
					if ($(index+'_error')){
						$(index+'_error').innerHTML = item;
					} else {
						$('publieur_error').innerHTML = item;
					}
				}.bind(this));
			}.bind(this),
			onSuccess : function(response){
				this.divList.style.height = "285px";
				this.options.removeClass('dNone');
				var msg = JSON.decode(response);
				contentHtml =
					"<div class='itemMail'><p class='bolder'><input type='checkbox' id='checkAll' />" + 
					"<label for='checkAll'>Tout cocher / Tout décocher</label></p></div>" ;
				var classe = "";
				$each(msg, function(item, index){
					
					if (index%2 == 0){
						contentHtml = contentHtml + "<div class='itemMail'>";
					}
					
					var nom = "";
					if (item['name'] == ""){
						nom = item['email'];
					} else {
						nom = item['name'];
					} 
					if (index%2 != 0){
						classe = " secondOne";
					} else {
						classe = "";
					}
					contentHtml = contentHtml + 
					"<p class='"+classe+"'><input class='emailDest' type='checkbox' value='" + item['email'] + "' id='email_destinataire_" + index + "' name='email_destinataire[]' />" +
					"<label for='email_destinataire_" + index + "'>" + nom + "</label></p>";
					
					if (index%2 != 0){
						contentHtml = contentHtml + "</div>";
					} 
				}.bind(this));
				
				// fonction de traitement
				//this.ccool();
				this.divList.innerHTML = contentHtml;
				$('checkAll').addEvent('click',function(e){
					$$('.emailDest').each(function (eltCheck){
						eltCheck.checked = $('checkAll').checked;
					});
				});
				
			}.bind(this)
		});
		this.button = this.form.getElement('a.submit');
		if(this.button){
			this.button.removeEvents();
			this.button.addEvent('click', function(e){
				e.stop();
				//alert(this.form.id);
				this.form.send();
			}.bind(this));
		}	
		this.form.removeEvents();
		this.form.addEvent('submit', function(e){
			e.stop();
			this.form.send();
		}.bind(this));
	},
	reposition : function(){
		if(this.nobs){
			this.coords = this.divList.getCoordinates(this.nobs);
			this.coordsMain = this.divFullContact.getCoordinates(this.nobs);
		}else{
			this.coords = this.divList.getCoordinates();
			this.coordsMain = this.divFullContact.getCoordinates();
		}
		
		if(Browser.Engine.trident){
			var topTop = (this.coords.top - 1) +'px';		
		} else {
			var topTop = (this.coords.top - this.coordsMain.top - 1) +'px';
		}
		
		this.divContact.setStyles({
			'left': '21px',
			'top': topTop
		});
		this.divWait.setStyles({
			'left': '21px',
			'top': topTop
		});
		
	},
	simpleMailParse : function(){
		var mailTip = $('email_publieur').get('value');
		if(mailTip.contains('@aol')){
			$('registrar_publieur').set('value', "AOL");
			return;
		}
		if(mailTip.contains('@live')){
			$('registrar_publieur').set('value', "LIVE");
			return;
		}
		if(mailTip.contains('@msn')){
			$('registrar_publieur').set('value', "MSN");
			return;
		}
		if(mailTip.contains('@hotmail')){
			$('registrar_publieur').set('value', "Hotmail");
			return;
		}
		if(mailTip.contains('@yahoo')){
			$('registrar_publieur').set('value', "Yahoo");
			return;
		}
		if(mailTip.contains('@gmail')){
			$('registrar_publieur').set('value', "Gmail");
			return;
		}
		/*if(mailTip.contains('@lycos')){
			$('registrar_publieur').set('value', "Lycos");
			return;
		}*/
		if(mailTip.contains('@mac')){
			$('registrar_publieur').set('value', "Mac Mail");
			return;
		}
		if(mailTip.contains('@me')){
			$('registrar_publieur').set('value', "Mac Mail");
			return;
		}
	}
});
FormContact.implement(new Events);


var FormPublication = new Class({
	initialize : function(){
		if(navigator.appName != "Microsoft Internet Explorer"){
			var mySlide = new Fx.Slide('fairepart-publier');
		}
		
		this.contactManuel = $('contacts').getElement('div.manuel');
		this.contactRecup = $('contactsAuto');
		this.contactRecup2 = $('contacts').getElement('div.auto');
		this.contactRecup3 = $('contacts').getElement('div.list');
		this.options = $('options');
		
		this.form = $('formPublication');
		this.button = this.form.getElement('a.submit');
		
		this.linkRecup = $('method').getElement('a.auto');
		this.linkManuel = $('method').getElement('a.manuel');
		
		
		this.linkRecup.addEvent('click',function(e){
			//e.stop();
			this.linkRecup.addClass("current");
			this.linkManuel.removeClass("current");
			this.contactManuel.addClass("dNone");
			this.contactRecup.removeClass("dNone");
			this.contactRecup2.removeClass("dNone");
			this.contactRecup3.removeClass("dNone");
			this.contactRecup3.set("text","");
			this.contactRecup3.style.height = "320px";
			this.options.addClass("dNone");
			this.form.getElements('p.error').each(function(p){
				p.innerHTML = "";
			});
		}.bind(this));
		this.linkManuel.addEvent('click',function(e){
			//e.stop();
			this.linkRecup.removeClass("current");
			this.linkManuel.addClass("current");
			this.contactManuel.removeClass("dNone");
			this.contactRecup.addClass("dNone");
			this.contactRecup2.addClass("dNone");
			this.contactRecup3.addClass("dNone");
			this.options.removeClass("dNone");
			this.form.getElements('p.error').each(function(p){
				p.innerHTML = "";
			});
			
		}.bind(this));
		this.cur = 1;
		
		
		
		this.form.set('send', {
			onRequest : function(){
				this.form.getElements('p.error').each(function(p){
					p.innerHTML = "";
				});
			}.bind(this),
			onComplete : function(response){
			},
			onFailure : function(response){
				var errors = JSON.decode(response.responseText);
				// fonction de traitement
				$each(errors, function(item, index){
					if ($(index+'_error')){
						$(index+'_error').innerHTML = item;
					} else {
						$('publication_error').innerHTML = item;
					}
				}.bind(this));
			}.bind(this),
			onSuccess : function(response){
				$('fairepart-publier_success').removeClass("dNone");
				this.contactRecup.addClass('dNone');
				if(navigator.appName != "Microsoft Internet Explorer"){
					mySlide.toggle();			
				} else {
					$('fairepart-publier').addClass('dNone');
				}
			}.bind(this)
		});
		
		if(this.button){
			this.button.removeEvents();
			this.button.addEvent('click', function(e){
				e.stop();
				//alert(this.form.id);
				this.form.send();
			}.bind(this));
		}	
		this.form.removeEvents();
		this.form.addEvent('submit', function(e){
			e.stop();
			this.form.send();
		}.bind(this));
	}
});
FormPublication.implement(new Events);