/*
 * Dynamic HTML / JavaScript Menus
 * for ALL BROWSERS
 * Version 2.6
 * Last revision by LJB
 */

// GLOBAL VARIABLES
var gCurrentResource = null;
var gMouseOutTimer = null;
var gMenuBarName = "TheMenuBar";

// BROWSER SNIFF
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && (agt.indexOf('webtv') == -1));
var is_nav4up = (is_nav && (is_major >= 4)); 
var is_ie = (agt.indexOf("msie") != -1);
var is_ie3  = (is_ie && (is_major < 4));
var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5") == -1) );
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
var is_mac = (agt.indexOf("mac") != -1);
var is_gecko = (agt.indexOf("gecko") != -1);

// DISPLAY MENU & GENERIC FUNCTIONS
function DisplayMenu() {
	if (is_mac && !(is_nav)) {
		Mac_Menu();
	} else if (is_ie4up || is_gecko) {
		Flyout();
	} else if (is_nav4up) {
		NS_Menu();
	} else {
		Unsupported_Menu();
	}

}

function grabObject (id) {
	if (is_ie4) {
		var view = eval(id);
	}
	if (is_ie5up || is_gecko) {
		var view = document.getElementById(id);
	}
	if (is_nav && !is_gecko) {
		var view = document.layers[id];
		if (view == null) {
			view = document.layers[gMenuBarName].document.layers[id];
		}
	}			
	return view;
}

function ShowSubmenu(res) {
	if (gCurrentResource == res) {
		return;
	}

	var id = res.id;
	if (id == null) {
		alert("Resource does not have an id");
		return;
	}

	SetBackgroundColor(id, COLOR_MENU_ITEM_SELECTED);

	if (res.isContainer()) {	
		var html = "";
		var counter = 0;

		if (TYPE == TYPE_DROPDOWN) {
			var x0 = GetLeft(id);
			var y0 = GetTop(id) + GetHeight(id);
		} else {
			var x0 = GetLeft(id) + GetWidth(id);
			var y0 = GetTop(id);
		}

		for (var label in res.map) {
			html += ItemAsHTML(counter, label, res.map[label]);
			counter++;
		}

		OpenFlyout(x0, y0, html);
	}

	gCurrentResource = res;
}

function HideSubmenu() {
	if (gCurrentResource == null) {
		return;
	}

	CloseFlyout();
	SetBackgroundColor(gCurrentResource.id, COLOR_MENU_ITEM);
	gCurrentResource = null;
}

function OpenItem(url) {
	document.location = url;
}

function OnHandleFMIMouseOver(id) {
	SetBackgroundColor(id, COLOR_SUBMENU_ITEM_SELECTED);
}

function OnHandleFMIMouseOut(id) {
	SetBackgroundColor(id, COLOR_SUBMENU_ITEM);
}

function GetWidth(id) {
	return WIDTH_MAINMENU;
}

function OnHandleMouseOver(resID) {
	ClearMouseOutTimer();
	HideSubmenu();
	ShowSubmenu( __oMenuModel[resID]);
}

function OnHandleMouseOut(resID) {
	InitMouseOutTimer();
}

function OnHandleClick(resID) {
	ClearMouseOutTimer();
	HideSubmenu();
	var res = __oMenuModel[resID];
	OpenItem(res.getURL());
}

function OnHandleFMMouseOver() {
	ClearMouseOutTimer();
}

function OnHandleFMMouseOut() {
	InitMouseOutTimer();
}

function InitMouseOutTimer() {
	ClearMouseOutTimer();
	gMouseOutTimer = setTimeout("HideSubmenu();", TIMER_MOUSE_OUT_DELAY)
}

function ClearMouseOutTimer() {
	clearTimeout(gMouseOutTimer);
}

function OpenFlyout(x0, y0, htmlContent) {
	SetLeft("TheFloatingMenu", x0);
	SetTop("TheFloatingMenu", y0);
	SetContent("TheFloatingMenu", htmlContent);
	SetVisibility("TheFloatingMenu", "visible");
}

function CloseFlyout() {
	SetVisibility("TheFloatingMenu", "hidden");
	SetContent("TheFloatingMenu", "");
}

