/********************************/
/* JQuery inits for Yves Rocher */
/********************************/

<!-- 
// Set ajax defaults
$(document).ready(function() { 

	$.ajaxSetup({
		global: true,
		type: 'GET',
		dataType: 'html',
		cache: false
	 });

});
//-->

<!--
// Manage color div
function manageColorDiv(productId, productCase, nbView) {
	manageColorDiv(productId, productCase, nbView, null);
}
//-->

<!-- 
// Manage color div
function manageColorDiv(productId, productCase, nbView, jsFunction) {
	var id = productId + productCase;
	var divClicked = '.currentColor' + id;
	var divSelect = '.select' + id;
	var	divToShow = divSelect + ' .nuancier';
	var colorLink = divToShow + ' a';
	
	// deal with divToShow showing and hiding when click event occurs 
	$(divClicked).click(
		function() {
			if ($(divToShow).css('display')=='none'){
				$(divToShow).show();
				$(divToShow).bgiframe();
				return false;
			}
			else if ($(divToShow).css('display')=='block'){
				$(divToShow).hide();
				return false;
			}
	});

	// deal with divToShow hiding when blur event occurs (doesn't work with chrome)
	if (productCase != "productPushNavMenu") {
		var mouseOver = false;
		$(divToShow).mouseover(
			function() {
				mouseOver = true;
				return false;
			});
			
		$(divToShow).mouseout(
			function() {
				mouseOver = false;
				return false;
			});
	
		$(divClicked).blur(
			function() {
				if (mouseOver) {
					return false;
				}
				$(divToShow).hide();
				return false;
		});
	}
	// deal with color selected changing
	appliColorChanges(colorLink, productId, productCase, nbView, jsFunction);	
}
//-->

<!-- 
// Manage color navigator
function manageColorNav(productId, productCase, nbView) {
	manageColorNav(productId, productCase, nbView, null);
}
//-->

<!-- 
// Manage color navigator
function manageColorNav(productId, productCase, nbView, jsFunction) {
	var id = productId + productCase;
	var colorToChange = '.colors' + id;
	var colorLink = colorToChange + ' a';
	
	// deal with color selected changing
	appliColorChanges(colorLink, productId, productCase, nbView, jsFunction);

}
//-->

<!--
// Manage window rollover
function manageWindowRoll(productId, productCase) {
	var id = productId + productCase;
	var windowToChange = '.window' + id;	
	$(windowToChange).mouseover(
		function() {
			switchDirectory(windowToChange, '2', 'window')
	});
	$(windowToChange).mouseout(
		function() {
			switchDirectory(windowToChange, '1', 'window')
	});
	return false;
}
//-->

<!--
// Appli color changes
function appliColorChanges(colorLink, productId, productCase, nbView, jsFunction) {
	var id = productId + productCase;
	var	divToShow = '.select' + id + ' .nuancier';
	var imgToChange = '.select' + id + ' .currentColor img';
	var labelToChange = '.colorLabel' + id;
	var idToChange = '.colorId' + id;
	var colorToChange = '.colors' + id;
	
	var singleViewToChange = '.thumbnail' + id;
	var multipleViewToChange = '.thumbnails' + id;
	var detailProductToChange = '.items' + id;
	var zoomToChange = '.items' + id;
	var windowToChange = '.window' + id;
	var formToChange = '#addform' + id;
	var botanicalImgToChange = '#botanical' + id;
	
	$(colorLink).click(
		function() {
			var classIndex = $(this).attr('class');
			var colorIndex = classIndex.substr(1);
			
			// change the color src
			var colorSrc = $(this).find('img').attr('src');
			$(imgToChange).attr('src', colorSrc);

			// change the color labels (colors.ftl and productpane.ftl)
			colorLabel = 'COLOR' + id + '[' + colorIndex + '].label';
			$(labelToChange).html(eval(colorLabel));

			// change the color id (reference.ftl)
			var colorId = 'COLOR' + id + '[' + colorIndex + '].id';
			var color = eval(colorId);
			$(idToChange).html(color);
			
			// set selected color with ajax
			$.ajax({
				url: '/control/setSelectedVariantProductId/?virtualProductId=' + productId + '&variantProductId=' + color,
				cache: false,
				success: function(data){}
			   }
			);
			//alert('productId : '+productId);
			//alert('color : '+color);
			// change the single view
			switchImages(1, singleViewToChange, 'view', color);
			
			//change the multiple views
			switchImages(nbView, multipleViewToChange, 'view', color);
			
			// change the multiple detail_product
			switchImages(nbView, detailProductToChange, 'detail_product', color);

			// change the multiple zoom
			switchImages(nbView, zoomToChange, 'zoom', color);
			
			// change the single window
			switchImages(1, windowToChange, 'window', color);
			
			// other colors (viewsandcolors.ftl)
			$(".navigators " + colorToChange + " li[class='active']").removeClass('active');
			$(".navigators " + colorToChange + " a[class='" + classIndex + "']").parent().addClass('active');
			
			// hide div if needed
			if ($(divToShow).css('display')=='block'){
				$(divToShow).hide();
			}
			
			// change the variant id for the addtocart
			$(formToChange).find("input[name='add_product_id']").attr('value',color);
			
			//ref
			var nRef = 'product_minidetail_'+productId;
			document.getElementById(nRef).value = color;
			//alert(document.getElementById(nRef).value);
			
			// change the botanical image
			var imgId = 'IMG' + id + '[' + colorIndex + '].url';
			$(botanicalImgToChange).attr('src', eval(imgId));

            if (jsFunction) {
            	var jsFunctionToEval = 'function jsFunction(arg){ return '+ jsFunction + '(arg); }';
				eval(jsFunctionToEval);
                function useFunction(varColor) {
                	jsFunction(varColor);
                }
                useFunction(color);
            }

			return false;
	});
	
}
//-->

