/*
$Id: lc1_menus.js,v 1.2 2007/04/06 19:17:31 pmorrill Exp $
*/

var 	browserType = 'unknown';
var 	doc = '',sty = '',doc1 = '';
var	submenus;

//
// for some reason, the browser <body> style cannot be reliable queried below, so these
// values need to be set to the top and left margins respectively, as set in that class
//
var 	browserCorrectionVertical = 0;
var 	browserCorrectionHorizontal = 0;
var 	cellBorderCorrectionWidth = 0;

//
// determine which type of browser the client has
//
if (document.getElementById) {
	doc = "document.getElementById('";
	doc1 = "')";
	sty = "').style";
	browserType = 'n6';
} else if (document.layers) {
	doc = "document.";
	browserType = 'n4';
} else if (document.all) {
	doc = "document.all.";
	sty = ".style";
	browserType = 'ie';
}

//
// if the browser is not IE, we lower the correction by one
// to accomodate the borders on our top-level menu cells
//
//if (!document.all) cellBorderCorrectionWidth = -1;

var delay = 100;
var menu_close_timeout = Array();
var active_m = new Array();

//var consoleCom = Components.classes['@mozilla.org/consoleservice;1'];

//
// place the division on the page (absolute coordinates only)
//
function placeIt(elem,leftPos,topPos) {
	docObj = get_object('sub_'+elem,1);
	if ( docObj ) {
		if (browserType == 'n4' || browserType == 'n6') {
			docObj.left = leftPos+'px';
			docObj.top= topPos+'px';
		} else if (browserType == 'ie') {
			docObj.pixelLeft = leftPos+'px';
			docObj.pixelTop = topPos+'px';
		}
	}
}

//
// set the background colour of a menu table cell
//
function set_active(elem,cname) {
	var cellObj = get_object('cell_'+elem,0);
	if ( cellObj ) { cellObj.className = cname; }
//	else alert("Failed to locate elem "+elem);
}

//
// sets the visibility of this object, and checks to cancel any
// pending close sub-menu calls
//
function set_visible(item,on) {
	divObj = get_object('sub_'+item,1);
	if ( divObj ) { 
		if ( on == 1 ) cancel_close('cell_'+item);
		divObj.visibility = (on == 1 ? 'visible' : 'hidden');
	}
}


//
// position and make sub-table visible
//
function fnctTopMenuHover(item,setclass) {
	if ( setclass == 1 ) set_active(item,'horMenuTopHover');
	set_visible(item,1);
}

//
// cursor has entered a sub-menu cell: we set the cell visble, as well as all parent divs
//
function fnctSubMenuHover(item) {
	//
	// if this sub-menu has a parent set, we make sure it is set as
	// visible and hovered
	//
	if ( parent_div_ids[item] ) {
		var pObj = get_object('cell_'+parent_div_ids[item]);
		var rgx = /cell_(\d+)/;
		while ( pObj ) {
			var parent_id = (pObj.id).replace(rgx,'$1');
			var pID = parent_div_ids[parent_id];
			//
			// if pID exists the pObj is a submenu, else it is a top-level menu
			//
			set_active(parent_id,(pID ? 'subMenuItem' : 'horMenuTop')+(active_cell == parent_id ? 'Selected' : 'Open'));
			set_visible(parent_id,1);
			if ( !pID ) break;
			pObj = get_object('cell_'+parent_div_ids[parent_id]);
		}
	}
	set_active(item,(item == active_cell ? 'subMenuItemSelected' : 'subMenuItemHover'));
	set_visible(item,1);
}

//
// mouse is moving out of a sub-menu cell: we reset the class on parent items
// then call a close on the menus found to be linked to this one
//
function fnctSubMenuOut(item) {
	if ( parent_div_ids[item] ) {
		var pObj = get_object('cell_'+parent_div_ids[item]);
		var rgx = /cell_(\d+)/;
		if ( pObj ) fnctHideSubMenu(parent_div_ids[item]);
		while ( pObj ) {
			var parent_id = (pObj.id).replace(rgx,'$1');
			var pID = parent_div_ids[parent_id];
			set_active(parent_id,(pID ? 'subMenuItem' : 'horMenuTop')+(active_cell == parent_id ? 'Selected' : 'Normal'));
			if ( !pID ) break;
			pObj = get_object('cell_'+parent_div_ids[parent_id]);
			if ( pObj ) fnctHideSubMenu(parent_div_ids[parent_id]);
		}
	}
	set_active(item,(item == active_cell ? 'subMenuItemSelected' : 'subMenuItemNormal'));
	fnctHideSubMenu(item);
}

//
// hide all of the sub-menus
//
function fnctCloseAllMenus() {
	if ( !submenus ) return;
	for ( i = 0; i < submenus.length; i++ ) { set_visible(submenus[i],0); }
}

