$(document).ready(function() {
	$('#featured').orbit({
		animation: 'fade',                  // fade, horizontal-slide, vertical-slide, horizontal-push
		animationSpeed: 800,                // how fast animtions are
		timer: true, 			 // true or false to have the timer
		advanceSpeed: 4000, 		 // if timer is enabled, time between transitions 
		pauseOnHover: true, 		 // if you hover pauses the slider
		startClockOnMouseOut: true, 	 // if clock should start on MouseOut
		startClockOnMouseOutAfter: 0, 	 // how long after MouseOut should the timer start again
		directionalNav: true, 		 // manual advancing directional navs
		captions: false, 			 // do you want captions?
		captionAnimation: 'fade', 		 // fade, slideOpen, none
		captionAnimationSpeed: 800, 	 // if so how quickly should they animate in
		bullets: false			 // true or false to activate the bullet navigation 
	});
});


function valide(element, valAVerifier, valeurDeVerification, messageAfficheErr, valErr){
        for (var id in valeurDeVerification) {
            if(valAVerifier==valeurDeVerification[id]){
                    element.css('background-color','#f26c4e').val("Champ obligatoire.");
                    $("#resultat-formErr").append(messageAfficheErr);
                    return true;
            }
        }
        if(!valErr)
                return false;
        else
                return true;
}


$(function(){
		 $("#booking").click ( function () {		 	
		 	if ($("#booking_window").css("display") == "none")
        		$("#booking_window").show('slow');
        	else	
        		$("#booking_window").hide('slow');
        		
        	return false;
        });
        
        $("#booking_window_close_div").click ( function () {		 	
		 	$("#booking_window").hide('slow');
		 	return false;        	
        });
        
        
         $(".nav-lien").each(function(){
                 if($(this).hasClass("active")){
                         $(this).find("img.img-active-hover").css('z-index', '3');
                 }
         });
         $(".nav-lien").mouseover(function(){
                $(this).find("img.img-active-hover").css('z-index', '3');
         });
         $(".nav-lien").mouseout(function(){
                if(!($(this).hasClass("active"))){ 
                        $(this).find("img.img-active-hover").css('z-index', '-1');
                }
         });
        
         $(".text-field").each(function(){
                var defaultText = $(this).val();
                $(this).focus(function(){
                        if( ($(this).val()==defaultText) || ($(this).val()=="Champ obligatoire.")){
                                $(this).val("").css('background-color','#ffffff');
                        }
                })
                $(this).blur(function(){
                        if($(this).val()==""){
                                $(this).val(defaultText);
                        }
                })
        })

        $("#contact-form").submit(function(){
                valerr=false;
                $(this).find("input[type=text]").css('background-color','#ffffff');
                $(this).find("textarea").css('background-color','#ffffff');
                $("#resultat-formErr").empty().hide().append("<p>Vous n'avez pas rempli le(s) champ(s) suivant(s):</p>");

                prenom = $(this).find("input[name=prenom]").val();
                valerr=valide($(this).find("input[name=prenom]"), prenom, {defaultval : "Votre prénom.", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - prenom</p>", valerr);

                nom = $(this).find("input[name=nom]").val();
                valerr=valide($(this).find("input[name=nom]"), nom, {defaultval : "Votre nom.", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - nom</p>", valerr);

                mail = $(this).find("input[name=email]").val();
                valerr=valide($(this).find("input[name=email]"), mail, {defaultval : "Votre email.", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - email</p>", valerr);

                message = $(this).find("textarea[name=message]").val();
                valerr=valide($(this).find("textarea[name=message]"), message, {defaultval : "Votre message à envoyer.", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - message</p>", valerr);

				date = $(this).find("input[name=date]").val();
				nb_guests = $("#nb_guests").val();
				type = $("#type").val();
				moment = $("#moment").val();

                if(valerr){
                        $("#resultat-formErr").show();
                }
                else{
                        if(!(/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/i.test(mail))){
                                $("#resultat-formErr").empty().show().append("<p>L'email n'est pas valide.</p>");
                        }
                        else{
                                $.post("sendmail.php", {envoi: true, prenom: prenom, nom: nom, email: mail, message: message, date: date, nb_guests: nb_guests, type: type, moment: moment},function(data){
                                        if(/parvenu/i.test(data)){
                                                $("#resultat-formErr").hide();
                                                $("#resultat-formOK").show().append(data);
                                                $("#contact-formulaire").hide();
                                        }
                                        else{
                                                $("#resultat-formErr").empty().show().append(data);
                                        }
                                }); 
                        }
                }
                return false;
        });               
})
