 $(document).ready(function(){
 

/**
 *jQuery 1.3
 *
 * Takes the main navigation submenus and inserts it as a section menu
 *
 * The function will make two clones of the main nested navigation
 * sytem. The second clone will have all unordered list (ul) children
 * removed from the system. The "href" attributes of the remaining 
 * top level menu will attempt to match up with the pathname in the URL.
 * If a match is found, the script will inject the first child unordered
 * list (ul) into a constructed div, if a child unordered list (ul)
 * exists. The corresponding div element will be injected into the top of
 * the "content" div
 * 
 * @author		Applied Technologies for Learning in the Arts and Sciences (ATLAS) UIUC
 * @param menu	id of the div element that contains the menu
 * @param level interger representing the menu level of the navigation (0 is main, 1 is secondary)
 * @return 		void
 * @see			menuHighlight
 */
function subMenuInsert(menu,level){
	
	//obtain menus, content divs, make clones, and destroy child unordered lists
	
	var subMenu =document.getElementById(menu);

							
	//Obtain both the hostname and the pathname of the current page
	var hostName = window.location.hostname.toString();
	var pathName = window.location.pathname.toString().split("/");
	
	//traverse through the list to try to find a match in the list anchors
	
	var k = true;
	
			
	var menuItems = subMenu.getElementsByTagName("li");
	
	
	for (var i=0; i<menuItems.length; i++) {
		if(k) {
		
			var menuItem = menuItems[i];
			var anchors = menuItem.getElementsByTagName("a");
			var anchoref = anchors[0].href.toString().split("/");
			
			if(anchoref.length == 5) {
				if (anchoref[2] == hostName) {
					var success = true;
					for (var j = 0; j<(level +1); j++) {
						
						if (anchoref[j + 3] == pathName[j +1]){
							k = false;
						}
						else {
							success = false;
						}
					}
				}
			}
			
			//use a clone of one of the previous menus to inject a copy of the unordered list into the site
			
			if(success == true) {
				$("#content").prepend("<div id='subnavContainer'><div id='subnav'><h2>Sections</h2></div></div>");

				
				menuItem2=menuItem.cloneNode(true);
				
				var container=	document.createElement("ul");
					document.getElementById("subnav").appendChild(container);
				$("li>ul",menuItem2).empty();
				var $kids=$("li",menuItem2).clone();
				$kids.appendTo(container);

				anchors[0].className = "current";

				break;
				
				
			}
			
		}
	}
}
subMenuInsert('nav',0);
});