flagtoExecuteQuickView = true;

_ieversion = 99;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
	var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ieversion >= 8)
		_ieversion = 8;
	else if (ieversion >= 7)
		_ieversion = 7;
	else if (ieversion >= 6)
		_ieversion = 6;
	else if (ieversion >= 5)
		_ieversion = 5;
}

var img_Perspective = "1";
var productImages = {};
var colorWithImage = '';
var itemConfigurableArrQV = [];


jQuery( document ).ready( function(){
	j$('#zoomLarge').jqzoom(zoomoptionsQV);
});


function getSizes(colorId, itemId) {
	DWREngine._execute(_cfCatLevelsLocation, null, 'getSizesByColorItem',
			itemId, colorId, updateSelect_result);
}

function resetZoom(){
	jQuery('#zoomLarge').unbind();
	jQuery('#zoomLarge').jqzoom(zoomoptionsQV);
}

function updateSelect_result(result) {
	_affect = 'sizeId-0';
	_resultKeys = new Array('SKU_ID', 'SIZE_DESC');
	dropDownSetRelation(result)
	
}

function showQuickViewAlert(obj) {
	showQuickviewLoading();
	var path, targetAlert, cssClass, closeBText, itemid, colorid, date_published, flagid, anchorid, hideclosebtn, parentClass, callbackFct;

	if (flagtoExecuteQuickView) {
		flagtoExecuteQuickView = false;
		path = (!obj.path) ? '' : obj.path;
		targetAlert = (!obj.target) ? 'alertbox' : obj.target;
		cssClass = (!obj.cssClass) ? 'alertbox' : obj.cssClass;
		closeBText = (!obj.closeBText) ? 'X' : obj.closeBText;
		itemid = (!obj.itemid) ? '' : obj.itemid;
		colorid = (!obj.colorid) ? '' : obj.colorid;
		date_published = (!obj.date_published) ? '' : obj.date_published;
		anchorid = (!obj.anchorid) ? '' : obj.anchorid;
		hideclosebtn = (!obj.hideclosebtn) ? 'false' : obj.hideclosebtn;
		parentClass = (!obj.parentClass) ? '' : obj.parentClass;
		callbackFct = (!obj.callbackFct) ? 'callText_Result' : obj.callbackFct;
		
		img_ColorID = colorid;
		
		//_callHasSpecialAction = 'resetBoxesNormalDropDownToObject('+colorid+')';

		if ($('generalFlagID'))
			flagid = $('generalFlagID').value;
		else
			flagid = '';

		var args = {
			PATH : path,
			DIVID : targetAlert,
			CSSCLASS : cssClass,
			PARENTCLASS : parentClass,
			CLOSEBUTTONTEXT : closeBText,
			ITEMID : itemid,
			COLORID : colorid,
			DATE_PUBLISHED : date_published,
			FLAGID : flagid,
			ANCHORID : anchorid,
			HIDECLOSEBTN : hideclosebtn
		};

		j$(".quickViewButton").each(function() {
			if (j$(this).hasClass("displayBlock")) {
				j$(this).removeClass("displayBlock").addClass("displayNone");
			}
		});

		if (callbackFct == 'callText_Result') {
			j$(".alertboxQuickView").remove();
			j$(".ui-dialog").remove();
			j$(".ui-widget-overlay").remove();
			showQuickviewLoading();
		}

		j$
				.post(
						siteUrl
								+ "/com/b2c/genericProxy.cfc?returnFormat=json&method=getQuickView",
						args, eval(callbackFct), "json");

	}

}

function showQuickViewAlert_result(result) {
	j$("#formContainer").html( result.CONTENT );
	j$.globalEval(result.JS);
	j$("#qvLoading").hide();
}

