
$(document).ready(function(){
	
	$("a[href='#']").click(function(){return false;});
	
	$("#logo_princ").click(function(){window.location = 'http://www.selectiondupatissier.com'});
	
	
	
	/*####################################################################*/
	/*##  Fonction qui simule le target _blank  ##########################*/
	//$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$('a[href^=http://]').not('a[href*='+window.location.host+']').attr('target', '_blank');
	$('a[href$=.pdf]').attr('target', '_blank');
	//$('a[href^=http://]').has('a[attr*=prettyphoto]').click(function(e){e.preventDefault();});
	
	//#####################################################################################
    //##  Ajoute le comportement du bouton search  ####################################
	$("#btn_search").click(function(e){
		$("#f_recherche").submit();	
	});
		
		
	//#####################################################################################
    //##  Fix le menu de produit du footer  ####################################
	var nb_li_fproduit = Math.ceil($("#f_menu_produit ul li").size()/3);
	//alert(nb_li_fproduit);
	i=0;
	$("#f_menu_produit ul li").each(function(){
		if(i%nb_li_fproduit == 0){
			$("#f_menu_produit ul").append("<div></div>")
		}
		eq = Math.floor(i / nb_li_fproduit);
		$("#f_menu_produit ul div:eq("+eq+")").append($(this));
		i++;
	});
	
	//#####################################################################################
    //##  Fix le h1 des page de contenu  ####################################
	$(".tiny_ed h1").each(function(){
		var html = $(this).html();
		$(this).html("<span>"+html+"</span>");
	});
	
	
	//#####################################################################################
    //##  Fix la margin du 4e produit de la liste  ####################################
	$(".produit_list_line").each(function(){
		$(this).find(".produit_wrapper:eq(3)").addClass("last_list");
	});
	
	
	//#####################################################################################
    //##  Fix la hauteur des items de produit de la liste  ####################################
	$(".produit_list_line").each(function(){
		var maxHeight = 0;
		$(this).find(".produit_wrapper").each(function(){
			if(maxHeight < $(this).height()){
				maxHeight = $(this).height();
			}
		});
		$(this).find(".produit_wrapper").height(maxHeight);
	});
	
	//#####################################################################################
    //##  Fix la margin du premier nav seq de la liste produit  ####################################
	$("#produit_list_wrapper .list_nav_seq:eq(0)").css("padding-top", "1em");

	
	//#####################################################################################
    //##  Événement de la navigation séquentiel de la liste de produit ####################################
	$("select[name='nb_product_page']").live('change',function(){
	    var value = $(this).val();
		window.location = window.location.pathname+"?nb_product_page="+value;
	});
	
	//#####################################################################################
    //##  Événement de la fiche de produit ####################################
	$("#btn_print").click(function(){
	    window.print();
	});
	

		
	/*####################################################################*/
	/*##  Partager par courriel  ################################*/
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	  $( "#dialog:ui-dialog" ).dialog( "destroy" );
	  
	  var your_name = $( "#your_name" ),
		  your_email = $( "#your_email" ),
		  friend_name = $( "#friend_name" ),
		  friend_email = $( "#friend_email" ),
		  tips = $( ".validateTips" );


	  function init_dialog_sendfriend(){
		  $( "#dialog-form-sendfriend" ).dialog({
			  autoOpen: false,
			  height: 440,
			  width: 300,
			  modal: true,
			  buttons: {
				  "Envoyer": function() {
					  
					  if(form_validation($( "#dialog-form-sendfriend" ))){	
						  $.ajax({
							  type: 'POST',
							  url: '/inc/form/inc_partage_courriel.php',
							  data: {your_name : $("#your_name").val(), your_email : $("#your_email").val(), friend_name : $("#friend_name").val(), friend_email : $("#friend_email").val(), message : $("#message").val(), page : $("#page").val(), flag : "send"},
							  dataType: 'html',
			  
							  success: function(html, textStatus){
								  $( "#dialog-form-sendfriend .ready_state" ).hide();
								  $( "#dialog-form-sendfriend .finish_state" ).show();
								  $("#dialog-form-sendfriend").dialog( "option", "height", 180 );
								  $("#dialog-form-sendfriend").dialog( "option", "buttons", {} );
								  
								  $("#your_name").val("");
								  $("#your_email").val("");
								  $("#friend_name").val("");
								  $("#friend_email").val("");
								  $("#message").val("");
								  setTimeout('$("#dialog-form-sendfriend").dialog( "destroy" )',3000);
							  },
							  error: function(xhr, textStatus, errorThrown){
								  var error = 'An error occurred! ' + (errorThrown ? errorThrown : xhr.status);
								  
							  }			
						  });
						  //$( this ).dialog( "close" );
					  }
				  },
				  Cancel: function() {
					  $( this ).dialog( "close" );
				  }
			  },
			  close: function() {
				  //allFields.val( "" ).removeClass( "ui-state-error" );
			  }
		  });
	  }

	$( "#btn_mail" ).click(function() {
		init_dialog_sendfriend();
		$( "#dialog-form-sendfriend .ready_state" ).show();
		$( "#dialog-form-sendfriend .finish_state" ).hide();
		$( "#dialog-form-sendfriend" ).dialog( "open" );
	});

	
	//#####################################################################################
    //###  Set du lien actif de section  ##################################################

    var current_url = document.location.href;
    var cur_url_split = current_url.split("/");
    var last_param = cur_url_split.length - 1;
    var needle = cur_url_split[last_param];
	
    $("#lc_nav").find("a[href$='/"+needle+"']").each(function(){
		$(this).addClass('active');
    });
    
	//#####################################################################################
    //##  Fonction d'accodeon pour le menu principale  ####################################

    var menu_container = "lc_nav"; // ID du "div" qui contient le menu
    var sub_menu_type = "ul"; // Tag du conteneur de sous-menu
    var active_accordeon = "current"; // La class donnee au "li" dont le sous-menu est ouvert
    var accordeon_menu = "accordeon_menu"; // La class donnee aux "li" qui contiennent un sous-menu
    var accordeon_trigger = "accordeon_trigger";
    var $menu_container = $('#'+menu_container); // Caching du "div" qui contient le menu

    $(".active").closest('ul').show().parent().addClass(active_accordeon);


    $menu_container.find("ul li ul").each(function(){$(this).parent().addClass(accordeon_menu).end().prev('a').addClass(accordeon_trigger);});

	$menu_container.find("."+accordeon_trigger).click(function(e){
		//e.preventDefault();
        $menu_container.find('.'+active_accordeon).removeClass(active_accordeon)
        if($(this).parent().hasClass(accordeon_menu)){$(this).parent().addClass(active_accordeon);}
		if($(this).next(sub_menu_type).is(":visible")){
            //console.log('test');
            $(this).next(sub_menu_type).slideUp("medium", "easeOutQuad");
        }else{
            //console.log('test2');
			$menu_container.find("ul li "+sub_menu_type+":visible").slideUp("medium", "easeOutQuad");
			$(this).next(sub_menu_type).slideToggle("medium", "easeOutQuad");
		}
	});
	
	$(".active").each(function(){$(this).prepend('<span></span>');});
	
	
	

	
	
	$(".textfield").focusin(function(){
		if($(this).val() == $(this).attr('title'))
		{
			$(this).val('');
			
		}
		$(this).css("color","#000000");
	});
	
	$(".textfield").focusout(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'));
			$(this).css("color","#989898");
		}
	});
	$(".textfield").each(function (){
		if($(this).val() == $(this).attr('title'))
		{
			$(this).css("color","#989898");
		}
	});
	
	
	
	
});


