/*
 * Menu methods.
 */

/*
 * Menu class definition.
 */
Menu = function() {}

/*
 * Set up the mouseover and mouseout methods.
 */
Menu.setup = function( divName )
{
	var top = dojo.byId( divName ),
		as = top ? dojo.query( "a", top ) : null;
	
	dojo.forEach( as, function( a ) {
		var table = dojo.byId( "sub" + a.id );

		/*
		 * I tried putting the mouseout and mouseover handlers on the tables,
		 * but there was no mouseout message when the mouse moved off the side.
		 */
		if( table )
		{
			dojo.forEach( dojo.query( "tr", table ), function( tr ) {
				dojo.connect( tr, "onmouseover", function( e ) {
					dojo.stopEvent( e );
					Menu.stopTimer();
				} );
				
				dojo.connect( tr, "onmouseout", function( e ) {
					dojo.stopEvent( e );
					Menu.startTimer();
				} );
				
				dojo.connect( tr, "onclick", function( e ) {
					Menu.hide();
				} );
			});
			
			dojo.connect( a, "onmouseover", function( e )
			{
				Menu.hide();
	
				var c = new cts.Coords( a.parentNode );
	
				dojo.style( table, "position", "absolute" );
				dojo.style( table, "left", ( c.left - 13 ) + "px" );
				dojo.style( table, "top", ( c.top + c.height ) + "px" );
				dojo.style( table, "display", "block" );
				
				Menu.popup = table;
				dojo.stopEvent( e );
			} );
			
			dojo.connect( a, "onmouseout", function( e )
			{
				Menu.startTimer();
				dojo.stopEvent( e );
			} );
			
			dojo.connect( a, "onclick", function( e )
			{
				Menu.hide();
			} );
		}
	});
}

/*
 * Start the timer that will hide the popup.
 */
Menu.startTimer = function()
{
	Menu.stopTimer();
	
	Menu.timer = setTimeout( function() {
		if( Menu.popup )
		{
			Menu.hide();
		}
	}, Menu.delay );
}

/*
 * Hide the menu.
 */
Menu.hide = function()
{
	Menu.stopTimer();
	
	if( Menu.popup )
	{
		dojo.style( Menu.popup, "display", "none" );
		Menu.popup = null;
	}
}

/*
 * Stop the timer.
 */
Menu.stopTimer = function()
{
	if( Menu.timer )
	{
		clearTimeout( Menu.timer );
		Menu.timer = null;
	}
}

Menu.popup = null;
Menu.timer = null;
Menu.delay = 250;
