/**********************************************************************
 * Menu bar implementation module.
 * 
 *   Note: This module is mainly a slightly modified version of the
 *         SmartMenu v1.1 (30 July 1999) by Constantin Kuznetsov Jr.
 * 
 * Marcin Struzak (C) 2003
 *
 * Version 1.1.1
 *   to do:
 *     * investigate why Kuznetsov's version does not need a <div> 
 *       in the submenu
 *     * [long term] rewrite to use ABT objects for setup, then 
 *       generate string when calling showMenu()
 * 
 * History:
 * ms  2003-01-15 Fixed hotspot calculation when page is scrolled
 * ms  2003-01-14 Added support for NS6+; largely inspired by ver. 2.1
 *                of static menus by C.Kuznetsov Jr. (featured on
 *                DynamicDrive.com)
 * ms  2003-01-10 Fixed NS side of the script; 
 *                tested with NS4.51 (Linux)
 * ms  2003-01-06 Initial version adapted from static menus by 
 *                C.Kuznetsov Jr.; tested with IE6 (Win)
 *
 **********************************************************************/

/**********************************************************************
 * Constants
 */
menuBgOnColor  = "#999966" ;  // menu item bg color when selected
menuBgOffColor = "#336666" ;  // menu item bg color when not selected

/***********************************************************************
 * Menu class constructor.
 */
function Menu(){

	if ( ie ) 
		this.menuFont = "bold x-small Verdana";
	if ( n ) 
		this.menuFont = "bold small Verdana";

	// declare class methods
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;

	// declare class attributes	
	this.bgColor           = menuBgOffColor ;	
	this.mainPaneBorder    = 0;
	this.subMenuPaneBorder = 0;

	// define the menu string
	HTMLstr = "<!-- MENU PANE DECLARATION BEGINS -->\n" +
	          "\n" +
	          (( ie || ns6 ) ? "<div id='MainTable'>\n" : "" ) +
			  "<table bgcolor='" + this.bgColor + "' border='" + this.mainPaneBorder + "' cellpadding=0 cellspacing=0>\n" +
			  "<tr>\n" +
//			  ( n ? "<td nowrap align=left width=800>\n" : "" ) +
			  "<!-- MAIN MENU STARTS -->\n" +
			  "<!-- MAIN_MENU -->\n" +
			  "<!-- MAIN MENU ENDS -->\n" +
//			  ( n ? "</td>\n" : "" ) +
			  "</tr>\n" +
			  "</table>\n" +
			  "\n" +
			  "<!-- SUB MENU STARTS -->\n" +
			  "<!-- SUB_MENU -->\n" +
			  "<!-- SUB MENU ENDS -->\n" +
			  (( ie || ns6 ) ? "\n</div>\n" : "" ) +
			  "\n" +
			  "<!-- MENU PANE DECALARATION ENDS -->\n";
}

/***********************************************************************
 * Add a top menu item.
 */
function addItem( idItem, text, hint, location ){

	var Lookup = "" ;
	var MENUitem = "" ;
	
	Lookup = "<!-- ITEM " + idItem + " -->" ;
	if ( HTMLstr.indexOf( Lookup ) != -1 )
	{
		alert( idParent + " already exist" ) ;
		return;
	}
	
	// declaration of a menu item
	MENUitem = "<!-- ITEM " + idItem + " -->\n";
	
	if ( n ){
		MENUitem += "<td width=\"10\">&nbsp;</td><td>" +
		            "<ilayer name=" + idItem + " bgcolor='" + this.bgColor + "'>" +
		            "<a href='" + ( location != null ? location : "." ) + "' class=clsMenuItemNS onmouseover=\"displaySubMenu('" + idItem + "')\" " +
			    ( location == null ? "onclick=\"return false;\"" : "" ) + ">" + 
					text +
					"</a>" +
					"</ilayer>\n" +
					"</td><td width=\"10\">&nbsp;</td>" +
		            "<td bgcolor=#ffffff><img src=images/spacer.gif></td>\n" ;
	}

	if ( ie || ns6 ){
		MENUitem += "<td id='" + idItem + "' " + 
		                 "onmouseover=\"menuItemOn( this ); displaySubMenu( '" + idItem + "' )\" " + 
						 "onmouseout=\"menuItemOff( this )\" " +
						 ( location != null ? "onclick=\"location.href = '" + location + "'\" " : "" ) +
						 ( hint != null ? "title='" + hint + "' " : "" ) +
		                 "style=\"cursor: hand\">\n" + 
		            "<table cellspacing='0' cellpadding='0' border='0'><tr>\n" +
					"<td width=10></td>\n" +
					"<td class=clsMenuItemIE>\n" + 
						text +
					"</td>\n" +
					"<td width=10></td>\n" + 
					"</tr></table></td>\n" +
					"<td bgcolor=#ffffff><img src=images/spacer.gif></td>\n";
	}
	
	MENUitem += "<!-- END OF ITEM " + idItem + " -->\n\n" +
	            "<!-- MAIN_MENU -->";

	// place the menu item in the main string
	HTMLstr = HTMLstr.replace( "<!-- MAIN_MENU -->", MENUitem ) ;
//	alert( HTMLstr );
}

