// get browser versions that support dhtml
var useDHTML = false;
if (document.getElementById) {
	if ((navigator.platform.indexOf("Win") != -1) || (navigator.platform.indexOf("Mac") != -1)) {
	// it works on these
		useDHTML = true;
	}
}


function setDivHeight () {
// set content and sidebar divs to the larger height
	if (document.getElementById) {
		var c = document.getElementById("contentdiv").offsetHeight;
		var s = document.getElementById("sidebardiv").offsetHeight;
		if (s > c) {
			document.getElementById("contentdiv").style.height = s;
		} else {
			document.getElementById("sidebardiv").style.height = c;
		}
		document.getElementById("bottomdiv").style.visibility = "visible";
	}
}


function popUp(page,w,h) {
// pops up window
	var t = 100;
	var l = 100;
	if (screen && screen.height) {
		t = Math.round((screen.height - h)/2);
		l = Math.round((screen.width - w)/2);
	}
	top.open(page,"thePopUp","top="+t+",left="+l+",height="+h+",width="+w+",status=no,toolbar=no,menubar=no,location=no,titlebar=yes,resizable=no,scrollbars=no");
}

function switchClass(whichElement,classname) {
// swaps classname for element
	if (useDHTML) {
		whichElement.className = classname;
	}
}

function getAbsoluteTop(whichElement) {
// returns the top pixel position of an element on a page
	var t; 
	if (whichElement == document.body) {
		t = 0;
	} else {
		t = whichElement.offsetTop + getAbsoluteTop(whichElement.offsetParent);
	}
	return t;
} 

function getAbsoluteLeft(whichElement) {
// returns the left pixel position of an element on a page
	var l; 
	if (whichElement == document.body) {
		l = 0;
	} else {
		l = whichElement.offsetLeft + getAbsoluteLeft(whichElement.offsetParent);
	}
	return l;
} 

function getElementHeight(whichElement) {
// returns the height of an element
	var h = whichElement.offsetHeight;
	return h;
}

function getElementWidth(whichElement) {
// returns the width of an element
	var w = whichElement.offsetWidth;
	return w;
}

function showMenu(whichMenu,direction) {
// shows a dhtml menu
// direction = over, under, left, right
	if (javascriptEnd && useDHTML) {
		var leftOffset = -2; // set this
		var topOffset = 2;  // set this
		var visibleLinks = 8; // set this, 0 based
		var top;
		var left;
		var rightEdge;
		if (direction == "over") {
			top = getAbsoluteTop(document.getElementById(whichMenu + "_link")) - getElementHeight(document.getElementById(whichMenu));
			left = getAbsoluteLeft(document.getElementById(whichMenu + "_link")) + leftOffset;
		} else if (direction == "under") {
			top = getAbsoluteTop(document.getElementById(whichMenu + "_link")) + getElementHeight(document.getElementById(whichMenu + "_link"));
			left = getAbsoluteLeft(document.getElementById(whichMenu + "_link"));
			rightEdge = left + getElementWidth(document.getElementById(whichMenu));
			if (rightEdge > getElementWidth(document.body)) {
			// won't fit in window, so align to right
				left = getElementWidth(document.body) - getElementWidth(document.getElementById(whichMenu)); 
				// hack for ie??
				if (document.all) left -= 30;
			}
		} else if (direction == "right") {
			top = getAbsoluteTop(document.getElementById(whichMenu + "_link")) - 1 + topOffset;
			left = getAbsoluteLeft(document.getElementById(whichMenu + "_link")) + getElementWidth(document.getElementById(whichMenu + "_link")) + leftOffset;
			rightEdge = left + getElementWidth(document.getElementById(whichMenu));
			if (rightEdge > getElementWidth(document.body)) {
			// won't fit in window, so do left
				left = getAbsoluteLeft(document.getElementById(whichMenu + "_link")) - getElementWidth(document.getElementById(whichMenu)) - leftOffset;
			}
		} else { // left
			top = getAbsoluteTop(document.getElementById(whichMenu + "_link")) - 1 + topOffset;
			left = getAbsoluteLeft(document.getElementById(whichMenu + "_link")) - getElementWidth(document.getElementById(whichMenu)) - leftOffset;
		}
		document.getElementById(whichMenu).style.top = top;
		document.getElementById(whichMenu).style.left = left;
		setVisibility(whichMenu,"visible");
		if (whichMenu.length >= 8) {
		// show parent menu
			var theDirection = "right";
			if ((whichMenu.length == 8) && (parseInt(whichMenu.substring(5,6)) <= visibleLinks)) { 
			// top main menus go under
				theDirection = "under";
			}
			showMenu(whichMenu.substring(0,whichMenu.length-2),theDirection);
		} else if (whichMenu.length <= 7) {
		// show rollover
			//showRoll(whichMenu+"_link");
		}
	}
}

var menuclicked = false;
function hideMenu(whichMenu) {
// hides a dhtml menu
	if (useDHTML && (menuclicked == false)) {
		setVisibility(whichMenu,"hidden");
		if (whichMenu.length >= 8) {
		// hide parent menu
			hideMenu(whichMenu.substring(0,whichMenu.length-2));
		} else if (whichMenu.length <= 7) {
		// hide rollover
			//hideRoll(whichMenu+"_link");
		}
	}
}

