// JavaScript Document

var strDDMClass = 'dropDownMenu';
var strMOClass = 'mouseOn';
var intDelay = 300;
var linkTag = 'a';
var menuTag = 'div';
var showMenuTag = 'block';
var hideMenuTag = 'none';


var menuTimer;
var i;

// get all the hyperlinks and menu containers
var arLinks = document.getElementsByTagName(linkTag);
var arMenus = document.getElementsByTagName(menuTag);

// loop through the links array and pick out those that have a drop down menu associated with them
for (i = 0; i<arLinks.length;i++) {
	// is the link a menu with an associated drop down menu in the menus array
	if(arLinks[i].rel in parseArray(arMenus) ) {
		// add mouseOver and mouseOut events
		arLinks[i].onmouseover = function() {
			currentItem = this;
			showMenu();
		}
		arLinks[i].onmouseout = function() {
			menuTimer = setTimeout("hideMenu()",intDelay);
		}
	}
}

// loop through the menu items and add mouseOver and mouseOut events
for (i = 0; i < arMenus.length; i++) {
	// is the item a menu?
	if(arMenus[i].className.match(strDDMClass)) {
		// add mouseOver and mouseOut events
		arMenus[i].onmouseover = function() {
			clearTimeout(menuTimer);
		}
		arMenus[i].onmouseout = function() {
			menuTimer = setTimeout("hideMenu()",intDelay);
		}
	}
}

// Show the menu
function showMenu() {
	// clear all other menus
	clearMenus();
	// set the links mouseOver class
	var strCss = currentItem.className;
	strCss = strCss.replace(strMOClass,'');
	strCss = strCss + ' ' + strMOClass;
	currentItem.className = strCss;
	// make the menu visible
	document.getElementById(currentItem.rel).style.display = showMenuTag;
}

function hideMenu() {
	// remove the mouseOver class
	var strCss = currentItem.className;
	strCss = strCss.replace(strMOClass,'');
	currentItem.className = strCss;
	// hide the menu
	document.getElementById(currentItem.rel).style.display = hideMenuTag;
}

function clearMenus() {
	var strCss
	// stop any menu timers
	clearTimeout(menuTimer);
	// loop through the links and remove mouseOver class
	for(i = 0; i < arLinks.length; i++) {
		strCss = arLinks[i].className;
		strCss = strCss.replace(strMOClass,'');
		arLinks[i].className = strCss;
	}
	// loop through the menus and hide them
	for(i = 0; i < arMenus.length; i++) {
		// check that it is a menu container and then hide it if it is
		if(arMenus[i].className.match(strDDMClass)){
			arMenus[i].style.display = hideMenuTag;
		}
	}
}

function parseArray(arInput) {
	var oOutput = {};
	for(var i=0;i<arInput.length;i++) {
		// is the item a drop down menu?
		if(arInput[i].className.match(strDDMClass)) {
			// add it's ID to the object
			oOutput[arInput[i].id]='';
		}
	}
	// output the object
	return oOutput;
}

