
var wipeOut;
var facetSearchUpdate = "";
var strFacetSource;
var nRedirect = 0;
var currentAnimation;
var okAnimation = 0;
var status = 0;
var numSelected = 0;

var initModel = 0;
var arModel = new Array();


function setRedirect(nInt){	nRedirect = nInt;}
function getRedirect(){		return nRedirect;}
function setFacetTarget(strDivId){	facetSearchUpdate = dojo.byId(strDivId);}
function getFacetTarget(){	return facetSearchUpdate;}
function setFacetSource(strPage){	strFacetSource = strPage;}
function getFacetSource(){	return strFacetSource;}
function getCategoryHolder(){	return dojo.byId("facetOptions");}

function cleanDiv(tarDiv)
{
	// clean out divs
	for (i=tarDiv.childNodes.length-1;i>=0;i--)
	{
		tarDiv.removeChild(tarDiv.childNodes[i]);
	}
	tarDiv.innerHTML = "";
}

function buildFacets(objFacet, objTarget)
{
	var facetLabel;
	var facetData;
	var facetName;
	var facetUrl;
	var pageUrl;
	var facetCat;
	var facetCatName;
	var addFacet;
	var removeFacet;
	var facetOptionsDiv = dojo.byId(objTarget);
	var id;
	var isNew;
	var last=false;



		id = objFacet.id;
/*
		var divId = "facetoptions_"+id;
		var facetSingleOptionDiv = dojo.byId(divId);
		if (facetSingleOptionDiv) {
			cleanDiv(facetSingleOptionDiv);
			isNew = false;
		} else {
			facetSingleOptionDiv = document.createElement("div");
			facetSingleOptionDiv.id = divId;
			facetSingleOptionDiv.setAttribute("style", "background:none;");
			facetSingleOptionDiv.style.cssText = "background:none;padding-left:0px;";						
			isNew = true;

			//this is the hidden div that stores the catid of the facet selected in the cateogry menu so that we can make it appear later
			hiddenDiv = document.createElement("div");
			hiddenDiv.id = "swapcat_" + id;
			hiddenDiv.setAttribute("style", "display:none;");
			hiddenDiv.style.cssText = "display:none;";
			hiddenDiv.innerHTML = "none"; //default
		}
		facetSingleOptionDiv.setAttribute("style", "background:none;");
		facetSingleOptionDiv.style.cssText = "background:none;padding-left:0px;";
//		if (j==(objFacetList.length-1)) last=true;
		buildFacetOption(objFacetList[j], facetSingleOptionDiv,last);
		facetSingleOptionDiv.style.display="block";
		facetOptionsDiv.appendChild(facetSingleOptionDiv);

		if (isNew) {
			facetOptionsDiv.appendChild(facetSingleOptionDiv);
			facetOptionsDiv.appendChild(hiddenDiv);
			addFacetCheck();
		}
*/
	//checkEmptyFacet();
	

}

function addFacetCheck()
{
	numSelected++;
	checkEmptyFacet();
}

function deleteFacetCheck()
{
	numSelected--;
	checkEmptyFacet();
}

function checkEmptyFacet()
{
	var tarId = "facetEmptyMessage";
	var facetOptionsDiv = dojo.byId("facetOptionsSort");
	var txtString = "Select filters below.";

	if (numSelected <= 0)
	{
		// we are now empty;
		numSelected = 0;

		if (dojo.byId(tarId) && dojo.byId(tarId).style.display == "block")
		{
	
		} else {
			// adding message

			if (dojo.byId(tarId))
			{
				facetMessage = dojo.byId(tarId);			
			} else {
				facetMessage = document.createElement("div");
			}
			facetMessage.innerHTML = txtString;
			facetMessage.id = tarId;
			facetMessage.style.display = "block";
			facetOptionsDiv.appendChild(facetMessage);
		}
	} else {
		// we are now full, remove any old message
		if (dojo.byId(tarId))
		{
			objDelete = dojo.byId(tarId);
			objDelete.innerHTML = "";
			objDelete.style.display= "none";
		}
	}
}