function showRoll(whichImg) {
// changes to roll over image based on src name
// /path/to/img_name_02.gif to /path/to/img_name_roll_01.gif
		if (document.images) {
		var imgSrc = document.images[whichImg].src;
		var ind = imgSrc.indexOf("/",8);
		imgSrc = imgSrc.substring(ind);
		if (imgSrc.indexOf("_roll") == -1) {
		// not in rolled over state, so change src
			var newSrc;
			if (imgSrc.indexOf("_") == -1) {
			// no _01
				var s = imgSrc.split(".");
				newSrc = s[0] + "_roll." + s[1];
			} else {
				var s = imgSrc.split("_");
				newSrc = s[0];
				for (var i=1; i<s.length; i++) {
					if (i == s.length-1) {
						newSrc += "_roll";
					}
					newSrc += "_" + s[i];
				}
			}
			document.images[whichImg].src = newSrc;
		}
	}
}

function hideRoll(whichImg) {
// changes to normal image based on src name
// /path/to/img_name_roll_02.gif to /path/to/img_name_01.gif
	if (document.images) {
		var imgSrc = document.images[whichImg].src;
		var ind = imgSrc.indexOf("/",8);
		imgSrc = imgSrc.substring(ind);
		if (imgSrc.indexOf("_roll") != -1) {
		// in rolled over state, so change src
			var s = imgSrc.split("_roll");
			var newSrc = s[0] + s[1];
			document.images[whichImg].src = newSrc;
		}
	}
}

function setVisibility(elementID,visibility) {
// set the visibility of elementID
	//alert(elementID+" "+visibility);
	var e = document.getElementById(elementID);
	if (e && (e.style.visibility != visibility)) {
		e.style.visibility = visibility;
	}
}

function doMenuOver(whichElementName) {
// changes cssclass of dhtml menu item
	var whichElement = document.getElementById(whichElementName);
	whichElement.className = "menuItemOver";
}

function doMenuOut(whichElementName) {
// changes css class of dhtml menu item
	var whichElement = document.getElementById(whichElementName);
	whichElement.className = "menuItem";
}

function showPage(whichPage) {
// load a file to the top level window
	top.location = whichPage;
}

// not used ///////////////////////////////////////////
function doPreload() {
// preload rollovers
	if (useDHTML) {
		var imgSrcs = new Array("/shared/img/menu_roll_02.gif",
								"/shared/img/menu_roll_03.gif",
								"/shared/img/menu_roll_04.gif",
								"/shared/img/menu_roll_05.gif",
								"/shared/img/menu_roll_06.gif",
								"/shared/img/menu_roll_07.gif",
								"/shared/img/menu_roll_08.gif",
								"/shared/img/side_bar_roll_01.gif");
		var imgObjs =new Array();
		for (var i=0; i<imgSrcs.length; i++) {
			imgObjs[i] = new Image();
			imgObjs[i].src = imgSrcs[i];
		}
	}
}

function writeMenus() {
// insert dhtml menus
	var isIE = false;
	if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.userAgent.indexOf("Opera") == -1) && (navigator.platform.indexOf("Win") != -1)) isIE = true;
	// go through the menu items and write out the dhtml menu tables
	for (i=0; i<menuItems.length; i++) {
		var menuID = new String(menuItems[i][0]);
		if ((menuID.length > 1) && (menuID.substring(menuID.length-1,menuID.length) == "0")) {
		// this is the first element in a table
			var tableID = menuID.substring(0,menuID.length-2);
			var theDirection = "right";
			if (tableID.length == 1) theDirection = "under";
			document.writeln("<div id=\"menu_"+tableID+"\" 	style=\"position:absolute; visibility:hidden; top:-500; left:-500;\" onclick='menuclicked=true;' onmouseover='showMenu(\"menu_"+tableID+"\",\""+theDirection+"\");' onmouseout='hideMenu(\"menu_"+tableID+"\");'><table cellspacing=\"1\" class=\"menuTable\">");
			var tableItem = 0;
			var loop = true;
			var ii = i;
			while (loop) {
				if (menuItems[ii][0] == tableID+"_"+tableItem) {
				// there's a menu item
					var nexti = ii + 1;
					if ((nexti < menuItems.length) && (menuItems[nexti][0] == tableID+"_"+tableItem+"_0")) {
					// there's a submenu for this item
						document.write("<tr><td nowrap class=\"menuItem\" id=\"menu_"+menuItems[ii][0]+"_link\" onmouseover='doMenuOver(\"menu_"+menuItems[ii][0]+"_link\");showMenu(\"menu_"+menuItems[ii][0]+"\",\"right\");' onmouseout='doMenuOut(\"menu_"+menuItems[ii][0]+"_link\");hideMenu(\"menu_"+menuItems[ii][0]+"\");' onclick='showPage(\""+menuItems[ii][2]+"\");'><div class=\"menuItemArrow\">");
						if (isIE) document.write("<span class=\"webdings\">u</span>");
						else document.write("&gt;");
						document.writeln("</div><div class=\"menuItemText\">"+menuItems[ii][1]+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td></tr>");
					} else {
					// no submenu for this item
						document.writeln("<tr><td nowrap class=\"menuItem\" id=\"menu_"+menuItems[ii][0]+"_link\" onclick='showPage(\""+menuItems[ii][2]+"\");' onmouseover='doMenuOver(\"menu_"+menuItems[ii][0]+"_link\");' onmouseout='doMenuOut(\"menu_"+menuItems[ii][0]+"_link\");'>"+menuItems[ii][1]+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>");
					}
					tableItem++;
				}
				ii++;
				if (ii == menuItems.length) {
					loop = false;
				} else if (menuItems[ii][0].substring(0,tableID.length) != tableID) {
					loop = false;
				}
			}
			document.writeln("</table></div>");
		}
	}
}

javascriptEnd = true;
