//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//JQUERY functions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var addsku = "";
var addname = "";
var adddesc = "";
var addprice = "";
var addqty = "";
var addcartqty = "";
var addcarttotal = "";
var varCarouselSize = 0;
var varBundlerCatalogPath = "";

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//FIXED OVERLAY -- product quick preview
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showFixedOverlay(varOverlayId, varWidth, varClass, varProductId, varZoomifyUrl, varContainerId)
{
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: varClass,
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		show: 'fade',
		stack: false,
		width: varWidth,
		open: function(event, ui){
			$('body').css('overflow','hidden');
			$('.ui-widget-overlay').css('width','100%');
			showZoomViewer(varProductId, varZoomifyUrl,'viewer1' + varContainerId);
			$("#DetailProductContainer" + varContainerId + " .Image").mouseenter(function(){
				showViewer("viewer1" + varContainerId);
			});
		}, 
		close: function(event, ui){$('body').css('overflow','auto');}
	});
}

//initialize elements and bind them to the overlay before opening
function bindOverlay(varOverlayId,varProductId)
{
	$("#" + varOverlayId).bind('dialogopen', function() { 
		initTabs(varProductId);
	});
}

function initTabs(varProductId)
{
	$("#TabContainer" + varProductId + " .TabContent").hide(); //Hide all content
	$("ul#Tabs" + varProductId + " li:first").addClass("active").show(); //Activate first tab
	$("#TabContainer" + varProductId + " .TabContent:first").show(); //Show first tab content

	$("ul#Tabs" + varProductId + " li").click(function() {
		$("ul#Tabs" + varProductId + " li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#TabContainer" + varProductId + " .TabContent").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//ZOOM VIEWER
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showZoomViewer(varProductName, varUrl, varViewer)
{
	var options = {
		viewerDomName : varViewer,
		imageUrlBase : varUrl,
		imageNameBase : varProductName,
		zIndex			: 32,
		zoomRegionWidth	: '480px',
		zoomRegionHeight	: '480px',
		thumbOffsetLeft	: '-309px',
		thumbOffsetTop	: '6px'
	};

	displayZoomViewer(viewer1,  options);
}

//actually display the zoomed image, if the viewer is available
function showViewer(varViewer)
{
	if (document.getElementById(varViewer).children.length > 0 && document.getElementById(varViewer).style.visibility != 'visible')
		document.getElementById(varViewer).style.visibility = 'visible';
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//TOGGLE PAGE VIEW
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function toggleTag(varTag, varIdTag, varStringToKeep, varStringToReplace)
{
	if ($("#" + varIdTag).attr(varTag))
	{
		varSearchString = "&view=" + varStringToReplace;
		if ($("#" + varIdTag).attr(varTag).indexOf(varSearchString) > -1) {
			$("#" + varIdTag).attr(varTag, $("#" + varIdTag).attr(varTag).replace("&view=" + varStringToReplace,"&view=" + varStringToKeep));
		}
		else {
			if (!$("#" + varIdTag).attr(varTag).match("&view=")) {
				$("#" + varIdTag).attr(varTag,$("#" + varIdTag).attr(varTag) + "&view=" + varStringToKeep);
			}
		}
	}
}

function getToggleState()
{
	var thequerystring = getParameterByName("view");
	if (thequerystring == "list")
	{
		$("a.SwitchThumb").addClass("Swap");
		$("ul.Display").fadeOut("fast", function() {
			$(this).fadeIn("fast").removeClass("ThumbView");
			$("li.ItemContainer").removeClass("Thumb").addClass("HorizontalLayout");
			$("div.ContentBlock").removeClass("GridStyle");
			$("div.Details").addClass("HorizontalLayoutDetail");
		});
		toggleTag("href","PagingViewAll", "list", "grid");
		toggleTag("href","PagingPrevious", "list", "grid");
		toggleTag("href","PagingNext", "list", "grid");
		toggleTag("action","FormSort", "list", "grid");
	}
	if (thequerystring == "grid")
	{
		$("a.SwitchThumb").removeClass("Swap");
		$("ul.Display").fadeOut("fast", function() {
			$(this).fadeIn("fast").addClass("ThumbView");
			$("li.ItemContainer").addClass("Thumb").removeClass("HorizontalLayout");
			$("div.ContentBlock").addClass("GridStyle");
			$("div.Details").removeClass("HorizontalLayoutDetail");
		});
		toggleTag("href","PagingViewAll", "grid", "list");
		toggleTag("href","PagingPrevious", "grid", "list");
		toggleTag("href","PagingNext", "grid", "list");
		toggleTag("action","FormSort", "grid", "list");
	}
}

function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Generic Simple Overlay
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showSimpleOverlay(varOverlayId, varWidth, varClass, varPosition, varAutoPosition)
{
	if (varPosition == '')
	{
		varPosition = "center";
		varAutoPosition = true;
	}
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: varClass,
		closeOnEscape: true,
		draggable: true,
		modal: true,
		position: varPosition,
		resizable: false,
		autoReposition: varAutoPosition,
		show: 'fade',
		stack: false,
		width: varWidth,
		open: function(event, ui){$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');}, 
		close: function(event, ui){$('body').css('overflow','auto');} 
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Product Display
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showProductDetails()
{
	$.ui.dialog.prototype.options.autoReposition = true;
	$( window ).resize(function() {
		$( ".ui-dialog-content:visible" ).each(function() {
			var dialog = $( this ).data( "dialog" );
			if ( dialog.options.autoReposition ) {
				dialog.option( "position", dialog.options.position );
			}
		});
	});
	$("#dialogProductDisplay").dialog({
		autoOpen: true,
		dialogClass: 'ProductDetail',
		closeOnEscape: true,
		draggable: true,
		modal: true,
		position: 'center',
		resizable: true,
		autoReposition: true,
		show: 'fade',
		stack: true,
		width: 730,
		open: function(event, ui){
			$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');
			if ($(this).parent().height() > $(window).height())
			{
				$(this).height($(window).height()*0.8);
			}
			if ($(this).parent().width() > $(window).width())
			{
				$(this).parent().width($(window).width());
			}
			$(this).dialog({position: "center"});
		}, 
		close: function(event, ui){$('body').css('overflow','auto');} 
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Product Display Multiple
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showProductDetailsMulti(varOverlayId)
{
	$.ui.dialog.prototype.options.autoReposition = true;
	$( window ).resize(function() {
		$( ".ui-dialog-content:visible" ).each(function() {
			var dialog = $( this ).data( "dialog" );
			if ( dialog.options.autoReposition ) {
				dialog.option( "position", dialog.options.position );
			}
		});
	});
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: 'ProductOptionsOverlay',
		closeOnEscape: true,
		draggable: true,
		modal: true,
		position: 'center',
		resizable: true,
		autoReposition: true,
		show: 'fade',
		stack: true,
		width: 730,
		open: function(event, ui){
			$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');
			if ($(this).parent().height() > $(window).height())
			{
				$(this).height($(window).height()*0.8);
			}
			if ($(this).parent().width() > $(window).width())
			{
				$(this).parent().width($(window).width());
			}
			$(this).dialog({position: "center"});
		}, 
		close: function(event, ui){$('body').css('overflow','auto');} 
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Show Email Benefits
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showEmailBenefits(varOverlayId)
{
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: 'EmailBenefits',
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		show: 'fade',
		stack: false,
		width: 260,
		open: function(event, ui){$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');}, 
		close: function(event, ui){$('body').css('overflow','auto');} 
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Show Options
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showOverlay(varOverlayId)
{
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: 'ProductOptionsOverlay',
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		show: 'fade',
		stack: false,
		width: 430,
		open: function(event, ui){$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');}, 
		close: function(event, ui){$('body').css('overflow','auto');} 
	});
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Cart Return
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function showDialog()
{
	$("#dialogBuyIt").dialog({
		autoOpen: true,
		dialogClass: 'CartOverlay',
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		show: 'fade',
		stack: false,
		width: 398,
		open: function(event, ui){$('body').css('overflow','hidden');$('.ui-widget-overlay').css('width','100%');}, 
		close: function(event, ui){$('body').css('overflow','auto');}
	});
}

function addToCartCallback(data)
{
	if (data.getElementsByTagName("ItemAdded")[0].hasChildNodes)
	{
		addsku = data.getElementsByTagName("ItemAdded")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("QtyAdded")[0].hasChildNodes)
	{
		addquantity = data.getElementsByTagName("QtyAdded")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Description")[0].hasChildNodes)
	{
		addname = data.getElementsByTagName("Description")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Attributes")[0].firstChild != null)
	{
		adddesc = data.getElementsByTagName("Attributes")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Price")[0].hasChildNodes)
	{
		addprice = data.getElementsByTagName("Price")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("Image")[0].hasChildNodes)
	{
		addimage = data.getElementsByTagName("Image")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("CartQty")[0].hasChildNodes)
	{
		addcartqty = data.getElementsByTagName("CartQty")[0].childNodes[0].nodeValue;
	}
	if (data.getElementsByTagName("CartTotal")[0].hasChildNodes)
	{
		addcarttotal = data.getElementsByTagName("CartTotal")[0].childNodes[0].nodeValue;
	}
	if (document.getElementById("itemSku") != null)
	{
		document.getElementById("itemSku").innerHTML = addsku;
	}
	if (document.getElementById("itemName") != null)
	{
		document.getElementById("itemName").innerHTML = addname;
	}
	if (document.getElementById("itemDesc") != null)
	{
		document.getElementById("itemDesc").innerHTML = adddesc;
	}
	if (document.getElementById("itemQty") != null)
	{
		document.getElementById("itemQty").innerHTML = addquantity;
	}
	if (document.getElementById("itemPrice") != null)
	{
		document.getElementById("itemPrice").innerHTML = addprice;
	}
	if (document.getElementById("itemImage") != null)
	{
		document.getElementById("itemImage").src = addimage;
		document.getElementById("itemImage").alt = addname;
	}
	if (document.getElementById("cartQty") != null)
	{
		document.getElementById("cartQty").innerHTML = addcartqty;
	}
	if (document.getElementById("cartTotal") != null)
	{
		document.getElementById("cartTotal").innerHTML = addcarttotal;
	}
	if (document.getElementById("cartSummaryQty") != null)
	{
		document.getElementById("cartSummaryQty").innerHTML = addcartqty;
	}
	if (document.getElementById("CartSummary") != null)
	{
		document.getElementById("CartSummary").className = "CartSummary CartBackgroundOn";
	}
}

function turnOnCartSummary()
{
	document.getElementById("CartSummary").className = "CartSummary CartBackgroundOn";
}

function addToCart(varFormName)
{
	dataString = $("#" + varFormName).serialize();
	$.ajax({
		url: 'ClientAction.aspx?return=true',
		type: 'post',
		cache: false,
		dataType: "xml",
		data: dataString,
		success: function(data) {
			addToCartCallback(data);
			showDialog();
		},
		error: function(msg) {
			alert(msg);
		}
	})
}

function addToCartCloseDialog(varFormName, varOverlayId)
{
	dataString = $("#" + varFormName).serialize();
	$.ajax({
		url: 'ClientAction.aspx?return=true',
		type: 'post',
		cache: false,
		dataType: "xml",
		data: dataString,
		success: function(data) {
			addToCartCallback(data);
			$("#" + varOverlayId).dialog('close');
			showDialog();
		},
		error: function(msg) {
			alert(msg);
		}
	})
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//MINI CART
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function addToMiniCart(varFormName, varImageUrl, varStrSelection, varStrQty, varStrPrice, varStrDelete, varStrNoImage)
{
	dataString = $("#" + varFormName).serialize();
	$.ajax({
		url: 'ClientAction.aspx?return=true',
		type: 'post',
		cache: false,
		dataType: "xml",
		data: dataString,
		success: function(data) {
			getMiniCartContents(varImageUrl, varStrSelection, varStrQty, varStrPrice, varStrDelete, varStrNoImage)
		},
		error: function(msg) {
			//alert(msg);
		}
	})
}

function deleteFromMiniCart(varFormName, varItemCount, varImageUrl, varStrSelection, varStrQty, varStrPrice, varStrDelete, varStrNoImage)
{
	dataString = $("#" + varFormName).serialize();
	$.ajax({
		url: 'ClientAction.aspx?action=deletecartitem',
		type: 'post',
		cache: false,
		dataType: "xml",
		data: dataString,
		success: function(data) {
			if (varItemCount == 1)
			{
				$('#CartSummaryQty').html("0");
				$('#CartTrigger').removeClass('CartBackgroundOn').removeClass('CartBackgroundHover').addClass('CartBackgroundOff');
				$('.CartSummary').find('#MiniCart').slideUp('fast');
			}
			else
			{
				getMiniCartContents(varImageUrl, varStrSelection, varStrQty, varStrPrice, varStrDelete, varStrNoImage);
			}
		},
		error: function(msg) {
			//alert(msg);
		}
	})
}

function getMiniCartContents(varImageUrl, varStrSelection, varStrQty, varStrPrice, varStrDelete, varStrNoImage)
{
	$.ajax({
		url: 'ClientAction.aspx?action=getMiniCartContents',
		type: 'get',
		cache: false,
		dataType: "xml",
		success: function(xml) {
			$('#MiniCartItems').contents().remove();
			$('#MiniCartSubtotal span').contents().remove();
			$(xml).find('Cart').each(function(){
				var varItemCount = $(this).attr('itemcount');
				if (varItemCount != "0")
				{
					$('#CartTrigger').removeClass('CartBackgroundOff').removeClass('CartBackgroundOn').addClass('CartBackgroundHover');
					$('.CartSummary').find('#MiniCart').slideDown('fast');
					$('#CartSummaryQty').html(varItemCount);
				}
				else
				{
					$('#CartTrigger').removeClass('CartBackgroundOff').removeClass('CartBackgroundHover').addClass('CartBackgroundOn');
				}
				$(this).find('Items').each(function(){
					var varSubtotal = 0;
					$(this).find('Item').each(function(){
						var strHtml = null;
						var varItemId = $(this).attr('name');
						if (varItemId != null)
						{
							var varImage = varStrNoImage;
							if ($(this).find('Image').text())
							{
								varImage = $(this).find('Image').text();
							}
							var varName = $(this).find('Description').text();
							var varAttributes = $(this).find('Attributes').text();
							var varQty = $(this).find('Qty').text();
							var varPrice = "\$" + Number($(this).find('Price').text()).toFixed(2);
							var varRemovable = $(this).attr('removable');
							varSubtotal = varSubtotal + Number($(this).find('Total').text());

							strHtml += "<div class=\"Item\">";
							strHtml += "<div class=\"Image\">";
							strHtml += '<img src="' + varImageUrl + varImage + '" border="0" alt="' + varName + '" title="' + varName + '" />';	
							strHtml += "</div>";
							strHtml += "<div class=\"Details\">";
							strHtml += "<div class=\"Name EllipsisTwo\">" + varName + "</div>";
							strHtml += "<div class=\"Selection\">";
							strHtml += "<div class=\"Label\">" + varStrSelection + ":&#160;</div>";
							strHtml += "<div class=\"Value\">" + varAttributes + "</div>";
							strHtml += "<div style=\"clear: both;\"></div>";
							strHtml += "<div class=\"Label\">" + varStrPrice + ":&#160;</div>";
							strHtml += "<div class=\"Value\">" + varPrice + "</div>";
							strHtml += "<div style=\"clear: both;\"></div>";
							strHtml += "<div class=\"Label\">" + varStrQty + ":&#160;</div>";
							strHtml += "<div class=\"Value\">" + varQty + "</div>";
							strHtml += "<div style=\"clear: both;\"></div>";
							strHtml += "</div>";
							strHtml += "</div>";
							if (varRemovable = 'true')
							{
								strHtml += "<form name=\"formDeleteMiniCartItem" + varItemId + "\" id=\"formDeleteMiniCartItem" + varItemId + "\" method=\"post\" onsubmit=\"return false;\">";
								strHtml += "<input type=\"hidden\" value=\"" + varItemId + "\" name=\"item_id\" />";
								strHtml += "<div class=\"DeleteItem\" onClick=\"deleteFromMiniCart('formDeleteMiniCartItem" + varItemId + "', '" + varItemCount + "', '" + varImageUrl + "', '" + varStrSelection + "', '" + varStrQty + "', '" + varStrPrice + "', '" + varStrDelete + "', '" + varStrNoImage + "');\"></div>";
								strHtml += "</form>";
							}
							strHtml += "<div style=\"clear: both;\"></div>";
							strHtml += "</div>";
							$(strHtml).appendTo('#MiniCartItems');
						}
					});
					$('<span>\$' + Number(varSubtotal).toFixed(2) + '</span>').appendTo('#MiniCartSubtotal');
				});
			});
		},
		error: function(msg) {
			//alert("error");
		}
	})
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BUNDLER OVERLAY
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//opens the bundler overlay
function showBundlerOverlay(varImageUrl, varOverlayId, varWidth, varHeight, varClass, varCatalogPath,varStrNoImage)
{
	$("#" + varOverlayId).dialog({
		autoOpen: true,
		dialogClass: varClass,
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		show: 'fade',
		stack: false,
		width: varWidth,
		height: varHeight,
		open: function(event, ui){
			$(".ui-dialog-titlebar").hide();
			$('body').css('overflow','hidden');
			$('.ui-widget-overlay').css('width','100%');
			getBundlerContents(varImageUrl, varCatalogPath, varStrNoImage);
		}, 
		close: function(event, ui){$('body').css('overflow','auto');}
	});
}

//on products with more than one sku, this displays the sku choices once the product has been dropped into the drop area
function getBundlerProductVariants(varProductId,varPositionId)
{
	var strHtml = "";
	var strVariants = "";
	var strSku = "";
	var blnHasVariants = false;
	$.ajax({
		url: 'ClientAction.aspx?action=getproductcatalog&cp=' + varBundlerCatalogPath,
		type: 'get',
		async: false,
		cache: false,
		dataType: "xml",
		success: function(xml) {
			$(xml).find('CatalogContent').each(function(){
				$(this).find('Content').each(function(){
					$(this).find('Products').each(function(){
						$(this).find('Product[name=\"' + varProductId + '\"]').each(function(){
							$(this).find('SKU').each(function(){
								$(this).find('Var').each(function(){
									blnHasVariants = true;
									strVariants += $(this).text() + " ";
								});
								if (blnHasVariants)
								{
									strSku += "<div class=\"Radio\"><input type=\"radio\" class=\"RadioInput\" value=\"" + $(this).attr("name") + "\" name=\"sku\" onclick=\"document.getElementById('ProductBundlerVariants" + varPositionId + "').style.display='none';submitVariant('ClientAction.aspx?action=addtobundler&sku=" + $(this).attr("name") + "&pfid=" + varProductId + "&position=" + varPositionId + "','BundleItemAdd','" + $(this).attr("name") + "','" + varProductId + "');\"></input></div>";
									strSku += "<div class=\"Variant\">" + strVariants + "</div>";
									strSku += "<div style=\"clear: both;\"></div>";
									strVariants = "";
								}
								else
								{
									strHtml = $(this).attr("name");
								}
							});
						});
					});
				});
			});
			if (blnHasVariants)
			{
				strHtml += "<div id=\"ProductBundlerVariants" + varPositionId + "\" class=\"ProductBundlerVariants\">";
				strHtml += "<form method=\"post\">"
				strHtml += "<input type=\"hidden\" name=\"positionId\" value=\"" + varPositionId + "\" />";
				strHtml += strSku;
				strHtml += "</form>"
				strHtml += "</div>";
				strSku = "";
			}
		}
	});
	return strHtml;
}

//sets up the data for the second page display of the bundler
function getBundlerSummary(varImageUrl, varStrNoImage)
{
	var strHtml = "";
	//fetch the xml with the products
	$.ajax({
		url: 'ClientAction.aspx?action=getbundlersummary',
		type: 'get',
		cache: false,
		dataType: "xml",
		success: function(xml) {
			$('#BundleSummaryProductsList').contents().remove();
			$(xml).find('BundleItemsGet').each(function(){
				var varTotalCost = "\$" + Number($(this).find('TotalCost').text()).toFixed(2);
				$("#BundleSummaryPriceDollarFigure").html(varTotalCost);
				$(this).find('Sku').each(function(){
					var varSku = $(this).attr("sku");
					var varPfId = $(this).attr("pfid");
					if (varSku)
					{
						strHtml += "<li class=\"Product\"><div class=\"ImageContainer\"><img src=\"" + varImageUrl + varPfId + ".jpg\" border=\"0\" id=\"" + varPfId + "-" + varSku + "\" alt=\"" + varSku + "\" class=\"BundlerProductImage\" /></div></li>";
					}
				});
				$(strHtml).appendTo('#BundleSummaryProductsList');
			});
		},
		error: function(msg) {
			//alert("error");
		}
	})
}

//gets the available products when the bundler initially opens or re-opens and also re-initiates the drop area
function getBundlerContents(varImageUrl, varCatalogPath, varStrNoImage)
{
	//store this in a global variable so it can be used later on
	varBundlerCatalogPath = varCatalogPath;

	//fetch the xml with the products
	$.ajax({
		url: 'ClientAction.aspx?action=getbundlercontents&cp=' + varBundlerCatalogPath,
		type: 'get',
		cache: false,
		dataType: "xml",
		success: function(xml) {
			$('#AvailableProducts').contents().remove();
			$(xml).find('CatalogContent').each(function(){
				$(this).find('Content').each(function(){
					var varTitle = $(this).attr('name');
					$('#BundlerTitle').html(varTitle);
					var varDescription = $(this).find('PromoText').text();
					$('#BundlerDesc').html(varDescription);
					$(this).find('Products').each(function(){
						var i = 0;
						$(this).find('Product').each(function(){
							var strHtml = null;
							var varItemId = $(this).attr('name');
							var varSkuCount = $(this).find("SKU").length;
							var varSku = varItemId;
							if (varSkuCount == 1)
							{
								var varSku = $(this).find('SKU').attr('name');
							}
							var varPrice = "\$" + Number($(this).find('Attributes').attr('price')).toFixed(2);
							if (varItemId != null)
							{
								i++;
								var varImage = varStrNoImage;
								if ($(this).find('Image').text())
								{
									varImage = $(this).find('Image').text();
								}
								var varTag = $(this).find('Tag').text();
								var varName = $(this).find('Name').text();
								//var varPrice = "\$" + Number($(this).find('Price').text()).toFixed(2);

								strHtml = "<div class=\"Product\">";
								strHtml += "<div class=\"ImageContainer\">";
								strHtml += "<div class=\"Category\">";
								strHtml += "<div class=\"Text\" title=\"" + varTag + "\">" + varTag + "</div>";
								strHtml += "</div>";
								strHtml += "";
								strHtml += "<ul>";
								strHtml += "<li class=\"Image ImagePosition\" id=\"ProductImage" + i + "\"><div class=\"AddItem\" onclick=\"addToCarousel('" + varImageUrl + varImage + "','" + varItemId + "-" + varSku + "','" + varName.replace(/\'/g, '\\\'') + "'); return false;\"></div><div class=\"ImageDiv ImagePosition\"><img src=\"" + varImageUrl + varImage + "\" border=\"0\" id=\"" + varItemId + "-" + varSku + "\" alt=\"" + varName + "\" class=\"BundlerProductImage\" /></div></li>";
								strHtml += "</ul>";
								strHtml += "</div>";
								strHtml += "<div style=\"clear: both;\"></div>";
								strHtml += "<div class=\"Name EllipsisTwo\">" + varName + "</div>";
								strHtml += "<div class=\"Price\">" + varPrice + "</div>";
								strHtml += "</div>";
								$(strHtml).appendTo('#AvailableProducts');
							}
						});
						$('<div style=\"clear: both;\"></div>').appendTo('#AvailableProducts');
					});
				});
			});
			$(xml).find('BundleItemsGet').each(function(){
				var x = 0;

				//reset the carousel before adding the items
				var carousel = jQuery('#bundlerCarousel').data('jcarousel');
				carousel.size(0);
				carousel.reset();
				$('#CreateBundle').css('display','block');
				$('#BundlerSummary').css('display','none');
				var varTotalSavings = "\$" + Number($(this).find('TotalSavings').text()).toFixed(2);
				var varTotalCost = "\$" + Number($(this).find('TotalCost').text()).toFixed(2);
				$('#BundlerCostDollarFigure').html(varTotalCost);
				$(this).find('Sku').each(function(){
					++x;
					var varSku = $(this).attr('sku');
					var varPfId = $(this).attr('pfid');
					if (varSku)
					{
						if (varTotalSavings != "$0.00")
						{
							$('#BundlerSavingsText').css('display','block');
							$('#BundlerSavingsFigure').contents().remove();
							$('#BundlerSavingsFigure').html(varTotalSavings);
						}
						$('#BundlerSubmit').css('display','block');

						carousel.size(x);
						var varHtml = "<li class=\"Image\" id=\"DroppedProduct" + x + "\"><div class=\"RemoveItem\" onclick=\"reloadCarousel(" + x + ")\"></div><img src=\"" + varImageUrl + varPfId + ".jpg\" border=\"0\" id=\"" + varPfId + "-" + varSku + "\" alt=\"" + varSku + "\" class=\"BundlerProductImage\" /></li>";
						carousel.add(x, varHtml);
						varCarouselSize = carousel.size();
					}
				});
				if (varCarouselSize > 0)
				{
					$('.PlaceHolder').css('display','none');
				}
				carousel.reload();
			});
			$('.ImageDiv').draggable({
				helper: "clone",
				appendTo: ('#CreateBundle'),
				start: function(event, ui) {
					$(ui.helper).addClass("ui-draggable-helper");
				}
			});
			$('#Bundler').droppable({
				accept: ".ImageDiv",
				drop: function(event, ui) {
					buildAddToCarousel(ui.draggable.clone().html(), varCarouselSize + 1);
				}
			});
		},
		error: function(msg) {
			//alert("error");
		}
	})
}

//adds the items in the bundler to the cart and ends the bundler session
function submitBundlerContents(varLocation)
{
	$.ajax({
		url: 'ClientAction.aspx?action=bundleitemsaddtocart',
		type: 'post',
		cache: false,
		async: false,
		dataType: "xml",
		success: function(xml) {
			var carousel = jQuery("#bundlerCarousel").data("jcarousel");
			carousel.reset();
			$('#dialogProductBundler').dialog('close');
			if (varLocation == "checkout")
			{
				window.location.href="Cart.aspx";
			}
			else
			{
				window.location.href=window.location.href;
			}
		},
		error: function(msg) {
			//alert("error");
		}
	})
}

//used to interact with ClientAction.aspx
function submitVariant(varClientAction, varXmlNode,varSku,varPfId)
{
	$.ajax({
		url: varClientAction,
		type: 'post',
		cache: false,
		dataType: "xml",
		success: function(xml) {
			//if (varSku)
			//{
			//	//find an image with the pfid and change the id to the sku
			//	$("img#" + varPfId).attr("id",varSku);
			//}
			y = 0;
			$('#BundlerSavingsFigure').contents().remove();
			$(xml).find(varXmlNode).each(function(){
				var varTotalSavings = "\$" + Number($(this).find('TotalSavings').text()).toFixed(2);
				var varTotalCost = "\$" + Number($(this).find('TotalCost').text()).toFixed(2);
				if (varTotalSavings != "$0.00")
				{
					$('#BundlerSavingsText').css("display","block");
					$("#BundlerSavingsFigure").html(varTotalSavings);
				}
				else
				{
					$('#BundlerSavingsText').css("display","none");
				}
				$("#BundlerCostDollarFigure").html(varTotalCost);
				$("#BundlerSubmit").css("display","block");
				y++;
			});
			if (y = 0)
			{
				$('#BundlerSavingsText').css("display","none");
				$("#BundlerSubmit").css("display","none");
				$(".PlaceHolder").css("display","block");
			}
		},
		error: function(msg) {
			//alert(msg);
		}
	});
	varClientAction = null;
}

//Adds a product to the carousel
function buildAddToCarousel(varImageHtml, index)
{
	var carousel = jQuery('#bundlerCarousel').data('jcarousel');

	//get sku & pfid out of image id: image id is "PFID-SKU")
	var varImageId = $(varImageHtml).attr("id")
	var mySplitResult = varImageId.split("-");
	var varProductId = mySplitResult[0];
	var varItemId = mySplitResult[1];
	var strHtml = getBundlerProductVariants(varProductId,index);

	var el=$("<li class=\"Image\" id=\"DroppedProduct" + index + "\"><div class=\"RemoveItem\" onclick=\"reloadCarousel(" + index + ")\"></div>" + varImageHtml + "</li>");
	if (strHtml.indexOf("ProductBundlerVariants") > 0)
	{
		$(el).filter("li").append(strHtml).find("img").attr("id",varProductId + "-" + varItemId);
	}
	else
	{
		submitVariant("ClientAction.aspx?action=addtobundler&sku=" + varItemId + "&pfid=" + varProductId + "&position=" + index,"BundleItemAdd",strHtml,varProductId);
	}
	carousel.size(index);
	carousel.add(index, el);
	carousel.reload();
	varCarouselSize = carousel.size();
	if (varCarouselSize != 0)
	{
		$(".PlaceHolder").css("display","none");
	}
	if (varCarouselSize > 5)
	{
		carousel.scroll(varCarouselSize, 0);
	}
}

//Handles gathering the data on the click event (versus drag and drop) add to cart
function addToCarousel(varImageUrl,varItemId,varCategory)
{
	varImageHtml = "<img src=\"" + varImageUrl + "\" border=\"0\" id=\"" + varItemId + "\" alt=\"" + varCategory + "\" class=\"BundlerProductImage\" />";
	buildAddToCarousel(varImageHtml, varCarouselSize+1);
}

function itemLoadCallbackFunction(carousel, state)
{
	//alert(state);
	if (state == 'init') {
		carousel.size(0);
		varCarouselSize == 0;
	}
}

//rebuilds carousel after an item is deleted so that we aren't left with gaps where the product was.
function reloadCarousel(varDeletedItem)
{
	var carousel = jQuery("#bundlerCarousel").data("jcarousel");
	var itemsHTML = new Array();
	var counter = 0;
	var index = carousel.size();
	for (i = 1; i <= index; i++) {
		if (i != varDeletedItem)
		{
			var varSrc = $("#bundlerCarousel").find('li[jcarouselindex|="' + i + '"]').find("img").attr("src");
			var varId = $("#bundlerCarousel").find('li[jcarouselindex|="' + i + '"]').find("img").attr("id");
			var varAlt = $("#bundlerCarousel").find('li[jcarouselindex|="' + i + '"]').find("img").attr("alt");
			//var varString = $("#bundlerCarousel").find('li[jcarouselindex|="' + i + '"]').html();
			var varString = "<img src=\"" + varSrc + "\" border=\"0\" id=\"" + varId + "\" alt=\"" + varAlt + "\" class=\"BundlerProductImage\" />"
			itemsHTML[counter++] = varString;
		}
		else
		{
			var varCurrentProduct = $("#bundlerCarousel").find('li[jcarouselindex|="' + i + '"]').find("img").attr("id");
			var mySplitResult = varCurrentProduct.split("-");
			var varItemId = mySplitResult[1];
			submitVariant("ClientAction.aspx?action=removefrombundler&sku=" + varItemId + "&position=" + i,"BundleItemDelete",null,null);
		}
	}
	carousel.size(0);
	carousel.reset();
	counter = 0;
	for (x = 1; x <= itemsHTML.length; x++) {
		var varString = "<li class=\"Image\" id=\"DroppedProduct" + x + "\"><div class=\"RemoveItem\" onclick=\"reloadCarousel(" + x + ")\"></div>" + itemsHTML[counter++] + "</li>";
		carousel.size(x);
		carousel.add(x, varString);
	}
	varCarouselSize = carousel.size();
	carousel.reload();
	if ((varCarouselSize == 0) || (counter == 0))
	{
		$(".PlaceHolder").css("display","block");
		$("#BundlerSubmit").css("display","none");
		$('#BundlerSavingsFigure').contents().remove();
		$('#BundlerSavingsText').css("display","none");
	}
}