function buildFacetOption(facetOpton, facetSingleOptionDiv,blast)
{
	var facetLabel;
	var facetData;
	var facetName;
	var facetUrl;
	var pageUrl;
	var facetCat;
	var facetCatName;
	var addFacet;
	var removeFacet;
	var linkingDIV; //used to link facets with category roots *important
	
	facetName = facetOpton.name;
	facetUrl = facetOpton.urlFacet;
	pageUrl = facetOpton.urlPage;
	facetCat = facetOpton.id;
	facetCatName = facetOpton.catName;

	trailSize = facetOpton.trailSize;
	addFacet = facetOpton.addFacet;
	removeFacet = facetOpton.removeFacet;
	facetValue = facetOpton.value;
	var facetUl = document.createElement("ul");
	facetSingleOptionDiv.appendChild(facetUl);
	var facetLi = document.createElement("li");
	facetLi.className = ""
	if (blast) {
		facetLi.setAttribute("style", "background:none;");
		facetLi.style.cssText = "background:none;";
	}
	facetUl.appendChild(facetLi);
	if (!getRedirect())
	{

		facetLi.innerHTML = "<a href=\"javascript:facetClickAction('" + facetCat + "', '" + facetUrl + "', '" + pageUrl + "', 0," + trailSize + ",0,'" + addFacet + "','" + removeFacet + "', '" + facetValue +"')\" class='remove'><img style='padding-right:12px;padding-left:4px;' border=0 src='/images/_ui/images/products-filters_remove.png'/></a>";                          //tempA.href="javascript:facetClickAction('" + strCat + "', '" + optUrl + "', '" + pageUrl + "', 1, " + trailSize + "," + qty + "," + addFacet + "," + removeFacet + ");";
	} else {

		facetLi.innerHTML = "<a href=\"" + pageUrl + "\"><img border=0 style='padding-right:12px;padding-left:4px;' src='_ui/images/products-filters_remove.png'/></a>";
	}
	facetLi.innerHTML = facetLi.innerHTML + "<span style='color:#5F99DB;font-size:1em;font-weight:bold;padding:0 0 0 0px;background:none;' id='facetcontent_" + facetOpton.catid + "'>" + facetName + "</span>";
	linkingDIV = document.createElement("div");
	linkingDIV.id = "linkingdiv_" + facetOpton.catid;
	linkingDIV.innerHTML = facetOpton.id;
	linkingDIV.style.display = "none";


	facetSingleOptionDiv.appendChild(facetUl);
	facetSingleOptionDiv.appendChild(linkingDIV);
}

function updateCategory(objCategory, categoryCount, category,last, activeFacet)
{
	var catId = objCategory.catid;
	var catName = objCategory.name;
	var catDOMId = "facet_"+catId;
	
	if (activeFacet == 0)
	{ 
		if (dojo.byId(catDOMId) )
		{
			cleanDiv(dojo.byId(catDOMId));	
			// already there
			handleCreateCategory(objCategory, categoryCount,last, true);
		} else {
			// not there yet
			handleCreateCategory(objCategory, categoryCount,last, false);
		}
		dojo.byId(catDOMId).style.display="block";
//		dojo.byId(catDOMId).style.background = "white";
		
	} else {
		cleanDiv(dojo.byId(catDOMId));
		//alert("We have an active facet: " + objCategory.name + " / " + activeFacet.name);
		
		
		buildActive(objCategory, categoryCount, last, true, activeFacet);
		
		
		//removeOthers(activeFacet, objCategory, catDOMId);
		
		dojo.byId(catDOMId).style.display="block";
//		dojo.byId(catDOMId).style.background = "white";
		
		
	}
	
	
}

function removeOthers(curFacet, curCategory, domId)
{
	
	var categoryContent = dojo.byId("facetoptions_" + curCategory.catid);
	
	categoryContent.style.display = "none";
	
	var tempObj = dojo.doc.createElement("div");
	
	tempObj.innerHTML = curFacet.name;	
	
}

function buildActive(objCategory, categoryCount, last, update, curActive)
{
	var catId = objCategory.catid;
	var catName = objCategory.name;
	var catDOMId = "facet_"+catId;
	var curCategoryHolder = getCategoryHolder();
	var cleardiv = document.createElement("div");
	cleardiv = document.createElement("div");
	cleardiv.className = "clear";
	var container = null;
	
	if (update) {
		container = dojo.byId(catDOMId);
	} else {
		container = document.createElement("div");
	}

	var addstyle = "";
	container.id=catDOMId;
	container.className="atg_b2cblueprint_facetsGroup";
	var containerHeadUL = dojo.doc.createElement("ul");

	if (last) addstyle="border-bottom:none;";
	containerHeadUL.setAttribute("style", "display:block;"+addstyle);
	containerHeadUL.style.cssText = "display:block;"+addstyle;
	var containerHead = dojo.doc.createElement("li");

	containerHead.id="li"+catDOMId;
	containerHead.className="selected";	
	containerHead.setAttribute("style", "background:none;padding-left:0px;margin-left:8px;padding-right:5px;width:162px;"+addstyle);
	containerHead.style.cssText = "background:none;padding-left:0px;margin-left:8px;padding-right:5px;width:162px;"+addstyle;

	var tempA = document.createElement("A");
	tempA.href="javascript:facetClickActionCategory('" + catId + "', '', '', 1,undefined,2,undefined,undefined);nodeflip('img" + catId + "');";
	tempA.id="ahref"+catDOMId;
	var nodeicon = "<img id='img" + catId + "' src='/images/_ui/images/btn_minus_sign.gif' border=0 style='padding-right:8px;padding-left:0px;'/>";
	tempA.innerHTML = nodeicon;
	tempA.className = "lnavCat";
	/*add for text click*/
	var tempAText = document.createElement("A");
	tempAText.href="javascript:facetClickActionCategoryForText('" + catId + "', '', '', 1,undefined,2,undefined,undefined);";
	tempAText.id="ahrefText"+catDOMId;
	tempAText.className = "lnavCat";
	tempAText.innerHTML = catName;
	/*add for text click*/

	var temphead = document.createElement("h5");
	temphead.appendChild(tempA);
	/*add for text click*/
	temphead.appendChild(tempAText);
	/*add for text click*/
	containerHead.appendChild(temphead);

	containerHeadUL.appendChild(containerHead);
	container.appendChild(containerHeadUL);
	
	container.appendChild(buildCategoryRemoveOption(curActive, catId, categoryCount));
	
	
	
	

	curCategoryHolder.appendChild(container);
	curCategoryHolder.appendChild(cleardiv);
	
	currentAnimation = dojo.fx.wipeIn({node: "facet_" + catId, duration: 500});
	currentAnimation.play();
}