function showModelQuickViewAlert(obj) {

	var path, targetAlert, cssClass, closeBText, modelid, colorid, date_published, flagid, anchorid, hideclosebtn, parentClass, callbackFct;

	if (flagtoExecuteQuickView) {
		flagtoExecuteQuickView = false;
		path = (!obj.path) ? '' : obj.path;
		targetAlert = (!obj.target) ? 'alertbox' : obj.target;
		cssClass = (!obj.cssClass) ? 'alertbox' : obj.cssClass;
		closeBText = (!obj.closeBText) ? 'X' : obj.closeBText;
		modelid = (!obj.modelid) ? '' : obj.modelid;
		colorid = (!obj.colorid) ? '' : obj.colorid;
		date_published = (!obj.date_published) ? '' : obj.date_published;
		anchorid = (!obj.anchorid) ? '' : obj.anchorid;
		hideclosebtn = (!obj.hideclosebtn) ? 'false' : obj.hideclosebtn;
		parentClass = (!obj.parentClass) ? '' : obj.parentClass;
		itemid = '';

		//_callHasSpecialAction = 'resetBoxesNormalDropDownToObject('+colorid+')';

		if ($('generalFlagID'))
			flagid = $('generalFlagID').value;
		else
			flagid = '';

		var args = {
			PATH : path,
			DIVID : targetAlert,
			CSSCLASS : cssClass,
			PARENTCLASS : parentClass,
			CLOSEBUTTONTEXT : closeBText,
			ITEMID : itemid,
			COLORID : colorid,
			DATE_PUBLISHED : date_published,
			FLAGID : flagid,
			ANCHORID : anchorid,
			HIDECLOSEBTN : hideclosebtn,
			MODELID : modelid
		};

		j$(".quickViewButton").each(function() {
			if (j$(this).hasClass("displayBlock")) {
				j$(this).removeClass("displayBlock").addClass("displayNone");
			}
		});

		j$(".alertboxQuickView").remove();

		j$
				.post(
						siteUrl
								+ "/com/b2c/genericProxy.cfc?returnFormat=json&method=getQuickView",
						args, callText_Result, "json");

	}

}

function resetBoxesNormalDropDownToObject() {
	values = $('colorDropDown').value.split('@')[0].split('~');
	texts = $('colorDropDown').value.split('@')[1].split('~');
	SingleColortexts = $('colorDropDown').value.split('@')[2].split('~');
	selectedColorID = (arguments.length > 0) ? arguments[0] : "";

	$("colorQuickView-0").options.length = 1;
	lasnN = 0;
	for (x = 1; x < values.length; x++) {
		try {
			colorID = values[x].split('|')[0];
			if (colorID == selectedColorID) {
				lasnN = x;
			}
		} catch (e) {
		}
		$("colorQuickView-0").options[x] = new Option(SingleColortexts[x], values[x]);
	}

	if ($('colorQuickView-0').options.length == 2) {
		$('colorQuickView-0').style.display = 'none';
		$('colorLabel').style.display = 'none';

		Cval = $('colorQuickView-0').options[1].value;
		Cval = Cval.split('|');
		Cval = Cval[0];

		if (Cval != 'NOCOLOR') {
			$('singleColor').innerHTML = SingleColortexts[1];
			$('colorLabel').style.display = 'block';
		}
	}

	if (lasnN == 0) {
		lasnN = $('colorQuickView-0').options.length - 1;
	}
	$('colorQuickView-0').options[lasnN].selected = true;
	dropDownColorActionsQuickView($('colorQuickView-0').options[lasnN].value);
	flagtoExecuteQuickView = true;
}

function dropDownColorActionsQuickView(img_value, imgPath) {

	img_value = img_value.split('|');
	img_ColorID = img_value[0];
	img_price = img_value[1];
	img_priceOnSale = img_value[2];
	img_memberPrice = img_value[7];
	img_image = img_value[4];
	img_priceMinSale = img_value[11];
	img_priceMaxSale = img_value[12];
	img_priceMinPermanent = img_value[13];
	img_priceMaxPermanent = img_value[14];
	if (img_value[8])
		convertPrice = img_value[8];
	if (img_value[9])
		convertPriceONSale = img_value[9];
	if (img_value[10])
		convertPriceMember = img_value[10];
	if (img_value != '') {

		if (translate.showDollarAmountSaveOnSalePrice == 'true') {
			if (img_priceOnSale != '' && img_priceOnSale != '0.00') {
				tempDiff = img_price - img_priceOnSale;
				priceDescription = ''
				priceDescription = translate.productprice + ': <strike>'
						+ img_price + '</strike>';
				priceDescription = priceDescription + '<br>' + img_priceOnSale
						+ ' ' + translate.yousave + ' ' + tempDiff.toFixed(2);
			} else {
				priceDescription = ''
				priceDescription = translate.productprice + ' ' + img_price;
			}
		} else {
			priceDescription = '';
			if (img_priceOnSale != '' && img_priceOnSale != '0.00') {
				if (img_priceMinSale != img_priceMaxSale)
					priceDescription = '<span class="startingAt">'
							+ translate.priceStartingAt + '</span> ';
				priceDescription = priceDescription + '<strike><span id="regularPrice">'
						+ img_priceMinPermanent + '</span></strike><span id="currentDisplayedPrice">'
						+ img_priceMinSale + '</span>';
			} else {
				if (img_priceMinPermanent != img_priceMaxPermanent)
					priceDescription = priceDescription
							+ '<span class="startingAt">'
							+ translate.priceStartingAt + ' '
							+ img_priceMinPermanent + '</span> ';
				else
					priceDescription = priceDescription + img_priceMinPermanent;
			}
		}
		if (img_memberPrice != '' && img_memberPrice != '0.00') {
			priceDescription = priceDescription + '<br>' + img_memberPrice;
		}
		if (img_value[8])
			priceDescription = priceDescription + '<br> ( ' + convertPrice;
		if (img_value[9])
			priceDescription = priceDescription + ' | ' + convertPriceONSale;
		if (img_value[10])
			priceDescription = priceDescription + ' | ' + convertPriceMember;
		if (img_value[8])
			priceDescription = priceDescription + ' )';

		if (imgPath) {
			if (img_value[4])
				$("zoomSmall").src = imgPath + '/' + img_image;
		}

		if ($("isNewInd")) {
			if ($("isNewInd").value == 'Y')
				priceDescription = priceDescription + ' | '
						+ translate.newLabel;
			;
		}

		if ($("productPrice")) {
			$("productPrice").innerHTML = priceDescription;
		}

		/*bypassImageZoomer = false;
		if(_itemId=='')
		{*/
		_itemId = img_value[3];
		bypassImageZoomer = true;
		/*}
		 */

		img_Perspective = 1;

		setAlternateViewThumbnails();
		updateMainImageFromThumbnail( img_ColorID, 1 );
		getSizesDetailsQuickView(img_ColorID, _itemId);
		

	}
}

