/* $Id: lc1_menus.js,v 1.3 2009/11/25 21:09:35 pmorrill Exp $ */
var 	browserType = 'unknown';
var 	doc = '',sty = '',doc1 = '';
var 	parent_uls = new Array;
var 	menu_cells = new Array;
var	active_cell = 0;
var	subMenuULs = [];

/* override any of these in a separately included js file */
var	g_submenu_fixed_width = 0;	/* set to 1 to force submenu width to match that of main cells */
var	g_submenu_space_adjust = 2;	/* space between main cell and the submenu */

//
// for some reason, the browser <body> style cannot be reliably 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 = 1;

//
// 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 = 200;
var menu_close_timeout = Array();
var active_m = new Array();

//
// to bust the IE6 problem: we assign a distinct class as if a hover
//
function set_cell_hover(item,on) {
	liObj = get_object('cell_'+item,0,1);
	if ( liObj && liObj.className != 'selected' && liObj.className != 'nolink' ) liObj.className = (on == 1  ? 'active' : '');
}

function set_sib_active(liObj,on) {
	if ( liObj && liObj.className != 'selected' && liObj.className != 'nolink' ) liObj.className = (on == 1  ? 'active' : '');
}

//
// sets the visibility of any sub-ul attached to this object, and cancel any
// pending close sub-menu calls
//
function set_child_visible(item,on) {
	//
	// ie6 hack: no hover support grrrr.r.r.r..
	//
	set_cell_hover(item,on);

	ulObj = get_object('ul_'+item,1);
	if ( ulObj ) { 
		if ( on == 1 ) cancel_close('ul_'+item);
		ulObj.visibility = (on == 1 ? 'visible' : 'hidden');
	}
}


//
// position and make sub-menu visible
//
function fnctTopMenuHover(item) { set_child_visible(item,1); }

//
// cursor has entered a sub-menu cell: we set the cell visible, 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_uls[item] ) {
		var parent_id = parent_uls[item];
		var pObj = get_object('ul_'+parent_id);
		while ( pObj ) {
			set_child_visible(parent_id,1);
			var pCell = get_object('cell_'+parent_id);
			if ( pCell.className != 'selected' && pCell.className != 'nolink' ) pCell.className = 'open';
			parent_id = parent_uls[parent_id];
			pObj = get_object('ul_'+parent_id);
		}
	}
	set_child_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_uls[item] ) {
		var parent_id = parent_uls[item];
		var pObj = get_object('ul_'+parent_id);
		while ( pObj ) {
			fnctHideSubMenu(parent_id);
			var pCell = get_object('cell_'+parent_id);
			if ( pCell.className != 'selected' && pCell.className != 'nolink'  ) pCell.className = '';
			parent_id = parent_uls[parent_id];
			pObj = get_object('ul_'+parent_id);
		}
	}
	fnctHideSubMenu(item);
}

//
// delayed hide function: create an associative array of timeout objects
//
function delayed_hide(item) {
	menu_close_timeout['ul_'+item] = setTimeout('set_child_visible(\''+item+'\',0)',delay);
}

//
// hide an individual menu, possibly with a delay
//
function fnctHideSubMenu(item) {
	if ( delay > 0 ) return delayed_hide(item);
	set_child_visible(item,0);
}

function cancel_close(item) {
	if ( menu_close_timeout[item] ) clearTimeout(menu_close_timeout[item]);
}

//
// called from onload - build menu lists dynamically using json srv for info
//
function dMenuBuild(active,d) {
	IncludeJavaScript('/srv/json.php/DROPDOWN_MENUS/'+active+'/dBuild/'+d,'menuBuild');
}


