/* Faux Select Functions */

function setFilterCriteria(theLink, controlName)
{
	var theElement = theLink.parentNode.parentNode;
	if (!theElement.getAttribute("isClean")) {
		var oLinks = theElement.getElementsByTagName("li");
		for (i = 0; i < oLinks.length; i++)
		{
			// Set an attribute 'order' on each list item, we will use to sort later on
			oLinks[i].setAttribute("order", i);
			// Set this attribute on the list so I can check against it to see if whitespace has already been removed and the order put on the list items
			theElement.setAttribute("isClean", "yes");
		}
	}
	
	// Set this attribute to check against, to see if the select needs to be opened or closed
	if (!theElement.getAttribute("selectOpen")) {
		theElement.setAttribute("selectOpen", "no");
	}
	
	if (controlName != "")
		var idPrefix = controlName + "_";
	else
		var idPrefix = "";
	
	if (theLink == theElement.getElementsByTagName("a")[0]) {
		// Toggle the list off and on
		toggleOptions(theLink, theElement);
	} else {
		// Select a specific option if it is changing
		pickThisOption(theLink, theElement, idPrefix);
	}
}

function toggleOptions(theLink, theElement)
{
	// hide 'fit guide' link on product detail page (display error in IE)
	if (theLink.id.indexOf("colorId") != -1) {
		if (theElement.getAttribute("selectOpen") == "yes") {
			document.getElementById("sizingGuide").style.display = "block";
		} else {
			document.getElementById("sizingGuide").style.display = "none";
		}
	}
	
	// Start this loop on the second list item
	var oLinks = theElement.getElementsByTagName("li");
	for (i = 1; i < oLinks.length; i++)
	{
		if (theElement.getAttribute("selectOpen") == "yes") {
			// Turning the list off
			// Setting parent DIV and list to position: static so the non-active lists won't show through the active ones
			theElement.parentNode.style.position = "static";
			theElement.style.position = "static";
			oLinks[i].style.display = "none";
		} else {
			if (i == 1) {
				closeOpenSelects(theLink);
			}
			// Turning the list on
			// Set parent DIV to position: relative, list to position: absolute, and throw a z-index on the list as well
			// List items set to display: block
			theElement.parentNode.style.position = "relative";
			theElement.style.position = "absolute";
			theElement.style.zIndex = "100";
			oLinks[i].style.display = "block";
		}
	}
	
	// Reset the attribute of whether it's open or not
	if (theElement.getAttribute("selectOpen") == "yes") {
		theElement.setAttribute("selectOpen", "no");
	} else {
		theElement.setAttribute("selectOpen", "yes");
	}
	
	// Removes the dotted line focus style
	theLink.blur();
}

// this function looks to see if any selects are open before opening a brand new select
function closeOpenSelects(theLink)
{
	/*theDivs = document.getElementById("envelope").getElementsByTagName("DIV");
	theListItems = new Array();
	for (j = 0; j < theDivs.length; j++)
	{
		// get all the selects on the page
		if (theDivs[j].className == "selectContainer") {
			
			for (k = 0; k < theListItems.length; k++)
			{
				if (theListItems[k].style.position == "static") {
					alert(1);
				}
			}
		}
	}
	*/
	
}