<!--
// Appli directory switch
function switchDirectory(imageToChange, numView, type) {
	var imgToChange = $(imageToChange + ' .t0').find('img');
	var srcToChange = imgToChange.attr('src');
	var pattern = '(^.*\/' + type + ')\[0-9\](\/\[A-Z|0-9\]*\.jpg)$';
	var expr = new RegExp(pattern);
	if (expr.test(srcToChange)) {
		var result = expr.exec(srcToChange);
		imgToChange.attr('src', result[1] + numView + result[2]);
	}
}

<!-- 
// Appli images switch
function switchImages(nbView, imageToChange, type, colorId) {
	for (var i=0; i<nbView; i++) {
		var imgToChange = $(imageToChange + ' .t' + i).find('img');
		var srcToChange = imgToChange.attr('src');
		var pattern = '(^.*\/' + type + (i+1) + '\/)\[A-Z|0-9\]*(\.jpg)$';
		var expr = new RegExp(pattern);
		if (expr.test(srcToChange)) {
			var result = expr.exec(srcToChange);
			imgToChange.attr('src', result[1] + colorId + result[2]);
		}
	}
}
//-->

<!-- 
// Open tutorial modal popin
function openModalTutorial(url) {
	return openModalTutorial(url, null);
}
//-->

<!-- 
// Open tutorial modal popin
function openModalTutorial(url, waitingMsg) {
	return openModalPopin('tutorial', waitingMsg, url);
}
//-->

<!-- 
// Open modal popin
function openModalPopin(target, waitingMsg, url) {
	return openModalPopin(target, waitingMsg, url, null);
}
//-->

<!-- 
// Open modal popin with callback
function openModalPopin(target, waitingMsg, url, callback) {

	processModalPopin(target, waitingMsg);
	
	var targetToLoad = '#' + target + 'Modal';
	
	$.ajax({
		url: url,
		success: function(data){
			$(targetToLoad + ' .inner').html(data);
			$('#zoomExtended .scrollbarExtended').jScrollPane({showArrows:true});
			if (target == "compare") centerDivY(target);
			if (callback) callback();
		}
	   }
	);

	return false;

}
//-->



<!-- 
// Open modal cart popin
function openModalCartPopin(productId, qty, price, timezone, hbxAccountNo, hbxCategory, productName, target, waitingMsg, url, cmSrc, categoryId, coremetricsCategoryId) {
	
	processModalPopin(target, waitingMsg);
	sendManualPageViewTag("addToCartPopin");
	var targetToLoad = '#' + target + 'Modal';

	$.ajax({
		url: url,
		success: function(data){
			$(targetToLoad + ' .inner').html(data);
			addToCart(productId, qty, price, timezone, hbxAccountNo, hbxCategory, productName, null, null, cmSrc, categoryId, coremetricsCategoryId);
		}
	   }
	);

	return false;

}
//-->