function quickViewSwapColorGroupImages(s){
	var j$OverImg = j$('#quickViewColorGroupSwatchImageOver_'+s).val();
	var j$ToSwatch = j$('#quickViewColorGroupSwatchImage_'+s);
	var j$ToOriginal = j$('img[id^="quickViewColorGroupSwatchImage_"]');

	j$ToOriginal.each(function(){
		var j$loc = j$(this);
		var j$ThisColorId = j$loc.attr('id').split('_',2)[1];
		var j$ImgSrc = j$('#quickViewColorGroupSwatchImg_'+ j$ThisColorId).val();						
		var j$ImgPos = j$('#quickViewColorGroupSwatchImage_'+ j$ThisColorId);
		//j$ImgPos.attr('src',j$ImgSrc);//Sets to the original Image
		j$ImgPos.css('border','1px solid #FFF');
		
	});
	
	//j$ToSwatch.attr('src',j$OverImg);//Changes the Image
	j$ToSwatch.css('border','1px solid #b5b7b4');

}//Closes SwapColor

function CheckOutOfStock(sizeObj) {
	val = sizeObj.options[sizeObj.selectedIndex].text;
	val = val.toUpperCase();

	if (val.indexOf('OUT OF STOCK') >= 0) {
		showAlert({
			message : 'Cannot select size that is OUT OF STOCK',
			closeBText : 'x',
			cssClass : 'alertboxSmall'
		});
		sizeObj.options[0].selected = true;
	}
	if ($("ItemPriceValueID")
			&& sizeObj.options[sizeObj.selectedIndex].id != '') {
		img_value = sizeObj.options[sizeObj.selectedIndex].id.split('~');
		img_price = img_value[0];
		img_priceOnSale = img_value[1].replace('ON SALE', '');

		if (img_priceOnSale != '') {
			$("ItemPriceValueID").innerHTML = img_price + '<br/> '
					+ img_priceOnSale;
		} else {
			$("ItemPriceValueID").innerHTML = img_price;
		}

	}

}

