// Salon de toilettage Larochelle
// Scripts communs à toutes les pages du site

$(document).ready(function(){
						   
	// Add bookmark script
	
	// add a "rel" attrib if Opera 7+  
	if(window.opera) {  
			if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set  
					$("a.jqbookmark").attr("rel","sidebar");  
			}  
	}  

	$("a.jqbookmark").click(function(event){  
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link  
			var url = this.href;  
			var title = $("title").text();//this.title
			
			//alert(url + " - " + title);

			if (window.sidebar) { // Mozilla Firefox Bookmark  
					window.sidebar.addPanel(title, url,"");  
			} else if( window.external ) { // IE Favorite  
					window.external.AddFavorite(url, title);  
			} else if(window.opera) { // Opera 7+  
					return false; // do nothing - the rel="sidebar" should do the trick  
			} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)  
					 alert("Malheureusement, votre fureteur ne supporte pas l'ajout de favoris par JavaScript,"  
					 + " veuillez ajouter le favori manuellement.");  
			}  

	});	
	
	// Secondary navigation image rollovers
	$("#secNav a").mouseover(function() {
		imgsrc = $(this).children("img").attr("src");
		// Fail if no image found
		if (typeof(imgsrc) != 'undefined') {
			// Replace .gif with _over.gif
			imgsrcON = imgsrc.replace('.gif', '_over.gif');
			// Now change the src to the 'ON' value
			$(this).children("img").attr("src", imgsrcON);
		}
	});
	
	$("#secNav a").mouseout(function(){
	  // trap for 'undefined' errors again
	  if (typeof(imgsrc) != 'undefined') {
		  $(this).children("img").attr("src", imgsrc);
	  }
	});
	
	// Rollovers for other buttons (class="roll")
	$("a.roll").mouseover(function() {
		imgsrc = $(this).children("img").attr("src");
		// Fail if no image found
		if (typeof(imgsrc) != 'undefined') {
			// Replace .gif with _over.gif
			imgsrcON = imgsrc.replace('.gif', '_over.gif');
			// Now change the src to the 'ON' value
			$(this).children("img").attr("src", imgsrcON);
		}
	});
	
	$("a.roll").mouseout(function(){
	  // trap for 'undefined' errors again
	  if (typeof(imgsrc) != 'undefined') {
		  $(this).children("img").attr("src", imgsrc);
	  }
	});
	
	// Validation de nom d'usager et mot de passe formulaire de login	
	$("#memberLoginForm").validate(
		{
		   errorElement: "div",
		   onsubmit: true,
			 onfocusout: true,
		   focusInvalid: true,
		   errorPlacement: function(error, element) {
			 			error.insertAfter(element);		
   		},			
		   
		   messages: {
			 j_password: "S.V.P. entrez votre mot de passe",	
			 j_username: {
			   required: "S.V.P. entrez votre adresse de courriel",
			   email: "S.V.P. entrez une adresse de courriel valide"
			 }
		}
	});
	
	// Validation du champ courriel du formulaire de récupération de mot de passe	
	$("#passwordRetrieve").validate(
		{
		   errorElement: "div",
		   onsubmit: true,
			 onfocusout: true,
		   focusInvalid: true,
		   errorPlacement: function(error, element) {
			 			error.insertAfter(element);		
   		},			
		   
		   messages: {	
			 email: {
			   required: "S.V.P. entrez votre adresse de courriel",
			   email: "S.V.P. entrez une adresse de courriel valide"
			 }
		}
	});
	
	// Validation du formulaire de contact	
	$("#commentForm").validate(
		{
		   errorElement: "div",
		   onsubmit: true,
			 onfocusout: true,
		   focusInvalid: true,
		   errorPlacement: function(error, element) {
			 			error.insertAfter(element);		
   		},			
		   
		   messages: {
			 frmName : "S.V.P. entrez votre nom.",			 			 	
			 frmEmail: {
			   required: "S.V.P. entrez votre adresse de courriel",
			   email: "S.V.P. entrez une adresse de courriel valide"
			 },
			 frmSubject: "S.V.P. entrez un sujet pour votre message.",
			 frmMessage: "S.V.P. entrez un message."
		}
	});
	
	// Ouverture et fermeture des FAQs (definition lists dl/dt/dd)
	// En premier lieu, cacher le texte principal (dd) et laisser seulement les questions visibles
	// Ensuite, ajouter la classe par défaut requise (closed) au questions pour les rendre "cliquables"
	$(".faq dd").hide();
	$(".faq dt").addClass("closed");
	// Ajouter variable pour garder la dernière question ouverte en mémoire et la fermer lorsque'une autre
	// question est ouverte
	var lastOpen;

	$(".faq dt").click(function() {
		$(".faq dt").attr("class", "closed"); // set class of all dt elems to closed
		if (!$.browser.msie) {
			$(lastOpen).next().toggle(500); // Hide the last showing dd
		} else {
			$(lastOpen).next().toggle(); // Hide the last showing dd without animation to avoid loss of anti-aliasing issue in IE
		}
		$(this).toggleClass("open"); // Set the class of the dt that was just clicked to open
		if (!$.browser.msie) {
			$(this).next().toggle(500); // Show the clicked dt's dd
		} else {
			$(this).next().toggle(); // Show the clicked dt's dd without animation to avoid loss of anti-aliasing issue in IE
		}
		lastOpen = this;
		console.log($(this).attr("class"));
	});
	
});