



//Preload the loading image for both uc and qv.
preload_QV_UC_loading = new Image();
preload_QV_UC_loading.src = "/assets/images/uc_qv/ima-glo-loading.gif";

/* Persistant Cart (universal cart) Javascript */
/* --------------------------------------------- */

// Commands
// persistantCartCommands[0] = "showProduct" action
// persistantCartCommands[1] = "addProduct" action
// persistantCartCommands[2] = "addEnsemble" action
// persistantCartCommands[3] = "remove" action
// persistantCartCommands[4] = "addCatalogItems" action
// persistantCartCommands[5] = "addProductWishlist" action
// persistantCartCommands[6] = "addAllProductsWishlist" action

var persistantCartCommands = new Array(7);
persistantCartCommands[0] = "/checkout/universal_cart.jsp";
persistantCartCommands[1] = "/checkout/add_item_pc.cmd";
persistantCartCommands[2] = "/checkout/add_items_pc.cmd";
persistantCartCommands[3] = "/checkout/delete_item_in_cart.cmd";
persistantCartCommands[4] = "/checkout/add_catalog_order_item_pc.cmd";
persistantCartCommands[5] = "/user/add_wishlist_item_to_basket_pc.cmd";
persistantCartCommands[6] = "/user/add_all_wishlist_items_to_basket_pc.cmd";
//persistantCartCommands[7] = "/user/instore_pickup_zip_json_pc.jsp";

productCompareCommands = new Array(2);
productCompareCommands[0] = "/user/add_item_to_comparison.cmd";
productCompareCommands[1] = "/includes/compare_items.jsp";

var requestURL = "";
var persistantCartContainerId = "#widget-ucart";
var persistantCartCloseButClass = ".widget-ucart-close-but";
var hideTimeOuts= new Array();

var ucartLoadingHTML = 	'<div id="widget-ucart">' +
				 		'  <div id="glo-ucart-top" class="widget-ie6png"><!--  --></div>' +
				  		'  <div id="glo-ucart-body" class="widget-ie6png">' +
				  		'  <div id="glo-ucart-content">'+
						'  <div class="widget-ima-loader"><img src="/assets/images/uc_qv/ima-glo-loading.gif" alt="Loading..." /></div>' +
				  		'  </div>' +
				  		'  </div>' +
				  		'  <div id="glo-ucart-bottom" class="widget-ie6png"><!--  --></div>' +
  				  		'</div>';

var ucartSimpleHTML = 	'<div id="widget-ucart"></div>';

/* Function(s) to Show the Basket Layer */
function showBasket(action,params,refreshPage,refreshDelayTime) {

	var showUcTimer = 0;

	if( (action == "show") || (action == "showFromQuickview") )
	{ showloading(ucartLoadingHTML); requestURL = persistantCartCommands[0]; }
	else if(action == "addProduct")
	{ showloading(ucartSimpleHTML);requestURL = persistantCartCommands[1];}
	else if(action == "addEnsemble")
	{ showloading(ucartSimpleHTML);requestURL = persistantCartCommands[2];}
	else if(action == "remove")
	{ showloading(ucartLoadingHTML);requestURL = persistantCartCommands[3];}
	else if(action == "addCatalogItems")
	{ showloading(ucartSimpleHTML);requestURL = persistantCartCommands[4];}
	else if(action == "addProductWishlist")
	{ showloading(ucartSimpleHTML);requestURL = persistantCartCommands[5];}
	else if(action == "addAllProductsWishlist")
	{ showloading(ucartSimpleHTML);requestURL = persistantCartCommands[6];}
	else
	{ alert("missing action"); }

	requestURL = requestURL;
	params = "ts=" + timestamp() + "&action=" + action + "&" + params;

	$.ajax({
		type: "POST",
		url: requestURL,
		data: params,
		dataType: "html",
      	timeout: 15000,
		success: function(data) {
			hideloading();
			$(persistantCartContainerId).append(data);
			$(persistantCartContainerId).show();
			if (refreshPage != undefined && refreshPage) {
            setTimeout( function() { location.reload(true); }, refreshDelayTime != undefined? refreshDelayTime : 0);
         }
			return true;
		},
		error: function() {
			hideloading();
			return false;
		}
	});
};