function pickThisOption(theLink, theElement, idPrefix)
{
	
	var theList = theLink.parentNode.parentNode;
	
	var oLinks = theList.getElementsByTagName("li");
	
	// save how many list items there are
	var listLength = oLinks.length;
	
	// set variable to indicate which dropdown is hitting this function
	// at the bottom of the function, we trigger other functions based off of this
	var whichDropdown = "";
	if (theLink.getAttribute("id")) {
		if (theLink.id.indexOf("colorId") != -1) {
			whichDropdown = "productDetailColorChange";
		}
		if (theLink.id.indexOf("sizeId") != -1) {
			whichDropdown = "productDetailSizeChange";
		}
		if (theLink.id.indexOf("categoryFilter") != -1) {
			whichDropdown = "filterCategoryChange";
		}
		if (theLink.id.indexOf("priceFilter") != -1) {
			whichDropdown = "filterPriceChange";
		}
		if (theLink.id.indexOf("sizeFilter") != -1) {
			whichDropdown = "filterSizeChange";
		}
		if (theLink.id.indexOf("colorFilter") != -1) {
			whichDropdown = "filterColorChange";
		}
	}
			
	// set 'whichDropdown' variable when using continue shopping dropdown
	if (theLink.parentNode.parentNode.parentNode.id == "continueShopping") {
		whichDropdown = "continueShopping";
	}
	
	// which faux select are we dealing with?
	var whichSelectId = theList.getAttribute("id");
	var whichSelectNumber = whichSelectId.charAt(whichSelectId.length - 1);
	
	// update class of selected item
	var selectedList = theLink.parentNode;
	selectedList.className = "defaultOption";
	oLinks[0].className = "";
	theList.insertBefore(selectedList, oLinks[0]);
	oLinks[1].style.display = "block";
		
	// move previous selected option back to proper sort order
	var sortOrder = parseInt(oLinks[1].getAttribute("order"), 10);
	if (sortOrder > 0) {
		if (sortOrder == oLinks.length - 1) {
			theList.appendChild(oLinks[1]);
		} else {
			for (var i = 2, j = oLinks.length; i < j; i++) {
				var targetSort = parseInt(oLinks[i].getAttribute("order"), 10);
				if (targetSort == sortOrder - 1) {
					if (i + 1 == oLinks.length) {
						theList.appendChild(oLinks[1]);
					} else {
						theList.insertBefore(oLinks[1], oLinks[i + 1]);
					}
					break;
				} else if (targetSort == sortOrder + 1) {
					theList.insertBefore(oLinks[1], oLinks[i]);
					break;
				}
			}
		}
	}
	
	// set the hidden form field for this faux select to the correct value
	var theHiddenFieldId = idPrefix + "captureSelect" + whichSelectNumber;
	document.getElementById(theHiddenFieldId).value = selectedList.childNodes[0].text;

	// run functions based upon which dropdown we are using
	// 'whichDropdown' variable is set at top of function
	switch (whichDropdown)
	{
		case "productDetailColorChange":
			updateColorFromDropdown(theLink);
			break;
		case "productDetailSizeChange":
			updateSizePriceFromDropdown(theLink);
			break;
		case "filterCategoryChange":
			updateCategoryFilter(theLink, idPrefix);
			break;
		case "filterPriceChange":
			updatePriceFilter(theLink, idPrefix);
			break;
		case "filterSizeChange":
			updateSizeFilter(theLink, idPrefix);
			break;
		case "filterColorChange":
			updateColorFilter(theLink, idPrefix);
			break;
		case "continueShopping":
			continueShopping(theLink);
			break;
	}
	
	// go back through the main function to close the select
	setFilterCriteria(selectedList.childNodes[0], idPrefix);
}

function updateColorFromDropdown(theLink)
{
	// update the selectedItemId variable
	var theNewItemId = theLink.getAttribute("id");
	theNewItemId = theNewItemId.substring(7,theNewItemId.length);
	selectedItemId = theNewItemId;
	
	// change 'selected color' and 'selected fabric' text (displayed above content)
	document.getElementById("selectedColor").innerHTML = theLink.innerHTML;
	document.getElementById("selectedFabric").innerHTML = items[selectedItemId]["fabric"];
	var oClassification = document.getElementById( "gq" );
	oClassification.innerHTML = items[selectedItemId]["classification"].title;
	oClassification.className = "classification" + items[selectedItemId]["classification"].id;
	
	// toggle visibility of limited stock
	var oLimited = document.getElementById("limitedSupply");
	if (items[selectedItemId]["limited"]) {
		oLimited.style.visibility = "visible";
	} else {
		oLimited.style.visibility = "hidden";
	}
	
	// update Flash movie color shown (this function is on the page)
	if (theLink.getAttribute("fromFlash") == null || theLink.getAttribute("fromFlash") == "notFromFlash") {
		updateFlashColor(items[selectedItemId]["itemId"]);
	} else {
		theLink.setAttribute("fromFlash", "notFromFlash");
	}
	
	// update size dropdown
	updateSizes();
}

function updateColorFromFlash(itemId)
{
	var prefix = "ctl00_ctl00_masterPlaceHolder_contentPlaceHolder";
	
	var theColorList = getThis("fauxSelect1");
	if (theColorList) {
		var theListItems = theColorList.getElementsByTagName("li");
		
		// Find the link that matches the swatch
		for (i = 0; i < theListItems.length; i++)
		{
			if (theListItems[i].getElementsByTagName("a")[0].id.indexOf(itemId) != -1) {
				var theLink = theListItems[i].getElementsByTagName("a")[0];
				break;
			}
		}
		
		// open the dropdown	
		setFilterCriteria(theListItems[0].getElementsByTagName("a")[0], prefix);
		
		// set an attribute that tells us we are coming from the Flash movie
		theLink.setAttribute("fromFlash", true);
		setFilterCriteria(theLink, prefix);
		// reset the attribute so the dropdown still works
		theLink.setAttribute("fromFlash", "notFromFlash");
	}
}

