

/* NAVIGATION */

/* These are global varialbes - needed for loading, showing, hiding, etc */
var headNodes;
var subMenus;
var lvl2Sub;
var mainUL;
var caption;
var timeouts;
var lvl2timeouts;
var marked2;
var resized;
var resized2;
var isIE;

/* Rearrange the menu - if browser doesnt support JS, then they will just see the UL */
function prepareMenu(divId)
{
	if(navigator.appName == "Microsoft Internet Explorer") isIE = true; else isIE = false;
	lvl2Sub = Array();
	timeouts = Array();
	resized = Array();
	resized2 = Array();
	lvl2timeouts = Array();
	marked2 = Array();
	caption = document.getElementById(divId);		// This is the DIV that contains the menu
	for(i = 0; i < caption.childNodes.length; i++)
		if(caption.childNodes[i].tagName == "UL")
			mainUL = caption.childNodes[i];
			
	if(mainUL.childNodes.length % 2 == 1) headNodes = new Array((mainUL.childNodes.length - 1) / 2);		// array to hold the list of all of the top-level entries
	else headNodes = new Array(mainUL.childNodes.length);
	var ct = 0;											// keeps track of how many LI nodes we have found so far

	// Go through the list and get all of the LI tags in it - these are the top list of nodes
	for(i = 0; i < mainUL.childNodes.length; i++)
	{
		if(mainUL.childNodes[i].tagName == "LI")
		{
			// Save reference to this menu title
			headNodes[ct++] = mainUL.childNodes[i];
			
			// Set the classname (saves designer from having to set it)
			mainUL.childNodes[i].className = "menuTitle";

			mainUL.childNodes[i].onmouseover = new Function("showDrop("+(ct - 1)+")");
			mainUL.childNodes[i].onmouseout = new Function("hideDrop("+(ct - 1)+")");
			
			// Assign classname to childless LI's
			if(mainUL.childNodes[i].getElementsByTagName("ul").length == 0) mainUL.childNodes[i].className = "noChildren";
			
			// Set the classname of each of the drop-down menus for this title
			for(j = 0; j < mainUL.childNodes[i].childNodes.length; j++)
			{
				if(mainUL.childNodes[i].childNodes[j].tagName == "UL")
				{
					mainUL.childNodes[i].childNodes[j].className = "dropMenu";
					
					// Go through each LI in this sub menu and add a mouseover event to it
					for(k = 0; k < mainUL.childNodes[i].childNodes[j].childNodes.length; k++)
						if(mainUL.childNodes[i].childNodes[j].childNodes[k].tagName == "LI")
						{
							// Check if it has any UL children
							var ar2 = mainUL.childNodes[i].childNodes[j].childNodes[k].getElementsByTagName("UL");
							if(ar2.length > 0)
							{
								var ul2 = ar2[0];
								if(!isIE) ul2.parentNode.marginBottom = "1px";
								var lip = mainUL.childNodes[i].childNodes[j].childNodes[k];
								var il = lvl2Sub.length;
								lvl2Sub[il] = ul2;
								ul2.style.display = "none";
								lip.className = "menuParent";
								ul2.className = "lvl2Menu";
								lip.onmouseover = new Function("showDrop2("+il+","+(ct - 1)+")");
								lip.onmouseout  = new Function("hideDrop2("+il+")");
								
								ul2.style.position = "absolute";
								ul2.style.left = lip.clientWidth + "px";
								ul2.style.top  = lip.offsetTop + "px";
								
								var ul2kids = ul2.getElementsByTagName("LI");
								for(mm = 0; mm < ul2kids.length; mm++)
								{
									if(isIE) 
									{
										ul2kids[mm].style.paddingTop = "0px";
									}
									ul2kids[mm].className = "menuItem";
								}
							}
							else
							{
								mainUL.childNodes[i].childNodes[j].childNodes[k].className = "menuItem";
							}
						}
				}
			}
		}
	}
	mainUL.style.display = "block";
}

function showDrop(ind) 
{
	try {
		clearTimeout(timeouts[ind]);
		headNodes[ind].getElementsByTagName('UL')[0].style.position = "absolute";
		
		var w = headNodes[ind].offsetLeft;
		var kidUL = headNodes[ind].getElementsByTagName('UL')[0];
		kidUL.style.left = w + "px";
		kidUL.style.top  = headNodes[ind].clientHeight + "px";
		kidUL.style.display = "block"; 
		var lis = headNodes[ind].getElementsByTagName("UL")[0].getElementsByTagName("LI");
		if(!resized[ind])
		{
			resized[ind] = true;
			var lastm = 0;
			for(m = 0; m < lis.length; m++)
				try	{
					if(lis[m].parentNode == headNodes[ind].getElementsByTagName("UL")[0])
						if(m == 0)
						{
							lis[m].style.width = lis[m].parentNode.clientWidth + "px";
							lis[m].getElementsByTagName("A")[0].style.width =  lis[m].style.width;
							lis[m].getElementsByTagName("A")[0].style.display = "block";
						}
						else
						{
							lis[m].style.width = lis[lastm].style.width;
							lis[m].getElementsByTagName("A")[0].style.width =  lis[lastm].style.width;
							lis[m].getElementsByTagName("A")[0].style.height =  lis[lastm].style.height;
							lis[m].getElementsByTagName("A")[0].style.display = "block";
							lastm = m;
						}
				}
				catch(e) { }
		}
	} catch(e) { } 
}

function hideDrop(ind) {
	timeouts[ind] = setTimeout("try { headNodes["+ind+"].getElementsByTagName('UL')[0].style.display = 'none'; } catch(e) { }", 50);
}


function showDrop2(ind, parent)
{
	try {
		clearTimeout(lvl2timeouts[ind]);
		clearTimeout(timeouts[parent]);
		
		var l = 0;
		var w = 0;
		var obj = lvl2Sub[ind].parentNode;
		if(isIE) obj.style.marginBottom = "-4px";
		lvl2Sub[ind].style.position = "absolute";
		lvl2Sub[ind].style.left    = (obj.style.width.replace("px","") - 1 + 10) + "px"; /* (obj.offsetLeft + obj.offsetWidth - 1) + "px"; */
		lvl2Sub[ind].style.top     = obj.offsetTop + "px";
		lvl2Sub[ind].style.background = "black";
		lvl2Sub[ind].style.display = "block";
	} catch(e) { }
}

function hideDrop2(ind)
{
	try {
		lvl2timeouts[ind] = setTimeout('if(isIE) lvl2Sub['+ind+'].parentNode.style.marginBottom = "-2px"; lvl2Sub['+ind+'].style.display = "none";', 50);
	} catch(e) { }
}

function init(divId)
{
	prepareMenu(divId);
}