
// ------------------------------------------------------------------------------------------------//
// NAME:		 LoadSideNav
// DESCRIPTION:	 Called when the page loads to hide/display active portions of the side navigation. 
// PURPOSE: 	 Loads the <li> elements from an <ul id="sidenav"> element within an html document.
//				 It then compares each <a> element contained within each <li> element until the 
//				 a match is found in relation to the document's URL. 
// RESTRICTIONS: To speed the process of parsing large menus and support antiquated menus, the first
//				 main level of <li> elements should be assigned an attribute value of 'class="nav"'
//				 if they contain an additional <ul> element. Additionally, the first main level of 
//				 <a> elements should have an attribute value of 'class="btn-yellow"' (HINT: these
//				 are the first level navigation items that display in gold when rendered).
// ------------------------------------------------------------------------------------------------//
function LoadSideNav() 
{
	var sideNavNode;
	var listItemNodes;
	var listNode;
	var activeMenuFound;
	var hyperlinkNode;
	var counter;
	var linkAddress; 
	
	sideNavNode 	= document.getElementById("sidenav");
	listItemNodes 	= sideNavNode.getElementsByTagName("li");
	activeMenuFound = false;
	counter 		= 0;
	linkAddress 	= "";
	
	// Hide all other submenus.	
	while((counter < listItemNodes.length) && (activeMenuFound == false))	
	{
		listNode = listItemNodes[counter];
		
		if (listNode.className == "nav")
		{ 
			hyperlinkNode = listNode.getElementsByTagName("a")[0];
							
			if (hyperlinkNode.className == "btn-yellow")
			{
				linkAddress = hyperlinkNode.href;

				activeMenuFound = CheckAddress(listNode, hyperlinkNode, linkAddress, location);
			}// end if	
		}// end if
		
		counter++;
	}// end while	
}


// ------------------------------------------------------------------------------------------------//
// NAME:		CheckAddress 
// DESCRIPTION:	Called recursively from the the "LoadSideNav" function 
// PURPOSE: 	Compares an unordered lists of links against the current URL to see which sections 
//				should be displayed.
// ------------------------------------------------------------------------------------------------//
function CheckAddress(listNode, hyperlinkNode, linkAddress, location)
{
	var activeMenuFound 	= false;
	var activeSubMenuFound 	= false;				
	var subcounter			= 0;
	var listSubNodes;
	
	if (linkAddress == location)
	{
		activeMenuFound = true;
		hyperlinkNode.className = hyperlinkNode.className + " active"; 
		
		if (listNode.getElementsByTagName("ul").length > 0)
			listNode.getElementsByTagName("ul")[0].style.display = "block";
	}
	else if (linkAddress == location.href.substr(0, linkAddress.length))
	{					
		hyperlinkNode.className = hyperlinkNode.className + " active";
		activeMenuFound = true;

		if (listNode.getElementsByTagName("ul").length > 0)
			listNode.getElementsByTagName("ul")[0].style.display = "block";					

		listSubNodes = listNode.getElementsByTagName("li");
		
		while ((subcounter < listSubNodes.length) && (activeSubMenuFound == false))	
		{
			listNode = listSubNodes[subcounter];
			hyperlinkNode = listNode.getElementsByTagName("a")[0];	
			linkAddress = hyperlinkNode.href;
			
			activeSubMenuFound = CheckAddress(listNode, hyperlinkNode, linkAddress, location);

			subcounter++;
		}
	}
	
	return activeMenuFound;
}

// -- Render the Side Navigation when the page loads. ------------------------------ //

var prevOnLoad = window.onload;

if (typeof window.onload != 'function') 
{	
	window.onload = LoadSideNav;	
}
else
{
	window.onload = function () 
	{
		if (prevOnLoad) 
			prevOnLoad();

		LoadSideNav();
	}
}

