// 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) {
	var numNodes;
	var currentNode;
	var q = 0;
	//var done = "false";
  	//numNodes = nodeId.childNodes.length; // Number of nodes we are working with.
        /**
         * added Mar 25, 2010. Using the childNodes function does not grab
         * any subnodes of the childNodes so all you get is the immediate
         * children of the current node. By using Protoype.js's decendants
         * function you get all children of the current node.
         *
         *
         */
        var children = $(nodeId).descendants();
        //var cLen = children.length;
        numNodes = children.length;
        //for (q=0; q<numNodes; q++)
        while( q < numNodes && done == 0)
	{
	  //currentNode = nodeId.childNodes.item(q);

          currentNode = children[q];
          //var testNode = children[q].inspect();
          //alert("testNode: "+testNode+",currentNode: "+currentNode.nodeName+", testNodeName: "+children[q].nodeName);  
          //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.
			
	 	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))
		{
			
			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.
	if(currentNode.nodeName == "LI" && currentNode.firstChild != null)
	{

           //don't want to go into this condition if we are just at a top level
           //faculty or department home page. So we check to see if the
           //markup is complete (done).
	   if(currentNode.firstChild.nodeName == "A" && done == 0)
	   {
			
		daPath = currentNode.firstChild.pathname; // save the current link path		
	 	daUrl = getUrlName(daPath);		
	  	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.
		}//end if
		//if we have found the page we are currently on then we
                //want to highlight it on the menu so we add the appropriate
                //class names to the element.
                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";
                   }//end if
                /**
                 * If the currentNode is baried three levels
                 * deep then we need to markup its parent and
                 * grandparents correctly.
                 */
                 if(currentNode.parentNode.parentNode.parentNode.nodeName == "UL" && currentNode.parentNode.parentNode.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";
                    currentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("a")[0].className = "parent";
                  }//end if
                  done = 1; //stop looping if we have found the page
                }//end if
                
           }//end if
	}//end outer if
        q++;
	}//end for loop
        
return done;
}//end function

function walkTheDom(nodeId, curUrl, curFile)
{
	menuNodes = nodeId.childNodes.length;
	var done = 0;
	
	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" || 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