/***********************************************************************
 * Add a submenu item.
 */
function addSubItem( idParent, text, hint, location ){

	var Lookup   = "" ;
	var MENUitem = "" ;

	Lookup = "<!-- ITEM " + idParent + " -->" ;
	if ( HTMLstr.indexOf( Lookup ) == -1 ){
		alert( idParent + " not found" ) ;
		return;
	}
	
	Lookup = "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->" ;
	if ( HTMLstr.indexOf( Lookup ) == -1 ){
		// first item of the submenu: intialize the submenu table
		if ( n ){
			MENUitem += "\n" +
			            "<layer name='" + idParent + "submenu' visibility=hide><div>\n" +
						"<table border='" + this.subMenuPaneBorder + "' bgcolor='" + this.bgColor + "' cellpadding=0 cellspacing=0>\n" +
						"<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n" +
						"</table>\n" +
						"</div></layer>\n" +
						"\n";
		}
		if ( ie || ns6 ){
			MENUitem += "\n" +
			            "<div id='" + idParent + "submenu' style='position:absolute; visibility: hidden; top: -300;'>\n" +
						"<table border='" + this.subMenuPaneBorder + "' bgcolor='" + this.bgColor + "' cellpadding='0' cellspacing='0'>\n" +
						"<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n" +
						"</table>\n" +
						"</div>\n" +
						"\n";
		}
		
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace( "<!-- SUB_MENU -->\n", MENUitem ) ;
	}

	// fill out the actual menu item definition.
	if ( n ){
		MENUitem = "<tr><td width=10>&nbsp;</td><td><a class=clsSubMenuItemNS " +
		                       ( hint != null ? "title='" + hint + "' " : "" ) +
							   "href='" + location + "'>" + 
					text + 
					"</a></td><td width=10>&nbsp;</td></tr>\n"; 				 
	} else if ( ie || ns6 ){
		MENUitem = "<tr><td onmouseover=\"menuItemOn( this )\" " +
		                   "onmouseout=\"menuItemOff( this )\" " +
						   "onclick=\"location.href = '" + location + "'\" " + 
						   ( hint != null ? "title='" + hint + "' " : "" ) + 
						   "style=\"cursor: hand\">\n" + 
		           "<table cellspacing=0 cellpadding=0 border=0><tr>\n" + 
				   "<td width=10></td><td class=clsSubMenuItemIE>" + text + "</td><td width=10></td>\n" + 
				   "</tr></table></td></tr>\n";
	}

	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);
//	alert( HTMLstr );
}

/***********************************************************************
 * Display the main menu panel here.
 */
function showMenu(){
	document.writeln( HTMLstr ) ;
}

/***********************************************************************
 * Private declarations.
 */
function menuItemOn( obj ){
	obj.style.backgroundColor = menuBgOnColor ; 
}

function menuItemOff( obj ){
	obj.style.backgroundColor = menuBgOffColor ; 
}

