var areMenusAvailable		= false;
var currentMenu				= null;
var currentMenuRef			= null;
var currentMenuTimeoutID	= null;


/***** functions called via events *****/

// called when the body is clicked on
function handleBodyClick( ) {
	handleMenuDisappearance( currentMenu, currentMenuRef );
}

// called when a person presses a key
function handleBodyKeyUp( ) {
	handleMenuDisappearance( currentMenu, currentMenuRef );
}

// called on mouse events over the menu root
function handleMouseOverMenuRoot( aMenuName, aMenuRefName ) {
	requestShowMenu( aMenuName, aMenuRefName );
}

// called when the mouse is brought over a menu item
function handleMouseOverMenuItem( aMenuItemName ) {
	requestCurrentMenuTimerReset( );
}

// function handle
function handleMenuDisappearance( aMenuName, aMenuRefName ) {
	requestHideMenu( aMenuName );
}

/***** functions called to complete work *****/

// if menus are loaded, will close current menu and display new one relative the name of the id specified
function requestShowMenu( aMenuName, aMenuRefName ) {
	if( areMenusAvailable && aMenuName != null ) {
		if( currentMenu != null ) {
			handleMenuDisappearance( currentMenu, currentMenuRef );
		}
		showMenu( aMenuName, aMenuRefName );
		currentMenu = aMenuName;
		currentMenuRef = aMenuRefName;
		currentMenuTimeoutID = window.setTimeout( "handleMenuDisappearance( '" + currentMenu + "', '" + currentMenuRef + "' )", 7500 );
	}
}

// if the current menu is up, resend for a new timer
function requestCurrentMenuTimerReset( ) {
	if( currentMenu != null ) {
		window.clearTimeout( currentMenuTimeoutID );
		currentMenuTimeoutID = window.setTimeout( "handleMenuDisappearance( '" + currentMenu + "', '" + currentMenuRef + "' )", 7500 );
	}
}


// hides the requested menu (doing all properly checks)
function requestHideMenu( aMenuName ) {
	if( areMenusAvailable && aMenuName != null ) {
		if( aMenuName == currentMenu ) {			
			hideMenu( aMenuName );
			window.clearTimeout( currentMenuTimeoutID );
			currentMenu = null;
			currentMenuRef = null;
			currentMenuTimeoutID = null;
		} else {
			hideMenu( aMenuName );
		}
	}
}


// called to notify that menus are available for displaying, etc.
function setMenusAvailable( areAvailable ) {
	areMenusAvailable = areAvailable;
}

/***** low-level functions for manipulating menus, etc *****/
 
// takes the menu id and changes the style to display it
function showMenu( aMenuName, aMenuRef ) {
	if( document.layers == null ) {
		var menuStyle;
		
		if( document.all ) {
			menuStyle = document.all[ aMenuName ].style;
		} else {
			menuStyle = document.getElementById( aMenuName ).style;
		}
		
		if( aMenuRef != null ) {
			var element;
			
			if( typeof( aMenuRef ) == "object" ) {
				element = aMenuRef;
			} else {
				element = document.getElementById( aMenuRef )
			}
			
			menuStyle.left = ( calculateLeftOffset( element ) - 2 ) + "px";
			menuStyle.top = ( calculateTopOffset( element ) + element.offsetHeight ) + "px";
		}
		menuStyle.visibility = "visible";
	}
}

 // takes the menu id and changes the style to hide it
function hideMenu( aMenuName ) {
	if( document.layers == null ) {
		var menuStyle;
		
		if( document.all ) {
			menuStyle = document.all[ aMenuName ].style;
		} else {
			menuStyle = document.getElementById( aMenuName ).style;
		}
		
		menuStyle.visibility = "hidden";
	}
}

/***** functions used to assist the manipulation of menus *****/

// to calculate base screen coords, start with the element and move out
function calculateTopOffset( anElement ) {
	var offset	= 0;
	var parent	= null;
	
	offset = anElement.offsetTop;
	parent = anElement.offsetParent;
	
	while( parent != null ) {
		offset += parent.offsetTop;
		parent = parent.offsetParent;
	}
	
	return offset;
}

// to calculate base screen coords, start with the element and move out
function calculateLeftOffset( anElement ) {
	var offset	= 0;
	var parent	= null;
	
	offset = anElement.offsetLeft;
	parent = anElement.offsetParent;
	
	while( parent != null ) {
		offset += parent.offsetLeft;
		parent = parent.offsetParent;
	}
	
	return offset;
}