function handleUpdateCategory(objCategory, category)
{
	var catId = objCategory.catid;
	var catName = objCategory.name;
	var catDOMId = "facet_"+catId;
	var optDOMId = "facetoptions_" +catId;
	//only have to update options for category
	var curCategory = dojo.byId(catDOMId);
	// clean category
	cleanDiv(curCategory);		
	var containerHead = dojo.doc.createElement("li");
	containerHead.setAttribute("class", "selected");
	containerHead.id="lifacet_" + category;
	var tempA = document.createElement("A");
	tempA.href="javascript:facetClickActionCategory('" + catId + "', '', '', 1,undefined,2,undefined,undefined);";
	tempA.onclick = "javascript:alert(4);";
	tempA.innerHTML = catName;
	var temphead = document.createElement("h5");
	temphead.appendChild(tempA);
	containerHead.appendChild(temphead);
	curCategory.appendChild(containerHead);
	curCategory.appendChild(buildCategoryOptions(objCategory.options, catId));
	if (objCategory.options.length > 0 && ("facet_"+category) == catDOMId) {
		curCategory.style.display="block";  //modify in 11.22 2008 browser incompatablity
	}
}

function onClickEventCat(liid,ahref)
{

	//document.getElementById(ahref).style.color="#5F99DB"; //change this to a class in css
}

function handleCreateCategory(objCategory, categoryCount,last, update)
{
	var catId = objCategory.catid;
	var catName = objCategory.name;
	var catDOMId = "facet_"+catId;
	var curCategoryHolder = getCategoryHolder();
	var cleardiv = document.createElement("div");
	cleardiv = document.createElement("div");
	cleardiv.className = "clear";
	var container = null;
	if (update) {
		container = dojo.byId(catDOMId);
	} else {
		container = document.createElement("div");
	}
	var addstyle = "";
	container.id=catDOMId;
	container.className="atg_b2cblueprint_facetsGroup";
	var containerHeadUL = dojo.doc.createElement("ul");

	if (last) addstyle="border-bottom:none;";
	containerHeadUL.setAttribute("style", "display:block;"+addstyle);
	containerHeadUL.style.cssText = "display:block;"+addstyle;
	var containerHead = dojo.doc.createElement("li");

	containerHead.id="li"+catDOMId;
	containerHead.className="selected";	
	containerHead.setAttribute("style", "background:none;padding-left:0px;margin-left:8px;padding-right:5px;width:162px;"+addstyle);
	containerHead.style.cssText = "background:none;padding-left:0px;margin-left:8px;padding-right:5px;width:162px;"+addstyle;

	var tempA = document.createElement("A");
	tempA.href="javascript:facetClickActionCategory('" + catId + "', '', '', 1,undefined,2,undefined,undefined);nodeflip('img" + catId + "');";
	tempA.id="ahref"+catDOMId;
	var nodeicon = "<img id='img" + catId + "' src='/images/_ui/images/btn_minus_sign.gif' border=0 style='padding-right:8px;padding-left:0px;'/>";
	tempA.innerHTML = nodeicon;
	tempA.className = "lnavCat";
	/*add for text click*/
	var tempAText = document.createElement("A");
	tempAText.href="javascript:facetClickActionCategoryForText('" + catId + "', '', '', 1,undefined,2,undefined,undefined);nodeflip('img" + catId + "');";
	tempAText.id="ahrefText"+catDOMId;
	tempAText.className = "lnavCat";
	tempAText.innerHTML = catName;
	/*add for text click*/

	var temphead = document.createElement("h5");
	temphead.appendChild(tempA);
	/*add for text click*/
	temphead.appendChild(tempAText);
	/*add for text click*/
	containerHead.appendChild(temphead);

	containerHeadUL.appendChild(containerHead);
	container.appendChild(containerHeadUL);
	
	container.appendChild(buildCategoryOptions(objCategory.options, catId, categoryCount));
	curCategoryHolder.appendChild(container);
	curCategoryHolder.appendChild(cleardiv);
	
	currentAnimation = dojo.fx.wipeIn({node: "facet_" + catId, duration: 500});
	currentAnimation.play();
}

