/*
 * menuDropdown.js - implements an dropdown menu based on an HTML list
 * Author: Dave Lindquist (dave@gazingus.org)
 * Additions/changes: David Viner (post@davidviner.com)
 */

var currentMenu = null,
	killMenu = false,
	tm = false;

if (!document.getElementById)
	document.getElementById = function() { return null; }

function initializeMenu (menuId, actuatorId)
{
	var menu = document.getElementById (menuId);
	var actuator = document.getElementById (actuatorId);

	if (menu == null && actuator == null) return;

	actuator.onmouseover = function()
	{
		killMenu = false;
		if (tm) clearTimeout (tm);
		tm = false;

		if (currentMenu != this)
		{
			if (currentMenu)
			{
				currentMenu.style.visibility = "hidden";
			}

			this.showMenu();
		}
		else
		if (currentMenu)
		{
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		}

		return false;
	}

	actuator.onmouseout = function()
	{
		killMenu = true;
		tm = setTimeout ("timer()", 500);
	}

	actuator.showMenu = function ()
	{
		if (menu)
		{
			menu.style.left = findPosX (this) + "px";

			if (this.offsetTop < 5)	// IE
			{
				menu.style.top = "140px";
			}
			else	// Opera/Mozilla/Firefox
			{
				menu.style.top = (135 - this.offsetTop) + "px";
			}

//			menu.style.top = (findPosY (this) + 31 - this.offsetTop )+ "px";
//alert ("posY=" + findPosY (this) + "  offTop=" + this.offsetTop + " height=" + this.offsetHeight);
			menu.style.visibility = "visible";
			currentMenu = menu;
		}
	}

	if (menu == null) return;

	menu.onmouseout = function()
	{
		killMenu = true;
		tm = setTimeout ("timer()", 500);
	}

	menu.onmouseover = function()
	{
		killMenu = false;

		if (tm) clearTimeout (tm);
	}
}

function timer ()
{
	if (killMenu && currentMenu)
	{
		currentMenu.style.visibility = "hidden";
		currentMenu = null;
		clearTimeout (tm);
		tm = false;
	}
}

// findPosX/Y from: http://www.quirksmode.org/js/findpos.html

function findPosY (obj)
{
	var curtop = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else
	if (obj.y)
	{
		curtop += obj.y;
	}

	return curtop;
}

function findPosX (obj)
{
	var curleft = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else
	if (obj.x)
	{
		curleft += obj.x;
	}

	return curleft;
}