// MAC CODE
function Mac_Menu() {
	var mod_collapse_code = MAC_COLLAPSE_CODE.split("=\'");
	var mod_collapse_code2 = mod_collapse_code[1].split("\' ");
	MAC_COLLAPSE_CODE = mod_collapse_code2[0];

	var mod_expand_code = MAC_EXPAND_CODE.split("=\'");
	var mod_expand_code2 = mod_expand_code[1].split("\' ");
	MAC_EXPAND_CODE = mod_expand_code2[0];

	html = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=" + WIDTH_MAINMENU + "><TR><TD>";
	if ( __menuModel ) {
		counter = 1;
		for (var label in __menuModel) {
			var res = __menuModel[label];
			if (res.length) {
				var id = "I_" + counter;
			html += "<DIV CLASS=\"MenuItem\" style=\"width:" + WIDTH_MAINMENU + "; background-color:" + COLOR_MENU_ITEM + ";\" id=\"" + id + "\" onmouseout=\"Mac_SetBackgroundColor('" + id + "','" + COLOR_MENU_ITEM + "\','OUT');\" onmouseover=\"Mac_SetBackgroundColor('" + id + "','" + COLOR_MAC_HIGHLIGHT + "\','OVER');\">" + TOP_FONT_SPECS + BULLET_CODE + "<A HREF=\"" + res + "\"STYLE=\"text-decoration:none\">" + TOP_FONT_SPECS + label + "</FONT></A></FONT></DIV>";
			} else {
				if (res) {
					var id = "C_" + counter;
					html += Mac_SubMenu(id, label, res);
				}
			}
			counter++;
		}
	}
	html += "</TD></TR></TABLE>";
	document.write(html);
}

function Mac_CloseAll() {
	counter = 1;
	for (var label in __menuModel) {
		var res = __menuModel[label];
		if (res.length) {
		} else {
			if (res) {
				var id = "C_" + counter;
				Mac_CloseNav(id);
			}
		}
		counter++;
	}
}

function Mac_OpenNav(id) {
	handle = eval("document.all." + id);
	subnav = eval("document.all." + id + "nav");
	newImage = eval("document.img" + id);
	handle.style.backgroundColor = COLOR_MAC_HIGHLIGHT;
	subnav.style.display = "block";
	newImage.src = MAC_COLLAPSE_CODE;
}

function Mac_CloseNav(id) {
	handle = eval("document.all." + id);
	subnav = eval("document.all." + id + "nav");
	newImage = eval("document.img" + id);
	handle.style.backgroundColor = COLOR_MENU_ITEM;
	subnav.style.display = "none";
	newImage.src = MAC_EXPAND_CODE;
}

function Mac_IsOpen(id) {
	subnav =  eval ("document.all." + id + "nav");
	if (subnav.style.display == "block") {	
		return true;
	} else {
		return false;
	}
}

function Mac_SubNav(id) {
	if (MAC_MULTIPLE_OPEN) {
		if (Mac_IsOpen(id)) {
			Mac_CloseNav(id);
		} else {
			Mac_OpenNav(id);
		}
	} else {
		if (!Mac_IsOpen(id)) {
			Mac_CloseAll();
			Mac_OpenNav(id);
		} else {
			Mac_CloseAll(id);
		}
	}
}
	
function Mac_SetBackgroundColor(id, color) {
	var view = eval("document.all." + id);
	var isContainer = id.charAt(0);
	if (isContainer == "C") {
		subnav = eval ("document.all." + id + "nav");
		if (subnav.style.display == "block") {
			return;			
		}
	}
	if (view != null) {
		view.style.backgroundColor = color; 
	}
}	

function Mac_SubMenu(id, label, res) {
	var SubMenuCode = "<div style=\"width:" + WIDTH_MAINMENU +"; background-color:" + COLOR_MENU_ITEM + ";\" onclick=\"Mac_SubNav('" + id + "')\" onmouseout=\"Mac_SetBackgroundColor('" + id + "','" + COLOR_MENU_ITEM + "\','OUT');\" onmouseover=\"Mac_SetBackgroundColor('" + id + "','" + COLOR_MAC_HIGHLIGHT + "\','OVER');\" id=\"" + id + "\" CLASS=\"MenuItem\" STYLE=\"cursor:hand\">" + TOP_FONT_SPECS + BULLET_CODE + label + "<table style='position:absolute; left:85px;'><tr><td valign='middle'><img name=\"img" + id + "\" src=\"" + MAC_EXPAND_CODE + "\" border=0 align='middle' width='11' height='16'></td></tr></table></FONT></div>\n";
	SubMenuCode += "<DIV id=\"" + id + "nav\" style=\"display:none; background-color:" + COLOR_SUBMENU_ITEM + "\";>";
	var counter2 = 1;
	for (var sublabel in res) {
		var subid = "I_" + counter + "_" + counter2;
		var subres = res[sublabel];
		SubMenuCode += "<DIV style=\"width:" + WIDTH_MAINMENU + "px;\" id=\"" + subid + "\" onmouseout=\"Mac_SetBackgroundColor(' " + subid + "','" + COLOR_SUBMENU_ITEM + "\','OUT');\" onmouseover=\"Mac_SetBackgroundColor(' " + subid + "','" + COLOR_SUBMENU_ITEM_SELECTED + "\','OVER');\">";
		SubMenuCode += "<table border='0' cellpadding='0' cellspacing='0'><tr valign='top'><td width='15' ALIGN='RIGHT'>" + TOP_FONT_SPECS + BULLET_CODE + "</font></td><td><A HREF=\"" + subres + "\"STYLE=\"text-decoration:none\">" + TOP_FONT_SPECS + sublabel + "</FONT></A></td></tr></table></DIV>";
		counter2++;
	}
	SubMenuCode += "</DIV>\n";
	return SubMenuCode;
}