function buildCategoryRemoveOption(objActive, strCat, categoryCount)
{
	var tarContainer = document.createElement("div");
	var facetContainerParentUL;
	var facetContainerParentLI;
	var facetContainer;
	var singleFacet;
	
	var catId = objActive.catid;
	var facetName = objActive.name;
	var facetUrl = objActive.urlFacet;
	var pageUrl = objActive.urlPage;
	var facetCat = objActive.id;
	var trailSize = parseInt(objActive.trailSize);
	var facetCatName = objActive.catName;
	var addFacet = objActive.addFacet;
	var removeFacet = objActive.removeFacet;
	var facetValue = objActive.value;



	tarContainer.id = "facetoptions_" + strCat;
	tarContainer.setAttribute("style", "background:none;");
	tarContainer.style.cssText = "background:none;";


	facetContainerParentUL = document.createElement("ul");
	facetContainerParentLI = document.createElement("li");


	facetContainerParentLI.setAttribute("style", "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;");
	facetContainerParentLI.style.cssText = "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;";
	facetContainer = document.createElement("ul");
	facetContainer.setAttribute("style", "padding-top:0px;margin-top:0px;");
	facetContainer.style.cssText = "padding-top:0px;margin-top:0px;";
	facetContainerParentUL.appendChild(facetContainerParentLI);
	facetContainerParentLI.appendChild(facetContainer);



	var objLink = dojo.doc.createElement("li");
	
	objLink.setAttribute("style", "background:none;padding: 0px 0 0 0;");
	objLink.style.cssText="background:none;padding:0 0 0px;";
	
	
	//objLink.style.height="12px";
	
	
	objLink.onmouseover=function() {
		objLink.style.cursor="pointer";
	}	
	
	
	
	if (!getRedirect())
	{
		objLink.innerHTML = "<a style='padding: 2px 5px 0 0; margin: 0px; width: 14px; height: 14px;float: left;' href=\"javascript:facetClickAction('" + facetCat + "', '" + facetUrl + "', '" + pageUrl + "', 0," + trailSize + ",0,'" + addFacet + "','" + removeFacet + "', '" + facetValue +"')\"><img style='' border=0 src='/images/_ui/images/products-filters_remove.png'/></a>";
		objLink.innerHTML = objLink.innerHTML + "<a href=\"javascript:facetClickAction('" + facetCat + "', '" + facetUrl + "', '" + pageUrl + "', 0," + trailSize + ",0,'" + addFacet + "','" + removeFacet + "', '" + facetValue +"')\">" + facetName + "</a>";
	} else {
		objLink.innerHTML = "<a style='padding: 2px 5px 0 0; margin: 0px; width: 14px; height: 14px;float: left;' href=\"" + pageUrl + "\"><img border=0 style='' src='_ui/images/products-filters_remove.png'/></a>";
		objLink.innerHTML = objLink.innerHTML +  "<a href=\"" + pageUrl + "\">" + facetName + "</a>";
	}


	facetContainer.appendChild(objLink);


	tarContainer.appendChild(facetContainerParentUL);
	return tarContainer;
}

