var tab_email={};
var cpt_email=0;
$(document).ready(function() {
	$(".fiche_produit img").each(function() {
		$(this).after("<div class=\"photo_no_contract\">Photo non contractuelle</div>");
	});
	
	$("input[type=radio]").parent().click(function() {
		$(this).parent().children().removeClass("selected");
		$(this).parent().children().children("input[type=radio]").attr("checked", false);
		$(this).addClass("selected");
		$(this).children("input[type=radio]").attr("checked", true)
	});
	$("input[type=checkbox]").parent().children("span").click(function() {
		if ($(this).parent().children("input[type=checkbox]").attr("checked")==false || $(this).parent().children("input[type=checkbox]").attr("checked")==undefined) {
			$(this).parent().addClass("selected");
			$(this).parent().children("input[type=checkbox]").attr("checked", true);
			$(this).parent().children("div").show();
		}
		else {
			$(this).parent().removeClass("selected");
			$(this).parent().children("input[type=checkbox]").attr("checked", false);
			$(this).parent().children("div").hide();
		}
	});
	
	$(".produit").click(function() {
		location.href = $(this).children("a").attr("href");
	});
	
	$(".mail_crypte").each(function() {
		var mail = $(this).html();
		mail=mail.replace("{#}", "@");
		mail=mail.replace("{{#}}", ".");
		tab_email[cpt_email]=mail;
		$("<a href=\"#\" rel=\"nofollow\" class=\"lien_mail\" id=\"mail"+cpt_email+"\">"+mail+"</a>").insertAfter(this);
		cpt_email++;
		$(this).remove();
	});
	$(".lien_mail").click(function() {
		var ref = $(this).attr("id");
		ref=ref.replace("mail", "");
		$(location).attr("href","mailto:"+tab_email[ref]);
		return false;
	});
	
	var sudoSlider = $("#slider").sudoSlider({
		fade:true,
		auto:true,
		pause:'5000',
		prevNext:false
	});
	if (jQuery().prettyPhoto) {
	   $("a[rel^='prettyPhoto']").prettyPhoto({
		   overlay_gallery: false,
		   social_tools:false,
		   deeplinking: false
	   });
	}
	var oldt = 0;
	var sudoSlider2 = $("#slider-home").sudoSlider({ 
	 prevNext:false,
	 pause:5000,
	 auto:true,
	 beforeAniFunc: function(t){ 
		var nbphotohome = $('#slidemenu ul').children("li").length-1;
		var scroll = -t+2;
		if (scroll == 1) scroll = 0;
		if (scroll < (-1*(nbphotohome-3))) scroll = (-1*(nbphotohome-3));
		var scroll = scroll * $('.custom').eq(0).outerHeight(true);
		var diff = Math.sqrt(Math.abs(oldt-t));
		var speed = parseInt(diff*800);
		$('#slidemenu ul').animate(
		   { marginTop: scroll },
		   {
			  queue:false,
			  duration:speed
		   }
		);
		var substract = $('#slidemenu ul').offset();
		var posi = $('#slidemenu ul li').eq(t-1).offset();
		var topvari =  posi.top - substract.top;
		$('#slidemenu ul li.currentone').animate({
		   marginTop: topvari
		   }, speed);
		oldt = t;
	   },
	   customLink: '.custom'
	});
	
	
	$("#slider-home li").hover(function() {
		$(this).children(".info_lot").animate( {
			bottom:"0px"
		});
	},function() {
		$(this).children(".info_lot").animate({
			bottom:"-"+($(this).children(".info_lot").height()-28)+"px"
		});
	});
	$("#slider-home li .info_lot").each(function() {
		$(this).css("bottom", "-"+($(this).height()-28)+"px");
	});
	
	
	$('#btn_submit_form').click(function() {  
 
        $(".error").remove();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
 
        var societeVal = $("#ch_societe").val();
        if(societeVal == '') {
            $("#ch_societe").after('<span class="error">Entrez votre société</span>');
            hasError = true;
        }

        var interlocuteurVal = $("#ch_interlocuteur").val();
        if(interlocuteurVal == '') {
            $("#ch_interlocuteur").after('<span class="error">Entrez l\'interlocuteur</span>');
            hasError = true;
        }

        var telVal = $("#ch_tel").val();
        if(telVal == '') {
            $("#ch_tel").after('<span class="error">Entrez votre numero de téléphone</span>');
            hasError = true;
        }
 
        var emailaddressVal = $("#ch_liam").val();
        if(emailaddressVal == '') {
            $("#ch_liam").after('<span class="error">Entrez votre adresse e-mail</span>');
            hasError = true;
        }
        else if(!emailReg.test(emailaddressVal)) {
            $("#ch_liam").after('<span class="error">Entrez votre adresse e-mail valide</span>');
            hasError = true;
        }

        if(hasError == true) { return false; }
 
    });
})
