var dropDownMenuID = "DropDownMenu";
var dropDownMenuContentID = "DropDownMenuContainer";

var closeDelay = 200;

var closeTimer;

function showDropDownMenu(elem, contentToShow)
{
	showDropDownMenuWithOffset(elem, contentToShow, 4);
}

function showDropDownMenuWithOffset(elem, contentToShow, offsetX, offsetY)
{
	// -- Clear the close timeout
	clearTimeout(closeTimer);
	// -- Reset the menu first, just to be safe
	hideDropDownMenu();
	
	offsetX = offsetX || 0;
	offsetY = offsetY || 0;

	// -- Position the menu
	var linkX = findPosX(elem);
	var linkY = findPosY(elem);
	var linkHeight = elem.offsetHeight;

	document.getElementById(dropDownMenuID).style.left = (linkX - offsetX) + "px";
	document.getElementById(dropDownMenuID).style.top  = (linkY + linkHeight - offsetY) + "px";
	
	// -- Show the menu
	document.getElementById(dropDownMenuID).style.visibility = "visible";
	document.getElementById(dropDownMenuID).style.zIndex = 150;

	// -- Put the content into the menu
	document.getElementById(dropDownMenuContentID).innerHTML = document.getElementById(contentToShow).innerHTML;
}

function hideDropDownMenu()
{
	// -- Clear the close timeout
	clearTimeout(closeTimer);

	// -- Clear the menu content
	document.getElementById(dropDownMenuContentID).innerHTML = "";

	// -- Hide the menu
	document.getElementById(dropDownMenuID).style.visibility = "hidden";
}

function closeDropDownMenu()
{
	closeTimer = setTimeout("hideDropDownMenu()", closeDelay);
}

function mouseOverMenu()
{
	// -- Clear the close timeout
	clearTimeout(closeTimer);
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1)
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}