function updateSizes()
{
	// repopulate the size/price dropdown
	var sizePriceDropdown = getThis("fauxSelect2");
	sizePriceDropdown.removeAttribute("isClean");
	// remove all the options that are already there
	while (sizePriceDropdown.hasChildNodes())
	{
		sizePriceDropdown.removeChild(sizePriceDropdown.firstChild);
	}
	// add in all the new list items
	for (i = 0; i < itemUpcs[selectedItemId].length; i++)
	{
		// create list item
		var newListItem = document.createElement("li");
		// append a class if it is the first one
		if (i == 0) {
			newListItem.setAttribute("class", "defaultOption");
			newListItem.style.display = "block";
		}
		
		// create link within list item and attach appropriate attributes
		var newLink = document.createElement("a");
		newLink.setAttribute("href", "#");
		var theSizeId = "sizeId" + itemUpcs[selectedItemId][i]["skuId"];
		newLink.setAttribute("id", theSizeId);
		// for firefox
		newLink.setAttribute("onclick", "setFilterCriteria(this, 'ctl00_ctl00_masterPlaceHolder_contentPlaceHolder'); return false;");
		// for IE
		newLink.onclick = function(){setFilterCriteria(this, 'ctl00_ctl00_masterPlaceHolder_contentPlaceHolder'); return false;};
		
		// grab sizes and price from array and populate link
		var newLinkText = itemUpcs[selectedItemId][i]["size"] + " | $" + itemUpcs[selectedItemId][i]["price"];
		newLinkText = document.createTextNode(newLinkText);
			
		// append text to the link, link to the list item, then list item to the list
		newLink.appendChild(newLinkText);
		newListItem.appendChild(newLink);
		sizePriceDropdown.appendChild(newListItem);
		
		// reset selected SKU id to the first available size in this color
		if (i == 0) {
			selectedSkuId = itemUpcs[selectedItemId][i]["skuId"];
			document.getElementById("defaultUpcId").value = itemUpcs[selectedItemId][i]["skuId"];
			document.getElementById("ctl00_ctl00_masterPlaceHolder_contentPlaceHolder_ihSku").value = itemUpcs[selectedItemId][i]["skuId"];
		}
		
	}
}

function updateSizePriceFromDropdown(theLink)
{
	// update the selectedSkuId variable and form fields
	var theNewSkuId = theLink.id;
	theNewSkuId = theNewSkuId.substring(6,theNewSkuId.length);
	selectedSkuId = theNewSkuId;
	document.getElementById("defaultUpcId").value = theNewSkuId;
	document.getElementById("ctl00_ctl00_masterPlaceHolder_contentPlaceHolder_ihSku").value = theNewSkuId;
}

function updateCategoryFilter(theLink, idPrefix)
{
	// update the proper hidden form field with the category id number
	var theFilterCategoryId = theLink.id;
	theFilterCategoryId  = theFilterCategoryId.substring(16,theFilterCategoryId.length);
	document.getElementById(idPrefix + "captureSelect4").value = theFilterCategoryId;
	//populateSizeDropdownByCategory (theFilterCategoryId);
	//buildFilterRequest(idPrefix);
}

function updatePriceFilter(theLink, idPrefix)
{
	// update the proper hidden form field with the price range string
	var theFilterPriceId = theLink.id;
	theFilterPriceId  = theFilterPriceId.substring(13,theFilterPriceId.length);
	document.getElementById(idPrefix + "captureSelect1").value = theFilterPriceId;
	//buildFilterRequest(idPrefix);
}

function updateSizeFilter(theLink, idPrefix)
{
	// update the proper hidden form field with the size id number
	var theFilterSizeId = theLink.id;
	theFilterSizeId = theFilterSizeId.substring(12,theFilterSizeId.length);
	document.getElementById(idPrefix + "captureSelect2").value = theFilterSizeId;
	//buildFilterRequest(idPrefix);
}

function updateColorFilter(theLink, idPrefix)
{
	// update the proper hidden form field with the color id number
	var theFilterColorId = theLink.id;
	theFilterColorId = theFilterColorId.substring(13,theFilterColorId.length);
	document.getElementById(idPrefix + "captureSelect3").value = theFilterColorId;
	//buildFilterRequest(idPrefix);
}

function rewriteFlash(theNewColor)
{
	var FO1 = { movie: sFolderLevel + "images/swf/product-detail.swf", width:"391", height:"363", majorversion:"7", build:"40", flashvars:"pid=" + theNewColor };
	UFO.create(FO1, "flashDetail");
}

function continueShopping(theLink)
{
	
	window.location = theLink.href;
}

function performSearch(idPrefix)
{
	if (isdefined(window, 'sSearchTerm')) {
		var term = sSearchTerm;
	}
	else {
		var term = "";
	}
	
	if ( document.getElementById(idPrefix+ "_captureSelect4") ) {
		var categoryid = document.getElementById(idPrefix + "_captureSelect4").value
	}
	else
	{
		var categoryid =  sCategoryId;
	}
	
	var selectedprice = document.getElementById(idPrefix + "_captureSelect1").value;
	var minprice = selectedprice.substring(0,selectedprice.indexOf("-"));
	var maxprice = selectedprice.substring(selectedprice.indexOf("-")+1, selectedprice.length);

	var sizeid = 0;
	//var sizeid = document.getElementById(idPrefix + "_captureSelect2").value;
	var colorid = document.getElementById(idPrefix + "_captureSelect3").value;

	window.location = sBaseUrl + "/catalog/search-results.aspx?term=" + term + "&categoryid=" + categoryid + "&minprice=" + minprice + "&maxprice=" + maxprice + "&colorid=" + colorid + "&sizeid=" + sizeid ;
	
}

