//-----------------------------------------------------------------------------
// menu01.js
//
// customizable drop down menu support
// Version 1
//
// Authors:	Stephen Detwiler
//-----------------------------------------------------------------------------

var posRight = 1;
var posLeft = 2;
var posTop = 4;
var posBot = 8;

var menuState = 0;

var activeMenu = null;

var menuOptionFocusClass = "";
var menuHeadingFocusClass = "";

// Added this so that we can set to false when the 
// menus are mouse-over activated so Steve's menustate logic
// is ignored
var ignoreMenuState = false;

bName = navigator.appName;
if (bName == "Netscape")
	document.captureEvents(Event.CLICK);	// capture the click event for netscape

document.onclick = CheckClick;
function CheckClick( )
{
	// hideMenu gets called immediately after opening a menu
	// this ensures we ignore the first call
	if( ignoreMenuState==false && menuState == 1 )
	{
		//alert( menuState );
		menuState = 0;
		return;
	}

	if( activeMenu != null )
	{
		activeMenu.state = 0;
		//alert( "4" );
		hideMenu( activeMenu );
		activeMenu = null;
	}
	
	if( window.ExtCheckClick )
	{
		ExtCheckClick();
	}
}

// call to set the css class to apply to a menu option when it has focus
function setMenuOptionFocusClass( c )
{
	menuOptionFocusClass = c;
}

// call to set the css class to apply to a menu heading when it has focus
function setMenuHeadingFocusClass( c )
{
	menuHeadingFocusClass = c;
}

// call to set focus on a menu option: OnMouseOver()
function onMenuOptionFocus( option )
{
	option.oldClass = option.className;
	option.className =menuOptionFocusClass;
}

// call to remove focus from a menu option: onMouseOut()
function onMenuOptionBlur( option )
{
	option.className=option.oldClass;
}

// call to set focus on the menu heading: onMouseOver()
function onMenuHeadingFocus( menu )
{
	if( menu.state != 1 )		// if menu isn't showing
		menu.oldClass = menu.className;
	menu.className=menuHeadingFocusClass;
}

// call to remove focus from the menu heading: onMouseOver()
function onMenuHeadingBlur( menu )
{
	if( menu.state != 1 )	// if menu isn't showing
		menu.className=menu.oldClass;
}


// called internally when menu heading is clicked: onClick()
function showMenu( menu, pos, w, h )
{
	menuState = 1;					// flag that we are opening a menu so hideMenu doesnt hide
	onMenuHeadingFocus( menu );
	
	name = menu.id + "menu";
	m = document.getElementById( name );
	
	if( m == null )
		return;
		
	thetop = 0;
	theleft = 0;

	// optional padding offset
	if( w )
		theleft+=w;
	if( h )
		thetop+=h;

	p = menu;

	if( pos&posRight )				// if menu should be to the right of the parent
		theleft+= p.offsetWidth;	// add the width of the parent to the left corner
	
	if( pos&posLeft )				// if menu should be to the left of the parent
		theleft-= m.offsetWidth;	// subtract the width of the menu from the left corner

	if( pos&posTop )				// if menu should be on top of the parent
		thetop-= m.offsetHeight;	// subtract the height of the menu from the top corner

	if( pos&posBot )				// if menu should be below parent
		thetop+=p.offsetHeight;		// add height off parent to top corner



	while( p.offsetParent )
	{
		thetop+= p.offsetTop;
		theleft+= p.offsetLeft;
		p = p.offsetParent;
	}

	m.style.top = thetop;
	m.style.left = theleft;
	m.style.visibility = "visible";
	m.style.bottom = "";
	
//	alert( m.style.visibility );
}

// called internally when menu loses focus
function hideMenu( menu )
{
	//alert( "hideMenu" );
	onMenuHeadingBlur( menu );
	name = menu.id + "menu";
	m = document.getElementById( name );
	
	if( m == null )
		return;
	
	m.style.visibility = "hidden";

}

// called when menu heading is clicked: onClick()
function onMenuHeadingClick( menu, pos, w, h )
{
	if( activeMenu != null )		// there is another menu open
	{
		if( activeMenu == menu )
		{
// Commented out 7/31/06 as it breaks the fAIS menu code.
//			activeMenu.state = 0;
//			hideMenu( activeMenu );
//			activeMenu = null;
			return;
		}

		activeMenu.state = 0;
		hideMenu( activeMenu );
		activeMenu = null;
	}
	
	//alert( menu.state );


	// show the menu
	if( (menu.state == null) || (menu.state == 0) || (menu.state == "undefined") )
	{
		activeMenu = menu;
		menu.state = 1;



		showMenu( menu, pos, w, h );
	}

	// hide the menu
	else
	{
		activeMenu = null;
		menu.state = 0;
		hideMenu( menu );
	}
}

// called when a menu option is clicked: onClick()
function gotoUrl( url )
{
	location.href = url;
}


function showLayer( layer, pos, w, h )
{
	name = layer.id + "layer";
	m = document.getElementById( name );

	thetop = 0;
	theleft = 0;

	// optional padding offset
	if( w )
		theleft+=w;
	if( h )
		thetop+=h;

	p = layer;

	if( pos&posRight )				// if menu should be to the right of the parent
		theleft+= p.offsetWidth;	// add the width of the parent to the left corner
	
	if( pos&posLeft )				// if menu should be to the left of the parent
		theleft-= m.offsetWidth;	// subtract the width of the menu from the left corner

	if( pos&posTop )				// if menu should be on top of the parent
		thetop-= m.offsetHeight;	// subtract the height of the menu from the top corner

	if( pos&posBot )				// if menu should be below parent
		thetop+=p.offsetHeight;		// add height off parent to top corner



	while( p.offsetParent )
	{
		thetop+= p.offsetTop;
		theleft+= p.offsetLeft;
		p = p.offsetParent;
	}

	m.style.top = thetop;
	m.style.left = theleft;
	m.style.visibility = "visible";
	m.style.bottom = "";
}
/*
function hideLayer( layer )
{
	name = layer.id + "layer";
	m = document.getElementById( name );
	m.style.visibility = "hidden";

}

function toggleLayer( layer, pos, w, h )
{
	if( event!=null )
		event.cancelBubble = true;

	// show the layer
	if( (layer.state == null) || (layer.state == 0) )
	{
		layer.state = 1;
		showLayer( layer, pos, w, h );
	}

	// hide the layer
	else
	{
		layer.state = 0;
		hideLayer( layer );
	}

}*/