// 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) {
	//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;
  numNodes = nodeId.childNodes.length; // Number of nodes we are working with.
	for (q=0; q<numNodes; q++) {
	  currentNode = nodeId.childNodes.item(q);
		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);
		}
		if (currentNode.nodeName == "LI" && currentNode.firstChild.nodeName == "A") {
			//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);
			}
			daPath = currentNode.firstChild.pathname; // save the current link path
			//alert("daPath = " + daPath);
		 	daUrl = getUrlName(daPath); // Get the url part of the link
		  daFile = getFileName(daPath); // Get the file name minus extention of the link
			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.
			}
			//alert("daUrl: " + daUrl + " \tcurUrl: " + curUrl + "\ndaFile: " + daFile + " \t\tcurFile: " + curFile + "\n");
		  if ((curUrl == daUrl) && (curFile == daFile)) {
				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";
				}
			}
		}
	}
}

function walkTheDom(nodeId, curUrl, curFile) {
	menuNodes = nodeId.childNodes.length;
	//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);
		if (nodeId.childNodes.item(i).nodeName == "UL") {
			foundAUl(selectedNode, curUrl, curFile);
		}
	}
}

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);
}
