//Default Site Studio Generated .js Script File

// Returns the specified level facet from a facet list
// i.e. "Performance | Drum | Conga" returns "Performance"
function getLevelFacet(facet, level)
{
	var f = facet;
	
	// If they are wanting a level higher than is in the fragment, don't give them anything
	if (level >= getFacetLength(facet))
	{
		return '';
	}
	
	for (j=0; j<level; j++)
	{
		var x = f.indexOf('|');
		f = f.substring(x+2, f.length)		
	}	
	
	if (f.indexOf('|') > 0)
	{
		return trimAll(f.substring(0, f.indexOf('|')-1));
	}
	else
	{
		return trimAll(f);
	}
}

// Returns the full facet level
// I.E. Performance | Instrument | Woodwind, 2 
// returns: Performance | Instrument
function getLinkedLevelFacet(facet, level)
{
	var link = facet;
	var fLen = getFacetLength(facet);
	
	// If they are wanting a level higher than is in the fragment, don't give them anything
	if (level >= fLen)
	{
		return '';
	}
	
	for (j=0; j<fLen-level-1; j++)
	{
		link = link.substring(0, link.lastIndexOf('/')-1);				
	}	
	return trimAll(link);
}

// Determines whether or not a facet contains an item or not
function facetContains(facet, name)
{
	if (facet.indexOf('|') > -1)
	{
		return facet.indexOf(name) != -1;
	}
	else
	{
		return facet == name;
	}
}

// Returns the level of a given item in a facet
function getFacetLevel(facet, name)
{
	return name.split('|').length-1;
}

// Returns the length of a facet
function getFacetLength(facet)
{
	if (facet != null)
	{
		var facets = facet.split('|');
		return facets.length;
	}
	else
	{
		return 0;
	}
}

// Returns the maximum facets length of an arr of facets
function getMaxFacetLength(facets)
{
	var i = 0;
	var maxLength = 0;
	for (i=0; i<facets.length; i++)
	{
		if (getFacetLength(facets[i]) > maxLength)
		{
			maxLength = getFacetLength(facets[i]);
		}
	}
	return maxLength;
}

// Prints out an array of facets
function displayRootFacets(facets, hiddenName, usFacets)
{
	document.write("<ul class=\"content_item\">");
	var allOthers = '';
	
	for (p=0; p<facets.length; p++)
	{
		if (usFacets[p] != 'All Others')
		{
			if (document.getElementById('facet_crumb').value != '')
			{
				document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '," + hiddenName + "=" + facets[p] + "';document.faceted_nav." + hiddenName + ".value='" + usFacets[p] + "';faceted_nav.submit();\">" + facets[p] + "</a></li>");	
			}
			else
			{
				document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '" + hiddenName + "=" + facets[p] + "';document.faceted_nav." + hiddenName + ".value='" + usFacets[p] + "';faceted_nav.submit();\">" + facets[p] + "</a></li>");				
			}
		}
		else
		{
			allOthers = facets[p];
		}
	}
	
	if (allOthers != '')
	{
		if (document.getElementById('facet_crumb').value != '')
		{
			document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '," + hiddenName + "=" + allOthers + "';document.faceted_nav." + hiddenName + ".value='" + 'All Others' + "';faceted_nav.submit();\">" + allOthers + "</a></li>");	
		}
		else
		{
			document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '" + hiddenName + "=" + allOthers + "';document.faceted_nav." + hiddenName + ".value='" + 'All Others' + "';faceted_nav.submit();\">" + allOthers + "</a></li>");				
		}
	}
	
	document.write("</ul>");
}

// Populates an array with facets passed in a comma-delimited list.  It also
// only adds the items to the array if they don't already exist so we don't get dups.
function getRootFacets(facets, arr)
{			
	for (i=0; i<facets.length; i++)
	{
		var rootFacet = getLevelFacet(facets[i],0);
		if (rootFacet != '' && !arrContains(arr, rootFacet))
		{
			// Add the new item to the array
			var newIndex = arr.length;
			arr[newIndex] = rootFacet;
		}			
	}
}

// Helper function that checks to see if an array contains a value.
function arrContains(arr, field)
{
	for (k=0; k<arr.length; k++)
	{
		if (arr[k] == field)
		{
			return true;
		}
	}
}

// Removes any leading spaces from a string
function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

