/**
 * Dynamic Dropdown and Flyout Navigation Menu
 * Zac Hester - 2005-08-09
 *
 * This script is based on numerous examples from all over the web and
 * is combined with some of my own design/implementation requirements.
 * The actual show/hide events are only used for MS browsers since they
 * don't want to play nice with CSS (which replaces this entire file with
 * one line of style information).
 */


/**
 *  Shows a submenu.
 */
function show_sub_menu() {
	change_selects('hidden');
	var eles = this.childNodes;
	for(var i = 0; i < eles.length; ++i) {
		if(eles[i].nodeName == 'UL') {
			eles[i].style.display = 'block';
		}
	}
}

/**
 *  Hides a submenu.
 */
function hide_sub_menu() {
	change_selects('visible');
	var eles = this.childNodes;
	for(var i = 0; i < eles.length; ++i) {
		if(eles[i].nodeName == 'UL') {
			eles[i].style.display = 'none';
		}
	}
}

/**
 * Initialize a navigation menu list (generic and recursive).
 */
function init_menu_list(ul_element) {

	//Shortcut object references.
	var likids;
	var lis = ul_element.childNodes;

	//Loop ever each <li> element in current <ul>.
	for(var i = 0; i < lis.length; ++i) {

		//Set up a shortcut object.
		likids = lis[i].childNodes;

		//Scan li children.
		var aindex = 0;
		for(var j = 0; j < likids.length; ++j) {

			//Look for anchor index.
			if(likids[j].nodeName == 'A') {
				aindex = j;
			}

			//Check for a <ul> child.
			if(likids[j].nodeName == 'UL') {

				//Assign handlers to display the submenus (MS browsers only).
				if(is_ms) {
					lis[i].onmouseover = show_sub_menu;
					lis[i].onmouseout = hide_sub_menu;
				}

				//Set class to help style the anchors with submenus.
				likids[aindex].className = 'hassubmenu';

				//Scan this list for submenus.
				init_menu_list(likids[j]);
			}
		}
	}
}


/**
 * Initialize expanding/cascading navigation menu.
 */
function init_menu() {
	if(is_ms) {
		//Grab the menu's root element.
		var menu = document.getElementById('menu');
		var loader = null;
		if(menu) {
	
			//Calculate the position of the menu.
			/* var coords = get_coords(menu);
			//Display a little loading thingy.
			loader = document.createElement('div');
			loader.style.position = 'absolute';
			loader.style.left = coords[0];
			loader.style.top = coords[1];
			loader.style.color = '#FF0000';
			loader.style.fontFamily = 'Arial,sans-serif';
			loader.innerHTML = 'Loading menu...';
			document.body.appendChild(loader); */
	
			//Scan root menu list for submenus.
			init_menu_list(menu);
	
			//Drop loader display.
			//document.body.removeChild(loader);
			//Display the previously hidden menus for IE.
			//menu.style.visibility = 'visible';
		}
	}
}


/**
 *  Hides or shows all the select menus on a page.
 *  This is to combat the super-annoying IE bug that makes certain
 *  elements float over other elements no matter what Z-Index you use.
 */
function change_selects(setting) {
	var sels = document.getElementsByTagName("select");
	for(var i = 0; i < sels.length; i++) {
		sels[i].style.visibility = setting;
	}
}

function get_coords(elem) {
    var top_pos = elem.offsetTop;
    var left_pos = elem.offsetLeft;
    var elem_parent = elem.offsetParent;
    while(elem_parent) {
        top_pos += elem_parent.offsetTop;
        left_pos += elem_parent.offsetLeft;
        elem_parent = elem_parent.offsetParent;
    }
    return([left_pos, top_pos]);
}

//MS browser detection code.
var uastring = navigator.userAgent.toLowerCase();
var is_ms = ((uastring.indexOf("msie") != -1)
	&& (uastring.indexOf("opera") == -1));
