// This file maintains javascript rollover menus

var activeSubmenu = null;
var activeSubmenuGuid = 1;
var activeSubmenuIndex = -1;
var submenuLineBreak = "<br>";
var disableSubmenus = false;
var menuData = new Array();

// Adds an element given by 'htmlStr' to the list of items in the menu.  'index'
// refers to the 'menuDataIndex' passed to showSubmenu.  Be sure 'index' begins
// with 0 and moves upward, jumps in numbers could cause memory usage problems
// (and ofcourse is a potiential incompatability with old browsers).
//
// Example:
// addMenuData(0, '<a href="google.com">Google</a>');
// addMenuData(0, '<a href="yahoo.com">Yahoo</a>');
function addMenuData(index, htmlStr)
{
	var shouldBreakLine = false;
	
	while(index >= menuData.length) {
		
		shouldBreakLine = false;
		menuData[menuData.length] = '';
	}
	
	shouldBreakLine = !(!shouldBreakLine && (menuData[index] == ''));
	
	if(shouldBreakLine)
		menuData[index] += submenuLineBreak;
	
	menuData[index] += htmlStr;
}

// Used by 'pollHideActiveSubmenu' internally.
function hideSubmenu(node)
{
	if(disableSubmenus)
		return;
	
	if(node == activeSubmenu)
		activeSubmenu = null;
	
	if(node["submenu"] == null)
		return;
	
	node.submenu.parentNode.removeChild(node.submenu);
	node.submenu = null;
}

// Used by 'releaseActiveSubmenu' internally.
function pollHideActiveSubmenu(guid, count)
{
	if(disableSubmenus)
		return;
	
	if(activeSubmenu == null)
		return;
	
	if(activeSubmenu.guid != guid) {
		
		return;
	}
	
	if(count != activeSubmenu.activityCount)
		return;
	
	hideSubmenu(activeSubmenu);
}

// Call this for 'onmouseout' events (will close the popup soon).
function releaseActiveSubmenu(guid)
{
	if(disableSubmenus)
		return;
	
	if(activeSubmenu == null)
		return;
	
	if(activeSubmenuGuid != guid)
		return;
	
	activeSubmenu.activityCount++;
	
	setTimeout("pollHideActiveSubmenu(" + guid + ", " + activeSubmenu.activityCount + ")", 333);
}

// Call this for 'onmouseover' events.
function lockActiveSubmenu(guid)
{
	if(disableSubmenus)
		return;
	
	if(activeSubmenu == null)
		return;
	
	if(activeSubmenuGuid != guid)
		return;
	
	activeSubmenu.activityCount++;
}

// 'node' refers to the div or whatever containing the link that is making this happen.  The submenu
// will be placed as the last child of this node.
// 
// 'menuDataIndex' refers to the 'index' passed to 'addMenuData'.  If 'addMenuData' is never
// called with the given index this function does nothing.
// 
// 'width' allows you to specify a specific.  If you do not pass a width the width will be
// automatically copied from the width of 'node'.
function showSubmenu(node, menuDataIndex, width)
{
	var div = null;
	
	if(activeSubmenu && activeSubmenuIndex == menuDataIndex) {
		
		lockActiveSubmenu(node.guid);
		
		return;
	}
	
	try {
		
		if(menuData.length <= menuDataIndex)
			return;
		
		if(menuData[menuDataIndex] == '')
			return;
		
		if(disableSubmenus)
			return;
		
		if(activeSubmenu)
			hideSubmenu(activeSubmenu);
		
		if(width == null)
			width = node.offsetWidth;
		
		node.activityCount = 0;
		node.guid = ++activeSubmenuGuid;
		
		div = document.createElement("div");
		
		div.className = "submenuFlyout";
		
		if(width != null)
			div.style.width = width + "px";
		
		div.parentMenu = node;
		
		div.innerHTML = '<div onmouseover="lockActiveSubmenu(' + node.guid + ')" onmouseout="releaseActiveSubmenu(' + node.guid + ')">'
			+ menuData[menuDataIndex]
			+ '</div>';
		
		var container = document.createElement("div");
		
		container.innerHTML = "<table cellspacing=0 cellpadding=0 style=\"border:0;padding:0;margin:0;\"><tr><td> </td></tr></table>";
		node.appendChild(container);
		container.getElementsByTagName("td")[0].appendChild(div);
		
		activeSubmenu = node;
		node.submenu = container;
		
		activeSubmenuIndex = menuDataIndex;
	}
	catch(e) {
		disableSubmenus = true;
		
		// This browser sucks, try to undo the damage!
		try {
			if(div)
				if(div.parentNode)
					div.parentNode.removeChild(div);
		}
		catch(e) {
			
		}
	}
}