//
// hide all of the sub-menus
//
function fnctCloseMenu(item) {
	set_visible(item,0);
}

function fnctHideSubMenus() {
	fnctCloseAllMenus();
}

//
// delayed hide function: create an associative array of timeout objects
//
function delayed_hide(item) {
	menu_close_timeout['cell_'+item] = setTimeout('set_visible(\''+item+'\',0)',delay);
}

//
// hide an individual menu, possibly with a delay
//
function fnctHideSubMenu(item) {
	if ( delay > 0 ) return delayed_hide(item);
	set_visible(item,0);
}

function cancel_close(item) {
	if ( menu_close_timeout[item] ) clearTimeout(menu_close_timeout[item]);
}

//
// place all the submenus in the global array: note that
// the visibility is set previously, with a static class attribute 
// in the div element itself, therefore it is assumed to be 'hidden'
//
function menuInitialize() {
	if ( !submenus ) return;

	//
	// first disable the hover changes on the nested links in each top-menu (they
	// are only there for non-javascript browsers, which this is not
	//
	for ( i = 0; i < menus.length; i++ ) {
		var linkObj = get_object('link_'+menus[i]);
		if ( linkObj ) linkObj.className = 'horMenuTopJ';
	}

	var parent_divs = new Array;
	
	//
	// set global window size variables
	//
	win_size();

	//
	// find the horizontal menu and proceed
	//
	var divObj = get_object('horizontal_menu');
	if ( !divObj ) { return; }

	var main_top = get_absolute_menu_top() + divObj.offsetHeight + browserCorrectionVertical;
	var main_left = get_absolute_menu_left() + divObj.offsetLeft + browserCorrectionHorizontal;

	//
	// now inspect the submenus
	//
	for ( i = 0; i < submenus.length; i++ ) {
		//
		// these are the *absolute* coordinates of the <div> element
		//
		var pageoffsetTop = main_top;
		var pageoffsetLeft = main_left;
			
		//
		// get the submenu's parent cell element and proceed
		//
		var cellObj = get_object('cell_'+submenus[i]);
		if ( !cellObj ) { continue; }

		//
		// see if this cell's parent division has been placed
		//
		var p = parent_div_ids[submenus[i]];
		if ( p ) {
			//
			// a parent sub-menu cell exists: it's containing <div>
			// element *should* be already placed so use its coords
			// for the current offsets
			//
			var pDivCoords = parent_divs[p];
			
			if ( pDivCoords ) {
				pageoffsetLeft = pDivCoords[0] + cellObj.offsetWidth - 1;

				//
				// make sure that the page space is not being exceeded
				//
				var newDivObj = get_object('sub_'+submenus[i]);
				if ( pageoffsetLeft + newDivObj.offsetWidth > winRight ) { pageoffsetLeft = pDivCoords[0] - cellObj.offsetWidth - 0; }
				pageoffsetTop = pDivCoords[1] + cellObj.offsetTop + 2;
			}
		} else {
			//
			// get the cell styles - this does not work! (want to query the border width but
			// it is not being set by all browsers
			//
//			var cellStyle = get_object('cell_'+submenus[i],1,1);

			//
			// get the table and set the width appropriately
			//
			var tableObj = get_object('table_'+submenus[i]);
			if ( tableObj ) tableObj.width = cellObj.offsetWidth + 1;

			pageoffsetLeft += cellObj.offsetLeft + cellBorderCorrectionWidth;
		}
		placeIt(submenus[i],pageoffsetLeft,pageoffsetTop);
		parent_divs[submenus[i]] = [pageoffsetLeft,pageoffsetTop];
	}

}

//
// return an object, with some debug dump: style param
// causes return of the style object, rather than the object
// itself (though latter is checked first)
//
function get_object(str,style,d) {
	var e = doc + str + doc1;
	var obj = eval(e);
	if ( obj && style == 1 ) obj = eval(doc + str + sty);
	if ( d == 1 ) {
		if ( obj )	alert("Dump: "+ e + ":\n"+dump_dom(obj));
		else alert("Failed to evaluate: "+e);
	}
	return obj;
}

//
// relies on the rows above the menu row being id-ed correctly
//
function get_absolute_menu_top() {
	var t = 0;
	var myrows = new Array('page_body','main_table','dropdown_row');

	for ( i = 0; i < myrows.length; i++ ) {
		var divObj = get_object(myrows[i],0);
		if ( divObj ) { t += (divObj.offsetTop); }
//		else alert('Failed to locate '+myrows[i]);
	}
	return t;
}

//
// relies on the cells to left of the menu row being id-ed correctly
//
function get_absolute_menu_left() {
	var t = 0;
	var myrows = new Array('page_body','main_table','dropdown_row');

	for ( i = 0; i < myrows.length; i++ ) {
		var divObj = get_object(myrows[i],0);
		if ( divObj ) { t += divObj.offsetLeft; }
	}
	return t;
}