function updateSelectQuickView_result(result) {
	var sb = $("SizeSelectBoxQuickView-0");
	if (sb) {
		if (sb.options.length == 0 || sb.options[0].value != "")
			initialLength = 0;
		else
			initialLength = 1;

		// Clear the current drop down box value
		$("SizeSelectBoxQuickView-0").options.length = initialLength;
		// alert(result);
		// Build drop down box here.

		checkDifferentPrices = 0;
		differentPriceCheck = "";
		//Check if different prices for items
		for (i = 0; i < result.length; i++) {
			if (differentPriceCheck != result[i].PRICE) {
				differentPriceCheck = result[i].PRICE;
				checkDifferentPrices = checkDifferentPrices + 1;
			}
		}
		for (i = 0; i < result.length; i++) {
			addToSizeDropDown = true;

			if (result[i].INSTOCK == 'false') {
				sizeTEXT = result[i].SIZE_DESC + ' (Out of Stock)';

				if ($('showOutOfStockInSizes').value == 'false') {
					addToSizeDropDown = false;
				}
			} else {
				sizeTEXT = result[i].SIZE_DESC;
			}

			if (checkDifferentPrices > 1)
				sizeTEXT = sizeTEXT + ': ' + result[i].PRICE;

			if (addToSizeDropDown) {
				optObj = new Option(sizeTEXT, result[i].SKU_ID, false);
				// Append the new option...
				sb.options[i + initialLength] = optObj;

				if (result.length == 1) {
					tempText = sb.options[initialLength].text;

					if (tempText.indexOf('Out of Stock') < 0)
						sb.selectedIndex = initialLength;

					//if only one size is available
					if ($('singleSize')) {
						//If the size is UNIQUE don't display anything in size, else display the only size and price available
						sb.style.display = 'none';
						if (jQuery.trim($('singleSize').innerHTML) == 'UNIQUE') {
							$('sizeLabel').style.display = 'none';
						} else {
							//Display the only size available
							$('singleSize').innerHTML = sizeTEXT;
							$('singleSize').style.display = 'block';

							//Display the only price available
							$('singlePrice').innerHTML = result[i].PRICE;
							$('singlePrice').style.display = 'block';
							$('productPrice').style.display = 'none';
						}
					}
				} else {
					if ($('singleSize')) {
						if (sb.style.display == 'none') {
							sb.style.display = 'block';
							$('productPrice').style.display = 'block';
							$('singleSize').style.display = 'none';
							$('singlePrice').style.display = 'none';
						}

					}
				}

				if (result[i].PRICE) {
					sb.options[i + initialLength].id = result[i].PRICE + '~'
							+ result[i].TEMPSALEPRICE;
				} else {
					sb.options[i + initialLength].id = '';
				}

			}

		}

		$('QuickViewAddcartProcessing').style.display = 'none';
		$('QuickViewAddcartButton').style.display = 'block';
	}
}

function resetBoxesToObject() {
	_listMenus = new _ListMenu();
	_listMenus.init();
	_listMenus.openBoxEvent = 'click';
	_listMenus.maxHeight = 150;

	//color box
	values = $('colorDropDown').value.split('@')[0].split('~');
	texts = $('colorDropDown').value.split('@')[1].split('~');
	SingleColortexts = $('colorDropDown').value.split('@')[2].split('~');

	_listMenus.initJsDropDown();

	_listMenus.menuName = 'colorId-0';
	_listMenus.mainClass = 'menuDrop long swatchHeight adjustMargin';
	_listMenus.setDropDownSize('showAll');

	_listMenus.setMenuStructure();

	myMenu = _listMenus.setMenuObjects(values, texts);

	_listMenus.appendMenu($('lineItem_color_0'), myMenu);

	_listMenus.assignEvent("dropDownColorFromQuickView('colorId-"
			+ _listMenus.currentObjectN + "')", _listMenus.currentObjectN);

	objEvent = getObjectEvent($('colorId-0_' + _listMenus.currentObjectN));
	_listMenus.setCurrentMenu(objEvent);
	currentMenu = _listMenus.getCurrentMenu();

	selectMenu($('colorId-0_1'));
	_affect = 'sizeID-0';

	//size box

	_listMenus.initJsDropDown();

	sizeValues = new Array();
	sizeValues[0] = '';

	sizeTexts = new Array();

	if ($('actualCurLang').value == 'fre')
		sizeTexts[0] = 'Taille';
	else
		sizeTexts[0] = 'Size';

	_listMenus.menuName = 'sizeID-0';
	_listMenus.mainClass = ' menuDrop short adjustMargin';
	_listMenus.setDropDownSize('colapse');

	_listMenus.setMenuStructure();

	myMenu = _listMenus.setMenuObjects(sizeValues, sizeTexts);

	_listMenus.appendMenu($('lineItem_size_0'), myMenu);

	//qty box
	_listMenus.initJsDropDown();
	qtyValues = new Array();
	qtyValues[0] = '1';
	qtyValues[1] = '2';
	qtyValues[2] = '3';
	qtyValues[3] = '4';

	qtyTexts = new Array();
	qtyTexts[0] = '1';
	qtyTexts[1] = '2';
	qtyTexts[2] = '3';
	qtyTexts[3] = '4';

	_listMenus.menuName = 'qty-0';
	_listMenus.mainClass = ' menuDrop xshort adjustMargin';
	_listMenus.setDropDownSize('colapse');

	_listMenus.setMenuStructure();

	myMenu = _listMenus.setMenuObjects(qtyValues, qtyTexts);

	_listMenus.appendMenu($('lineItem_qty_0'), myMenu);

	objEvent = getObjectEvent($('qty-0_' + _listMenus.currentObjectN));
	_listMenus.setCurrentMenu(objEvent);
	currentMenu = _listMenus.getCurrentMenu();

	selectMenu($('qty-0_0'));

	$('menuBody_1').style.width = "90px";

}