function buildCategoryOptions(objOptions, strCat, categoryCount)
{
	var tarContainer = document.createElement("div");
	var facetContainerParentUL;
	var facetContainerParentLI;
	var facetContainer;
	var singleFacet;
	
	//if it matches i need to populate swapcat with its id, and set it to invisible
	try {
		var linkingdiv=dojo.byId("linkingdiv_" + strCat);
		if(linkingdiv){
			parentFacetID = linkingdiv.innerHTML;
		}
	} catch (e) {

	}


	tarContainer.id = "facetoptions_" + strCat;
	tarContainer.setAttribute("style", "background:none;");
	tarContainer.style.cssText = "background:none;";


	facetContainerParentUL = document.createElement("ul");
	facetContainerParentLI = document.createElement("li");


	facetContainerParentLI.setAttribute("style", "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;");
	facetContainerParentLI.style.cssText = "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;";
	facetContainer = document.createElement("ul");
	facetContainer.setAttribute("style", "padding-top:0px;margin-top:0px;");
	facetContainer.style.cssText = "padding-top:0px;margin-top:0px;";
	facetContainerParentUL.appendChild(facetContainerParentLI);
	facetContainerParentLI.appendChild(facetContainer);

	var recShown = 5;
	var recCount = 0;

	if (objOptions.length <= recShown) {
		recShown = objOptions.length;
	}
	var last = false;
	for (var i=0; i<recShown; i++)
	{
		if (i==(recShown-1)) last=true;
		if(objOptions.length > recShown) last=false;
		if (objOptions[i].type == "subcategory")
		{
			var containerHead = dojo.doc.createElement("li");

			containerHead.className = "subcat";
			var tempA = document.createElement("A");
			tempA.href="javascript:facetClickActionCategory('" + objOptions[i].catid + "', '', '', 1,undefined,2,undefined,undefined);";
			tempA.onclick = "javascript:alert(4);";
			tempA.innerHTML = objOptions[i].name;
			var temphead = document.createElement("h5");
			temphead.appendChild(tempA);
			containerHead.appendChild(temphead);
			facetContainer.appendChild(containerHead);
			facetContainer.appendChild(createOptionCategory(objOptions[i].options,facetContainer, tarContainer.id, objOptions[i].name,objOptions[i].qty, strCat, 0, false, categoryCount, objOptions[i].trailSize,objOptions[i].addFacet,undefined,last));
		}
		else
		{
			var hideoption = false;
			try {
	
				if(dojo.byId("facetcontent_" + strCat)!=null && (objOptions[i].name == dojo.byId("facetcontent_" + strCat).innerHTML))
				{
					dojo.byId("swapcat_" + parentFacetID).innerHTML = objOptions[i].id;
					hideoption=true;
				}
			} catch (e) {
			}
			createOption(facetContainer, tarContainer.id, objOptions[i].name, objOptions[i].urlFacet, objOptions[i].urlPage, objOptions[i].qty, strCat, 0, false, categoryCount, objOptions[i].trailSize,objOptions[i].addFacet,undefined,last,objOptions[i].id,hideoption);
		}
		recCount = recCount + 1;
	}

	if (objOptions.length > recCount) {
		/*appand more link to the end of the options ul*/
		var moreLi = createOption(facetContainer, tarContainer.id, "More>>", "", "", "", strCat, 1, false, categoryCount, "", "", "",true);
		var moreDiv = document.createElement("div");
		moreDiv.style.display="none"; //modify in 11.22 2008 browser incompatablity
		moreDiv.style.cssText = "display:none;background:none;padding:0 0 0 0px;";
		moreDiv.id = "lessDiv" + tarContainer.id;
		last=false;
		for (var j = recShown; j < objOptions.length; j++) {
			createOption(moreDiv, tarContainer.id, objOptions[j].name, objOptions[j].urlFacet, objOptions[j].urlPage, objOptions[j].qty, strCat, 0, true, categoryCount, objOptions[j].trailSize,objOptions[j].addFacet,undefined,last,objOptions[j].id,hideoption);
		}
		var lessLi = createOption(facetContainer, tarContainer.id, "Less>>", "", "", "", strCat, 2, false, categoryCount, "", "", "",true);
		moreDiv.appendChild(lessLi);
		facetContainer.appendChild(moreDiv);
	}
	tarContainer.appendChild(facetContainerParentUL);
	return tarContainer;
}


function createOptionCategory(objOptions,optionContainer, idx, name, qty, strCat, optionType, debug, categoryCount, trailSize, addFacet, removeFacet,blast)
{
	var tarContainer = document.createElement("div");
	var facetContainerParentUL;
	var facetContainerParentLI;
	var facetContainer;
	var singleFacet;

	tarContainer.id = "facetoptions_" + strCat;

	facetContainerParentUL = document.createElement("ul");
	facetContainerParentLI = document.createElement("li");

	facetContainerParentLI.setAttribute("style", "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;");
	facetContainerParentLI.style.cssText = "font-weight:normal;border-bottom:none;padding:0 0 0 0px;margin:0 0 0 0px;background-image:none;";

	facetContainer = document.createElement("ul");
	facetContainer.setAttribute("style", "padding-top:0px;margin-top:0px;");
	facetContainer.style.cssText = "padding-top:0px;margin-top:0px;";


	facetContainerParentUL.appendChild(facetContainerParentLI);
	facetContainerParentLI.appendChild(facetContainer);

	var recShown = 5;
	var recCount = 0;

	if (objOptions.length <= recShown) {
		recShown = objOptions.length;
	}
	
	var last = false;

	for (var i=0; i<recShown; i++)
	{

		if (i==(recShown-1)) last=true;
		//passing optionid as undefined for now
		createOption(facetContainer, tarContainer.id, objOptions[i].name, objOptions[i].urlFacet, objOptions[i].urlPage, objOptions[i].qty, strCat, 0, false, categoryCount, objOptions[i].trailSize,objOptions[i].addFacet,undefined,last,undefined,false);

		recCount = recCount + 1;
	}

	tarContainer.appendChild(facetContainerParentUL);

	return tarContainer;
}