function displaySubMenu(idMainMenu)
{
	var menu;
	var submenu;

	// hide any open menus
	hideLastMenu() ;

	if ( n ){
		submenu = document.layers[ idMainMenu + "submenu" ] ;
		submenu.left = document.layers[ idMainMenu ].pageX - 10 ;
		submenu.top  = document.layers[ idMainMenu ].pageY + 
		               document.layers[ idMainMenu ].clip.height + 5 ;
		submenu.visibility = fShow ;

		// calculate the hot area; close menu if cursor moves outside
		leftX  = submenu.left ;
		rightX = Math.max( leftX + document.layers[ idMainMenu ].clip.width,
		                   leftX + document.layers[ idMainMenu + "submenu" ].clip.width );
		topY  = document.layers[ idMainMenu ].pageY ;
		bottomY = document.layers[ idMainMenu + "submenu" ].top + 
		          document.layers[ idMainMenu + "submenu" ].clip.height ;
		/* alert( "menu:\t" + idMainMenu + "\n" +
		       "pageX:\t" + document.layers[ idMainMenu ].pageX + "\n" +
		       "pageY:\t" + document.layers[ idMainMenu ].pageY + "\n" +
			   "hotL:\t" + leftX + "\n" +
			   "hotR:\t" + rightX + "\n" +
			   "hotT:\t" + topY + "\n" +
			   "hotB:\t" + bottomY + "\n" ); */
	} else if ( ie || ns6 ){
		menu    = ( ie ? eval( idMainMenu ) : document.getElementById( idMainMenu )) ;
		submenu = ( ie ? eval( idMainMenu + "submenu" ) : document.getElementById( idMainMenu + "submenu" )) ;
		submenu.style.left = calculateSumOffset(menu, 'offsetLeft');
		submenu.style.top  = calculateSumOffset(menu, 'offsetTop') + menu.offsetHeight + 1 ;
		submenu.style.visibility = fShow;

		// calculate the hot area; close menu if cursor moves outside
		leftX   = ( ie ? submenu.style.posLeft - document.body.scrollLeft : submenu.style.left );
		rightX  = Math.max( leftX + menu.offsetWidth,
		                    leftX + submenu.offsetWidth ) ;
		topY    = ( ie ? submenu.style.posTop - document.body.scrollTop : 
		                 submenu.style.top ) - menu.offsetHeight - 1 ;
		bottomY = ( ie ? submenu.style.posTop - document.body.scrollTop : 
		                 submenu.style.top ) + submenu.offsetHeight ;
	}
	
	lastMenu = submenu;
}

function hideLastMenu(){
	if ( lastMenu != null ) {
		if ( n ){
			lastMenu.visibility = fHide;
		}
		if ( ie || ns6 ){
			lastMenu.style.visibility = fHide;
			lastMenu.style.left = 0;
		}
		lastMenu = null ;
	}
}

function calculateSumOffset( idItem, offsetName ){

	var totalOffset = 0;
	var item = eval( 'idItem' );

	do {
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	
	return totalOffset;
}

function updateIt( e ){
	
	var x ;
	var y ;
	
	if ( n || ns6 ){
		x = e.pageX;
		y = e.pageY;
	}
	if (ie){
		x = window.event.clientX;
		y = window.event.clientY;
	}

	if ( x > rightX || x < leftX || 
	     y > bottomY || y < topY ){
		hideLastMenu();
	}
}

/***********************************************************************
 * Initialization code.
 */

// Global variables 
var lastMenu = null ;  // references last open menu for closing
var leftX   = 0 ;      // left side of hot area
var rightX  = 0 ;      // right side of hot area
var topY    = 0 ;      // top side of hot area
var bottomY = 0 ;      // bottom side of hot area

// Browser detection 
if ( document.all ) {
	// internet explorer
	n = 0 ;	ie = 1 ; ns6 = 0 ;
	fShow = "visible" ; fHide = "hidden" ;
} else if ( document.layers ) {
	// netscape prior to 6
	n = 1 ; ie = 0 ; ns6 = 0 ;
	fShow = "show" ; fHide= "hide";
} else if ( document.getElementById && !document.all ){
	// netscape 6 and later
	n = 0 ; ie = 0 ; ns6 = 1 ;
	fShow = "visible" ; fHide = "hidden" ; 
}

// Make sure that errors on the page will not create unnecessary alerts.
window.onerror = new Function("return true")

// Initialize default handlers
if ( ie || ns6 ){
	document.body.onclick     = updateIt ;
	document.body.onscroll    = hideLastMenu ;
	document.body.onmousemove = updateIt ;
}
if ( n ){
	document.onmousedown = hideLastMenu ;
	window.captureEvents( Event.MOUSEMOVE ) ;
	window.onmousemove = updateIt ;
}

/* EOF *****************************************************************/
