// menu.js - version 1.0 
// January 9th 2004 - Lloyd_Hannesson@umanitoba.ca
//
// This script exists to make the menus in the new templates look pretty. I walk the DOM
// under the DIV#menu and compare the current url to the url specified in the link.
// This script takes all the protocol and subdomain differences that may appear in a link
// and ignores them.

function getFileName(passedString) {
// Get the file name minus extention from the passed string
// and return it back to the user. If a dir was passed "/" then
// return "index"
  if (passedString.slice(-1) == "/") {
    return 'index';
  }
  url = passedString.split("/");
  file = url[url.length-1].split(".");
  return file[0];
}

function getUrlName(passedString) {
// Get the URL from the passed string and return it back 
// minus the http://*. part of the URL. This is done to prevent 
// confusion in the script if a page is using https or contains
// www in the url. 
  if (passedString.substring(0,3) == "htt") {
    start = passedString.indexOf("/",10);
    end = passedString.lastIndexOf("/");
    url = passedString.substring(start, end+1);
  } else {
    end = passedString.lastIndexOf("/");
    url = passedString.substring(0, end+1);
  }
  return url;
}

function foundAUl(nodeId, curUrl, curFile,done) {
	//alert("foundAUl");
	// Found a UL node - recursive function.
	// This function will walk through the provided nodelist signified by nodeId and 
	// check all the links for markup. If another UL list is contained here this function
	// will call itself to drill further and further down the list.
	var numNodes;
	var currentNode;
	var numNodes;
	var q;
	//var done = "false";
  	numNodes = nodeId.childNodes.length; // Number of nodes we are working with.
	for (q=0; q<numNodes; q++)
	{
	  currentNode = nodeId.childNodes.item(q);
	  //<div id="firstMnuItem"><a href="http://webdev.cc.umanitoba.ca/libraries">Libraries Home</a></div>
		if (currentNode.nodeName == "UL") {
			// Oh so you found a UL tag instead of a LI? This is malformed html, but i'll look for it anyways just incase :)
			foundAUl(currentNode, curUrl, curFile);
		}//end if.
		//check to see if we are at the Unit/Faculty home link in the menu i.e. at the
		//very top of the menu. This particular link is outside of the rest of the menu
		//there for we need to look for the DIV tag not the <UL><LI> tags like in the
		//rest of the menu.
		if (currentNode.nodeName == "A" && currentNode.parentNode.nodeName == "DIV")
		{
			//alert("found a DIV");
			daPath = currentNode.pathname; // save the current link path
			//substring(first_index,last_index)
			//determines if the last character of the string is a slash
			//in most cases it shouldn't since this is used mostly for
			//the Faculty/Unit home URL interogation. We need to ensure
			//that the slash is there in order for the comparison to work
			//later on.
			if(daPath.substring(daPath.length-1,daPath.length) != "/")
			{
				daPath = daPath + "/";
			}//end if.
			//alert("daPath = " + daPath);
		 	daUrl = getUrlName(daPath); 
			//alert("daUrl = " + daUrl);// Get the url part of the link
		  	daFile = getFileName(daPath); // Get the file name minus extention of the link
		  	//alert("daFile = " + daFile);
			if (daUrl.substr(0,1) != "/") {
				daUrl = "/"+daUrl; // Correct for some strange IE wierdness. For some reason it doesn't add a / to the start of the path.
			}//end if
			//alert("daUrl: " + daUrl + " \tcurUrl: " + curUrl + "\ndaFile: " + daFile + " \t\tcurFile: " + curFile + "\n");
			//compare the URL and File names of the current page the user is on and the URL/Page
			//of the Unit/Faculty home link.
		  	if ((curUrl == daUrl) && (curFile == daFile))
			{
				//alert("dlfjsljaskljfslkjaslfj" + currentNode.parentNode.getElementsByTagName("A")[0]);
				currentNode.parentNode.getElementsByTagName("a")[0].className = "selected"; // Current page is the home page so tag it
				done = 1;
			}//end if
			
		}//end if.
		//if the current node is a list object which contains a URL (a tag) and we haven't already
		//highlighted a URL then continue.
		//alert("currentNode.nodeName: "+currentNode.nodeName);
		//alert("currentNode.firstChild: "+currentNode.firstChild);
		//if(currentNode.nodeName == "LI" && currentNode.firstChild != null)
		//    then if( currentNode.firstChild.nodeName == "A" && done == 0)
		//        then do stuff.....
		//if( currentNode.firstChild != null )
		//{
		//if ( (currentNode.nodeName == "LI" && currentNode.firstChild.nodeName == "A") && done == 0 )
		//{
		if(currentNode.nodeName == "LI" && currentNode.firstChild != null)
		{
		if(currentNode.firstChild.nodeName == "A" && done == 0)
		{
			//alert("found li > a");
			if (currentNode.lastChild.nodeName == "UL")
			{
				// Does the current node have a child that is also a UL tag? 
				// If so recursivly call this function again to mark up the submenu as well...
				foundAUl(currentNode.lastChild, curUrl, curFile);
			}//end if.
			daPath = currentNode.firstChild.pathname; // save the current link path
			//alert("daPath = " + daPath);
		 	daUrl = getUrlName(daPath); 
			//alert("daUrl = " + daUrl);// Get the url part of the link
		  	daFile = getFileName(daPath); // Get the file name minus extention of the link
		  	//alert("daFile = " + daFile);
			if (daUrl.substr(0,1) != "/")
			{
				daUrl = "/"+daUrl; // Correct for some strange IE wierdness. For some reason it doesn't add a / to the start of the path.
			}//end if
			//alert("daUrl: " + daUrl + " \tcurUrl: " + curUrl + "\ndaFile: " + daFile + " \t\tcurFile: " + curFile + "\n");
		  if ((curUrl == daUrl) && (curFile == daFile))
		  {
			//if (curUrl == daUrl) {
				//alert("hereherehere: "+ currentNode.getElementsByTagName("a")[0]);
				currentNode.getElementsByTagName("a")[0].className = "selected"; // The current LI is pointing to the current page, so tag it
				if(currentNode.parentNode.nodeName == "UL" && currentNode.parentNode.parentNode.nodeName == "LI")
				{
					// if this link has a parent LI>UL then it is a sub menu so flag the parent as well
					currentNode.parentNode.parentNode.getElementsByTagName("a")[0].className = "parent";
				}//end if
		  }//end if
		}//end if
		}//end outer if
	}//end for loop
	return done;
}//end function

function walkTheDom(nodeId, curUrl, curFile)
{
	menuNodes = nodeId.childNodes.length;
	var done = 0;
	//alert("menuNodes = " + menuNodes);
	for (i=0; i<menuNodes; i++)
	{  
    	// Loop through all the div#leftNav nodes looking for a <UL> tag
		// This step was necessary just incase the IP places multiple menu blocks in the menu div
		selectedNode = nodeId.childNodes.item(i);
		//alert("in walkTheDom"+selectedNode.nodeName);
		if ( (nodeId.childNodes.item(i).nodeName == "UL" || nodeId.childNodes.item(i).nodeName == "DIV") && done == 0)
		{
			done = foundAUl(selectedNode, curUrl, curFile,done);
		}//end if
	}//end for loop
}//end walkTheDom

function leftNav()
{
  origPage = document.URL;
	
  curUrl = getUrlName(origPage);
  curFile = getFileName(origPage);
  //alert("origPage = " + origPage + "\ncurUrl = " + curUrl + "\ncurFile = " + curFile);
  //alert("Url: " + origPage + "\ncurUrl: " + curUrl + "\ncurFile: " + curFile);
  walkTheDom(document.getElementById("leftNav"), curUrl, curFile);
}//end leftNav