window.onload = function()
{
	LoadMenu();
	LoadPrintHeader();
	StyleLastLinks();
	IDcurrentPage();
};

function IDcurrentPage()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Searches the nav                                     |
	   | until it finds the link to the currently loaded page |
	   | Assigns the list the ID "currPage"                   |
	   | and the link the ID "currLink."                      |
	   | If no link is found, no IDs are assigned.            |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+*/

	// First, get the URL of the currently loaded page, 
	// removing any anchors (shortcuts).
	var expURL = /^[^#]+/;	// a regular expression that captures everything before the first "#" in the URL
	var strURL = location.href.match(expURL);
	strURL = strURL.toString();	// save as a string of text

	if ( document.getElementById("nav") )
	{
		// Get all the <li>s in nav.
		var navLinks = document.getElementById("nav").getElementsByTagName("li");

		// For each <li> in nav...
		for ( var i=0; i < navLinks.length; i++ )
		{
			// ...get the path of the link nested inside that <li>.
			var strPath = navLinks[i].getElementsByTagName("a")[0].href;

			// I'm using all caps so that a comparison won't fail to match
			// simply because the letter casing is off.
			if ( strPath.toUpperCase()==strURL.toUpperCase() )
			{
				navLinks[i].id = "currPage";
				navLinks[i].getElementsByTagName("a")[0].id = "currLink";
				// If the currLink is in the subnav, then the nested list needs to be displayed.
				// (If it's not nested, then this just reinforces that the nav <div> is a block.)
				var tempParent = navLinks[i].parentNode;
				
				while ( tempParent.id != "nav" )
				{
					if ( tempParent.nodeName=="UL" || tempParent.nodeName=="OL" )
						//alert("block");
						tempParent.style.display = "block";
					
					tempParent = tempParent.parentNode;				
				}

				return;	// no need to keep looping, you've found the match
			}
		}
	}
}

function LoadPrintHeader()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Creates an <img> element                        |
	   | and sticks it at the top of the page,           |
	   | right before the header.                        |
	   |	                                             |
	   | CSS will only make it visible at print time.    |
	   |	                                             |
	   | Creating this element after the page has loaded |
	   | will keep code that is strictly for looks       |
	   | out of our well-structured document.            |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	var objImg = document.createElement("img");
	objImg.id = "printHeader";

	// Problem: if we use "images/printHeader.gif" as the src,
	// it will only work in the root directory.
	// Solution: to make it dynamic, steal the src of the old header,
	// and replace "EPheader" with "printHeader." 
	var objOldHeader = document.getElementById("NPStitle");
	objImg.src = objOldHeader.src.replace("EPheader","printHeader");
	
	// Find the header and insert this img before the header.
	var objHeader = document.getElementById("header");
	objHeader.parentNode.insertBefore(objImg, objHeader);
}

function StyleLastLinks()
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Assigns the lastLink class |
	   | to the last list element   |
	   | in the specificied list.   |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */
	if ( document.getElementById("content") )
	{
		var aBackNext = document.getElementById("content").getElementsByTagName("div");
		
		for ( var i=0; i < aBackNext.length; i++ )
		{
			if ( aBackNext[i].className=="backNext" )
			{
				var listElements = aBackNext[i].getElementsByTagName("li");
				
				listElements[listElements.length-1].className = "lastLink";
			}
		}
	}

	if ( document.getElementById("footer") )
	{
		var footerLinks = document.getElementById("footer").getElementsByTagName("li");

		footerLinks[footerLinks.length-1].className = "lastLink";
	}
}

function SetSrcFromRoot(strImg)
{
	/* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
	   | Dynamically determines a path to the root /images/ directory. |
	   +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ */

	// (It does this by stealing the src of the header graphic,
	// and replacing "AEP_head1" with the name of your <img>.)
	if ( document.getElementById("NPStitle") )
	{
		var objHeader = document.getElementById("NPStitle");
		
		var strFullPath = objHeader.src.replace("EPheader.gif", strImg);
		
		return ( strFullPath );
	}
}