jQuery(function($){
	nlCtaHeader.init();
})

//Inscription Newsletter Header
var nlCtaHeader = {
		
	bloc : null,
	emailOld: null, 
	
	init: function(){
		
		nlCtaHeader.bloc = $("#cta-newsletter-bloc");
		
		$("#cta-newsletter-link").click(function(){		
			nlCtaHeader.bloc.hasClass("off")  ? nlCtaHeader.showCta() : nlCtaHeader.hideCta();
			return false;
		});
	
		$(document.body).click(function(e){
			if(!$(e.target).is('#cta-newsletter-bloc *:not')) {					
				nlCtaHeader.hideCta()
			}
			else{
				$(this).clearQueue();
			}
		});
	
		$("#cta-newsletter-bloc-close").click(function(){
			nlCtaHeader.hideCta();
		});
	
		//Placeholder
		$('#input-header-nl input').focus(function(){if( $(this).val() == "Entrez votre adresse e-mail" ){$(this).val("");$(this).removeClass('placeholder')}});
		$('#input-header-nl input').blur(function(){if( $(this).val() == "" ){$(this).val("Entrez votre adresse e-mail");$(this).addClass('placeholder')}});
		
		//Inscription ajax
		$("#form-nl-header").submit(function(e){
			e.preventDefault();
			email = $("input[name=email-header]").val();
			
			if(email != nlCtaHeader.emailOld){
				nlCtaHeader.checkEmail(email) ? nlCtaHeader.postAjax(email) : nlCtaHeader.ajaxFail();
			}
		});
	},
	
	//Fonction qui affiche le bloc cta
	showCta: function(){
		nlCtaHeader.bloc.animate({top:33},300,function(){
			$(this).removeClass("off").addClass("on");
		});
	},
	
	//Fonction qui cache le bloc cta
	hideCta: function(){
		nlCtaHeader.bloc.stop().animate({top:-nlCtaHeader.bloc.height()},300,function(){
			$(this).removeClass("on").addClass("off");
		});
	},
	
	//Fonction qui cache le bloc cta avec un délai
	hideCtaDelay: function(delay){
		nlCtaHeader.bloc.delay(delay).animate({top:-nlCtaHeader.bloc.height()},300,function(){
			$(this).removeClass("on").addClass("off");
			nlCtaHeader.infoReset();
		});
	},
	
	//Requête AJAX pour ajouter l'e-mail à la liste
	postAjax: function(email){
		nlCtaHeader.showLoader();
		$.ajax({
			type: "POST",
			url: "http://www.search-performance.fr/site/wp-content/themes/awe/catchEmail.php",
			data: "email="+email,
			async: true,
			success: nlCtaHeader.ajaxSuccess(email)
		});
	},
	
	//Fonction retournée en cas de success
	ajaxSuccess: function(){
		$("#input-header-nl").find("#iconInfo").removeClass().addClass("iconInfoOk").show();
		$("#info-header-nl").removeClass().addClass("emailOk").empty().append("inscription réussi !").slideDown("normal",function(){
			$("#inputSubmit-header-loader").hide(function(){					
				nlCtaHeader.hideCtaDelay(3000);
			});
		});
		nlCtaHeader.emailOld = email;
	},
	
	//Fonction retournée en cas d'erreur dans la saisie de l'adress e-mail
	ajaxFail: function(){
		$("#input-header-nl").find("#iconInfo").removeClass().addClass("iconInfoError").show();
		$("#info-header-nl").removeClass().addClass("emailError").empty().append("E-mail non valide !").slideDown("normal",function(){
			$("#inputSubmit-header-loader").hide();
			$("#input-header-nl input").focus();
		});	
	},
	
	//Fonction qui affiche le loader dans le bouton envoyé
	showLoader: function(){
		$("#inputSubmit-header-loader").show();
	},
	
	//Fonction qui reset la balise info
	infoReset:function(){
		$("#info-header-nl").hide().removeClass().empty();
	},
	
	checkEmail: function(email){
		testEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+)+\.([a-zA-Z0-9]{2,4})+$/;
		return check = testEmail.test(email) ?  true :  false;
	}
}