// IE & GECKO CODE
function Flyout() {
	if (TYPE == TYPE_DROPDOWN) {
		var tagName = "span";
	} else {
		var tagName = "div";
	}

	document.write("<div id='TheFloatingMenu' style='width:" + WIDTH_SUBMENU + "' class='FloatingMenu' onmouseover='OnHandleFMMouseOver()' onmouseout='OnHandleFMMouseOut()' onselectstart='return false;'></div>");
	SetBackgroundColor("TheFloatingMenu", COLOR_MENU_ITEM);
	
	for (var rid in __oMenuModel) {
		var res = __oMenuModel[rid];
		var html = "";
		if (res.isContainer()) {
			html= "<" + tagName + " id='" + rid + "' style='width:" + WIDTH_MAINMENU + "' class='MenuItem' onselectstart='return false;' onmouseout='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")'>" + TOP_FONT_SPECS + BULLET_CODE +	__oMenuModel[rid].asHTML() + EXPAND_CODE + "</font></" + tagName + ">"; 
		} else {
			html= "<" + tagName + " id='"+rid+"' style='width:" + WIDTH_MAINMENU + "' class='MenuItem' onselectstart='return false;' onmouseout='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")' onclick='OnHandleClick(\"" + rid + "\")'>" + TOP_FONT_SPECS + BULLET_CODE + __oMenuModel[rid].asHTML() + "</font></" + tagName + ">";
		}
		document.write(html);
		SetBackgroundColor(rid, COLOR_MENU_ITEM);
	}
}

function SetLeft(id, x0) {
	var view = grabObject(id);
	if (view != null) {
		view.style.left = x0 + "px"; 
	}		
}

function SetTop(id, y0) {
	var view = grabObject(id);
	if (view != null) {
		view.style.top = y0 + "px"; 
	}			
}

function GetLeft(id){
	var view = grabObject(id);
	if (view != null) {
		var xPos = view.offsetLeft;
		return xPos;
	} else {
		return 0;
	}	
}

function GetTop(id){
	var view = grabObject(id);
	if (view != null) {
		return view.offsetTop; 
	} else {
		return 0;
	}	
}

function GetHeight(id){
	var view = grabObject(id);
	if (view != null) {
		return view.offsetHeight; 
	} else {
		return 0;
	}	
}

function SetContent(id, htmlContent){
	var view = grabObject(id);
	if (view != null) {
		view.innerHTML = htmlContent; 
	}		
}

function SetVisibility(id, str){
	var view = grabObject(id);
	if (view != null) {
		view.style.visibility = str; 
	}	
}

function SetBackgroundColor(rid, color){
	var view = grabObject(rid);
	if (view != null) {
		view.style.backgroundColor = color;
		view.style.fontcolor = TOP_FONT_SELECTED_SPECS;
	}
}

function ItemAsHTML(id, label, url){
	var html = "<div id='FMI" + id + "' width='100%' STYLE='background:" + COLOR_SUBMENU_ITEM + "' class='MenuItem' onselectstart='return false;' onmouseout='OnHandleFMIMouseOut( this.id )' onmouseover='OnHandleFMIMouseOver( this.id )' onclick='OpenItem(\""+ url +"\")'>" + SUB_FONT_SPECS + BULLET_CODE + label + "</font></div>"; 
	return html;		
}