function performFilter(url, idPrefix)
{
	var urlprefix = url.protocol + "//" + url.host + url.pathname;
	if (isdefined(window, 'sSearchTerm')) {
		var term = sSearchTerm;
	}
	else {
		var term = "";
	}
	
	if ( document.getElementById(idPrefix+ "_captureSelect4") ) {
		var categoryid = document.getElementById(idPrefix+ "_captureSelect4").value
	}
	else
	{
		var categoryid = sCategoryId;
	}
	
	var subcategoryid = sSubcategoryId;
	var subsubcategoryid = sSubsubcategoryId;
	
	if (subsubcategoryid != 0) {
		subcategoryid = subsubcategoryid;
	}
	
	if (isdefined(window, 'sGenderId')) {
		var genderid = sGenderId;
	}
	else {
		var genderid = "0";
	}
	
	var selectedprice = document.getElementById(idPrefix+ "_captureSelect1").value;
	var minprice = selectedprice.substring(0,selectedprice.indexOf("-"));
	var maxprice = selectedprice.substring(selectedprice.indexOf("-")+1, selectedprice.length);
	
	if ( document.getElementById(idPrefix+ "_captureSelect2") ) {
		var sizeid = document.getElementById(idPrefix+ "_captureSelect2").value;
	}
	else
	{
		var sizeid = 0;
	}

	var colorid = document.getElementById(idPrefix+ "_captureSelect3").value;
	if (colorid == '')
		colorid = 0;

	window.location = urlprefix +  "?categoryid=" + categoryid + "&scid=" + subcategoryid + "&minprice=" + minprice + "&maxprice=" + maxprice + "&colorid=" + colorid + "&sizeid=" + sizeid + "&filter=true" ;
}

function showMoreHandler(e, obj) {
	YAHOO.util.Event.preventDefault(e);
	
	try {
		var currentState = YAHOO.util.History.getCurrentState("c" + obj.index);
		
		if ("cl" != currentState) {
			YAHOO.util.History.navigate("c" + obj.index, "cl");
		} else {
			YAHOO.util.History.navigate("c" + obj.index, "ex");
		}
	} catch (ex) {
		if (this.innerHTML.indexOf("see all") != -1) {
			showMore(this, obj.container, true);
		} else {
			showMore(this, obj.container, false);
		}
	}
}

function showMore(theLink, obj, show)
{
	var iSubCat = obj.className.match(/category(\d+)/)[1];
	var oItems = YAHOO.util.Dom.getElementsByClassName("productList", "div", obj);
	
	var sDisplay = "block";
	
	// show all the products
	var model = "";
	if (show) {
		model = theLink.innerHTML.replace("see all ", "");
		theLink.innerHTML = "see " + model + " highlights";
		theLink.style.backgroundImage = "url(../../shared/images/interface/more-link-minus-sign.gif)";
	}
	// hide the extra products if they are already showing
	else {
		if (theLink.innerHTML.indexOf("see all") == -1) {
			model = theLink.innerHTML.replace("see ", "");
			model = model.replace(" highlights", "");
			theLink.innerHTML = "see all " + model;
		}
		theLink.style.backgroundImage = "url(../../shared/images/interface/more-link-plus-sign.gif)";
		sDisplay = "none";
	}
	
	for (i = 0; i < oItems.length; i++)
	{
		if (YAHOO.util.Dom.hasClass(oItems[i], "additionalProduct")) {
			if (sDisplay == "block")
			{
				var image = oItems[i].getElementsByTagName("img")[0];
				if (image.src.match(/.+?loading.gif$/)) 
				{
					image.src = images_cat[iSubCat][i];
				}
			}
			oItems[i].style.display = sDisplay;
		}
	}
}