function createOption(optionContainer, idx, name, urlFacet, urlPage, qty, strCat, optionType, debug, categoryCount, trailSize, addFacet, removeFacet,blast,optionid,hideoption) {
	var optName = name;
	var optUrl = urlFacet;
	var pageUrl = urlPage;
	var optQty = qty;
	var addstyle = "";
	var optDisplay = "";
	var singleFacet = document.createElement("li");
	var containerDIV = document.createElement("div");
	containerDIV.id = "menu_" + optionid;
	containerDIV.name= "menu_" + optionid;
	containerDIV.className = "menuitem";
	
	if (hideoption) addstyle = "display:none;";
	containerDIV.setAttribute("style", addstyle);
	containerDIV.style.cssText = addstyle;
	
	singleFacet.setAttribute("style", "background:none;padding:0 0 0px;");
	singleFacet.style.cssText = "background:none;padding:0 0 0px;";


	if (blast) {
		containerDIV.setAttribute("style", "background:none;");
		containerDIV.style.cssText = "background:none;";
	}
//	containerDIV.style.height="12px";

	if (optionType == 2) {
		singleFacet.id = "lessDiv" + idx;
		singleFacet.style.display="block";  //modify in 11.22 2008 browser incompatablity
	}
	if (optionType == 1) {
		singleFacet.id = "moreDiv" + idx;
		singleFacet.style.display="block";  //modify in 11.22 2008 browser incompatablity
	}
	containerDIV.onmouseover=function(){
		containerDIV.style.cursor="pointer";
	}
	if (optionType == 1 || optionType == 2) {
		containerDIV.onclick=function(){
			atg.b2cblueprint.util.toggleBothDiv(idx,categoryCount);
		}
		optDisplay = "<a href=\"javascript:atg.b2cblueprint.util.toggleBothDivFacet('"+idx+"',"+categoryCount+");\">"+optName+"</a>";  //atg.b2cblueprint.util.toggleBothDiv("+idx+","+categoryCount+")
	} else if (optionType == 0) {
		if (myBrowser == "ie6")
			optDisplay = '<table cellpadding=0 cellspacing=0 width="149" style="padding:0 0 0 0px";margin:0 0 0 0px;"><tr><td align="left" style="padding:0 0 0 0px;">' + optName + '</td><td align="center" class="qty">(' + optQty + ')</td></tr></table>';
		else
			optDisplay = '<table cellpadding=0 cellspacing=0 width="155" style="padding:0 0 0 0px";margin:0 0 0 0px;"><tr><td align="left" style="padding:0 0 0 0px;">' + optName + '</td><td align="center" class="qty">(' + optQty + ')</td></tr></table>';
		if (!getRedirect())
		{
			containerDIV.onclick=function(){
			facetClickAction(strCat, optUrl, pageUrl, 1, trailSize ,qty ,addFacet ,removeFacet ,undefined,optionid);
			}
		} else {
			containerDIV.onclick=function(){
				facetClickAction(strCat, optUrl, pageUrl, 1, trailSize ,qty ,addFacet ,removeFacet ,undefined,optionid);
				window.location.href=pageUrl;
			}
		}
	}	
	containerDIV.innerHTML = optDisplay;
	//hidden div used to interchange facet text
	var hiddenDIV = dojo.doc.createElement("div");
	hiddenDIV.id = "optioncontent_" + optionid;
	hiddenDIV.style.display = "none";
	hiddenDIV.innerHTML = optName;
	containerDIV.appendChild(hiddenDIV);
	singleFacet.appendChild(containerDIV);
	optionContainer.appendChild(singleFacet);
	return singleFacet;
}

function facetClickActionCategory(category,urlFacet,urlPage,action, trailSize, qty, addFacet, removeFacet, facetValue)
{
	content.liveResult = "true";

	// action: 1 = add, 0 = remove
	var animation;
	var liid = 'lifacet_' + category;
	
	//content.trailSize = trailSize;
	content.numResults = qty;
	
   /* var elemArr =document.getElementsByName("menu_"+category);
		for(i=0; i < elemArr.length; i++ ){
		  alert(elemArr[i]);
		}*/

	if (document.getElementById(liid).className=='')
	{
		//content.addFacet = ""; // clear gloable content's addFacet to empty.
		currentAnimation = dojo.fx.wipeIn({node: "facet_" + category, duration: 500});
		okAnimation = 1;
		
		dojo.byId("facetoptions_"+category).style.display='block';
		currentAnimation.play();
		document.getElementById(liid).className="selected";
	}
	else
	{
		switch(action)
		{
			case 0:
				//content.addFacet = ""; // clear gloable content's addFacet to empty.
				currentAnimation = dojo.fx.wipeIn({node: "facet_" + category, duration: 500, properties: { height: 35}});
				dojo.byId("facetoptions_"+category).style.display='block';
				okAnimation = 1;
				break;
			case 1:
			//	content.addFacet = "";
				currentAnimation = dojo.animateProperty({node: "facet_" + category, duration: 500, properties: {  height: 35}});	
				//var facetIds = document.getElementById("facet_" + category);					
				//facetIds.style.overflow="hidden";									
				okAnimation = 0;			
				dojo.byId("facetoptions_"+category).style.display='none';		
				currentAnimation.play();
				break;
		}
		document.getElementById(liid).className="";
		
		
	}	
	//doFacetLoad(urlFacet, urlPage, trailSize, qty, category);
}	