function showloading(htmlToShow) {
	$(persistantCartContainerId).remove();

	//load, position, show new cart
	$("#mainDiv").append(htmlToShow);
	positionPersistantCart();
	$(persistantCartContainerId).show();

	// add an event for close layer.
	//$(persistantCartCloseButClass).click(function() { hideBasket(); });
};

function hideloading() {
	$(persistantCartContainerId + " *").remove();
	$(persistantCartContainerId).html("");
};

function addToCart() {
	params =  "productName=" + $("input[@name=productName]").val() +
			  "&productId=" + $("input[@name=productId]").val() +
   	 		  "&categoryId=" + $("input[@name=categoryId]").val() +
   	 		  "&parentCategoryId=" + $("input[@name=parentCategoryId]").val() +
   	 		  "&subCategoryId=" + $("input[@name=subCategoryId]").val() +
   	 		  "&quantity=" + $("input[@name=quantity]").val() +
   	 		  "&productVariantId=" + $("input[@name=productVariantId]").val() +
				  "&productAlias=" + $("input[@name=productAlias]").val() +
				  "&aliasEntered=" + $("input[@name=aliasEntered]").val();

	//see if this is an update.
  	if( $("input[@name=itemGUID]").val().length > 0 )
  	{ params = params + "&itemGUID=" + $("input[@name=itemGUID]").val() + "&isUpdate=1"; }

  	showBasket('addProduct',params);
};

function wishListAddToCart(params,refreshPage,refreshDelayTime) {
	showBasket('addProductWishlist',params,refreshPage,refreshDelayTime);
};

function wishListAddAllToCart(params,refreshPage,refreshDelayTime) {
	showBasket('addAllProductsWishlist',params,refreshPage,refreshDelayTime);
};

function addCatalogOrderItemsToCart() {
    params = "productId=" + $("input[@name=productId]").val() +
  		     "&itemNumber=" + $("input[@name=itemNumber]").val() +
  			 "&productName=" + $("input[@name=productName]").val() +
  			 "&productVariantId=" + $("input[@name=productVariantId]").val() +
  			 "&quantity=" + $("input[@name=quantity]").val();
    showBasket('addCatalogItems',params);
};

function addEnsembleToCart(type) {
	params =  "productName=" + $("input[@name=productName]").val() +
			  "&ensembleId=" + $("input[@name=ensembleId]").val() +
			  "&categoryId=" + $("input[@name=categoryId]").val() +
   	 		  "&parentCategoryId=" + $("input[@name=parentCategoryId]").val() +
   	 		  "&subCategoryId=" + $("input[@name=subCategoryId]").val();

	// iterate through products in the ensemble for variant id
	$(".the-variant-ids").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	// iterate through products in the ensemble for alias
	$(".the-product-aliases").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	// iterate through products in the ensemble to determine if singleVariant
	$(".product-single-var").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	// iterate through products for qty
	$(".the-variant-qtys").each(function() {
		if( type == 'all' )
		{
		  $(this).val("1");
		  params = params + "&" + $(this).attr("name") + "=1";
		}
		else
		{ params = params + "&" + $(this).attr("name") + "=" + $(this).val(); }
	});

	params = params + "&productCount=" + $(".the-variant-ids").length;
	showBasket('addEnsemble',params);
};



function updateHeader(amt) {
	if(amt == 1)
	{ $("#widget-ucart-item-count").text(amt + " Item"); }
	else
	{ $("#widget-ucart-item-count").text(amt + " Items");}
};