function showFullImage(itemId)
{
	// put overlays on the interface
	overlayConstants();
	overlayContent();
	
	// create the envelope to hold everything
	var fullImageEnvelope = document.createElement("DIV");
	fullImageEnvelope.id = "fullImageEnvelope";
	document.getElementById("detailContentDiv").appendChild(fullImageEnvelope);
	
	// set a variable to what folder the image is in
	var imgFolder = new Array();
	imgFolder = items[itemId]["itemId"].split('-');
	imgFolder = imgFolder[0];

	// get the zoomed image to show	and append it
	var fullImage = document.createElement("IMG");
	fullImage.src = sFolderLevel + "img/products/" + imgFolder + "/" + items[itemId]["itemId"] + "-z.jpg" ;
	fullImageEnvelope.appendChild(fullImage);
	
	// create DIV to hold the rest of the info and append it
	var fullImageDetails = document.createElement("DIV");
	fullImageDetails.id = "fullImageDetails";
	fullImageEnvelope.appendChild(fullImageDetails);
	
	// hide problem DIVs
	document.getElementById("flashDetail").style.display = "none";
		
	// create detail elements and append them to the detail div
	var fullImageNumber = document.createElement("P");
	fullImageNumber.id = "fullImageNumber";
	fullImageNumber.innerHTML = "No: " + document.getElementById("ProductNumber").innerHTML;
	fullImageDetails.appendChild(fullImageNumber);
	var fullImageHeader = document.createElement("H1");
	fullImageHeader.innerHTML = document.getElementById("productDetail").getElementsByTagName("h1")[0].innerHTML;
	fullImageDetails.appendChild(fullImageHeader);
	var fullImageColorFabric = document.createElement("P");
	fullImageColorFabric.id = "fullImageColorFabric";
	fullImageColorFabric.innerHTML = document.getElementById("selectedColor").innerHTML + " | " + document.getElementById("selectedFabric").innerHTML;
	fullImageDetails.appendChild(fullImageColorFabric);
	var fullImageClose = document.createElement("IMG");
	fullImageClose.src = sFolderLevel + "images/buttons/close.gif";
	fullImageClose.onclick = function(e)
	{
		closeFullImage();
	}
	fullImageDetails.appendChild(fullImageClose);
}

function closeFullImage()
{
	// show problem DIVs
	document.getElementById("flashDetail").style.display = "block";
	
	var theChild = document.getElementById("fullImageEnvelope");
	var theChild2 = document.getElementById("constantsOverlay");
	var theChild3 = document.getElementById("contentOverlay");
	theChild.parentNode.removeChild(theChild);
	theChild2.parentNode.removeChild(theChild2);
	theChild3.parentNode.removeChild(theChild3);
}

// adjust page styles for kids product detail
function adjustKids(isKidsProduct)
{
	if (!isKidsProduct) {
		return;
	}
		
	// make the ULs wider
	var theMainDiv = getThis("detailAddToCart");
	var theDivs = theMainDiv.getElementsByTagName("DIV");
	for (i = 0; i < theDivs.length; i++)
	{
		if (theDivs[i].className == "selectContainer") {
			theDivs[i].className = "selectContainer kidsSelect";
		}
	}
	var theULs = theMainDiv.getElementsByTagName("UL");
	for (i = 0; i < theULs.length; i++)
	{
		theULs[i].style.width = "230px";
	}
	if (document.getElementById("oneColor")) {
		document.getElementById("oneColor").style.width = "226px";
	}
	
	
	// move over the quantity field and the add to cart button and the limited supply focus area
	var theQuantity = getThis("detailQuantity");
	theQuantity.getElementsByTagName("LABEL")[0].style.width = "142px";
	
	var cartButton = getThis("addToCart") || getThis("updateCart");
	if (cartButton) {
		cartButton.style.marginRight = "50px";
		if (IS_IE) {
			cartButton.style.marginRight = "25px";
		}
	}
	
	var theSupply = getThis("limitedSupply");
	if (theSupply.style) {
		theSupply.style.left = "571px";
	}
	var theFit = getThis("sizingGuide");
	theFit.style.left = "191px";
}