function facetClickActionCategoryForText(category,urlFacet,urlPage,action, trailSize, qty, addFacet, removeFacet, facetValue)
{
	content.liveResult = "true";

	// action: 1 = add, 0 = remove
	var animation;
	var liid = 'lifacet_' + category;
	//content.trailSize = trailSize;
	content.numResults = qty;
	
	if (document.getElementById(liid).className=='')
	{ 	
	//	nodeflip('img' + category);
		//content.addFacet = ""; // clear gloable content's addFacet to empty.
		currentAnimation = dojo.fx.wipeIn({node: "facet_" + category, duration: 500});
		okAnimation = 1;
		dojo.byId("facetoptions_"+category).style.display='block';
		currentAnimation.play();
		document.getElementById(liid).className="selected";

	}
	else
	{
		//return;
		switch(action)
		{
			case 0:
				//content.addFacet = ""; // clear gloable content's addFacet to empty.
				currentAnimation = dojo.fx.wipeIn({node: "facet_" + category, duration: 500, properties: { height: 35}});
				dojo.byId("facetoptions_"+category).style.display='block';
				okAnimation = 1;
				break;
			case 1:
			//	content.addFacet = "";
				currentAnimation = dojo.animateProperty({node: "facet_" + category, duration: 500, properties: { height: 35}});
			//	var facetIds = document.getElementById("facet_" + category);					
			//	facetIds.style.overflow="hidden";		
			
				okAnimation = 0;				
				dojo.byId("facetoptions_"+category).style.display='none';
				currentAnimation.play();
	
				break;
		}
		document.getElementById(liid).className="";
	}
	//doFacetLoad(urlFacet, urlPage, trailSize, qty, category);
}	

function facetClickAction(category,urlFacet,urlPage,action, trailSize, qty, addFacet, removeFacet, facetValue,optionid)
{
	content.liveResult = "true";

	if (action == 0)
	{
		for (var i=0;i<arModel.length;i++)
		{
			if (arModel[i].isSelected && arModel[i].facet && arModel[i].facet.id == category)
			{
				arModel[i].isSelected = 0;
			}
		}
	}
	
	// action: 1 = add, 0 = remove
    //alert("  category="+category+"  urlFacet="+urlFacet+"  urlPage="+urlPage+addFacet+"  action="+action+"  trailSize="+trailSize+"   qty="+qty);
	var animation;
	var parentFacetID;
	if(content.searchmenu == 'allCategories' && content.trailtext != ''){
		if(action == 0){
			content.addFacet = content.addFacet.replaceAll(":"+removeFacet,"");
		}else{
			if(content.addFacet == content.trail){
				content.addFacet=content.trail+":"+addFacet;
			}else{
				content.addFacet=content.addFacet+":"+addFacet;
			}
			//content.removeAllFacets=false;
		}
	}else{
		content.addFacet=addFacet;
	}
	content.numResults = qty;
	content.trailSize = trailSize;
	switch(action)
	{
		case 0:
/*
	// Only needed if using "selected filters"
			try {
				var menuid="menu_" + category;
				dojo.byId("facetoptions_" + category).style.display="none";
				currentAnimation = dojo.fx.wipeIn({node: menuid, duration: 500});
				okAnimation = 0;
				currentAnimation.play();
			} catch (e) {
			}
*/			
			break;
		case 1:
			try {
					parentFacetID = dojo.byId("linkingdiv_" + category).innerHTML;
			} catch (e) {
			}
			try {
				if (dojo.byId("swapcat_" + parentFacetID))
				dojo.byId("menu_" + dojo.byId("swapcat_" + parentFacetID).innerHTML).style.display="block";
			} catch (e) {
			}
			//add it to the filters menu
			try {
				if(dojo.byId("facetoptions_" + parentFacetID)){
					dojo.byId("facetoptions_" + parentFacetID).style.display="block";
				}
				if(dojo.byId("facetcontent_" + category) && dojo.byId("optioncontent_" + optionid)){
					dojo.byId("facetcontent_" + category).innerHTML = dojo.byId("optioncontent_" + optionid).innerHTML;
				}
				if(dojo.byId("swapcat_" + parentFacetID)){
					dojo.byId("swapcat_" + parentFacetID).innerHTML = optionid;
				}
			} catch (e) {
			}
			currentAnimation = dojo.fx.wipeOut({node: "menu_" + optionid, duration: 500, properties: { height: -35}});
			okAnimation = 1;
			//currentAnimation.play();
			break;
	}
	doFacetLoad(urlFacet, urlPage, trailSize, qty, category);
}

function resetModel()
{
	// reset all inventory levels
	
	for (var i=0;i<arModel.length;i++)
	{
		var options = arModel[i].options;
		
		for (var j=0;j<options.length;j++)
		{
			arModel[i].options[j].qty = 0;
		}
	}
}

function resetFacets()
{
	// reset all inventory levels
	
	for (var i=0;i<arModel.length;i++)
	{
		var options = arModel[i].options;
		
		for (var j=0;j<options.length;j++)
		{
			arModel[i].isSelected = 0;
			arModel[i].facet = null;
		}
	}

}

function syncModel(modelIndex, node)
{
	arModel[modelIndex].catNode = node;
	
	for (var i=0;i<arModel[modelIndex].options.length;i++)
	{
		for (var j=0;j<node.options.length;j++)
		{
			if (arModel[modelIndex].options[i].addFacet == node.options[j].addFacet)
			{
				//alert("HIT");
				arModel[modelIndex].options[i].qty = node.options[j].qty;
			}
		}
	}

}

