var $j = jQuery.noConflict();
$j(function(){

	menu.init();
	compteur_textarea.init();
	details_commande_switch.init();

		/* =Reponse */
		$j('.voir-resultat-quiz').hover(function(){
				$j(this).addClass('hover');
			},
			function(){
				$j(this).removeClass('hover');
			});
		$j(".voir-resultat-quiz").click(function(){
			$j(this).parent(".questionnaire").find("#poll-answers li").each(function() {
				alert($(this).text);//.html('******');
			});
			$j(this).parent(".questionnaire").find(".reponse").show();
		});
	$j(".img_prod img").click(function(){
		var img_p=$j(this).attr("rel");
		var img_z=$j(this).attr("jqimg");
		$j(this).parents(".col-main").find(".product-image img").attr({
																		src: img_p,
																		jqimg: img_z
																		});
	});
	$j(".diaporama").carousel( {
								pagination: true,
								autoSlide: true,
								autoSlideInterval: 5000,
					        	delayAutoSlide: 2000,
								effect: "fade"
							} );
	$j(".carousel-pagination a").html("&nbsp;");
	
	
	$j(".caroussel_produit").carousel({autoSlide: false});
	
	$j(".caroussel_produit").each(function(index){
		if(index==0) $j(this).parent().show();
		else $j(this).parent().hide();
	});
	
	$j(".product-image").hover(function(){
		$j("#qty").hide();
	}, function(){
		$j("#qty").show();
	});
	if($j(".product-image").is('div')){
	$j(".product-image").jqueryzoom({
		xzoom: 353, //zooming div default width(default width value is 200)
		yzoom: 352 //zooming div default width(default height value is 200)
		});
	}
	$j('.item_categorie').adjustSize({periode:2});
	$j('#products-list-catalog .product').adjustSize({periode:5});
	$j('#liste_mesure_produits .desc-produit').adjustSize({periode:2});
});
/* qty_minus_plus */

var menu = function(){
	var menuItem, menuLinks;
	function _init(){
		menuItem = $j(".item-menu");
		menuLinks = $j("a", menuItem);
		ssmenuListe = $j('#menu .ssmenu ul');
		if(ssmenuListe) {
			ssmenuListe.each(function(){
			ssmenuListeWidth = $j(this).width();
			$j(this).css({'width':ssmenuListeWidth,'float':'none'});
			});
		}
		menuItem.hover(function(){showSsMenu($j(this));}, function(){hideSsMenu($j(this));});
		menuLinks.focus(function(){
			showSsMenu($j(this).parents(".item-menu"))
		}).blur(function(){hideSsMenu($j(this).parents(".item-menu"))});
	}
	
	function showSsMenu(elmt){
		elmt.addClass("hover");
		elmt.find(".ssmenu").css("z-index","100");
		elmt.find("a").addClass("hover");
	}
	function hideSsMenu(elmt){
		elmt.removeClass("hover");
		elmt.find(".ssmenu").css("z-index","0");
		elmt.find("a").removeClass("hover");
	}
	
	return {init:_init}
}();

function qtyMinus(id) {
	var value = parseInt($j('#'+id).val());
if (value == 0) return 0;
 value--;
 $j('#'+id).val(value);

             return value;
}

function qtyPlus(id) {
	var value = parseInt($j('#'+id).val());
	value++;
	$j('#'+id).val(value);

                return value;
}


/* =COMPTEUR_TEXTAREA */
var compteur_textarea = function(){
	var compteur, textearea;
	function _init(){
		textarea = jQuery('#message');
		compteur = textarea.parent().find('.text_compteur');
		textarea.keyup(function(){
			limitChars(jQuery(this), 250, compteur)
		}).blur(function(){
			limitChars(jQuery(this), 250, compteur)
		});

	}
	function limitChars(textid, limit, infodiv)
	{
		var text = textid.val();
		var textlength = text.length;
		if(textlength > limit)
		{
			infodiv.html('Vous pouvez pas dépasser les '+limit+' caractères!');
			textid.val(text.substr(0,limit));
			return false;
		}
		else
		{
			infodiv.html((limit - textlength) +' caractères restants');
			return true;
		}
	}

	return {init:_init}
}();

/* =DETAILS_COMMANDE_SWITCH */
var details_commande_switch = function() {
	function init() {
		jQuery('#my-orders-table tr.details').hide();
		jQuery('tr.ligne_produit').click(function(){

			jQuery(this).find('span.switch_bloc').toggleClass('moins');

			jQuery(this).next('tr.details').each(function(i, elem) {
				jQuery(elem).toggle(jQuery(elem).css('display') == 'none');
			 });

		});
	return ;
	}
	return {init:init};
}();



// Nouvelle fonction jquery de retaillage de blocks
$j.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $j.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if (options) {
		jQuery.extend(settings, options);
	}
	var totalSize = $j(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$j(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = $j(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$j(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId];
			if (ltie7) $j(this).height(blockComputedHeight+'px');
			else $j(this).css('minHeight',blockComputedHeight+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$j(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $j(this).width() ? $j(this).width() : $j(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$j(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $j(this).width(blockComputedWidth+'px');
			else $j(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}

	return this;
}
