$(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 
	});		
	$('#date').datepicker({
		dateFormat: 'dd-mm-yy', 
		minDate: 0,
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre'],
		monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jui','Jul','Aoû','Sep','Oct','Nov','Dec'],	
	});	
});

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(){
	 $(".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>");
	
					phone = $(this).find("input[name=phone]").val();
          valerr=valide($(this).find("input[name=phone]"), phone, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "", valerr);
	
          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);
          
          date = $(this).find("input[name=date]").val();
          valerr=valide($(this).find("input[name=date]"), date, {defaultval : "JJ/MM/AA", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - date</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);
          
					nb_guests = $("#nb_guests").val();
					valerr=valide($(this).find("#nb_guests"), nb_guests, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "<p class='tabulation'> - nb_guests</p>", valerr);
					
					cp = $("#cp").val();
					valerr=valide($(this).find("#cp"), cp, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "", valerr);
					
					type = $("#type").val();
					valerr=valide($(this).find("#type"), type, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "", valerr);
					
					moment = $("#moment").val();
					valerr=valide($(this).find("#moment"), moment, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "", valerr);
					
					typeevent = $("#typeevent").val();
					valerr=valide($(this).find("#typeevent"), typeevent, {defaultval : "", defaultObligatoire: "Champ obligatoire.", champVide: ""}, "", valerr);

          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, phone: phone, prenom: prenom, nom: nom, email: mail, message: message, date: date, nb_guests: nb_guests, type: type, typeevent: typeevent, cp: cp, 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;
  });

if ($('#contact-form-menu').length > 0)
{
  var top = $('#contact-form-menu').offset().top - parseFloat($('#contact-form-menu').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function (event) {
    var y = $(this).scrollTop();
  	if (y > top)
  		$('#contact-form-menu').css("top", y - top);
  	else
  		$('#contact-form-menu').css("top", y);       
  });
}
})
