/*************************************************
* based on same idea from brainjar.com
* (http://brainjar.com/dhtml/menubar/demo.html)
* 2004.04.19 Jason Finneyfrock (jfinney@aashto.org)
*
* the basic goal is to convert an HTML bulleted list
* of a site's pages into a dropdown menu using
* javascript and CSS, with minimal extraneous
* HTML or CSS markup. Needless to say, old browsers
* choke on this bigtime.
**************************************************/

// GLOBALS
var currentmenu;						// we need this to know if any menus are being shown
var activebutton;
var menubarid = "menubar";			// id of the ul that's our menubar
var submenuclass = "submenu"; 	// class name of child menus
var isIE = false;

// PAGE LOAD EVENTS
window.onload = function(){
	initMenu();
}

if(navigator.userAgent.indexOf("MSIE") >= 0){
	isIE = true;
	document.onmousedown = pageMousedown;
} else {
	document.addEventListener("mousedown",pageMousedown,true);
}

function pageMousedown(event){
	var el;
	if(currentmenu == null) return;

	if(isIE){
		el = window.event.srcElement;
	} else {
		el = (event.target.tagName) ? event.target : event.target.parentNode;
	}
	if(el == activebutton) return;

	//alert(el.tagName + el.innerHTML);
	if(isPartOfActiveMenu(el) == null){
		hideMenu(currentmenu);
		currentmenu = null;
	}
}


// MENU FUNCTIONS
function initMenu(){
	if(document.getElementById){
		var menubar = document.getElementById(menubarid);
		setMenuLinks(menubar);
	}
}

function setMenuLinks(objMenu){
	var links = getLinks(objMenu);
	var expandableChar;

	for(var i = 0; i < links.length; i++){

		if(hasSubmenu(links[i])){

			links[i].innerHTML += "<span class=\"rightarrow\"> &raquo;</span>";
			setArrowWidth(objMenu);

			links[i].onmouseover = function(){
				showMenu(this);
			}
			if(getSubmenu(links[i]) != null){
				setMenuLinks(getSubmenu(links[i]));
			}
		}
	}
}

// TESTING THIS
function setArrowWidth(menu){
	var spanList;
	var howWide;

	if(menu.id != menubarid){
		spanList = menu.getElementsByTagName("SPAN");
		for(var i = 0; i < spanList.length; i++){
			if(spanList[i].className == "rightarrow"){
				/*
				spanList[i].style.display = "block";
				spanList[i].style.width = "10px";
				spanList[i].style.marginLeft = "20px";
				*/
			}
		}
	}
}


function getSubmenu(objLink){
	var listitem = objLink.parentNode;
	for(var i = 0; i < listitem.childNodes.length; i++){
		var child = listitem.childNodes[i];
		if(child.tagName == "UL"){
			return child;
		}
	}
	return null;
}

// give me an A and I'll show whatever UL is below it
function showMenu(objLink){
	var menu;

	activebutton = objLink;

	if(objLink.tagName == "A"){
		menu = getSubmenu(objLink);
	}
	menu.style.display = "block";

	var top;
	var left;
	var parent = menu.parentNode;

	top = parent.offsetHeight + parent.offsetTop;
	left = parent.offsetLeft;

	// IE pushes the menus over to the right too much
	if(isIE) left = left - 20;

	// this means that we're more than 1 level down
	// so the menus have to be positioned differently
	if(!isFirstLevelMenu(menu)){
		top = parent.offsetTop + 0;
		left = parent.offsetLeft + parent.offsetWidth + 2;
	}

	menu.style.top = top;
	menu.style.left = left;

	if(currentmenu != null){
		if(isFirstLevelMenu(menu)){
			hideMenu(currentmenu); // hide first level menus
		} else {
			collapseSiblingMenus(menu);
		}
		currentmenu = null;
	}


	currentmenu = menu;
}

function isFirstLevelMenu(objMenu){
	if(objMenu.className == submenuclass && objMenu.parentNode.parentNode.className != submenuclass){
		return true;
	}
	return false;
}


function hideMenu(objMenu){
	objMenu.style.display = "none";
	if(!isFirstLevelMenu(objMenu)){
		hideMenu(objMenu.parentNode);
	}
}

function hasSubmenu(objLink){
	if(getSubmenu(objLink) != null){
		return true;
	}
	return false;
}

// give me a UL and I'll give you all child A's
function getLinks(objMenu){
	var links = new Array();
	var counter = 0;

	if(objMenu != null){
		if(objMenu.childNodes.length >= 1){
			for(var i = 0; i < objMenu.childNodes.length; i++){
				var child = objMenu.childNodes[i];
				if(child.tagName == "LI"){
					for(var j = 0; j < child.childNodes.length; j++){
						var grandchild = child.childNodes[j];
						if(grandchild.tagName == "A"){
							links[counter] = grandchild;
							counter++;
						}
					}
				}
			}
		}
	}
	return links;
}

function isPartOfActiveMenu(obj){
	while(obj != null){
		if(obj.tagName != null){
			if(obj.tagName == "UL"){
				if(obj.className == submenuclass || obj.id == menubarid){
					return obj;
				}
			}
		}
		obj = obj.parentNode;
	}
	return obj;
}


function collapseSiblingMenus(objMenu){
	var parentmenu = objMenu.parentNode.parentNode;
	var links = getLinks(parentmenu);
	for(var i = 0; i < links.length; i++){
		var siblingmenu = getSubmenu(links[i]);
		if(siblingmenu != null && siblingmenu != objMenu){
			siblingmenu.style.display = "none";
		}
	}
}