function errorAppend(area,msg) {
	$(area).html(msg.replace(/&amp;/g, "&").replace(/&lt;/g,
        "<").replace(/&gt;/g, ">").replace(/&#39;/g, "'"));
	$(area).show();
};

function resetErrorFields() {
	$(".glo-tex-error").hide();
	$(".glo-tex-error").html("");
};

function messageAppend(area,msg) {
	$(area).html(msg);
	$(area).show();
};

function resetMessageFields() {
	$(".glo-tex-info").hide();
};

function clearAllTimeouts() {
	for(x = 0; x < hideTimeOuts.length; x++)
	{ clearTimeout(hideTimeOuts[x]); }
};

function timestamp() { return new Date().getTime(); }

//Edit this function if need to do something special on basket close.
function hideBasket() {
	$(persistantCartContainerId).hide();
	$(persistantCartContainerId).remove();

	shoppingBagBut = $("#widget-header-active-link").eq(0);
	$(shoppingBagBut).attr("id","")
	$(shoppingBagBut).mouseout();
};

//edit this function to update the setup
function setupPersistantCartButtons() {
	// draw focus near this
	window.location = "#";

	$(persistantCartCloseButClass).unbind("click").click(function() {
		hideBasket();
	});
	$(persistantCartCloseButClass).attr("href","javascript:void(0)");
	$("#widget-ucart").bind("mouseleave",function() {
		hideBasket();
	});
	/* setup the timer */
	ucartTimer = setTimeout(function() { hideBasket(); }, 3000);
  	$("#widget-ucart").unbind("mousemove").mousemove(function() {
  		if(typeof ucartTimer != "undefined")
  	  	{ clearTimeout(ucartTimer); }
  	});
  	$("#widget-ucart").hover(
  		function() {},
  		function() {
  			if(typeof ucartTimer != "undefined")
  			{ ucartTimer = setTimeout(function() { hideBasket(); }, 100); }
  		}
  	);
};

//edit this function to position cart.
function positionPersistantCart() {
	newLeft = ($("body").width() / 2) + ( $("#mainDiv").width() / 2 ) - $(persistantCartContainerId).width() - 12;

	$(persistantCartContainerId).css("left", newLeft+"px");
	$(persistantCartContainerId).css("top", "84px");
};


//reset the height of the iframe for IE6 if messages are present. This gets called from universal_cart.jsp.
function iframeHeight() {
	var newHeight = "";
	// the iframe is a conditional element in universal_cart.jsp
   var iframeEl = "#ucartFrame";
	newHeight = $(persistantCartContainerId).height();
	$($(iframeEl)).css("height", newHeight+"px");
};


/* ---------------------------------------- */


/* QuickView Javascript */
/* ---------------------------------------- */

//quickViewcommands[0] = "show" action
//quickViewcommands[1] = "add product" action
var quickViewCommands = new Array(2);
quickViewCommands[0] = "/catalog/quickview.jsp";
quickViewCommands[1] = "/catalog/qv_add_item_pc.cmd";

var quickViewContainerId = "#widget-quickview";
var quickViewCloseButtonsClass = ".close";
var quickViewCloseButtonsAdd = ".widget-quickview-but-add";

var quickviewContainerHTML = '<div id="widget-quickview"><!--[if lte IE 6]><iframe src="javascript:false;" id="qvFrame"></iframe><![endif]--></div>';
var quickviewLoadingHTML = 	'  <div id="cat-quickview-body" class="widget-ie6png">' +
				  			'	 <div id="cat-quickview-content">'+
				  			'	   <div class="widget-ima-loader"><img src="/assets/images/uc_qv/ima-glo-loading.gif" alt="Loading..." /></div>' +
				  			'    </div>' +
				  			'  </div>';


/* Function(s) for QuickView */
function loadQuickView(params,selector) {
	closeQuickView();
	addQuickView(selector);
	ajaxQuickView(quickViewCommands[0],params);

	// if this is an edit, want to have this appear above the cart.
	if( params.indexOf("itemGUID") != -1 )
	{ $(quickViewContainerId).css("z-index", "20"); }
};

function addQuickView(selector) {
	$(selector).append(quickviewContainerHTML);
	$(quickViewContainerId).append(quickviewLoadingHTML);
	adjustQuickviewLocation();
	$(quickViewContainerId).show();
};

function closeQuickView() {
	$(quickViewContainerId).remove();
};

function ajaxQuickView(page,params) {
	params = "rId=" + new Date().getTime() + "&" + params;
	$.ajax({
   		type: "POST",
   		url: page,
   		data: params,
   		dataType: "html",
   		success: function(msg) {
   			$(quickViewContainerId +" *").remove();
   			$(quickViewContainerId).html("");
   			$(quickViewContainerId).append(msg);
   		}
   	});
};

function addProductFromQuickView(params) {
	params = params + "&productName=" + $(quickViewContainerId + " input[@name=productName]").val()  + "&productVariantId=" + $(quickViewContainerId + " input[@name=productVariantId2]").val() + "&quantity=" +$(quickViewContainerId + " input[@name=quantity]").val() +
				  "&productAlias=" + $("input[@name=productAlias]").val() +
			     "&categoryId=" + $("input[@name=categoryId]").val() +
   	 		  "&parentCategoryId=" + $("input[@name=parentCategoryId]").val() +
   	 		  "&subCategoryId=" + $("input[@name=subCategoryId]").val();
   if( $(quickViewContainerId + " input[@name=itemGUID]").size() > 0 )
  	{ params = params + "&itemGUID=" + $(quickViewContainerId + " input[@name=itemGUID]").val(); }
	if( $(quickViewContainerId + " input[@name=onBasketPage]").size() > 0 )
  	{ params = params + "&onBasketPage=" + $(quickViewContainerId + " input[@name=onBasketPage]").val(); }


	$(quickViewContainerId + " select").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	$(quickViewContainerId + " *").remove();
	$(quickViewContainerId).append(quickviewLoadingHTML);
	ajaxQuickView(quickViewCommands[1],params);
};

var qvInterval;
function setupQuickViewButtons() {
	$(quickViewCloseButtonsClass).unbind("click").click(function() { closeQuickView(); });
	$(quickViewCloseButtonsAdd).unbind("click").click(function() {
		params = "productId=" + $(this).attr("id").split("-")[1];
		addProductFromQuickView(params);
	});
	$(quickViewCloseButtonsClass).add( $(quickViewCloseButtonsAdd) ).attr("href","javascript:void(0)");

	qvInterval = setInterval( "adjustQuickviewLocation();" , 100);
	setTimeout(function() { clearInterval(qvInterval); }, 1000);
	adjustQuickviewLocation();
};

function adjustQuickviewLocation()
{
	adjustDivLocation(quickViewContainerId);
}

// Edit this per site to adjust location
function adjustDivLocation(divToAdjust) {
	var bWindowOffsets = getScrollXY();
	var bWindowViewport = getViewportSize();
//	We no longer set a height due to ensemble version being a variable height so we dont use the height to adjust
//	var qvTop = ((bWindowViewport[1] / 2) - ($(divToAdjust).height() / 2)) + bWindowOffsets[1];
	var qvTop = 0;
	if ($(divToAdjust).height() > bWindowViewport[1])
		qvTop = bWindowOffsets[1];
	else
		qvTop = ((bWindowViewport[1] / 2) - ($(divToAdjust).height() / 2)) + bWindowOffsets[1];
	qvTop = (qvTop < 0) ? 100 : qvTop;
	$(divToAdjust).css("top",qvTop+"px");
};
/* ---------------------- */

// Helper Function(s)
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getViewportSize() {
  var vpW = 0, vpH = 0;
  if (typeof window.innerWidth != 'undefined')
  {
    vpW = window.innerWidth,
    vpH = window.innerHeight
  }
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
  {
    vpW = document.documentElement.clientWidth,
    vpH = document.documentElement.clientHeight
  }
  else
  {
    vpW = document.getElementsByTagName('body')[0].clientWidth,
    vpH = document.getElementsByTagName('body')[0].clientHeight
  }
  return [  vpW, vpH ];
};

var iframeFix = function($) {
	return {
		add : function(id ,selector) {
			iframeFix.remove(id);
			iframeHTML = '<div id="' + id + '"><iframe width="100%" height="100%" src="" style="filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);" frameborder="0"></iframe></div>'
			$("body").append(iframeHTML);
			$("#" + id).css("position", "absolute");
			$("#" + id).css("top", $(selector).css("top"));
			$("#" + id).css("left", $(selector).css("left"));
			$("#" + id).css("margin-top", $(selector).css("margin-top"));
			$("#" + id).css("margin-right", $(selector).css("margin-right"));
			$("#" + id).css("margin-bottom", $(selector).css("margin-bottom"));
			$("#" + id).css("margin-left", $(selector).css("margin-left"));
			$("#" + id).css("height", $(selector).height() + "px" );
			$("#" + id).css("width", $(selector).width() + "px" );
			$("#" + id).css("z-index", $(selector).css("z-index") - 1);
		},
		remove : function(id) {
			$("#" + id).remove();
		}
	}
}($);
// ------------------------------------------------

/* Quickview setup */
function setup_quickview() {
	var qTimer;
	var imgHTML = '<a href="#"><img id="widget-quickview-but" class="widget-ie6png" src="/assets/images/uc_qv/but-cat-quickview.png" alt="Quickview" /></a>';
	$(".widget-app-quickview").each(function() {
/* the position relative is now set in the css file to the div classed itemCell */
//		$(this).parent().parent().css("position","relative");
		$(this).mouseover(function() {
			if( typeof qTimer != undefined )
			{
				clearTimeout(qTimer);
				$("#widget-quickview-but").parent().remove();
			}
			$(this).parent().parent().append(imgHTML);
			$("#widget-quickview-but").css("position","absolute");
			$("#widget-quickview-but").css("left", "50px");
/* we no longer need a height so the following line is not needed */
//			qTop = $(".widget-app-quickview", $("#widget-quickview-but").parent().parent()).height() - $("#widget-quickview-but").height() - 2 - 10;
//			$("#widget-quickview-but").css("top", qTop+"px");
/* the following line is a modification of the preceeding line */
			$("#widget-quickview-but").css("top", "190px");
			$("#widget-quickview-but").mouseover(function() {
				if(typeof qTimer != undefined)
				{ clearTimeout(qTimer); }
			});
			$("#widget-quickview-but").parent().click(function(ev) {
				type = $(this).parent().attr("id").split("-")[0];
				id = $(this).parent().attr("id").split("-")[1];
				parentCategoryId = $(this).parent().attr("id").split("-")[2];
				categoryId = $(this).parent().attr("id").split("-")[3];
				subCategoryId = $(this).parent().attr("id").split("-")[4];
				if( type == "p" )
				{ params = "productId="+id+"&parentCategoryId="+parentCategoryId+"&categoryId="+categoryId+"&subCategoryId="+subCategoryId;}
				else
				{ params = "ensembleId="+id+"&parentCategoryId="+parentCategoryId+"&categoryId="+categoryId+"&subCategoryId="+subCategoryId;}
				loadQuickView(params, $("body"));
			});
			$("#widget-quickview-but").parent().attr("href","javascript:void(0)");
		});
		$(this).mouseout(function() {
			qTimer = setTimeout(function() { $("#widget-quickview-but").parent().remove(); }, 100);
		});
	});
};

function setup_quickview_basket() {
	var qTimer;
	var imgHTML = '<a href="#"><img id="widget-quickview-but" class="widget-ie6png" src="/assets/images/uc_qv/but-cart-quickview.png" alt="Quickview" /></a>';
	$(".widget-app-quickview-basket").each(function() {
		$(this).parent().parent().css("position","relative");
		$(this).mouseover(function() {
			if( typeof qTimer != undefined )
			{
				clearTimeout(qTimer);
				$("#widget-quickview-but").parent().remove();
			}
			$(this).parent().parent().append(imgHTML);
			$("#widget-quickview-but").css("position","absolute");
			$("#widget-quickview-but").css("left", "10px");
			qTop = $(".widget-app-quickview-basket", $("#widget-quickview-but").parent().parent()).height() - $("#widget-quickview-but").height() - 2 - 10;
			$("#widget-quickview-but").css("top", qTop+"px");
			$("#widget-quickview-but").mouseover(function() {
				if(typeof qTimer != undefined)
				{ clearTimeout(qTimer); }
			});
			$("#widget-quickview-but").parent().click(function(ev) {
				params = $("span", $(this).parent()).text();
				loadQuickView(params, $("body"));
			});
			$("#widget-quickview-but").parent().attr("href","javascript:void(0)");
		});
		$(this).mouseout(function() {
			qTimer = setTimeout(function() { $("#widget-quickview-but").parent().remove(); }, 100);
		});
	});
};

/* Quickview for Browse Catalog page */
function setup_quickview_catalog() {
	var qTimer;
	var imgHTML = '<a href="#"><img id="widget-quickview-but" class="widget-ie6png" src="/assets/images/uc_qv/but-cat-quickview.png" alt="Quickview" /></a>';
	$(".widget-app-quickview-catalog").each(function() {
		$(this).parent().parent().css("position","relative");
		$(this).mouseover(function() {
			if( typeof qTimer != undefined )
			{
				clearTimeout(qTimer);
				$("#widget-quickview-but").parent().remove();
			}
			$(this).parent().parent().append(imgHTML);
			$("#widget-quickview-but").css("position","absolute");
			$("#widget-quickview-but").css("left", "20px");
			qTop = $(".widget-app-quickview-catalog", $("#widget-quickview-but").parent().parent()).height() - $("#widget-quickview-but").height() - 2 - 10;
			$("#widget-quickview-but").css("top", qTop+"px");
			$("#widget-quickview-but").mouseover(function() {
				if(typeof qTimer != undefined)
				{ clearTimeout(qTimer); }
			});
			$("#widget-quickview-but").parent().click(function(ev) {
				type = $(this).parent().attr("id").split("-")[0];
				id = $(this).parent().attr("id").split("-")[1];
				parentCategoryId = $(this).parent().attr("id").split("-")[2];
				categoryId = $(this).parent().attr("id").split("-")[3];
				subCategoryId = $(this).parent().attr("id").split("-")[4];
				var browseCatalog = $(this).parent().attr("id").split("-")[5];
            var offerId = $(this).parent().attr("id").split("-")[6];
				if( type == "p" )
					{ params = "productId="+id+"&parentCategoryId="+parentCategoryId+"&categoryId="+categoryId+"&subCategoryId="+subCategoryId+"&browseCatalog="+browseCatalog+"&offerId="+offerId;}
				else
					{ params = "ensembleId="+id+"&parentCategoryId="+parentCategoryId+"&categoryId="+categoryId+"&subCategoryId="+subCategoryId+"&browseCatalog="+browseCatalog+"&offerId="+offerId;}
				loadQuickView(params, $("body"));
			});
			$("#widget-quickview-but").parent().attr("href","javascript:void(0)");
		});
		$(this).mouseout(function() {
			qTimer = setTimeout(function() { $("#widget-quickview-but").parent().remove(); }, 100);
		});
	});
};
/* -------- END QUICKVIEW ------------ */


$(function() {
	setup_quickview();
	setup_quickview_basket();
	setup_quickview_catalog();
});
/* -------------------------- */

var arrQVColorNames = new Array();
var arrQVStaticImages = new Array();

function getColorIndexByName(key) {
	for (i = 0; i < arrQVColorNames.length; i++)
	{
		if (key == arrQVColorNames[i])
			return i;
	}
	return -1;
}

function swatchSetup() {
	// setup the large image and change of drop-down
	//colorDropdownId = "#Color_" + $(".widget-swatches").attr("class").split("-")[1] + "Variants";
	//sizeDropdownId = "#Size_" + $(".widget-swatches").attr("class").split("-")[1] + "Variants";
	colorDropdownId = "#Color_qv" + $(".widget-swatches").attr("class").split("-")[2] + "Variants";
	sizeDropdownId = "#Size_qv" + $(".widget-swatches").attr("class").split("-")[2] + "Variants";
	sizeDropdownId = "#Size_qv" + $(".widget-swatches").attr("class").split("-")[2] + "Variants";
	if (showSwatches) {
		//$(".widget-swatches > div").addClass("sw_off");
		$(".widget-swatches a").each(function(i) {
			$(this).unbind("click").click(function() { updateColorDropdown( $("img", $(this)).attr("alt") ); });
			$(this).attr("href", "javascript:void(0);");
		});
		$(sizeDropdownId).change(function() { setTimeout(function() { changeSwatch(); }, 10); });
		$(colorDropdownId).change(function() {
			if( $(this).val() != "" )
			{
				newColor = $(this).val();
				changeColorQV(newColor);
			}
		});
		changeSwatch();
		if ( $(sizeDropdownId).size() > 0 )
		{ updateSizeDropdown( $(sizeDropdownId).val() ); }
		if ( $(colorDropdownId).size() > 0 )
		{ $(colorDropdownId).change(); }
	} else {
		$(colorDropdownId).change(function() {
			changeColorQV();
		});
	}
};

function changeColorQV(passedColor) {
	var colorName = "";
	if (passedColor != null) {
		colorName = passedColor;
	} else {
		var colorDropdownList = document.getElementById("Color_qv"+qvProductId+"Variants");
		colorName = colorDropdownList.options[colorDropdownList.selectedIndex].value;
	}
	setSelectedSwatchQV(colorName);
}

function changeSwatch() {
	resetSwatches();
	showSwatch( $(this).val() );
};

function showSwatch(color) {
	if (color) {
		$(".cat-quickview-swatch[alt=" + color + "]").css("display","inline").css("float","left");
	}
};

function getColorCode(colorName) {
	var colorCode = colorName;
	if (colorName.indexOf(" - ") >= 0) {
		colorCode = colorName.substring(0, colorName.indexOf(" - "));
	}
	return colorCode;
}

function setProductImageQV(colorName) {
	var newMainImage = arrQVStaticImages[getColorIndexByName(colorName)];
	document.getElementById("widget-quickview-main-image").src = newMainImage.src;
}

function updateColorDropdown(colorName) {
	var colorDropdownList = document.getElementById("Color_qv"+qvProductId+"Variants");
	var colorFound = false;
	if (colorDropdownList && colorDropdownList.length > 1) {
		for(i = 0 ; i < colorDropdownList.length; i++ ){
			if (colorDropdownList.options[i].value) {
				currentColorValue = colorDropdownList.options[i].value;
				if (currentColorValue.indexOf(" - ") >= 0) {
					currentColorValue = currentColorValue.substring(0, currentColorValue.indexOf(" - "));
				}

				if((currentColorValue == colorName || colorDropdownList.options[i].value == colorName)) {
					if  (!colorDropdownList.options[i].selected) {
						colorDropdownList.options[i].selected = true;
						__getDropdownSupport(colorDropdownList).updateListsNew(colorDropdownList);
						__getDropdownSupport(colorDropdownList).setVariantField();
						setSelectedSwatchQV(colorDropdownList.options[i].value);
						colorFound = true;
					} else {
						colorFound = true;
					}
					break;
				}
			}
		}
		if (!colorFound) {
			colorDropdownList.selectedIndex = 0;
		}
	}
	setProductImageQV(colorName);
	$(".widget-swatches > div").removeClass("sw_on");
	$("#sw_" + getColorIndexByName(colorName)).addClass("sw_on");
};

function setSelectedSwatchQV(colorName) {
	if (colorName) {
		colorName = getColorCode(colorName);
		setProductImageQV(colorName);
		if (arrColorsTemp) {
			$(".widget-swatches > div").removeClass("sw_on");
			$("#sw_" + getColorIndexByName(colorName)).addClass("sw_on");
		}
	}
}

function updateSizeDropdown(size) {
	$(sizeDropdownId).val(size);
	$(sizeDropdownId).change();
	__getDropdownSupport(document.getElementById(sizeDropdownId.split("#")[1])).updateListsNew(document.getElementById(sizeDropdownId.split("#")[1]));
	__getDropdownSupport(document.getElementById(sizeDropdownId.split("#")[1])).setVariantField();
};

function resetSwatches() { /* $(".cat-quickview-swatch").hide(); */ };