<!-- 
//ppo : 20092010 : Open modal cart popin, for tutoriel pages (add multiple items)
function openTutorielModalCartPopin(numberOfProductsExceptGifts,errorMsg) {
	
		var target = 'addCart' ;
		var url = '/control/addtocartpopintutoriel/~numberOfProductsExceptGifts='+numberOfProductsExceptGifts+'/~errorMsg='+errorMsg;
		
		processModalPopin(target, false);
		sendManualPageViewTag("multipleAddToCartPopin");
		var targetToLoad = '#' + target + 'Modal';

		$.ajax({
			url: url,
			success: function(data){
				$(targetToLoad + ' .inner').html(data);
			}
		   }
		);
	return false;
}
//-->

<!-- 
// Update modal cart popin
function updateModalCartPopin(productId, qty, price, timezone, hbxAccountNo, hbxCategory, hbxProductName, url, cmSrc, categoryId, coremetricsCategoryId) {

	$.ajax({
		url: url,
		success: function(data){
			$('#with-or-without-errors').html(data);
			addToCart(productId, qty, price, timezone, hbxAccountNo, hbxCategory, hbxProductName, null, null, cmSrc, categoryId, coremetricsCategoryId);
		}
	   }
	);

	return false;

}
//-->

<!-- 
// Update modal cart popin
function loadCrossSellModalCartPopin(url) {

	$.ajax({
		url: url,
		dataType: 'html',
		success: function(data){
			$('#addCartScrollable .items').html(data);
		}
	   }
	);

	return false;

}
//-->

<!-- 
// Update modal list popin
function loadStepModalListPopin(url, target) {
	$.ajax({
		url: url,
		success: function(data){
			$(target).html(data);
		}
	   }
	);
	return false;
}
//-->

<!--
// Display modal popin
function displayModalPopin(target) {
	processModalPopin(target, null);
	return false;
}
//-->

<!--
// Process sampleSelected
function sampleSelected(url) {
				$.ajax({
					url: url ,
					success: function(data){
						$('#sample_popin').html(data);
						var hidden = $('#sample_popin').find("#sendManualPageViewTag");
						if(hidden.length > 0){
							sendManualPageViewTag("sampleAddToCartPopin");
						}
						$('#sample_popin').show();
						centerDiv("sample_popin");
					}
				   }
				);
}
//-->

<!--
// Process modal popin
function processModalPopin(target, waitingMsg) {

	var targetToLoad = '#' + target + 'Modal';
	if (waitingMsg) {
		$(targetToLoad + ' .inner').html(waitingMsg);
	}
	Overlay = true;
	var positionCss = 'fixed';
	if (jQuery.browser.msie) positionCss = 'absolute';
	$(targetToLoad).prependTo('body');
	
	var zindex = $(targetToLoad).css('z-index');
	var overLayZIndex = zindex - 1;

	if (Overlay==true) {
		// this variable corrects the bug display id 0012521 under ie6, ie7
		var scrollHeigh = document.documentElement.scrollHeight;		
		var overLay = $('<div id="'+target+'Overlay"></div>').css({'background-color':'#000000', height:scrollHeigh, width:'100%', position:positionCss, left:0, top:0, 'z-index':overLayZIndex, opacity:'0.5', 'display':'block'});
   	}
   	else {
    	var overLay = $('<div id="'+target+'Overlay"></div>').css({'display':'none'});
   	}

	if ($('#' + target + 'Overlay').size() < 1) {
   		$('body').append(overLay);
   		$('#' + target + 'Overlay').bgiframe();
   	}

	$(targetToLoad).show();
	
	if (target!="zoom") {
		centerDivY(target);
	}
}
//-->


<!-- 
// Open modal popin
function closeModalPopin(target) {
	var targetToHide = '#' + target + 'Modal';
	$(targetToHide).hide();
	$('#' + target + 'Overlay').remove();
}
//-->

<!-- 
// Set modal close links behaviour
$(document).ready(function() { 
	$('.modal .close').click(
		function() {
			closeModalPopin($(this).parent().attr('id'));
	 });
});
//-->