// Removes any trailing spaces from a string
function rightTrim(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

// Removes both leading and trailing spaces from a string
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

// Splits a comma-delimited list of facets into an array
function buildFacetList(facet, arr, translations, siteId, usArr)
{
	var facets = facet.split(',');
	var i = 0;
	var j = 0;
	
	var trans = translations.replace('<Translation>','');
	trans = trans.replace('<translations>','');
	trans = trans.replace('</translations>','');
	var arrTrans = trans.split('</Translation>');

	// Figure out which locale we need
	var arrTran = arrTrans[0].split(',');
	for (i=0; i<arrTran.length; i++)
	{
		if (trimAll(arrTran[i].replace('\n', '')) == siteId)
		{
			break;
		}
	}	
	
	// This is the index (column) of the translated value
	var siteIndex = i;
	
	for (j=0; j<facets.length; j++)
	{
		for (i=1; i<arrTrans.length-1; i++)
		{
			var arrTran = arrTrans[i].split(',');
			
			var compareValue = arrTran[0].replace('<Translation>', '');
			compareValue = compareValue.replace('\n', '').replace('\r','');
			compareValue = trimAll(compareValue);
			//document.write(compareValue + ' == ' + facets[j] + '<br/>')
			if (compareValue == trimAll(facets[j]))
			{		
				//if (facets[i] != '')
				//{							
					// Add the new item to the array
					var newIndex = arr.length;
					arr[newIndex] = arrTran[siteIndex].replace('<Translation>','').replace('\n', '');
					usArr[newIndex] = facets[j];
				//}
			}
		}
	}
	
	// Now we need to sort the arrays in the correct order.
	var myHash = new Array();
	var j = 0;
	for(j=0; j<arr.length; j++)
	{
		myHash[arr[j]] = usArr[j];
	}
	
	arr.sort();
	for(j=0; j<arr.length; j++)
	{
		usArr[j] = myHash[arr[j]];
	}
}

// Splits a comma-delimited list of facets into an array
function buildFacetListOLD(facet, arr, translations, siteId, usArr)
{
	var facets = facet.split(',');
	var i = 0;
	var j = 0;
	
	if (siteId != 'Shure.com')
	{
		var trans = translations.replace('<Translation>','');
		trans = trans.replace('<translations>','');
		trans = trans.replace('</translations>','');
		var arrTrans = trans.split('</Translation>');

		// Figure out which locale we need
		var arrTran = arrTrans[0].split(',');
		for (i=0; i<arrTran.length; i++)
		{
			if (trimAll(arrTran[i].replace('\n', '')) == siteId)
			{
				break;
			}
		}	
		
		// This is the index (column) of the translated value
		var siteIndex = i;
		
		for (j=0; j<facets.length; j++)
		{
			for (i=1; i<arrTrans.length-1; i++)
			{
				var arrTran = arrTrans[i].split(',');
				
				var compareValue = arrTran[0].replace('<Translation>', '');
				compareValue = compareValue.replace('\n', '').replace('\r','');
				compareValue = trimAll(compareValue);
				if (compareValue == facets[j])
				{		
					if (facets[i] != '')
					{							
						// Add the new item to the array
						var newIndex = arr.length;
						arr[newIndex] = arrTran[siteIndex].replace('<Translation>','').replace('\n', '');
						usArr[newIndex] = facets[j];
					}
				}
			}
		}
		
		// Now we need to sort the arrays in the correct order.
		var myHash = new Array();
		var j = 0;
		for(j=0; j<arr.length; j++)
		{
			myHash[arr[j]] = usArr[j];
		}
		
		arr.sort();
		for(j=0; j<arr.length; j++)
		{
			usArr[j] = myHash[arr[j]];
		}
	}	
	else
	{
		var allOthers = false;
		for(i=0; i<facets.length; i++)
		{
			if (facets[i] != '')
			{
				// Add the new item to the array
				var newIndex = arr.length;
				arr[newIndex] = facets[i];
				usArr[newIndex] = facets[i];
			}
		}
		
		// Sort the arrays
		arr.sort();
		usArr.sort();	
	}
}

// The main method called to build the menu.  
function displayFacets(facets, currentFacet, hiddenName, usFacets)
{
	if (currentFacet == '')
	{
		// display root facets
		var rootFacets = new Array();
		var usRootFacets = new Array();
		getRootFacets(facets, rootFacets);
		getRootFacets(usFacets, usRootFacets);
		displayRootFacets(rootFacets, hiddenName, usRootFacets);
	}
	else
	{
		var facetList = facets;	
		//var facetList = usFacets;
		var rootFacet;
		var currentLevel;
		var currFacet;
		
		// First, figure out where we're at. i.e. how deep and in which root facet
		for (b=0; b<facetList.length; b++)
		{			
			if (facetContains(usFacets[b], currentFacet))
			{
				rootFacet = getLinkedLevelFacet(usFacets[b], 0);
				currentLevel = getFacetLevel(usFacets[b], currentFacet);
				break;
			}
		}

		// Now that we have the info we need, start building the list
		for (d=0; d<currentLevel+1; d++)
		{
			document.write("<ul class=\"content_item\">");
			currFacet = getLevelFacet(facetList[b], d);			
			actualCurrFacet = getLinkedLevelFacet(facetList[b], d);			
			//if (d == currentLevel && endsWith(document.getElementById('facet_crumb').value, currFacet))
			if (d == currentLevel)
			{
				document.write("<li class=\"active\">" + currFacet + "</li>");	
			}
			else
			{
				// Need to only get current level bread crumb facet items
				//var facetCrumb = document.getElementById('facet_crumb').value.split(',');
				//var facetCrumbString = '';
				//var facetCrumbLinksString = '';
				//for (q=0; q<=d; q++)
				//{
				//	if (facetCrumb[q] != '')
				//	{
				//		facetCrumbString = facetCrumbString + facetCrumb[q];
				//		if (q < d)
				//		{
				//			facetCrumbString = facetCrumbString + ',';
				//		}
				//	}
				//}
				var facetCrumbString = document.getElementById('facet_crumb').value;
				//TCS-05/04
				//document.write("<li class=\"active\"><a href=\"#\" onclick=\"document.getElementById('facet_crumb_links').value='" + facetCrumbLinksString + "';document.getElementById('facet_crumb').value='" + facetCrumbString + "';document.getElementById('" + hiddenName + "').value='" + getLevelFacet(usFacets[b], d) + "';faceted_nav.submit();\">" + currFacet + "</a></li>");	
				//document.write("<li class=\"active\"><a href=\"#\" onclick=\"document.getElementById('facet_crumb_links').value='" + facetCrumbLinksString + "';document.getElementById('facet_crumb').value='" + facetCrumbString + "';document.getElementById('" + hiddenName + "').value='" + getLinkedLevelFacet(usFacets[b], d) + "';faceted_nav.submit();\">" + currFacet + "</a></li>");	
				//document.write("<li class=\"active\"><a href=\"#\" onclick=\"document.getElementById('facet_crumb').value='" + hiddenName + "=" + facetCrumbString + "';document.getElementById('" + hiddenName + "').value='" + getLinkedLevelFacet(usFacets[b], d) + "';faceted_nav.submit();\">" + currFacet + "</a></li>");	
				document.write("<li class=\"active\"><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.getElementById('facet_crumb').value='" + facetCrumbString + "';document.faceted_nav." + hiddenName + ".value='" + getLinkedLevelFacet(usFacets[b], d) + "';faceted_nav.submit();\">" + currFacet + "</a></li>");	
			}			
		}
		
		//alert('Curr Facet: ' + currentFacet + '\nCurr Level: ' + currentLevel + '\nList: ' + facetList[b] + '\nLength: ' + getFacetLength(facetList[b]));
		// Now we need to see if there are children below the current level before closing up the UL's
		if (currentLevel < getMaxFacetLength(facetList)-1)
		{			
			var childFacets = new Array();
			var usChildFacets = new Array();
			var childFacet;
			
			for (f=0; f<facetList.length; f++)
			{
				//alert('Level: ' + getLinkedLevelFacet(facetList[f], currentLevel) + '\n Curr: ' + currFacet);
				
				//TCS-05/04
				//if (getLevelFacet(facetList[f], currentLevel) == currFacet)
				if (getLinkedLevelFacet(facetList[f], currentLevel) == actualCurrFacet)
				{					
					//TCS-05/04
					//childFacet = getLevelFacet(facetList[f], currentLevel+1);
					childFacet = getLinkedLevelFacet(facetList[f], currentLevel+1);
					if (childFacet != '' && !arrContains(childFacets, childFacet))
					{
						// Add the new item to the array
						var newIndex = childFacets.length;
						childFacets[newIndex] = childFacet;
						usChildFacets[newIndex] = getLinkedLevelFacet(usFacets[f], currentLevel+1);
					}
				}
			}
			
			document.write("<ul>");
			for (g=0; g<childFacets.length; g++)
			{
				if (document.getElementById('facet_crumb').value != '')
				{
					//document.write("<li><a href=\"#\" onclick=\"document.getElementById('facet_crumb_links').value=document.getElementById('facet_crumb_links').value + ',' + location.href;document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '," + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "';document.getElementById('" + hiddenName + "').value='" + usChildFacets[g] + "';faceted_nav.submit();\">" + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "</a></li>");	
					document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.faceted_nav." + hiddenName + ".value='" + usChildFacets[g] + "';document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '," + hiddenName + "=" + getLinkedLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "';submitForm();\">" + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "</a></li>");	
					
				}
				else
				{
					//document.write("<li><a href=\"#\" onclick=\"document.getElementById('facet_crumb_links').value=document.getElementById('facet_crumb_links').value + location.href;document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '" + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "';document.getElementById('" + hiddenName + "').value='" + usChildFacets[g] + "';faceted_nav.submit();\">" + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "</a></li>");				
					document.write("<li><a onmouseover=\"this.style.cursor='pointer'\" onclick=\"document.faceted_nav." + hiddenName + ".value='" + usChildFacets[g] + "';document.getElementById('facet_crumb').value=document.getElementById('facet_crumb').value + '" + hiddenName + "=" + getLinkedLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "';submitForm();\">" + getLevelFacet(childFacets[g], getFacetLength(childFacets[g])-1) + "</a></li>");				
				}
			}
			
			document.write("</ul>");
		}
		
		// Close up the UL's
		for (d=0; d<currentLevel+1; d++)
		{
			document.write("</ul>");
		}
	}
}

function translateText(facet, table, siteId)
{
	var strTable = table.split(',');
	return strTable[siteId];
}

function endsWith(str, suffix) {
    var startPos = str.length - suffix.length;
    if (startPos < 0) {
    return false;
    }
    return (str.lastIndexOf(suffix, startPos) == startPos);
}