// JavaScript Document
function sub_newsletter(lg){
	if(!lg) lg = 'fr';
	if($('container') != null){
			$('container').remove();
	}
	
	//valeur de l'email saisi
	var email = get_email();
	if (email == ''){
		afficher_reponse_email_vide(lg); return false;	
	}
	
	affichage_de_base_inscription_newsletter();
	var url='/pl/newsletter.pl';
	var get_html_sub_newsletter = new Ajax(url, {
		method: 'post',
		data: 'ac=sub&wfec_user_email='+email,
		onSuccess: function(){
			if (get_html_sub_newsletter.response.text == 'ok'){ //personne déjà inscrite
				afficher_reponse_ok(lg)
			}else{ // la personne doit s'inscrire
				afficher_reponse_form(lg, get_html_sub_newsletter.response.text)
			}
		},
		onFailure: echec
	}).request();
}
function afficher_reponse_ok(lg){
	affichage_de_base_inscription_newsletter();
	var url='/tmpl_dev_shopping/'+lg+'/newsletter/popup_newsletter_inscription_result.html';
	var get_html_result_sub_newsletter = new Ajax(url, {
		method: 'post',
		update: $('info_sub_newsletter'),
		onSuccess: function(){
			//////////// affichage layer //////////////////			
			$('info_sub_newsletter').innerHTML = get_html_result_sub_newsletter.response.text;
			$('loading').remove();			
			ajout_comportement();
			
			var myFx = new Fx.Style($('info_sub_newsletter'), 'opacity').start(0,1);
			/////////////////////////////////////////////////////
		},
		onFailure: echec_apres_inscription
	}).request();
}
function afficher_reponse_form(lg, response){
	//valeur de l'email saisi
	var email = get_email();
	
	if($('container') != null){
		$('container').remove();
	}
	if (window.ie && !window.ie7){
		$('container2').remove();
	}
	if($('loading') != null){
		$('loading').remove();
	}
	document.location = '/pl/newsletter.pl?wfec_user_email='+email;
}
function afficher_reponse_email_vide(lg){
	affichage_de_base_inscription_newsletter();
	var url='/tmpl_dev_shopping/'+lg+'/newsletter/popup_newsletter_inscription_result_email_vide.html';
	var get_html_result_sub_newsletter = new Ajax(url, {
		method: 'post',
		update: $('info_sub_newsletter'),
		onSuccess: function(){
			//////////// affichage layer //////////////////			
			$('info_sub_newsletter').innerHTML = get_html_result_sub_newsletter.response.text;
			$('loading').remove();			
			ajout_comportement();
			
			var myFx = new Fx.Style($('info_sub_newsletter'), 'opacity').start(0,1);
			/////////////////////////////////////////////////////
		},
		onFailure: echec_email_vide
	}).request();
}
function ajout_comportement(){
	if ($('fermer')){
		$('fermer').addEvent('click', function(){
										var container = $('container');
										//effet de fondu avant la fermeture
										new Fx.Style(container, 'opacity', {
											onComplete : function(){
												//on detruit la galeries
												container.remove();
												if (window.ie && !window.ie7){
													$('container2').remove();
												}	
											}
										}).start(1,0);});
	}
}
function echec(){
	if($('container') != null){
			$('container').remove();
	}
	if (window.ie && !window.ie7){
		$('container2').remove();
	}
	if($('loading') != null){
		$('loading').remove();
	}
	
	//feinte pour le problème des accents
	var txt = new Element('span');
	txt.innerHTML = 'Votre inscription à la newsletter n\'a pas pu être enregistrée.\n';
	//txt.injectTop(document.body);
	alert(txt.innerHTML);
}
function echec_apres_inscription(){
	if($('container') != null){
			$('container').remove();
	}
	if (window.ie && !window.ie7){
		$('container2').remove();
	}
	if($('loading') != null){
		$('loading').remove();
	}
	
	//feinte pour le problème des accents
	var txt = new Element('span');
	txt.innerHTML = 'Votre inscription a bien été prise en compte.\n';
	//txt.injectTop(document.body);
	alert(txt.innerHTML);
}
function echec_email_vide(){
	if($('container') != null){
			$('container').remove();
	}
	if (window.ie && !window.ie7){
		$('container2').remove();
	}
	if($('loading') != null){
		$('loading').remove();
	}
	
	//feinte pour le problème des accents
	var txt = new Element('span');
	txt.innerHTML = 'Veuillez entrer un email.\n';
	//txt.injectTop(document.body);
	alert(txt.innerHTML);
}
function affichage_de_base_inscription_newsletter(lg){
	if(!lg) lg = 'fr';
	if($('container') != null){
			$('container').remove();
	}
	
	//creation des elements
	container = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '100%',
											'height': '100%',
											'padding': '0px',
											'left': '0',
											'margin': '0px',
											'position': 'fixed',
											'top': '0',
											'text-align': 'center',
											'background-image': 'url(/images/overlay.png)',
											'z-index': '5'
										}
								  }
							);
	
	if (window.ie && !window.ie7){
		container.style.position = 'absolute';
		container.style.height = document.documentElement.clientHeight;
		container.style.background = 'none';
		container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='/images/overlay.png')";
		container.style.zIndex = '0';
		
		container2 = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '100%',
											'height': '100%',
											'padding': '0px',
											'left': '0',
											'margin': '0px',
											'position': 'absolute',
											'top': '0',
											'text-align': 'center',
											'z-index': '10'
										}
								  }
							);
		//pour ie6 (pour avoir les liens cliquables
		container2.setAttribute('id','container2');
		container2.injectInside(document.body);
	}
	
	container.setAttribute('id','container');
	container.injectInside(document.body);
	
	//afficher barre de chargement
	loading = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '220px',
											'height': '19px',
											'padding': '0px',
											'text-align': 'left',
											'margin':'0 auto',
											'background-image': 'url(/images/smoothgallery/loading-bar-black.gif)',
											'top' : '50%'
										}
								  }
						 );
	loading.setAttribute('id','loading');
	loading.injectInside(container);
	//centrer verticalement par rapport à la page
	valigncenter(loading);
	
	var info_sub_newsletter = new Element('div', {
													'styles': 
													{
														'display': 'block',
														'width': '350px',
														'height': '105px',
														'padding': '0px',
														'text-align': 'left',
														'margin':'0 auto',
														'z-index':'1000',
														'position':'absolute'
													}
											  }
										);
		 
	info_sub_newsletter.setAttribute('id','info_sub_newsletter');
	
	if (window.ie && !window.ie7){
		info_sub_newsletter.injectInside($('container2'));
	}else{
		info_sub_newsletter.injectInside($('container'));
	}
	
	valigncenter(info_sub_newsletter);
	var myFx = new Fx.Style(info_sub_newsletter, 'opacity').set(0);
}
function get_email(){
	var email = '';
	if ($('wfec_user_email_sub_newsletter')){
		if ($('wfec_user_email_sub_newsletter').value != '' && $('wfec_user_email_sub_newsletter').value != ' Laissez votre email'){
			email = $('wfec_user_email_sub_newsletter').value;
		} else if($('wfec_user_email_sub_newsletter_home')){
			if ($('wfec_user_email_sub_newsletter_home').value != '' && $('wfec_user_email_sub_newsletter_home').value != ' Laissez votre email'){
				email = $('wfec_user_email_sub_newsletter_home').value;
			}
		}
	}
	return email;
}
function valigncenter(lmt)
{
	var page = document.documentElement;
	if(lmt && page)
	{
	    var containerHeight;
	    if (page.innerWidth)
	    {
            containerHeight = page.innerHeight;
		}
		else
		{
            containerHeight = page.clientHeight;
		}
	    var lmtHeight;
	    if (lmt.innerWidth)
	    {
            lmtHeight = lmt.innerHeight;
		}
		else
		{
            lmtHeight = lmt.offsetHeight;
		}
		var y = Math.ceil((containerHeight - lmtHeight) / 2);
		if(y < 0)
		{
			y = 0;
		}
		lmt.style.position = "relative";
		lmt.style.top = y + "px";
	}
	if (document.getElementById)
	{
		document.body.style.visibility = 'visible';
	}
}
