// script to hide/show drop down menu
// based on http://www.alistapart.com/articles/horizdropdowns
// modified: Jan 13 2007
//	added pause for mouseout, also added focus and blur events
//	modified to always close menu when going to the next one

// set onload event
addLoadEvent(stepThruAll);

// class of drop down menu
dropClass = "menu";

// class to show drop down
showDrop = "showdrop";

// tagName of dropdown part of menu
dropTag = "UL";

// set true if using images instead of text
// if true, will use width of image for width of dropdown menu
useImg = true;

// pause for menu to stay open on mouseout, milliseconds
outPause = 600;

// get elements by class name
// from http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// go through tree of parNode looking for an element with
// class dropClass
function stepThruAll() {
	var chNodes=getElementsByClass(dropClass);
	// go through all elements of class dropClass
	for(var i=0;i<chNodes.length;i++) {
		// need to step through sub menus
		addEvents(chNodes[i]);
	}
}


// add the mouse over and mouse out events to elements with
//    tag name dropTag
// also size this element to match the width of the first image
//    if there is an image in it
function addEvents(pNode) {
	var x=pNode.childNodes;
	for (var i=0; i<x.length; i++) {
		if (x[i].tagName==dropTag) {
			x[i].parentNode.onmouseover=showMenu;
			x[i].parentNode.onfocus=showMenu;
			x[i].parentNode.onmouseout=setHide;
			x[i].parentNode.onblur=setHide;
			// set width size
			var y = x[i].parentNode.getElementsByTagName('IMG');
			if (useImg && y[0]) x[i].style.width=y[0].width+"px";
		} else {
			// dig further
			if (x[i].firstChild) addEvents(x[i]);
		}
	}
}

function showMenu() {
	// need to check if a timeout has been set for
	// this element or a different element
	cmt(hideMenu.tgt!=this);
	var re=new RegExp("(^|\s)"+showDrop+"(\s|$)","gi");
	if (!this.className.match(re))
	 this.className+=" "+showDrop;
}
// globals: outPause
function setHide() {
	// check if timeout already set
	cmt(true);
	// set target
	hideMenu.tgt=this;
	hideMenu.ht=setTimeout(hideMenu,outPause);
}
function hideMenu() {
	var y=new RegExp(" *"+showDrop, "gi");
	hideMenu.tgt.className=hideMenu.tgt.className.replace(y, "");
}
// clear mouseout timeout
function cmt(runHide) {
	if (hideMenu.ht) { 
		clearTimeout(hideMenu.ht);
		hideMenu.ht=null;
		if (runHide) hideMenu();
	}
}

//onload event handler
//from http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}