// NS CODE
function NS_Menu() {
	SetLeft = NS_SetLeft;
	SetTop = NS_SetTop;
	GetLeft = NS_GetLeft;
	GetTop = NS_GetTop;
	GetHeight = NS_GetHeight;
	SetContent = NS_SetContent;
	SetVisibility = NS_SetVisibility;
	SetBackgroundColor = NS_SetBackgroundColor;
	ItemAsHTML = NS_ItemAsHTML;

	document.write("<layer id='TheFloatingMenu' width='" + WIDTH_SUBMENU + "' onmouseover='OnHandleFMMouseOver()' onmouseout='OnHandleFMMouseOut()'></layer>");
	SetBackgroundColor("TheFloatingMenu", COLOR_MENU_ITEM);

	var totItems = 0;
	for (var rid in __oMenuModel) {
		totItems++;
	}
	var width = WIDTH_MAINMENU;
	if (TYPE == TYPE_DROPDOWN) {
		width = totItems * WIDTH_MAINMENU;
	}
	var counter = 0;
	document.write("<table cellpadding=0 cellspacing=0 border=0 width="+width+"><tr><td>");	
	document.write("<ilayer id='" + gMenuBarName + "'>");	

	for (var rid in __oMenuModel) {
		var res = __oMenuModel[rid];
		var html = "";

		if (res.isContainer()) {
			html = "<layer id='" + rid + "' class='NSMenuItem' width='" + WIDTH_MAINMENU + "' onmouseout='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")'>" + TOP_FONT_SPECS + BULLET_CODE + "<A HREF='#' class='NSMenuItem' onclick='return false'>" + TOP_FONT_SPECS + __oMenuModel[rid].asHTML() + "</font></A>" +  EXPAND_CODE + "</font></layer>" + "<ilayer id='" + rid + "Shadow' class='NSMenuItem' visibility = 'hidden'>" + TOP_FONT_SPECS + BULLET_CODE + "<A HREF='#' class='NSMenuItem' onclick='return false'>" + TOP_FONT_SPECS + __oMenuModel[rid].asHTML() + "</font></a>" + EXPAND_CODE + "</font></ilayer>" 
		} else {
			var url = res.getURL();
			html = "<layer id='" + rid + "' class='NSMenuItem' width='" + WIDTH_MAINMENU + "' onmouseout='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")'>" + TOP_FONT_SPECS + BULLET_CODE + "<A HREF='" + url + "' class='NSMenuItem' onmouseout='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")' onclick='OnHandleClick(\"" + rid + "\")'>" + TOP_FONT_SPECS + __oMenuModel[rid].asHTML() + "</font></a>" + "</font></layer>" + "<ilayer id='" + rid + "Shadow' class='NSMenuItem' visibility = 'hidden'>" + TOP_FONT_SPECS + BULLET_CODE + "<A HREF='" + url + "' class='NSMenuItem' onmouseout	='OnHandleMouseOut(\"" + rid + "\")' onmouseover='OnHandleMouseOver(\"" + rid + "\")' onclick    ='OnHandleClick(\"" + rid + "\")'>" + TOP_FONT_SPECS + __oMenuModel[rid].asHTML() + "</font></a>" + "</font></ilayer>";
		}
		if (TYPE == TYPE_FLYOUT) {
			html += "<br>";
		}

		counter++;
		document.write(html);
	}
	document.write("</ilayer></td></tr></table>");

	var previousItem = null;
	for (var rid in __oMenuModel) {
		if (previousItem != null) {
			var dx = document.layers[gMenuBarName].document.layers[previousItem].clip.width;
			var dy = document.layers[gMenuBarName].document.layers[previousItem].clip.height;

			if (TYPE == TYPE_FLYOUT) {
				dx = 0;
			} else {
				dy = 0;
			}
 			document.layers[gMenuBarName].document.layers[rid].top = document.layers[gMenuBarName].document.layers[previousItem ].top  + dy;
 			document.layers[gMenuBarName].document.layers[rid].left = document.layers[gMenuBarName].document.layers[previousItem].left + dx;
		}	

		SetBackgroundColor(rid, COLOR_MENU_ITEM);
		SetVisibility(rid, "visible");
		previousItem = rid;
	}
}

function NS_SetLeft(id, x0) {
	var view = grabObject(id);
	if (view != null) {
		view.left = x0; 
	}
}

function NS_SetTop(id, y0) {
	var view = grabObject(id);
	if (view != null) {
		view.top = y0; 
	}			
}