function resetFilter(controlName)
{
	var thePriceList = getThis("fauxSelect1");
	var theListItems = thePriceList.getElementsByTagName("li");
	for (i = 0; i < theListItems.length; i++)
	{
		if (theListItems[i].getElementsByTagName("a")[0].innerHTML.indexOf("any") != -1) {
			
			var theLink = theListItems[i].getElementsByTagName("a")[0];
			theListItems[i].className = "defaultOption";
			break;
		} else {
			theListItems[i].className = "defaultOption";
		}
	}
	
	setFilterCriteria(theListItems[0].getElementsByTagName("a")[0], controlName);
	setFilterCriteria(theLink, controlName);
	
	if ( document.getElementById("fauxSelect2") ) {
		var theSizeList = getThis("fauxSelect2");
		var theListItems = theSizeList.getElementsByTagName("li");
		for (i = 0; i < theListItems.length; i++)
		{
			if (theListItems[i].getElementsByTagName("a")[0].innerHTML.indexOf("any") != -1) {
				
				var theLink = theListItems[i].getElementsByTagName("a")[0];
				theListItems[i].className = "defaultOption";
				break;
			} else {
				theListItems[i].className = "defaultOption";
			}
		}
			
		setFilterCriteria(theListItems[0].getElementsByTagName("a")[0], controlName);
		setFilterCriteria(theLink, controlName);
	}
	
	var theColorList = getThis("fauxSelect3");
	var theListItems = theColorList.getElementsByTagName("li");
	
	for (i = 0; i < theListItems.length; i++)
	{
		if (theListItems[i].getElementsByTagName("a")[0].innerHTML.indexOf("any") != -1) {
			var theLink = theListItems[i].getElementsByTagName("a")[0];
			theListItems[i].className = "defaultOption";
			break;
		} else {
			theListItems[i].className = "";
		}
	}
	
	setFilterCriteria(theListItems[0].getElementsByTagName("a")[0], controlName);
	setFilterCriteria(theLink, controlName);
	
	if ( document.getElementById("fauxSelect4") ) {
		var theCategoryList = getThis("fauxSelect4");
		var theListItems = theCategoryList .getElementsByTagName("li");
		for (i = 0; i < theListItems.length; i++)
		{
			if (theListItems[i].getElementsByTagName("a")[0].innerHTML.indexOf("any") != -1) {
				
				var theLink = theListItems[i].getElementsByTagName("a")[0];
				theListItems[i].className = "defaultOption";
				break;
			} else {
				theListItems[i].className = "defaultOption";
			}
		}
			
		setFilterCriteria(theListItems[0].getElementsByTagName("a")[0], controlName);
		setFilterCriteria(theLink, controlName);
	}

}
var images_cat = [];
var ProductListing = {
	filterModules: [],
	categoryModules: [],
	prefix: 'ctl00_ctl00_masterPlaceHolder_contentPlaceHolder_',
	initMoreLinks: function() {
		var oGroups = YAHOO.util.Dom.getElementsByClassName("productLists", "div", ProductListing.prefix + "catalogProductList_pnlSubcategories");

		for (var i = 0, j = oGroups.length; i < j; i++) {
			var oLinkContainer = YAHOO.util.Dom.getElementsByClassName("moreLink", "div", oGroups[i]);
			if (oLinkContainer.length > 0) {
				var oLink = oLinkContainer[0].getElementsByTagName("a")[0];
				
				YAHOO.util.Event.on(oLink, "click", showMoreHandler, { container: oGroups[i], index: (i + 1) });
			}
		}
	},
	registerHandler: function(state, obj) {
		var expand = (state == "cl") ? false : true;
		
		var oGroups = YAHOO.util.Dom.getElementsByClassName("productLists", "div", ProductListing.prefix + "catalogProductList_pnlSubcategories");
		
		if (oGroups[obj.index]) {
			var oLinkContainer = YAHOO.util.Dom.getElementsByClassName("moreLink", "div", oGroups[obj.index])
			
			if (oLinkContainer.length > 0) {
				showMore(oLinkContainer[0].getElementsByTagName("a")[0], oGroups[obj.index], expand);
			}
		}
	},
	registerFilterHandler: function(state, obj) {
		ProductListing.processFilter(state);
	},
	processFilter: function(filter) {
		var args = filter.split('_');
		var size = color = minPrice = maxPrice = 0;
		var page = "1";
		
		for(var i = 0, j = args.length; i < j; i++) {
			var parts = args[i].split('|');
			switch(parts[0]) {
				case "1":
					if (parts[1] == "-1") {
						minPrice = -1;
					} else {
						var prices = parts[1].split('-');
						if (prices.length == 2) {
							minPrice = prices[0];
							maxPrice = prices[1];
						}
					}
					break;
				case "2":
				case "4":
					size = parts[1];
					break;
				case "3":
					color = parts[1];
					break;
				case "p":
					page = parts[1];
					break;
			}
		}
		
		var lists = ["1", "2", "3", "4"];
		resetFilter('ctl00_ctl00_masterPlaceHolder_catalogFilter');
		for (i = 0, j = lists.length; i < j; i++) {
			var container = document.getElementById("fauxSelect" + lists[i]);
			if (container) {
				var links = document.getElementById("fauxSelect" + lists[i]).getElementsByTagName("a");
				for (var k = 0, l = links.length; k < l; k++) {
					var value = links[k].id.replace("priceFilterId", "");
					value = value.replace("sizeFilterId", "");
					value = value.replace("colorFilterId", "");
					value = value.replace("categoryFilterId", "");
					
					var match;
					switch(lists[i]) {
						case "1":
							match = minPrice + "-" + maxPrice;
							break;
						case "2":
						case "4":
							match = size;
							break;
						case "3":
							match = color;
							break;
					}
					if (match == value) {
						setFilterCriteria(links[0], 'ctl00_ctl00_masterPlaceHolder_catalogFilter');
						setFilterCriteria(links[k], 'ctl00_ctl00_masterPlaceHolder_catalogFilter');				
					}
				}
			}
		}
		
		ProductListing.filterCategories(size, color, minPrice, maxPrice, page);
	},
	initHistory: function() {
		var initialState;
		var oFilters = YAHOO.util.Dom.getElementsByClassName("selectBox", "ul", "filter");
		var args = [];
		for (var i = 0, j = oFilters.length; i < j; i++) {
			var id = oFilters[i].id.replace("fauxSelect", "");
			args.push(id + "|-1");
		}
		args.push("p|1");
		
		var initialState = YAHOO.util.History.getBookmarkedState("f") || args.join("_");
		
		YAHOO.util.History.register("f", initialState, ProductListing.registerFilterHandler);
		
		var oGroups = YAHOO.util.Dom.getElementsByClassName("productLists", "div", ProductListing.prefix + "catalogProductList_pnlSubcategories");
		
		for (i = 0, j = oGroups.length; i < j; i++) {
			var defaultState = "cl";
			var bookmarkedState = YAHOO.util.History.getBookmarkedState("c" + (i + 1));
		    var initialState = bookmarkedState || defaultState;
			
			var oLinkContainer = YAHOO.util.Dom.getElementsByClassName("moreLink", "div", oGroups[i]);
			
		    // Register our module. Module registration MUST
		    // take place before calling YAHOO.util.History.initialize.
			if (oLinkContainer.length > 0) {
				
			    YAHOO.util.History.register("c" + (i + 1), initialState, ProductListing.registerHandler, { index: i });
			}
		}
		
		YAHOO.util.Event.on("filterSearchButton", "click", ProductListing.filterSearchClick);
		
		YAHOO.util.History.onReady(function () {
			var skipCategories = false;
			
			var defaultFilter;
			var oFilters = YAHOO.util.Dom.getElementsByClassName("selectBox", "ul", "filter");
			var args = [];
			for (var i = 0, j = oFilters.length; i < j; i++) {
				var id = oFilters[i].id.replace("fauxSelect", "");
				args.push(id + "|-1");
			}
			args.push("p|1");
			defaultFilter = args.join("_");
	        var currentState = YAHOO.util.History.getCurrentState("f") || defaultFilter;
			
			if (currentState != defaultFilter) {
				ProductListing.processFilter(currentState);
				skipCategories = true;
			}
			
			for (var i = 0, j = oGroups.length; i < j; i++) {
				try {
			        currentState = YAHOO.util.History.getCurrentState("c" + (i + 1));
			        ProductListing.categoryModules.push((i + 1).toString());
					
					if (!skipCategories) {
						var oLinkContainer = YAHOO.util.Dom.getElementsByClassName("moreLink", "div", oGroups[i]);
						if (oLinkContainer.length > 0) {
							if (currentState == "cl") {
								showMore(oLinkContainer[0].getElementsByTagName("a")[0], oGroups[i], false);
							} else {
								showMore(oLinkContainer[0].getElementsByTagName("a")[0], oGroups[i], true);
							}
						}
					}
				} catch (ex) {
				
				}
			}
			
			ProductListing.initMoreLinks();
			SearchListing.initPageLinks();
	    });

		// Initialize the browser history management library.
	    try {
	        YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
	    } catch (e) {
	        ProductListing.initMoreLinks();
	    }
	},
	initLoadingGraphics: function() {
		// Init loading graphics
		
		if (document.getElementById("searchResults")) {
			var height = $('#searchResults').height();
            $('#contentLoading').addClass('show').height(height);
            $('#searchResults > div').fadeTo('normal', 0);
            document.body.style.cursor = 'wait';
			
			// Hide paging
			$("#pagination").css("visibility", "hidden");
		} else {
			var hideContainers = YAHOO.util.Dom.getElementsByClassName("hideLoading", "div", "envelope");
			for (var i = 0, j = hideContainers.length; i < j; i++) {
				YAHOO.util.Dom.setStyle(hideContainers[i], "display", "none");
			}
			
			var img2 = new Image();
			img2.src = sFolderLevel + "images/interface/filter-loader.gif";
			
			document.getElementById("statusLoading").innerHTML = "Loading... ";
			document.getElementById("statusLoading").appendChild(img2);
			
			YAHOO.util.Dom.setStyle("statusLoading", "display", "block");
		}
	},
	filterCategories: function(size, color, minPrice, maxPrice, page) {
		ProductListing.initLoadingGraphics();
		
		if (sControlId) {
		    Anthem_InvokeControlMethod(
				sControlId,
		        'FilterCategories', 
		        [size, color, minPrice, maxPrice, page],
		        ProductListing.filterCallback
		    );
		} else {
			Anthem_InvokePageMethod(
		        'FilterCategories', 
		        [size, color, minPrice, maxPrice, page],
		        ProductListing.filterCallback
		    );
		}
	},
	filterCallback: function(result) {
		ProductListing.initMoreLinks();
		if (document.getElementById("searchResults")) {
			SearchListing.initLinks();
			SearchListing.initPageLinks();
			
			$('#contentLoading').removeClass('show').height('auto');
            SearchListing.initLinks();
            $('#searchResults > div').fadeTo('normal', 1);
            document.body.style.cursor = 'default';
		}
		document.getElementById("filterLoading").innerHTML = "";
		
		var hideContainers = YAHOO.util.Dom.getElementsByClassName("hideLoading", "div", "envelope");
		for (var i = 0, j = hideContainers.length; i < j; i++) {
			YAHOO.util.Dom.setStyle(hideContainers[i], "display", "block");
		}
		YAHOO.util.Dom.setStyle("statusLoading", "display", "none");
		document.getElementById("statusLoading").innerHTML = "";
		
		var oGroups = YAHOO.util.Dom.getElementsByClassName("productLists", "div", ProductListing.prefix + "catalogProductList_pnlSubcategories");
		// expand categories
		for (var i = 0, j = oGroups.length; i < j; i++) {
			try {
		        currentState = YAHOO.util.History.getCurrentState("c" + (i + 1));
		       
				var oLinkContainer = YAHOO.util.Dom.getElementsByClassName("moreLink", "div", oGroups[i]);
				if (oLinkContainer.length > 0) {
					if (currentState == "ex") {
						showMore(oLinkContainer[0].getElementsByTagName("a")[0], oGroups[i], true);
					}
				}
			} catch (ex) {
			
			}
		}
	},
	performFilterSearch: function(page) {
		var obj = {};
		var oFilters = YAHOO.util.Dom.getElementsByClassName("selectBox", "ul", "filter");
		var args = [];
		for (var i = 0, j = oFilters.length; i < j; i++) {
			var id = oFilters[i].id.replace("fauxSelect", "");
			var value = oFilters[i].getElementsByTagName("li")[0].getElementsByTagName("a")[0].id.replace("priceFilterId", "");
			value = value.replace("priceFilter", "");
			value = value.replace("sizeFilterId", "");
			value = value.replace("colorFilterId", "");
			value = value.replace("categoryFilterId", "");
			args.push(id + "|" + value);
		}
		args.push("p|" + page);
		obj["f"] = args.join("_");
		for (i = 0, j = ProductListing.categoryModules.length; i < j; i++) {
			obj["c" + ProductListing.categoryModules[i]] = "cl";
		}
		
		try {
			YAHOO.util.History.multiNavigate(obj);
		} catch (ex) {
			ProductListing.processFilter(args.join("_"));
		}
	},
	filterSearchClick: function(e) {
		YAHOO.util.Event.preventDefault(e);
		
		var img = new Image();
		img.src = sFolderLevel + "images/interface/filter-loader.gif";
		var loading = document.getElementById("filterLoading");
		loading.innerHTML = "";
		loading.appendChild(img);
		
		ProductListing.performFilterSearch(1);
	},
	filterPostCallback: function() {
		ProductListing.initMoreLinks();
		if (document.getElementById("searchResults")) {
			SearchListing.initLinks();
		}
		document.getElementById("filterLoading").innerHTML = "";
	}
};
YAHOO.util.Event.onContentReady("filter", ProductListing.initHistory);

