/*
*	QuickSelect	
*
*	The QuickSelect plugin is a wrapper around the jQuery UI dialog widget that 
*	allows quick select functionality to be placed on any page. 
*
*	Methods:		
*		$(<selector>).QuickSelect(<options>)
*			Constructor. If the jQuery UI dialog options need to be tweaked,
*			an options object can be passed in. If options is null, defaults will be used.
*			See Dialog Widget in the jQuery UI docs for more info.
*
*		$(<selector>).QuickSelect("option", <options>)
*			Changes jQuery UI dialog options. See jQuery UI docs for more info
*
*		$(<selector>).QuickSelect("open", QSdata)
*			Opens the QuickSelect. QSdata 
*
*	Dependencies:
*		jquery-ui-1.7.2.custom.css
*		jquery-v1.3.2.js
*		jquery-ui.min.js
*		json2.js
*		mz-packed.js
*		MagicZoomNEW.css
*		pr_styles_review.css
*		prMerchantOverrideStylesheet.css
*		/Reviews/pwr/engine/js/full.js
*		QuickSelect.aspx
*		QuickSelect.aspx.cs
*/

(function($){
	$.fn.QuickSelect = function(options, QSdata){		
		//QuickSelect element is constructed by invoking the QuickSelect function with either no parameters, or an options object
		if (typeof options == 'object'|| options == null)
		{	
			$.QuickSelect.PopDivLeftFudge = $.browser.msie ? -2 : 0;
			
			var defaults = {
				autoOpen: false,
				bgiframe: true,
				resizable: false,
				minHeight: 400,
				width: $.browser.msie ? 660 : 650,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				}
			};				
			
			var opts = $.extend(defaults, options);
						
			return this.each(function(){
				//only one quick select should be on a page. if the selector contains more than one element, do not create the quick select
				if ($(this).length == 1)
				{
					//the html for the view larger dialog must be present when quick select is instantiated
					$(this).after("<div id='ViewLargerDialog'><div id='ViewLargerWrapper'><div class='LoadingGif'><img src='../../Images/loading.gif' /><br /><span class='LoadingText'>Loading...</span></div><div id='ViewLargerContent'><div id='ViewLargerImageColumn'><div class='PopupLocation'><span class='SwatchPlacement' id='ViewLargerSwatch'></span></div><div id='ViewLargerImageContainer'></div></div><div id='ViewLargerSwatchColumn'><span>VIEW COLORS:</span><div id='ViewLargerSwatches'></div><span>MORE VIEWS:</span><div id='ViewLargerMoreViews'></div><a id='CloseViewLargerLink' onclick='javascript:$.QuickSelect.CloseViewLargerWindow();' href='javascript:void(0);'>close this window</a></div></div></div></div>");
					
					$("#ViewLargerDialog").dialog({
						autoOpen: false,
						bgiframe: true,
						resizable: false,
						width:660,
						modal: true,
						overlay: {
							backgroundColor: '#000',
							opacity: 0.5
						},
						position: [100, 100]
					});
					
					$(this).dialog(opts);
					
				}
			});		
		}		
		else if (typeof options == 'string')
		{
			//here we can expose various methods from the jQuery UI dialog, or create new methods
			switch (options)
			{
				case "option":
					return this.each(function(){
						$(this).dialog("option", QSdata);
					});
					break;	
				case "open":
					return this.each(function(){
						
						//set public properties
						$.QuickSelect.Selector = "#"+$(this).attr('id');
						$.QuickSelect.AddToBagSuccessCallback = typeof QSdata.AddToBagSuccess == 'function' ? QSdata.AddToBagSuccess : function(){};
						$.QuickSelect.AddToBagOutOfStockCallback = typeof QSdata.AddToBagOutOfStock == 'function' ? QSdata.AddToBagOutOfStock : function(){};
						$.QuickSelect.AddToBagFailureCallback = typeof QSdata.AddToBagFailure == 'function' ? QSdata.AddToBagFailure : function(){};
						$.QuickSelect.ProductId = QSdata.ProductId.toString();
						$.QuickSelect.ProductName = QSdata.ProductName;
						$.QuickSelect.ProductDefaultPrice = QSdata.ProductDefaultPrice;
						$.QuickSelect.ProductMarkedDownPrice = QSdata.ProductMarkedDownPrice;
						$.QuickSelect.SizeColorCombinations = ""; //gets set when QuickSelect opens successfully			
											
						$(this).dialog("open").html("<div class='LoadingGif'><img src='../../Images/loading.gif'><br /><span class='LoadingText'>Loading...</span></div>");	
						
						$("#DialogContent", this).css("display", "none");	
						$(".LoadingGif", this).css("display", "block");
						$("#DialogError", this).css("display", "none"); 	
						
						var DialogTitle;
						if (QSdata.ProductMarkedDownPrice == "")
							DialogTitle = "<div id='DialogTitle'><a href='" + QSdata.ProductDetailUrl + "'><span class='DialogProductName'>" + QSdata.ProductName + "</span></a><span class='DialogRegularPrice'>" + QSdata.ProductDefaultPrice + "</span></div>";
						else
							DialogTitle = "<div id='DialogTitle'><a href='" + QSdata.ProductDetailUrl + "'><span class='DialogProductName'>" + QSdata.ProductName + "</span></a><span class='DialogRegularPrice DialogStrikeThruPrice'>" + QSdata.ProductDefaultPrice + "</span><span class='DialogSalePrice'>" + QSdata.ProductMarkedDownPrice + "</span></div>";	
						$(this).dialog('option', 'title', DialogTitle);
						
						$('#DialogTitle', this).click(function(e) {
							e.preventDefault();
							RetainSizeColorRedirect(QSdata.ProductDetailUrl, "");		
						});
						
						var QuickSelectUrl = QSdata.ProductDetailUrl.replace("products", "quickselect");
	
						
						$.ajax({
							type: "GET",
							url: QuickSelectUrl,	
							dataType: "html",
							success: function(data){
								QuickSelectOpenSuccess(data, QSdata.ProductId, QSdata.ProductName, QSdata.ProductDetailUrl);
							},
							error: function(xhr, ajaxOptions, thrownError){
								QuickSelectOpenFailure(xhr, ajaxOptions, thrownError, QSdata.ProductDetailUrl, QSdata.ProductId);
							}		
						});
						
						
					});
					break;
				default:
					break;
					
			}
		}
		
	};
	
	//define all public functions inside the $.Quickselect to avoid namespace cluttering
	$.QuickSelect = {
		DisplayViewLargerDialog: function(PaddedProductId, ProductName){		
			//open the dialog, display the loading gif, remove the selected color, and hide the rest of the content
			$("#ViewLargerDialog").dialog('open').dialog('option', 'title', ProductName);
			$("#ViewLargerDialog > .LoadingGif").css("display", "block");
			$("#ViewLargerContent").css("display", "none");
			$("#ViewLargerDialog a.SelectedColorSwatch").removeClass("SelectedColorSwatch");
				
			//display the color swatches
			var HiddenViewLargerSwatchesString = $("#ViewLargerColorsContainer").html();	
			$("#ViewLargerSwatches").html(HiddenViewLargerSwatchesString.replace(/ViewLargerColorData/g, "ViewLargerColorSwatches"));
			
			//display the alternate images
			var HiddenViewLargerAlternatesString = $("#ViewLargerAltImageContainer").html();
			$("#ViewLargerMoreViews").html(HiddenViewLargerAlternatesString.replace(/ViewLargerAlternatesData/g, "ViewLargerAlternateImages"));
			
			//display the main image
			$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f.jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class=ViewLargerImage name='ProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_reg.jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
			MagicZoom.refresh();	
			
			//make the mask close the dialog when clicked
			$(".ui-widget-overlay:last").one("click", function (){
				$("#ViewLargerDialog").dialog('close');
			});
				
			//show the content, hide the loading gif
			$("#ViewLargerDialog > .LoadingGif").css("display", "none");
			$("#ViewLargerContent").css("display", "block");
		},
		SwitchMainImage: function(AltImage, Type){
			var LargeAltImage = AltImage.replace("_alt1", "");
			
			if (Type == "MainImage")
			{
				$("#MainImageContainer").html("<div class='MainZoomedImageContainer'><div id='MainProductImageZoom-big'></div></div><div id='MainZoom'><a rel='opacity:30;zoom-width:300;zoom-height:200;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + LargeAltImage + "_f.jpg' id='MainProductImageZoom' class='MagicZoom' title=''><img class='DialogMainImage' name='MainProductImage' src='/en-us/Catalog/ProductImages/" + LargeAltImage + "_qv.jpg' id='MainProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
				MagicZoom.refresh();
			}
			else if (Type == "ViewLargerImage")
			{
				$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + LargeAltImage + "_f.jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class='ViewLargerImage' name='ProductImage' src='/en-us/Catalog/ProductImages/" + LargeAltImage + "_reg.jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
				MagicZoom.refresh();
			}
		},				
		ChangeDefaultImageToAlt: function(AltThumbnail, Type)
		{
			if (Type == "MainImage")
			{
				$("#dialog .DefaultImage").removeClass("DefaultImage");
				$("#" + AltThumbnail.id + " img").addClass("DefaultImage");
			}
			else if (Type == "ViewLargerImage")
			{
				$("#ViewLargerDialog .DefaultImage").removeClass("DefaultImage");
				$("#ViewLargerDialog .SelectedColorSwatch").removeClass("SelectedColorSwatch");
				$("#" + AltThumbnail.id + " img").addClass("DefaultImage");
			}
		},
		wopen: function(url, name, w, h)
		{
			// Fudge factors for window decoration space.
			// In my tests these work well on all platforms & browsers.
			w += 32;
			h += 96;
			var win = window.open(url,
				name,
				'width=' + w + ', height=' + h + ', ' +
				'location=yes, menubar=yes, ' +
				'status=yes, toolbar=yes, scrollbars=yes, resizable=yes');
			win.resizeTo(w, h);
			win.focus();
		},
		ReviewWindow: function (ProductID)
		{       
			if(navigator.appName=="Netscape") 
			{
				var objWin = window.open('/PowerReviews/SubmitReview.html?pageID=' + ProductID,'Review','width=700,height=450, resizable=yes, toolbar=no, status=no, menubar=no, scrollbars = yes');
			}
			else
			{
				var objWin = window.open('/PowerReviews/SubmitReview.html?pageID=' + ProductID,'Review','width=650,height=450, resizable=yes, toolbar=no, status=no, menubar=no, scrollbars = yes');
			}
			objWin.window.focus();
		},
		ShowDefaultImage: function(PaddedProductId, Type)
		{
			if (Type == "MainImage")
			{
				if ($("#dialog .DefaultImage").size() == 0)
				{
					$("#MainImageContainer").html("<div class='MainZoomedImageContainer'><div id='MainProductImageZoom-big'></div></div><div id='MainZoom'><a rel='opacity:30;zoom-width:300;zoom-height:200;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f.jpg' id='MainProductImageZoom' class='MagicZoom' title=''><img class='DialogMainImage' name='MainProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_qv.jpg' id='MainProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
					MagicZoom.refresh();
				}
				else
				{
					if ($("#dialog .DefaultImage div").size() != 0) //the default image will correspond to a color swatch
					{
						var Color = $("#dialog .DefaultImage div").attr("rel");
						for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
						{
							if ($.QuickSelect.SizeColorCombinations[i].color == Color)
							{
								$("#MainImageContainer").html("<div class='MainZoomedImageContainer'><div id='MainProductImageZoom-big'></div></div><div id='MainZoom'><a rel='opacity:30;zoom-width:300;zoom-height:200;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='MainProductImageZoom' class='MagicZoom' title=''><img class='DialogMainImage' name='MainProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_qv_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='MainProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
								MagicZoom.refresh();
								break;
							}
						}
					}
					else //otherwise, the default image will correspond to an alt thumbnail
					{
						var AltImage = $("#dialog .DefaultImage").attr("rel");						
						var qvAltImage = AltImage.replace("_alt1", "");
			
						$("#MainImageContainer").html("<div class='MainZoomedImageContainer'><div id='MainProductImageZoom-big'></div></div><div id='MainZoom'><a rel='opacity:30;zoom-width:300;zoom-height:200;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + AltImage + "_f.jpg' id='MainProductImageZoom' class='MagicZoom' title=''><img class='DialogMainImage' name='MainProductImage' src='/en-us/Catalog/ProductImages/" + qvAltImage + "_qv.jpg' id='MainProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
						MagicZoom.refresh();
					}
				}
			}
			else if (Type == "ViewLargerImage")
			{
				if ($("#ViewLargerDialog .DefaultImage").size() == 0)
				{
					$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f.jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class='ViewLargerImage' name='ProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_reg.jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
					MagicZoom.refresh();
				}
				else
				{
					if ($("#ViewLargerDialog .DefaultImage div").size() != 0) //the default image will correspond to a color swatch
					{
						var Color = $("#ViewLargerDialog .DefaultImage div").attr("rel");
						for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
						{
							if ($.QuickSelect.SizeColorCombinations[i].color == Color)
							{
								$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class='ViewLargerImage' name='ProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_reg_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
								MagicZoom.refresh();
								break;
							}
						}
					}
					else //otherwise, the default image will correspond to an alt thumbnail
					{
						var AltImage = $("#ViewLargerDialog .DefaultImage").attr("rel");				
						var qvAltImage = AltImage.replace("_alt1", "");
			
						$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + AltImage + "_f.jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class='ViewLargerImage' name='ProductImage' src='/en-us/Catalog/ProductImages/" + qvAltImage + "_reg.jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
						MagicZoom.refresh();
					}
				}					
			}
		},		
		SwatchOver: function(imageFileName, title, Selector)
		{
			var aImg = new Image();
			aImg.src = imageFileName;
			
			var SwatchHtml = '<div class="popdiv">';
			if (imageFileName != '')
				SwatchHtml += '<img src="' + imageFileName + '"/>';
			if (title != '')
				SwatchHtml += title;
			SwatchHtml += '</div>';

			$(Selector).html(SwatchHtml);	    
			var SwatchWidth = ($(Selector).width() * -1);

			if (Selector == "#MainSwatch")
			{
				$(Selector + " .popdiv").css("left", (SwatchWidth-43+$.QuickSelect.PopDivLeftFudge) + "px");
			}
			else if (Selector == "#ViewLargerSwatch")
			{
				$(Selector + " .popdiv").css("left", (SwatchWidth+366-$.QuickSelect.PopDivLeftFudge) + "px");
			}
		    
			$(Selector).css("visibility", "visible");
		},
		SwatchOff: function()
		{
			$("#MainSwatch, #ViewLargerSwatch").css("visibility", "hidden");
		},
		ColorClick: function (Index, Color, PaddedProductId)
		{	
			Color = Color.replace("&", "&amp;");

			//if the clicked color is not disabled change the selected color and disable unavailable sizes in that color
			if (!($("#Colors__ctl" + Index + "_ColorSwatch div").hasClass("DisabledColorSwatch")))
			{		
				//remove the selected color swatch class
				$("#DialogColorSwatches > a.SelectedColorSwatch").removeClass("SelectedColorSwatch");
				
				//remove any DefaultImageClass from anything that has it in the dialog
				$("#dialog .DefaultImage").removeClass("DefaultImage");
				
				//apply the selected color swatch class to the clicked color
				$("#Colors__ctl" + Index + "_ColorSwatch").addClass("SelectedColorSwatch DefaultImage");

				//set the selected color text
				$("#SelectedColorText").html(Color.toUpperCase());
		 
				//disable the appropriate sizes
				$("#DialogSizeSwatches a").addClass("DisabledSizeSwatch");
				for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
				{
					if ($.QuickSelect.SizeColorCombinations[i].color.toUpperCase() == Color.toUpperCase())
					{
						$("#DialogSizeSwatches a[rel='" + $.QuickSelect.SizeColorCombinations[i].size + "']").removeClass("DisabledSizeSwatch");
					}			
				}
				
				//change the main image
				for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
				{
					if ($.QuickSelect.SizeColorCombinations[i].color == Color)
					{
						$("#MainImageContainer").html("<div class='MainZoomedImageContainer'><div id='MainProductImageZoom-big'></div></div><div id='MainZoom'><a rel='opacity:30;zoom-width:300;zoom-height:200;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='MainProductImageZoom' class='MagicZoom' title=''><img class='DialogMainImage' name='MainProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_qv_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='MainProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
						MagicZoom.refresh();
						break;
					}
				}
						
				//if there is a selected size, enable the add to bag button, change stock status, and change the add to bag tooltip
				if (parseInt($(".SelectedSizeSwatch").size()) == 0)
				{
					$("#AddToBagButton").removeClass("AddToBagEnabled").addClass("AddToBagDisabled");
					
					if (parseInt($(".SelectedSizeSwatchDisabled").size()) != 0)
						$("#StockStatus").html("Availability: OUT OF STOCK");
				}
				else
				{
					$("#AddToBagButton").removeClass("AddToBagDisabled").addClass("AddToBagEnabled");		
					$("#StockStatus").html("Availability: IN STOCK");
					if (typeof(OG) != "undefined") {
						var a2b = document.getElementById("AddToBagButton");
						if ($.QuickSelect.SizeColorCombinations[Index].isReplenishable == 'A') {
							OG.Controller.show_offer();
							//$("#og-div").css("display", "block");
							a2b.setAttribute("rel", $.QuickSelect.SizeColorCombinations[Index].variantId);
							//if (typeof og_settings != "undefined") 
							//	og_settings.product.id = $.QuickSelect.SizeColorCombinations[Index].variantId;
						} else {
							OG.Controller.clear_offer();
							a2b.setAttribute("rel", "");
						}
					}
				}
			}
			else //if the clicked color is disabled, remove the selected color
			{
				//remove the selected color swatch class
				$("#DialogColorSwatches > a.SelectedColorSwatch").removeClass("SelectedColorSwatch");
				
				//remove the selected color text
				$("#SelectedColorText").html("");
				
				//change stock status
				$("#StockStatus").html("Availability: OUT OF STOCK");
				
				//disable the add to bag button
				$("#AddToBagButton").removeClass("AddToBagEnabled").addClass("AddToBagDisabled");		
			}	
		},
		ViewLargerColorClick: function(Index, Color, PaddedProductId)
		{
			//remove the current Selected Color
			$("#ViewLargerSwatches > a.SelectedColorSwatch").removeClass("SelectedColorSwatch");
			
			//remove default image
			$("#ViewLargerDialog .DefaultImage").removeClass("DefaultImage");
			
			//apply the selected color swatch and default image class to the clicked color
			$("#ViewLargerColorSwatches__ctl" + Index + "_ViewLargerColorSwatch").addClass("SelectedColorSwatch DefaultImage");
			
			
			
			//change the main image
			for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
			{
				if ($.QuickSelect.SizeColorCombinations[i].color == Color)
				{			
					$("#ViewLargerImageContainer").html("<div class='ViewLargerZoomedImageContainer'><div id='ProductImageZoom-big'></div></div><div id='zoom1'><a rel='opacity:30;zoom-width:250;zoom-height:250;zoom-distance:3;zoom-position:custom;zoom-fade:true;zoom-fade-in-speed:250;zoom-fade-out-speed:125;fps:35;' href='/en-us/Catalog/ProductImages/" + PaddedProductId + "_f_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='ProductImageZoom' class='MagicZoom' title=''><img class='ViewLargerImage' name='ProductImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_reg_" + $.QuickSelect.SizeColorCombinations[i].ColorNumber + ".jpg' id='ProductImage' /><div class='MagicZoomLoading'>Loading zoom..</div><div class='MagicZoomPup'></div></a></div>");
					MagicZoom.refresh();
					break;
				}
			}
		},
		SizeClick: function(Index, Size)
		{
			//remove the selected size swatch class
			$("#DialogSizeSwatches .SelectedSizeSwatchDisabled").removeClass("SelectedSizeSwatchDisabled");
			$("#DialogSizeSwatches .SelectedSizeSwatch").removeClass("SelectedSizeSwatch");

			//apply the appropriate selected size swatch class
			if($("#Sizes__ctl" + Index + "_SizeSwatch").hasClass("DisabledSizeSwatch"))
			{
				$("#Sizes__ctl" + Index + "_SizeSwatch").addClass("SelectedSizeSwatchDisabled");
				
				//disable the add to bag button, change the stock status
				$("#AddToBagButton").removeClass("AddToBagEnabled").addClass("AddToBagDisabled");
				$("#StockStatus").html("Availability: OUT OF STOCK");
			}
			else
			{
				$("#Sizes__ctl" + Index + "_SizeSwatch").addClass("SelectedSizeSwatch");
				
				//disable the add to bag button and change the stock status if a color is no color selected
				if (parseInt($(".SelectedColorSwatch").size()) == 0)
				{
					$("#AddToBagButton").removeClass("AddToBagEnabled").addClass("AddToBagDisabled");					
				}
				else
				{
					$("#AddToBagButton").removeClass("AddToBagDisabled").addClass("AddToBagEnabled");
					$("#StockStatus").html("Availability: IN STOCK");
					if (typeof(OG) != "undefined") {
						var a2b = document.getElementById("AddToBagButton");
						if ($.QuickSelect.SizeColorCombinations[Index].isReplenishable == 'A') {
							//$("#og-div").css("display", "block");
							OG.Controller.show_offer();
							a2b.setAttribute("rel", $.QuickSelect.SizeColorCombinations[Index].variantId);
							//if (typeof og_settings != "undefined") 
							//	og_settings.product.id = $.QuickSelect.SizeColorCombinations[Index].variantId;
						} else {
							OG.Controller.clear_offer();
							a2b.setAttribute("rel", "");
						}
					}
				}
			}

			//set the selected color text
			$("#SelectedSizeText").html(Size.toUpperCase());

			//disable the appropriate colors
			$("#DialogColorSwatches > a > div").addClass("DisabledColorSwatch").html("<a>N/A</a>");
			for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
			{
				if ($.QuickSelect.SizeColorCombinations[i].size == Size)
				{
					$("#DialogColorSwatches > a > div[rel=" + $.QuickSelect.SizeColorCombinations[i].color.replace("&amp;", "&").toLowerCase() + "]").removeClass("DisabledColorSwatch").html("");
				}
			}
		},
		ShowAddToBagTooltip: function()
		{
			//only show the tooltip if the add to bag button is enabled
			if (!($("#AddToBagButton").hasClass("AddToBagEnabled")))
			{
				if (parseInt($(".SelectedColorSwatch").size()) == 0 && parseInt($(".SelectedSizeSwatch").size()) == 0)
					$("#AddToBagTooltip").html("Please select<br/>a color and size"); //removeClass("AddToBagTooltipSize AddToBagTooltipColor").addClass("AddToBagTooltipBoth");
				else if (parseInt($(".SelectedColorSwatch").size()) == 0 && parseInt($(".SelectedSizeSwatch").size()) != 0)
					$("#AddToBagTooltip").html("Please select a color"); //removeClass("AddToBagTooltipSize AddToBagTooltipBoth").addClass("AddToBagTooltipColor");
				else if (parseInt($(".SelectedColorSwatch").size()) != 0 && parseInt($(".SelectedSizeSwatch").size()) == 0)
					$("#AddToBagTooltip").html("Please select a size"); //removeClass("AddToBagTooltipColor AddToBagTooltipBoth").addClass("AddToBagTooltipSize");
					
				$("#AddToBagTooltip").css("display", "block");
			}
		},
		HideAddToBagTooltip: function()
		{
			$("#AddToBagTooltip").html("").css("display", "none");
		},
		AddItemToBag: function(ProductDetailUrl)
		{
			//only add something to the bag if the button is enabled
			if ($("#AddToBagButton").hasClass("AddToBagEnabled"))
			{		
				//show loading status
				$("#StockStatus").html("<img src='/images/loadingsmall.gif' />Adding product to bag...");
				
				//disable the add to bag button
				$("#AddToBagButton").removeClass("AddToBagEnabled").addClass("AddToBagDisabled");
				
				//get the product variant
				var ProductVariant = "";
				for (var i = 0; i < $.QuickSelect.SizeColorCombinations.length; i++)
				{
					if ($.QuickSelect.SizeColorCombinations[i].color == $(".SelectedColorSwatch > div").attr("rel") && $.QuickSelect.SizeColorCombinations[i].size == $(".SelectedSizeSwatch").attr("rel"))
					{
						ProductVariant = $.QuickSelect.SizeColorCombinations[i].variantId;
						break;
					}
				}		
				
				if (ProductVariant != "")
				{
					$.ajax({
						type: "POST",
						url: ProductDetailUrl,
						data: {"CallBackType" : "AddToBag", "ProductVariant" : ProductVariant, "Quantity" : $("#QuantityDropdown").val() },
						dataType: "json",
						success: function(response){
							AddToBagSuccess(response);
						},
						error: function(xhr, ajaxOptions, thrownError){
							AddToBagFailure(xhr, ajaxOptions, thrownError, ProductDetailUrl);
						}		
					});
				}
				else
					AddToBagFailure(null, null, null, ProductDetailUrl);
			}
		},
		CloseViewLargerWindow: function()
		{
			$("#ViewLargerDialog").dialog('close');
		}

	};
	
	
	//Private functions	
	function QuickSelectOpenSuccess(data, ProductId, ProductName, ProductDetailUrl)
	{	
		$($.QuickSelect.Selector).html(data);
		
		MagicZoom.refresh();
		
		//add the multi price to the title bar
		$("#DialogTitle", $.QuickSelect.Selector).append($("#HiddenMultiPrice").html());
		
		//initialize tabs
		$("#tabs", $.QuickSelect.Selector).css("display", "block").tabs().tabs('select', 0);
		
		$("#ButtonColumn", $.QuickSelect.Selector).height(75);
		
		$("#AddToBagFeedBackContainer", $.QuickSelect.Selector).html("");
		
		//clear the stock status
		$("#StockStatus", $.QuickSelect.Selector).html("");
		
		//disable the add to bag button initially
		$("#AddToBagButton", $.QuickSelect.Selector).removeClass("AddToBagEnabled").addClass("AddToBagDisabled");
		
		//hide the checkout button initially
		$("#CheckOutButton", $.QuickSelect.Selector).css("display", "none");
		
		var SizeColorCombinationsJSONString = $("#SizeColorCombinations", $.QuickSelect.Selector).html();
		$.QuickSelect.SizeColorCombinations = JSON.parse(SizeColorCombinationsJSONString);
		
		//set the onClick of the add to bag button
		$("#AddToBagButton", $.QuickSelect.Selector).unbind().bind("click", {Url: ProductDetailUrl}, function(eventData){
			$.QuickSelect.AddItemToBag(eventData.data.Url);
		});		
		
		//if there is only one swatch, make it the selected swatch
		var PaddedProductId = ProductId.toString();
		while (PaddedProductId.length < 6)
			PaddedProductId = '0' + PaddedProductId;
		
		if ($("#DialogColorSwatches a", $.QuickSelect.Selector).size() == 1)
		{
			var DefaultColor = $("#DialogColorSwatches div", $.QuickSelect.Selector).attr("rel");
			$.QuickSelect.ColorClick('0', DefaultColor, PaddedProductId);
		}
		
		if ($("#DialogSizeSwatches div", $.QuickSelect.Selector).size() == 1)
		{
			var DefaultSize = $("#DialogSizeSwatches a", $.QuickSelect.Selector).attr("rel");
			$.QuickSelect.SizeClick('0', DefaultSize);
		}		
		
		$("#detailsTab", $.QuickSelect.Selector).bind("click", function(){
			//set the url for the read review link
					
			var SelectionProductDetailsUrl = ProductDetailUrl;
			
			if ($("#DialogColorSwatches .SelectedColorSwatch", $.QuickSelect.Selector).size() > 0)
			{
				var SelectedColorIndex = $("#DialogColorSwatches .SelectedColorSwatch", $.QuickSelect.Selector).attr("rel");
				if (SelectionProductDetailsUrl.indexOf("?") > 0)
				SelectionProductDetailsUrl += "&SelectedColor=" + SelectedColorIndex;
				else
				SelectionProductDetailsUrl += "?SelectedColor=" + SelectedColorIndex;
			}
			
			if ($("#DialogSizeSwatches .SelectedSizeSwatch", $.QuickSelect.Selector).size() > 0)
			{
				var SelectedSizeIndex = $("#DialogSizeSwatches .SelectedSizeSwatch", $.QuickSelect.Selector).parent().attr("rel");
				if (SelectionProductDetailsUrl.indexOf("?") > 0)
					SelectionProductDetailsUrl += "&SelectedSize=" + SelectedSizeIndex;
				else
					SelectionProductDetailsUrl += "?SelectedSize=" + SelectedSizeIndex;
			}
					
			pr_read_review = SelectionProductDetailsUrl + "#PRtab";	
			POWERREVIEWS.display.snippet({write : function(content) {$('div#PowerReviewSnippetContainer', $.QuickSelect.Selector).html(content);}}, { pr_page_id : ProductId.toString(10), pr_snippet_min_reviews : 1});
		});
		
		
			
		if ($("#QuickSelectDescriptionContainer font", $.QuickSelect.Selector).attr("size") == "\"0\"")
			$("#QuickSelectDescriptionContainer font", $.QuickSelect.Selector).attr("size", "0");
		
		//$("#ProdDesc", $.QuickSelect.Selector).html($("#QuickSelectDescriptionContainer", $.QuickSelect.Selector).html());	

		var QuickSelectUrl = ProductDetailUrl.replace("products", "quickselect");
		$.ajax({
			type: "GET",
			url: QuickSelectUrl,
			dataType: "json",
			data: {"CallbackType" : "DescriptionRequest"},
			success: function(data){
				DescriptionRequestSuccess(data);
			},
			error: function(xhr, ajaxOptions, thrownError){
				DescriptionRequestFailure(xhr, ajaxOptions, thrownError, QSdata.ProductDetailUrl, QSdata.ProductId);
			}
		});	

		//psPostProductView("QuickSelect");	
	};
	
	function QuickSelectOpenFailure(xhr, ajaxOptions, thrownError, ProductDetailUrl, ProductId)
	{	
		var PaddedProductId = ProductId.toString();
		while (PaddedProductId.length < 6)
			PaddedProductId = '0' + PaddedProductId;
		
		$("#dialog").html("<div id='DialogErrorImageContainer'></div><div id='DialogErrorCopyContainer'></div>");
		
		$("#DialogErrorImageContainer").html("<a href='" + ProductDetailUrl + "'><img class='DialogMainImage' src='/en-us/Catalog/ProductImages/" + PaddedProductId + "_qv.jpg' /></a>");
		$("#DialogErrorCopyContainer").html("We're sorry, this page is having problems loading. <br/><br/> Please see the <a class='DialogErrorLink' href='" + ProductDetailUrl + "'>product detail page</a> for an additional view. <br/><br/> If this problem continues, please try again later.");
		
		$("#DialogContent").css("display", "none");	
		$("#dialog .LoadingGif").css("display", "none");
		$("#DialogError").css("display", "block"); 
	};
	
	function DescriptionRequestSuccess(data)
	{
		data.Description = data.Description.replace(/\&\#34\;/g, '\"');
		$("#ProdDesc").html(data.Description);
		$("#QuickSelectDescriptionContainer").html(data.Description);
		$("a[rel^='prettyPhoto2']").prettyPhoto2();
	};

	function DescriptionRequestFailure(xhr, ajaxOptions, thrownError, ProductDetailUrl, ProductId)
	{
		//log error
	};

	function AddToBagSuccess(response)
	{

	// rebuild & display the bag dropdown
	$("#HeaderBagWrapper").html(response.BagDropDownHtml);
	
	$("#HeaderBagWrapper").addClass("bagmsvr");
	$("#dpbagouter").css("display","block");
		setTimeout(function(){
			$("#HeaderBagWrapper").removeClass("bagmsvr")
			$("#dpbagouter").css("display", "none");
		}, 3000);

	bagdropdownbindings();
	
		//re-enable the add to bag button
		$("#AddToBagButton").removeClass("AddToBagDisabled").addClass("AddToBagEnabled");
			
		//remove the loading text	
		if (($("#StockStatus").html()).indexOf("Adding") != -1)
			$("#StockStatus").html("")
		
		//show the checkout button
		$("#CheckOutButton").css("display", "block");
		
		//display the feedback message depending on if the amount of items requested to be added to the bag were actually added to the bag
		var FeedBackMessage = "";
		var FeedBackHeight = 0;
		if (response.AttemptedQuantityAdded == response.QuantityAdded)
		{	
			//set the FeedBackMesage
			FeedBackMessage = parseInt(response.QuantityAdded) > 1 ? response.QuantityAdded + " items have been added to your bag." : response.QuantityAdded + " item has been added to your bag";
			FeedBackHeight = $("#AddToBagFeedBackContainer").html("<div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div>").height();
			//FeedBackHeight = $("#AddToBagFeedBackContainer").html("<div id='AddToBagSuccessTop'><div id='AddToBagSuccessBottom'><div id='AddToBagSuccessMiddle'><div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div></div></div></div>").height();			
			if (parseInt(FeedBackHeight) > 75)
				$("#ButtonColumn").height(FeedBackHeight);
					
			//fade in and out the feedback message
			$("#AddToBagFeedBackContainer").fadeIn(1500, function(){
				setTimeout(function(){
					$("#AddToBagFeedBackContainer").fadeOut(1500);
				}, 1500);
			});		
									
			$.QuickSelect.AddToBagSuccessCallback();
		}
		else
		{
			//determine if some items have been added, or none at all
			if (response.QuantityAdded > 0)
				FeedBackMessage = "The quantity you selected is currently not available. <br/><br/>The available quantity has been added to your bag.";
			else
				FeedBackMessage = "This item is not currently available.";
			
			//if the feedback message is lengthy, it will cause wrapping issues. get the height of the feedback and set the button column to that height to prevent this
			FeedBackHeight = $("#AddToBagFeedBackContainer").css("display", "none").html("<div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div>").height();
			//FeedBackHeight = $("#AddToBagFeedBackContainer").css("display", "none").html("<div id='AddToBagErrorTop'><div id='AddToBagErrorBottom'><div id='AddToBagErrorMiddle'><div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div></div></div></div>").height();
			if (parseInt(FeedBackHeight) > 75)
				$("#ButtonColumn").height(FeedBackHeight);
						
			$("#AddToBagFeedBackContainer").fadeIn(1500);
			
			$.QuickSelect.AddToBagOutOfStockCallback();
		}
		
		psCreateShopAction5Tag($.QuickSelect.ProductId, $.QuickSelect.ProductName, response.QuantityAdded, $.QuickSelect.ProductMarkedDownPrice == "" ? $.QuickSelect.ProductDefaultPrice : $.QuickSelect.ProductMarkedDownPrice, location.href.toLowerCase().indexOf("shoppingbag.aspx") == -1 ? psGetValueFromCookie(G_PS_COOKIE_PROD_CATID, $.QuickSelect.ProductId) : "shoppingbag", cmCartID);		
		// Make sure to have actual postings
		psDisplayShop5s();
	
	};

	function AddToBagFailure(xhr, ajaxOptions, thrownError, ProductDetailUrl)
	{
		//remove the loading text	
		if (($("#StockStatus").html()).indexOf("Adding") != -1)
			$("#StockStatus").html("")

		var FeedBackMessage = "We're sorry, an unexpected error occurred. <br/><br/> Please see the <a class='FeedBackErrorLink' href='" + ProductDetailUrl + "'>product detail</a> page for an additional view. If this problem continues, please try again later.";
		
		var FeedBackHeight = $("#AddToBagFeedBackContainer").css("display", "none").html("<div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div>").height();
		//var FeedBackHeight = $("#AddToBagFeedBackContainer").css("display", "none").html("<div id='AddToBagErrorTop'><div id='AddToBagErrorBottom'><div id='AddToBagErrorMiddle'><div id='AddToBagFeedBackText'>" + FeedBackMessage + "</div></div></div></div>").height();
		if (parseInt(FeedBackHeight) > 75)
				$("#ButtonColumn").height(FeedBackHeight);
		
		$("#AddToBagFeedBackContainer").fadeIn(1500);
		
		$.QuickSelect.AddToBagFailureCallback();
	};
	
})(jQuery);

