//DECLARATION FUNCTION
	
	// On toogle l'affichage du panel
	function toogleItem(item,show){
		if(show) item.children(".panel-info-product").show();
		else item.children(".panel-info-product").hide();
	}

jQuery(document).ready(function() {
	
	/**************************************************/
	/****** 			SLIDER HOME				 ******/
	/**************************************************/
	
	if(jQuery('body').hasClass('cms-index-index')){
		jQuery('#slider_navi li:eq(0)').addClass('active');

		var slider = jQuery('#slider').scrollable({
			horizontal: true,
			easing: "easeInOutExpo",
			speed: 300,
			circular: true
		}).navigator({navi:'#slider_navi',activeClass:'active'}).autoscroll({ autoplay: true, interval: 6000 });
	}
	
	/**************************************************/
	/****** 		  Menu Principal 			 ******/
	/**************************************************/

	jQuery('#nav ul.level0').css("width","960px");
	
	jQuery('#nav ul.level0').masonry({
		itemSelector : '#nav ul.level0 li.level1',
		columnWidth     : 240
	});
	

	/**************************************************/
	/****** 		   Toogle Produit  			 ******/
	/**************************************************/
	if(jQuery('body').hasClass('catalog-product-view')){
	
		$container = jQuery('.toogle-desc');
		$trigger = jQuery('.toogle-desc-button span');
		$shortDesc = jQuery('.short-desc');
		$fullDesc = jQuery('.full-desc');
		
		$fullDesc.hide();
		$trigger.click(function() {
			$shortDesc.fadeTo(150,0,function(){
				jQuery(this).hide();
				$fullDesc.stop().show().fadeTo(500,1);
			});
		});
		
	}
	
	/**************************************************/
	/****** 		SLIDER CATEGORIES			 ******/
	/**************************************************/
	
	if(jQuery('body').hasClass('catalog-category-view')){
		jQuery(".push-slider .scrollable").scrollable();
	}
	
	
	
	/**************************************************/
	/****** 			QTY AJAX CARTPRO 		 ******/
	/**************************************************/
	
	// On défini la valeur de la quantité pour toud les champs
	jQuery('.qty-field-push').each(function(){
		jQuery(this).val(1);
		checkValueQty(jQuery(this));
	});
	
	// Au click sur le bouton addToCart on fait le setLocation pour l'ajaxCartPro
	jQuery('.btn-cart-qty').click(function(){
		var AddToCartUrl = jQuery(this).attr('name');
		setLocation(AddToCartUrl);
	return false;
	});
	
	// Au focus sur le champs qty on vérifié le contenu tapé
	jQuery('.qty-field-push').focus(function() {
		jQuery(this).keyup(function() {
			checkValueQty(jQuery(this));
		});
	});
	
	// Au blur on vérifié le contenu et on cache le panel
	jQuery('.qty-field-push').blur(function(){
		var current = jQuery(this);
		checkValueQty(current);
		var id = current.attr('id').substring(4);
		if(!current.data('hover')) jQuery('#panel-'+id).hide();
	});
	
	// PUSH : Au hover on affiche ou non le panel et on vérifie la qty
	jQuery(".push-item").hover(
	  function () {
		jQuery(this).find('.qty-field-push').data('hover',true);
	    toogleItem(jQuery(this),true);
		checkValueQty(jQuery(this).find('.qty-field-push'));
	  },
	  function () {
		jQuery(this).find('.qty-field-push').data('hover',false);
		if(!jQuery(this).find('.qty-field-push').is(':focus')) toogleItem(jQuery(this),false);
	  }
	);
	
	// LIST : Au hover on affiche ou non le panel et on vérifie la qty
	jQuery(".item").hover(
	  function () {
		jQuery(this).find('.qty-field-push').data('hover',true);
	    toogleItem(jQuery(this),true);
		checkValueQty(jQuery(this).find('.qty-field-push'));
	  },
	  function () {
		jQuery(this).find('.qty-field-push').data('hover',false);
		if(!jQuery(this).find('.qty-field-push').is(':focus')) toogleItem(jQuery(this),false);
	  }
	);
	
	
	// On vérifie la valeur du champs
	function checkValueQty(field){
		var fieldVal = field.val();
		if(fieldVal==''){
			if(!field.is(":focus")) setValueQty(field,1); // Si le champ est vide et qu'il n'est pas focus on le défni à 1
		}
		fieldVal = field.val();
		if(isNaN(fieldVal)==true || fieldVal=='') setUrlAddCartProduct(field,1); // Si champ n'est pas un nombre ou il est vide on redéfni la qty de l'url à 1
		else setUrlAddCartProduct(field,fieldVal); // Sinon on redéfni la qty de l'url avec la valeur rentrée
	}
	
	// On défini la valeur du champs et du onClick
	function setValueQty(field,value){
		field.val(value);
	}
	
	// On redéfini l'url addToCart avec la nouvelle valeur
	function setUrlAddCartProduct(field,value){
		if(field.attr('id')){
			var id = field.attr('id').substring(4);
			var buttonId = "button-"+id;
			var buttonRel = jQuery('#'+buttonId).attr('name');
			var out = buttonRel.split('/');
			out[out.length-2] = value;
			var finalUrl = out.join('/');
			jQuery('#'+buttonId).attr('name',finalUrl);
		}
	}
	
	// Hack jQuery pour detecter si un est element est focus avec is(':focus')
	jQuery.expr[':'].focus = function(elem){
	  return elem === document.activeElement && ( elem.type || elem.href );
	};
	

	

	
	/**************************************************/
	/****     SLIDE DOWN/UP FILTRAGE CATEGORIE     ****/
	/**************************************************/
	
	var isHideLayerCategorie=false;
	jQuery('.filter-categorie .hide-bloc .is-hide').hide();
	//jQuery('.filter-categorie .hide-bloc .isnot-hide').hide();
	
	jQuery('.filter-categorie  .hide-bloc').click(function() {
		jQuery('.block-layered-nav.filter-categorie .block-content').slideToggle('slow', function() {
			if(isHideLayerCategorie){
				jQuery('.filter-categorie .hide-bloc .isnot-hide').show();
				jQuery('.filter-categorie .hide-bloc .is-hide').hide();
				isHideLayerCategorie=false;
			}
			else{
				jQuery('.filter-categorie .hide-bloc .isnot-hide').hide();
				jQuery('.filter-categorie .hide-bloc .is-hide').show();
				isHideLayerCategorie=true;
			}
		});
	  jQuery(this).toggleClass("close");
	});
	
		
	/**************************************************/
	/****     QTY +/- POUR LA PAGE PANIER          ****/
	/**************************************************/
	
	if(jQuery('body').hasClass('checkout-cart-index')){
	
		jQuery("div.quantity").append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
		
		jQuery(".plus").click(function()
		{
		    var currentVal = parseInt(jQuery(this).prev(".qty").val());
		
		    if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 0;
		
		    jQuery(this).prev(".qty").val(currentVal + 1);
		});
		
		jQuery(".minus").click(function()
		{
		    var currentVal = parseInt(jQuery(this).next(".qty").val());
		    if (currentVal == "NaN") currentVal = 0;
		    if (currentVal >= 1)
		    {
		        jQuery(this).next(".qty").val(currentVal - 1);
		    }
		});
	
	}
		
	/**************************************************/
	/****     RECHERCHE AVANCEE MULTISELECT        ****/
	/**************************************************/
	if(jQuery('body').find(".multiselect").length != 0) {
		jQuery(".multiselect").multiSelect({selectedList:5});
	}
});