function NS_GetLeft(id) {
	var view = grabObject(id);
	if (view != null) {
		return view.pageX; 
	} else {
		return 0;
	}	
}

function NS_GetTop(id) {
	var view = grabObject(id);
	if (view != null) {
		return view.pageY; 
	}			
}

function NS_GetHeight(id) {
	var view = grabObject(id);
	if (view != null) {
		return view.clip.height; 
	} else {
      return 0;
   }	
}

function NS_SetContent(id, htmlContent) {
	var view = grabObject(id);
	if (view != null) {
		view.document.open();
		view.document.write(htmlContent);
		view.document.close(); 
	}		
}

function NS_SetVisibility(id, str) {
	var view = grabObject(id);
	if (view != null) {
		if (str == "hidden") {
			view.visibility = "hidden";
		} else {
			view.visibility = "show";
		}	 
	}	
}

function NS_SetBackgroundColor(rid, color) {
	var view = document.layers[rid];
	if (view == null) {
		view = document.layers['TheFloatingMenu'].document.layers[rid];
	}
	if (view == null) {
		view = document.layers[gMenuBarName].document.layers[rid];
	}
	if (view != null) {
		view.bgColor = color; 
	}
}

function NS_ItemAsHTML(id, label, url) {
	var html = "<ilayer id='FMI" + id + "' top=0 width='" + WIDTH_SUBMENU + "' bgcolor='" + COLOR_SUBMENU_ITEM + "'>" +  SUB_FONT_SPECS + BULLET_CODE + "<a href='" + url + "' class='NSMenuItem' onmouseout='OnHandleFMIMouseOut ( \"FMI" + id + "\" )' onmouseover='OnHandleFMIMouseOver( \"FMI" + id + "\" )' onclick='OpenItem(\"" + url + "\")'>" +  SUB_FONT_SPECS + label + "</font></a>" + "</font></ilayer><BR>";
	return html;		
}

// OTHER BROWSERS
function Unsupported_Menu() {
	if ( __menuModel ) {
		htmlCode = "";
		var counter = 1;
		for (var label in __menuModel) {
			var res = __menuModel[label];

			if (res.length) {
				html = BULLET_CODE + "<A HREF=\"" + res;
				html += "\" STYLE=\"text-decoration: none\">";
				html += TOP_FONT_SPECS + label + "</FONT></A><BR>";
				htmlCode += html;
			} else {
				if (res) {
					var id = label.split(" ")[0];
					id = id.split(",")[0];			
					for (var sublabel in res) {
						subres = res[sublabel];
						var mod_url = subres.lastIndexOf("/");
						var mod_url2 = subres.slice(0,mod_url+1);
						subres = mod_url2 + UNSUPPORTED_INDEX;
						break;
					}
					html = BULLET_CODE + "<A HREF=\"" + subres;
					html += "\" STYLE=\"text-decoration: none\">";
					html += TOP_FONT_SPECS + label + "</FONT></A><BR>";
					htmlCode += html;
				}
			}
		}
	}
	document.write(htmlCode);
}

// NS 4 REDRAW CODE
function MM_reloadPage(init) {  
	if (init == true) with (navigator) {
		if (is_nav4up) {
			document.MM_pgW = innerWidth;
			document.MM_pgH = innerHeight;
			onresize = MM_reloadPage; 
		}
	}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH)
		location.reload();
}
MM_reloadPage(true);

// OBJECT MODEL
function __I( id, label, itemURL ) {
	this.id = id;
	this.label = label;
	this.targetURL = itemURL;
	this.getURL = function() {
		return this.targetURL;	
	}
	this.asHTML = function() {
		return label;
	}
	this.label = function() {
		return label;
	}
	this.isItem = function() {
		return true;
	}
	this.isContainer = function() {
		return false;
	}
}

function __C( id, label, map ) {
	this.label = label;
	this.map = map;
	this.id = id;
	this.asHTML = function() {
		return label;
	}
	this.label = function() {
		return this.label;
	}
	this.isItem = function() {
		return false;
	}
	this.isContainer = function() {
		return true;
	}
}

__oMenuModel = new Object();
if ( __menuModel ) {
	var counter = 1;
	for (var label in __menuModel) {
		var id = "resource" + counter;
		counter ++;
		var obj = new Object();
		var res = __menuModel[label];
		if( res.length ) {
			__oMenuModel[id] = new __I( id, label, res );
		} else {
			__oMenuModel[id] = new __C( id, label, res );
		}
	}
}