function setAlternateViewThumbnails() {
	color = img_ColorID;
	psp = img_Perspective;

	while (color.indexOf('-') >= 0) {
		color = color.replace('-', '_');
	}
	if (color && psp) {
		//alert( "setAlternatateViewThumbnails() was called");
		p = eval('img_path_' + color);
		p1 = eval('img_img_1_' + color);
		p2 = eval('img_img_2_' + color);
		p3 = eval('img_img_3_' + color);

		for (i = 1; i <= 3; i++) {
			document.getElementById('img_img_' + i).src = p + eval("p" + i);
			if (eval("p" + i) == "no_image.jpg")
				j$("#alternate_" + i).hide();
			else
				j$("#alternate_" + i).show();
		}
	}
}

function updateMainImageFromThumbnail(color, perspective) {
	var smallImage = "/_static/webUpload/731/"
			+ productImages["color_" + color + "_" + perspective]["main"][0];
	var largeImage = "/_static/webUpload/731/"
			+ productImages["color_" + color + "_" + perspective]["large"][0];

	jQuery("#zoomSmall").attr("src", smallImage);
	jQuery("#zoomLarge").attr("href", largeImage);
	
	resetZoom();

}

function getSizesDetailsQuickView(colorId, itemId) {
	//if ($('productTypeQuickView').value != 'SINGLE_SKU') {
		
		DWREngine._execute(_cfCatLevelsLocation, null, 'getSizesByColorItem',itemId,colorId,actualLang,showOutOfStockSizes,updateSizeLinks);

	//} else {
	//	$('QuickViewAddcartProcessing').style.display = 'none';
	//	$('QuickViewAddcartButton').style.display = 'block';
	//}
}


function updateSelectedSize(sizeOption) {
	sizeOptionArray = sizeOption.id.split("_");
	jQuery("#SizeSelectBox-0").val(sizeOptionArray[1]);
	jQuery("#sizeLinks a").removeClass("selected");
	jQuery("#" + sizeOption.id).addClass("selected");

	//If this size has a price associated with it, update the price on the page
	if (jQuery("#" + sizeOption.id + "[alt]").length) {
		oldPrice = jQuery("#" + sizeOption.id ).attr( "regularprice" );
		jQuery("#regularPrice").html( oldPrice );
		newPrice = jQuery("#" + sizeOption.id).attr("alt");
		jQuery("span.startingAt").hide();
		if(jQuery("#currentDisplayedPrice").size() > 0)
			jQuery("#currentDisplayedPrice").html(newPrice);
		else
			jQuery("#productPrice").html(newPrice);
	}

}

function addSkusToFavorites_Result(result) {

	j$(".alertboxQuickView").remove();
	j$(".ui-dialog").remove();
	j$(".ui-widget-overlay").remove();
	ShowWishListPopUpDelay();

}

function callBackAddToCart(result) {
	closeQuickView();
	jQAddStyleToCart_Result(result);
}

function closeQuickView() {
	j$(".alertboxQuickView").remove();
	j$(".ui-dialog").remove();
	j$(".ui-widget-overlay").remove();
}

//Video player related methods
function toggleVideo(videoPath) {

	if (jQuery("#videoPlayer").hasClass("hideDiv")) {
		playVideo(videoPath);
	} else {
		stopVideo();
	}
}

function playVideo(videoPath) {

	var divHTML = '<object width="290" height="360" align="" id="VideoFile" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
	divHTML += '<param value="' + videoPath + '" name="movie">';
	divHTML += '<param value="high" name="quality">';
	divHTML += '<param value="#ffffff" name="bgcolor">';
	divHTML += '<param value="transparent" name="wmode">';
	divHTML += '<embed width="290" height="360" wmode="transparent" align="" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="VideoFile" bgcolor="#ffffff" quality="high" src="'
			+ videoPath + '">';
	divHTML += '</object>';

	jQuery("#videoPlayer").removeClass("hideDiv");
	jQuery("#videoPlayer").addClass("showDiv");

	//jQuery("#videoPlayer").html(divHTML);
	//the line of code below was requested by Simons as per SIM-1626
	jQuery("#videoPlayer").get(0).innerHTML = (divHTML);
	
}

function stopVideo() {
	jQuery("#videoPlayer").html("");
	jQuery("#videoPlayer").removeClass("showDiv");
	jQuery("#videoPlayer").addClass("hideDiv");

}
