/**
 * Takes a pathname and converts it into an array of foldernames
 * 
 * The function splitPathName will take a pathname, convert it into
 * a string (from an object), and convert it into an array using
 * a forward slash as a delimiter.
 *
 * @author				Applied Technology for Learning in the Arts and Sciences (ATLAS)
 * @param	pathName	A Location HTML DOM object
 * @return				An array with elements corresponding to the folders
 */

function splitPathName(pathName) {
	pathName = pathName.toString().split("/");
	var pathNameLength = pathName.length;
	if((pathName[pathNameLength -1].indexOf("."))!= -1) {
		pathName[pathNameLength -1] = "";	
	}
	return  pathName;
}

/**
 * Takes the URL and an unordered list menu and will highlight the current page
 * 
 * The function menuHighlight will take an element containing a list of links
 * and the pathname of the current page. The pathname is split along the forward
 * slash and placed into an array. Depending on the level indicated (0 being a
 * root level menu, 1 being a submenu, 2 being a submenu of the submenu, etc. ),
 * the function will attempt to match up certain parts of the pathname with the 
 * href attribute of a link in the list. Upon a successful match, the anchor 
 * element adds the class "current" to allow for styling. NOT FOR USE WITH A
 * NESTED SUBMENU DESIGN (e.g. http://www.jewishculture.uiuc.edu)
 * 
 * @author          	Applied Technology for Learning in the Arts and Sciences (ATLAS)
 * @param   menu    	Id of the 'div' element containing the linked list.
 * @param   level   	Level of traversal for the menu
 * @return          	void
 */  

function menuHighlight(menu,level){
	if (!document.getElementsByTagName) return;	
  
	//obtain the pathname (e.g. "/contact/things/index.html") and the domain 
    //(e.g  http://example.com) from the page
		
    var pathName = splitPathName(window.location.pathname);
	var hostName = window.location.hostname.toString();
	
	
	
	// loop through all the menu items to find the menu item matching the pathname of the URL

	var mainMenu = document.getElementById(menu);
	if(document.getElementById(menu)){
		var menuItems = mainMenu.getElementsByTagName("li");
		var menuItemsLength = menuItems.length
		for (var i=0; i<menuItemsLength; i++) {
				var menuItem = menuItems[i];
				var anchors = menuItem.getElementsByTagName("a");
				var anchors = anchors[0];
				var anchoref = splitPathName(anchors.href);
				if (anchoref[2] == hostName) {
					var success = true;
					for (var j = 0; j<(level + 1); j++) {
						if((pathName[j+1] == "giving") & (level == 0)) {
							pathName[j+1] = "about";	
						}
						if (anchoref[j + 3] == pathName[j + 1]){}
						else {success = false;}
					}
					
					//Adds the class to the proper page anchor
					
					if(success == true) {anchors.className = "current";}
				}
		}
	}
}


//Executes the function when the document is loaded
$(document).ready(function(){
	menuHighlight('nav',0);
	menuHighlight('subnav',1);
	
});