//
// callback for JSON server - build menu lists dynamically
//
function dBuild(json) {
	if ( json.Error ) return false;
	active_cell = json.Active;

	var parent_divs = new Array;

	//
	// set global window size variables
	//
	win_size();

	//
	// find the horizontal menu and proceed
	//
	var divObj = get_object('menubar');
	if ( !divObj ) { return; }

	var b = document.getElementsByTagName('body');
	var d = get_object('sub_menus');
	if ( !d ) {
		d = document.createElement('div');
		d.setAttribute('id','sub_menus');
		b[0].appendChild(d);
	}

	//
	// now inspect the submenus
	//
	var j = 0;

	//
	// menu_cells array is [page id,parent id,text,url]
	//
	menu_cells = json.Menu_cells;
	var ul_count = 0;
	for ( i = 0; i < menu_cells.length; i++ ) {
		//
		// get the parent cell first
		//
		var parent = get_object('cell_'+menu_cells[i][1]);
		if ( !parent ) { j++; continue; }

		//
		// if parent ul does not exist, create and append to body
		//
		var ul2 = get_object('ul_'+menu_cells[i][1]);
		if ( !ul2 ) {
			ul2 = document.createElement('ul');
			ul2.setAttribute('id','ul_'+menu_cells[i][1]);
			d.appendChild(ul2);
			subMenuULs[ul_count] = menu_cells[i][1];
			ul_count++;

			//
			// get parent location if it was dynamic build: this signals a horizontal offset
			//
			var pLi = parent_uls[menu_cells[i][1]];
			if ( pLi ) {
				pUL = get_object('ul_'+pLi);
				positionSubMenu(ul2,pUL.offsetLeft+pUL.offsetWidth,pUL.offsetTop+parent.offsetTop);
				var ul_width = parent.offsetWidth + 1;
				ul2.className = 'flyout';
			} else {
				var pos = findPos(parent);
				positionSubMenu(ul2,pos[0]-1,pos[1] + parent.offsetHeight + g_submenu_space_adjust);
				var ul_width = parent.offsetWidth + cellBorderCorrectionWidth;
				if ( g_submenu_fixed_width ) ul2.style.width = ul_width+'px';
			}
		}

		//
		// create the dynamic list item and add to ul
		//
		var li2 = document.createElement('li');
		li2.setAttribute('id','cell_'+menu_cells[i][0]);

		ul2.appendChild(li2);

		li2.innerHTML = menu_cells[i][2];

		//
		// for IE6 compatibility (grr.r.r.) we must set the width explicitly, in order to make
		// the cell hot outside the text area;
		// see: http://cmsproducer.com/IE6-bug-DOM-scripting-mouseover-web-standards-hover
		// Note that we are using the offsetWidth here which means the outer dimension of the parent cell
		//
		if ( is_ie6() ) {
			if ( ul_width < 150 ) ul_width = 150;
			li2.style.width = (ul_width) + 'px';
		}

		li2.onmouseover = new Function('fnctSubMenuHover('+menu_cells[i][0]+')');
		li2.onmouseout = new Function('fnctSubMenuOut('+menu_cells[i][0]+')');
		if ( json.Active == menu_cells[i][0] ) li2.className = 'selected';
		else li2.onclick = new Function('jump_to(\''+menu_cells[i][3]+'\')');

		//
		// build an associative array that links each <li> to its parent <ul>
		//
		parent_uls[menu_cells[i][0]] = menu_cells[i][1];
	}
	RemoveJavaScript('menuBuild');
	window.onresize = positionSubMenus;
}

//
// the main jump function
//
function jump_to(str) {	document.location.href = str; }

//
// called from the window onresize event to reposition sub-menus
//
function positionSubMenus() {
	for ( i = 0; i < subMenuULs.length; i++ ) {
		//
		// get the parent cell first
		//
		var parent = get_object('cell_'+subMenuULs[i]);
		if ( !parent ) continue;
		var pos = findPos(parent);
		positionSubMenu(ul2,pos[0]-1,pos[1] + parent.offsetHeight + g_submenu_space_adjust);
	}
}

function positionSubMenu(pUL,mleft,mtop) {
	pUL.style.left = mleft + 'px';
	pUL.style.top = mtop + 'px';
}

//
// 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_ex(str,style,d) {
	var e = doc + str + doc1;
	var obj = eval(e);
	if ( obj && style == 1 ) obj = eval(doc + str + sty);
	if ( d == 1 && !obj ) 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('menubar');
	for ( i = 0; i < myrows.length; i++ ) {
		var divObj = get_object(myrows[i],0);
		if ( divObj ) { t += (divObj.offsetTop); }
	}
	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();
	for ( i = 0; i < myrows.length; i++ ) {
		var divObj = get_object(myrows[i],0);
		if ( divObj ) { t += divObj.offsetLeft; }
	}
	return t;
}

function findPos(obj,n) {
	var posX = obj.offsetLeft;
	var posY = obj.offsetTop;
	while ( obj.offsetParent ) {
		if ( obj == document.getElementsByTagName('body')[0] ) break;
		else {
			posX = posX + obj.offsetParent.offsetLeft;
			posY = posY + obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
	}
	var posArray = [posX,posY];
	if ( n ) alert(n+' - Left: '+posX+'; Top: '+posY);
	return posArray;
}