function removeSelected()
{

}

function isSelected(needle, objFacets)
{

	for (var i=0;i<objFacets.length;i++)
	{
		if (needle.catid == objFacets[i].id)
		{
			return objFacets[i];
		}
	}
	
	

	for (i=0;i<arModel.length;i++)
	{
		if (arModel[i].catid == needle.catid && arModel[i].isSelected == 1)
		{
			return arModel[i].facet;
		}
	}

	return 0;
}

function isDisplay(objNode)
{
	if (objNode.isSelected)
		return 1;
	
	for (var i=0;i<objNode.options.length;i++)
	{
		if (objNode.options[i].qty > 0)
			return 2;
	}
	
	return 0;
	
}

function buildCategories(objCategoryList, category, objFacets)
{
	var i;
	objOut = dojo.byId("facetOptions");
	// iterate through all facets
	var last = false;

	//resetFacets();
	

	if (initModel)
	{
		resetModel();
		
		// update values in the model
		for (i=0;i<objCategoryList.length;i++)
		{
			for (j=0;j<arModel.length;j++)
			{
				if (arModel[j].catid == objCategoryList[i].catid)
				{
					syncModel(j, objCategoryList[i]);
				}
			}
		}		
		
		for (j=0;j<arModel.length;j++)
		{
			if (j == (arModel.length-1)) last = true;
			
			if (isSelected(arModel[j], objFacets) != 0 && objFacets.length > 0 && arModel[j].catid == objFacets[0].id)
			{
				arModel[j].isSelected = 1;
				arModel[j].facet = objFacets[0];
			}
			
			switch(isDisplay(arModel[j]))
			{
				case 1:
					// this is a selected category
					updateCategory(arModel[j], arModel.length, category, last, isSelected(arModel[j], objFacets));
					break;
				case 2:
					// this is a non selected, but active category
					updateCategory(arModel[j].catNode, arModel.length, category, last, isSelected(arModel[j], objFacets));	
					break;
				case 0:
					// other categories
					//updateCategory(arModel[j], arModel.length, category, last, isSelected(arModel[j], objFacets));
					break;
			}
		}
		
	} else {
		// initial page load
		for (i=0;i<objCategoryList.length;i++)
		{
			arModel[i] = objCategoryList[i];
			arModel[i].isSelected = 0;

			if (i == (objCategoryList.length-1)) last=true;

			updateCategory(objCategoryList[i], objCategoryList.length, category,last, 0);
		}
		initModel = 1;
	}
}

function handleFacetLoad(objStructure, category)
{
	//alert('back ' + 'facetTrail =| ' + objStructure.facetTrail + ' | trailSize =| ' + objStructure.trailSize + ' | trail =| ' + objStructure.trail);
	//alert("after1====="+content.facetTrail);
	//content.facetTrail = objStructure.facetTrail;

	content.trail = objStructure.trail;
	content.trailSize = objStructure.trailSize;
	var facetOptionsDiv = dojo.byId("facetOptions");
	for (i=0;facetOptionsDiv && facetOptionsDiv.innerHTML!="" && i<facetOptionsDiv.childNodes.length;i++){
		var obj=facetOptionsDiv.childNodes[i];
		if(obj && obj.style){
			obj.style.display="none";
		}
	}
	// load the facets

	var objFacets = objStructure.facets;
	//buildFacets(objFacets);
	// load categories
	var objCategories = objStructure.categories;

	buildCategories(objCategories, category, objFacets);


	if(content.trailSize && content.trailSize>0){
		atg.blueprint.facet.loadData(content);
	}
	if (okAnimation == 1)
	{
		currentAnimation.play();
		okAnimation = 0;
	}

}

function doFacetLoad(strFacet, strPage, trailSize, qty, category)
{
	// alert('   before ' + strFacet+'facetTrail =| ' + content.facetTrail + ' | trailSize =| ' + content.trailSize + ' | trail =| ' + content.trail);		
	if (strFacet != "")
	{
		var bindArgs = {
			url: strFacet,
			handleAs: "json",
			content: content,
			error: function (response, ioArgs)
			{
				//alert("Error: " + response);
			},
			load: function(response, ioArgs)
			{
				handleFacetLoad(response, category);
			}
		};

		dojo.xhrGet(bindArgs);
	}
}

function nodeflip(imgid)
{
	var objimg = dojo.byId(imgid);

	if (objimg.src.indexOf("btn_minus_sign.gif") > -1)
		objimg.src = "/images/_ui/images/btn_plus_sign.gif";
	else
		objimg.src = "/images/_ui/images/btn_minus_sign.gif";
}

String.prototype.replaceAll = strReplace;

function strReplace(findText, replaceText) {
	var str = new String(this);
	while (str.indexOf(findText)!=-1)
	{
		str = str.replace(findText, replaceText);
	}
	return str;
}