// JavaScript Document
/* File name: submenu.js 
 * Description: hide/show/highlight elements of an unordered list used for navigation
 * 
 * 1. Figure out which li/anchor corresponds to the page we're on by 
 *    pattern-matching the URL
 * 
 * 2. Expand the ul below that li, if it exists
 *
 * 3. Bounce up the tree, expanding ul's and setting .activelink on *anchors* 
 *    not on li's
 */

/* Initialize (hide) all of ul's below second level ul.
 * @param tree_id  
 * @return void
 */
function initializeNavTree(tree_id) {
	if (typeof automatic_left_nav!="undefined") {
		if (!automatic_left_nav) {
			return;
		}
	}
	
	var nav_tree = document.getElementById(tree_id);
	//hide the nav_tree to prevent flash of unstyled content
	if (nav_tree) { //in case the page doesn't have a nav_tree
		nav_tree.style.display = 'none';
		if (nav_tree) {
			var anchor = whereAmI(nav_tree);
			if (anchor) {
				hideChildUls(nav_tree); 
				//show the menu one below this anchor (if it exists)
				expandChildNavTree(anchor);
				//show the entire menu above this anchor
				expandParentsNavTree(anchor, tree_id);
			} else {
				hideChildUls(nav_tree); 
				//alert('This page is not linked to in the sidenav.');
			}
		}
		
		//show the nav_tree
		nav_tree.style.display = 'block';
	}
	return;
}


/* whereAmI()
 * @param Object node of ul with anchors to search
 * @return Object page_anchor of anchor of the page you're on
 */
function whereAmI(ul){
	//find anchor tags in unordered list
	var all_the_anchors = nodeChildrenByTagName(ul, "a");
	var where_you_are = document.location.href;
	var return_anchor;
	for (var i=0; i<all_the_anchors.length; i++) {
		var re = new RegExp ( '/formlogin', 'gi' );
		
		if( where_you_are.indexOf( "formlogin" ) >= 0 )
			where_you_are = where_you_are.replace( re, "" );
		/* end cbader */

		if (normalizeURL(where_you_are) == normalizeURL(all_the_anchors[i].href)) {
			return_anchor = all_the_anchors[i];
		}
	}

	// if nothing has matched yet, let's try the path alone
	if (null == return_anchor) {
		for (var i=0; i<all_the_anchors.length; i++) {
			if (normalizeURL(where_you_are).indexOf(normalizeURL(all_the_anchors[i].href))>-1) {
				return_anchor = all_the_anchors[i];
			}
		}
	}

	/* If we haven't matched anything, try matching on the title before returning false*/
	if (null == return_anchor) {
		//Try to match the page title against the text of the anchors
		var page_title = document.title;
		var link_text;
		for (var i=0; i<all_the_anchors.length; i++) {
			link_text = all_the_anchors[i].childNodes[0].nodeValue;
			if (page_title.indexOf(link_text)>-1) {
				return_anchor = all_the_anchors[i];
				return return_anchor;
			}				
		}
		//If we still haven't found an anchor
		if (null == return_anchor) {
			return false;
		}
	} 
	return return_anchor;
}

/* Don't choke if we don't have getElementById*/
if (!document.getElementById)
    document.getElementById = function() { return null; }

//End menuExpandable

/**
 * Hides all child ul's from a starting ul, descending into child nodes
 * @param Object el node to initialize
 * @return void
 */
function hideChildUls(el) {
	try {
		var children = el.childNodes;
	}
	catch (er) {
		return;
	}
	for (var n=0; n < children.length; n++) {
		var item = children[n];
		if (String(item.tagName).toUpperCase() == "LI") {
			var nodes = item.childNodes;
		  	for (var t=0; t < nodes.length; t++) {
				if (String(nodes[t].tagName).toUpperCase() == "UL") {
			  		nodes[t].style.display = 'none';
			  		hideChildUls(nodes[t]);
					}
		  	} // end for loop
		} 
	}
}

/* Starting from an anchor nested within a UL, display UL it's nested in,
 * then all of its parent, grandparent, great-grandparent ULs until ID is found.
 * @param Object node ul
 * @param string id ID of node to stop at
 * @return void
 */
function expandParentsNavTree(a, id) {
	//show this ul and all of its parents
	var node = a;
	var save_node = node;
	var cnt =0;
	if (node != null) {
		while (node.id != id) {
			if (String(node.tagName).toUpperCase() == "LI") {
				var children = nodeChildrenByTagName(node, "A", 1);
				if (children[0]) {
					nodeAddClass(children[0], 'activelink');
				}
			}
			if (String(node.tagName).toUpperCase() == "UL") {
				node.style.display = 'block';
			}
			save_node = node;
			node = node.parentNode;
			cnt=cnt+1;
		}
		if (cnt == 2) {
			nodeAddClass(save_node, 'active_item_top');
		}
	}
}

/* Starting from an anchor nested within a ul, displays its li,
 * then all of its parents, grandparents, great-grandparents, etc.
 * stopping only when it gets to a specific id
 * @param Object node ul
 * @param string id ID of node to stop at
 * @return void
 */
function expandChildNavTree(a) {
	var node = a;
	//get the LI that the anchor is nested in
	while (String(node.tagName).toUpperCase() != "LI") {
		node = node.parentNode;
	}
	
	nodeAddClass(node, 'active_item');

	//loop through children of that li, display the ul if it's in there
	var node_children = node.childNodes;
	for (var n=0; n < node_children.length; n++) {
		var this_node = node_children.item(n);
		if (String(this_node.tagName).toUpperCase() == "UL" ) {
			this_node.style.display = 'block';
			if (typeof(submenuExpandTree) != "undefined"
			&& submenuExpandTree == false) {
				this_node.style.display = 'none';
			}
		}
	}
}

/* chop index.html, index.php, page anchors from end of URLs*/
function normalizeURL(url_string) {
	var file_names = Array('index.html','index.shtml', 'index.htm', 'index.php');
	for (var i=0; i<file_names.length; i++ ){
		if (url_string.toLowerCase().indexOf(file_names[i]) != -1) {
			url_string = url_string.toLowerCase().replace(file_names[i], '');
		}
	}

	//Split on # and disregard everything after the #
	//Gets rid of page anchors
	var url_arr = url_string.split('#');
	url_string = url_arr[0];
	
	//Get rid of weblogin_random=XXXXXX
	var wl_arr = url_string.split('weblogin_random');
	url_string = wl_arr[0];
	//If we need to add back GET variables after the weblogin_random
	if (wl_arr[1]) {
		var query_string = wl_arr[1].split('&');
		if (query_string[1]) {
			url_string = wl_arr[0] + query_string[1];
		}
	}
	
	//Remove trailing question mark
	if (url_string.charAt(url_string.length-1)=='?') {
		url_string = url_string.substring(0,url_string.length-1);
	}
	
	return url_string;
}

