//*****************************************************************************
// Site menu code.
//
// Note: Requires common.js.
//*****************************************************************************

// Used to track the currently active menu.
var activeMenu;

//-----------------------------------------------------------------------------
// Opens the designated menu.
//-----------------------------------------------------------------------------
function openMenu(linkEl, id)
{
	// Close any currently active menu.
	closeMenu();

	// Get the named menu and initialize it, if not already done.
	var menuEl = document.getElementById(id);
	if (menuEl.isInitialized == null)
		initializeMenu(menuEl, linkEl);

	// Position the menu and make it visible.
	pt = getPageOffset(linkEl);
	menuEl.style.left = (pt.x - 1) + "px";
	menuEl.style.top  = (pt.y + linkEl.offsetHeight) + "px";
	menuEl.style.visibility = "visible";

	// If the menu has an underlying IFRAME (IE browsers), position, size and
	// display it.
	if (menuEl.iframeEl != null)
	{
		menuEl.iframeEl.style.left = menuEl.style.left;
		menuEl.iframeEl.style.top  = menuEl.style.top;
		menuEl.iframeEl.width  = menuEl.offsetWidth + "px";
		menuEl.iframeEl.height = menuEl.offsetHeight + "px";
		menuEl.iframeEl.style.display = "";
	}

	// Mark this menu as the active one.
	activeMenu = menuEl;

	return false;
}

//-----------------------------------------------------------------------------
// Closes the currently active menu.
//-----------------------------------------------------------------------------
function closeMenu()
{

	// Exit if there is no active menu.
	if (activeMenu == null)
		return;

	// Make the active menu invisible.
	activeMenu.style.visibility = "";

	// If the menu has an underlying IFRAME (IE browsers), hide it as well.
	if (activeMenu.iframeEl != null)
		activeMenu.iframeEl.style.display = "none";
}

//-----------------------------------------------------------------------------
// Initializes a menu.
//-----------------------------------------------------------------------------
function initializeMenu(menuEl, linkEl) {

	// Handle special IE problems.
	if (document.all != null && window.opera == null)
	{
		// Create an IFRAME element to place under the menu DIV. This will prevent
		// SELECT elements and other windowed controls from bleeding through.
		var iframeEl = document.createElement("IFRAME");
		iframeEl.frameBorder = 0;
		iframeEl.src = "javascript:;";
		iframeEl.style.display = "none";
		iframeEl.style.position = "absolute";
		iframeEl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
		menuEl.iframeEl = menuEl.parentNode.insertBefore(iframeEl, menuEl);

		// Fix the hover problem by setting an explicit width on first item of
		// the menu.
		var linkEls = menuEl.getElementsByTagName("A");
		if (linkEls.length > 0)
		{
			var w = linkEls[0].offsetWidth;
			linkEls[0].style.width = w + "px";
			dw = linkEls[0].offsetWidth - w;
			w -= dw;
			linkEls[0].style.width = w + "px";
		}
	}

	// Set event handlers for the menu and it's opener link.
	menuEl.onmouseout = menuMouseout;
	linkEl.onmouseout = menuLinkMouseout;

	// Mark the menu as initialized.
	menuEl.isInitialized = true;
}

//-----------------------------------------------------------------------------
// Handles a mouseout on a menu.
//-----------------------------------------------------------------------------
function menuMouseout(event)
{
	var current, related;

	try {
		if (window.event) {
			current = this;
			related = window.event.toElement;
		}
		else {
			current = event.currentTarget;
			related = event.relatedTarget;
		}

		// If the mouse has moved off the menu, close it.
		if (current != related && !contains(current, related))
			closeMenu();
	}
	catch (ex) {}
}

//-----------------------------------------------------------------------------
// Handles a mouseout on a menu bar link.
//-----------------------------------------------------------------------------
function menuLinkMouseout(event)
{
	var current, related;

	try
	{
		if (window.event)
		{
			current = this;
			related = window.event.toElement;
		}
		else
		{
			current = event.currentTarget;
			related = event.relatedTarget;
		}

		// If the mouse has moved off the menu bar link but not onto the menu, 
		// close it.
		if (current != related && related != activeMenu && !contains(activeMenu, related))
			closeMenu();
	}
	catch (ex) {}
}

//-----------------------------------------------------------------------------
// Determines if one element contains another.
//-----------------------------------------------------------------------------
function contains(a, b)
{
	// Return true if element b is within element a.
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;

	return false;
}

