$(document).ready(function()
{
	hauteurBrowser = document.documentElement.clientHeight;
	largeurBrowser = document.documentElement.clientWidth;
	
	$('#masque').hide();
	$('#popup').hide();
	$('#close').bind("click",function(){
		$('#popup').fadeOut(500);
		closePopupMask();
	});
	
	//RECUPERATION DES VALUES DES INPUT
	var input = new Array();
	$('input').each(function(){
		var id = $(this).attr('id');
		input[id] = $(this).val();
	});
	
	// INIT DU CAROUSEL
	$("div#jcarousel").jCarousel();
	
	$('#error').hide();
	
	$("input").focusin(function() {
		var id = $(this).attr('id');
		//alert($(this).id);
		if($(this).val() == input[id]){
         	$(this).val("");
		}
		$("#error").fadeOut("medium");
    });
	
	$("input").focusout(function() {
		var id = $(this).attr('id');
		if($(this).val() == "")
		{
         $(this).val(input[id]);
		}else{
			checkMail();
		}
    });
	
	$('#envoyer').click(function() {
		
		if(checkMail()){
			$.post( "index.php/home/checknewsletter", $('#myform').serialize(), function(data){
				
				if(data.result.email != null){
					$("#error").fadeIn("medium");
					
					if($('#pays').val() == "fr"){
						$("#error").html("Votre adresse e-mail est invalide");
					}else{
						$("#error").html("Your email address is not correct ");
					}
					
					
				}else if(data.result == 'emailexist'){
					$("#error").fadeIn("medium");
					
					if($('#pays').val() == "fr"){
						$("#error").html("Votre adresse e-mail est déjà enregistrée");
					}else{
						$("#error").html("Your email address has already been registered");
					}
					
				}else{
					//if(wysi==1){stat('yoplait','','','','inscription_newsletter_ok');};
					//_gaq.push(['_trackEvent', 'formulaire', 'inscription_newsletter_ok', ' ']);
					
					$("#error").fadeOut("medium");
					$('#popup').fadeIn(500);
					openPopupMask();
					$('#email').val("adresse e-mail");

					if($('#pays').val() == "fr"){
						$('#email').val("adresse e-mail");
					}else{
						$('#email').val("email address");
					}
				}
			});
		}
		  
	});
});

openPopupMask = function(){
	$('#masque').css({'opacity':'0.8' }).fadeIn("slow");
	$('#masque').css({'height':'' + hauteurBrowser + 'px' , 'width':'' + largeurBrowser + 'px' });
	$('#masque').css({'margin':'' + -hauteurBrowser/2 + 'px 0 0 ' + -largeurBrowser/2 + 'px' });		
};

closePopupMask = function(){
		$('#masque').fadeOut(500);
}


function checkMail()
{
	var adresse = $("#email").val();
	
	var regMail = /^[a-zA-Z0-9+\.\_\-]+@[a-zA-Z0-9+\-\.]{2,}[.][a-zA-Z]{2,4}$/;
	if (regMail.exec(adresse) == null)
	{
		$("#error").fadeIn("medium");
		if($('#pays').val() == "fr"){
			$("#error").html("Votre adresse e-mail est invalide");
		}else{
			$("#error").html("Your email address is not correct ");
		}
		return false;
	}
	else
	{
		$("#error").fadeOut("medium");
		return true;
	}

}