(function() {
	function handleFilterClick(e) {
		YAHOO.util.Event.preventDefault(e);
		setFilterCriteria(this, 'ctl00_ctl00_masterPlaceHolder_catalogFilter');
	}
	
	function initPriceFilter() {
		var lists = YAHOO.util.Dom.getElementsByClassName("selectBox", "ul", "filter");
		for (var i = 0, j = lists.length; i < j; i++) {
			YAHOO.util.Event.on(lists[i].getElementsByTagName("a"), "click", handleFilterClick);
		}
		
		YAHOO.util.Event.on("resetButton", "click", handleFilterReset);
	}
	
	function handleFilterReset(e) {
		YAHOO.util.Event.preventDefault(e);
		resetFilter('ctl00_ctl00_masterPlaceHolder_catalogFilter');
	}
	YAHOO.util.Event.onContentReady("filter", initPriceFilter);
})();

function gotoEmailAFriend( )
{
	var skuSelectedIndex;
	var skuId;

	if ( document.forms[0]["ddlSizes"] != null )
	{
		skuSelectedIndex = document.forms[0]["ddlSizes"].selectedIndex;
		skuId = document.forms[0]["ddlSizes"].options[skuSelectedIndex].value;
	}
	else
	{
		skuId = document.forms[0]["defaultUpcId"].value;
	}

	window.location = sBaseUrl + "/catalog/email-a-friend.aspx?upcid=" + skuId;
}

/* Email Update Functions */

function signupForEmailUpdates( emailAddress, redirect )
{
	window.location = redirect + "?email=" + emailAddress;
}