/* ============================================================================
 Dynamic Menu: This script is provided by Oregon State University.
 Created and enhanced by Central Web Services (CWS) http://oregonstate.edu/cws/
 Modified by Extended Campus (eCampus) http://ecampus.oregonstate.edu/
 This script is free to use provided these credits remain.
============================================================================ */
// Browser Compatibility Testing
//window.onerror = null; // Error Trapping
var omLyr = (document.layers)?1:0; // Legacy Navigator support, layers
var omAll = (document.all)?1:0; // Object model All support
var omID = (document.getElementById)?1:0; // Object model ID support

// Menu variables
var boxStart = 0;
var boxStay = null;
var onLayer;

if (omLyr) { // Legacy Navigator layer changer
	cssRef = "layer.";
	lyrRef = "document.layers";
	cssMod = "";
}
if (omAll) { // Object model layer changer
	cssRef = "layer.style.";
	lyrRef = "document.all";
	cssMod = ".style";
}

function showLayer(lyrName,shownOnMouse) { // Display box
	
	if (omLyr && !shownOnMouse) return;
	if (omID || omAll || omLyr) {
		if (boxStay != null) {
			clearTimeout(boxStay);
			hideLayer(onLayer);
		}
		if (omID || omAll) {
			var layerElement = document.getElementById(lyrName);
			layerElement.style.visibility = "visible";
			onLayer = lyrName;
			//alert (window.pageX);
			if (window.pageX < 400) {
				layerElement.style.left = 160;
			} else {
				//alert ();
				layerElement.style.left = document.body.offsetWidth - 200 - 180;
			}
			//alert (layerElement.style.left);

			// Set the focus for main menu item (always visible) to prevent errors
			if (omAll) {
				var setFocusElement;
				var elementID = "menu" + lyrName.charAt(3) + lyrName.charAt(4) + lyrName.charAt(5);
				setFocusElement = document.getElementById((elementID));
				setFocusElement.focus();
			}
		} else { // omLyr
			eval(lyrRef + "['" + lyrName + "']" + cssMod + ".visibility = \"visible\"");
			onLayer = lyrName;
		}
	} // End if omID, omAll, omLyr
}

function setMenuFocus(lyrName) { // Support for tabindex
	if (omAll) {
		var FirstMenuItemElement;
		var layerElement = document.getElementById(lyrName);
		var layerStatus = layerElement.style.visibility;
		
		// If layer is visible then set the focus, else do nothing
		if (layerStatus == "visible") {
			FirstMenuItemElement = document.getElementById((lyrName + "FirstMenuItem"));
			FirstMenuItemElement.focus();
		}
	}
	else hideLayer(lyrName);
}

function hideLayer(lyrName) { // Hide box
	if ((omID || omAll) && (boxStart == 0)) {
		var layerElement = document.getElementById(lyrName);
		layerElement.style.visibility = "hidden";
	} else if (omLyr && (boxStart == 0)) {
		eval(lyrRef + "['" + lyrName + "']" + cssMod + ".visibility = \"hidden\"");
	}
}

// Set defaults if undefined
if (boxDelay < 0.5) boxDelay = 1.5;
if (bdSize < 0) bdSize = 2;
if (dotWth < 0) bdSize = 20;
if (dotHgt < 0) bdSize = 20;

function boxTimer() { // Source mouseout delay
	//boxStay = setTimeout("btnOut()",boxDelay * 1000);
	boxStay = setTimeout("btnOut()",200);
}

function btnOut(lyrName) { // Source MouseOut
	if (boxStart == 0){hideLayer(onLayer);}
}

function boxOver() { // Box MouseOver image
	clearTimeout(boxStay);
	boxStart = 1;
}

function boxOut() { // Box MouseOut image
	boxStart = 0;
	boxStay = setTimeout("hideLayer(onLayer)", 1500);
}

function boxLink(title, url) { // Box elements
	this.title = title;
	this.url = url;
}

if(document.images){ // Checks for handling of MouseOver images.
 var dot0 = new Image(dotWth,dotHgt); dot0.src = imgs + dotSrc;
 var dot1 = new Image(dotWth,dotHgt); dot1.src = imgs + dotOvr;
}
function Chng(Pct,type){ // MouseOver bullets
 if(document.images && dotSrc != ""){document[Pct].src=eval("dot"+type+".src");}
}

function boxGen(boxArray){ // Build the dynamic box
	var elmtName = boxArray.name;
	var boxWth;
	N = eval(boxArray + ".length")-1;

	// Finds the longest title length in order to determine box width
	var boxWth = eval(boxArray + ".tblWidth");
	n = "\<div id=\"" + eval(boxArray + '.id') + "\" style=\"visibility:hidden;position:absolute;\">\<table";
	n += " border=\"" + bdSize + "\"";
	if (boxWth > 50) {
		n += " width=\"" + boxWth + "\"";
	} else {
		n += " width=\"200\"";
	}
	n += " cellspacing=\"0\" cellpadding=\"0\"";
	if (bdColor != "") n += " bordercolor=\"" + bdColor + "\"";
	if (bdLight != "") n += " bordercolorlight=\"" + bdLight + "\"";
	if (bdColor != "") n += " bordercolordark=\"" + bdDark + "\"";
	n += ">\<tr>\<td style=\"background-color:" + bdColor + "\">";
	n += "\<table border=\"0\" width=\"100%\"";
	n += " cellspacing=\"0\" cellpadding=\"3\">\<tr>";
	n += "\<td class=\"bxlnk\" style=\"background-color:" + bxBG + "\">";
	n += "\<table border=\"0\" width=\"100%\" cellspacing=\"1\" cellpadding=\"0\">";
	for (var i = 0; i <= N; i++ ) { // Process box list
		n += "\<tr>\<td>";
		var x1 = eval(boxArray + "[" + i + "].title");
		var x2 = eval(boxArray + "[" + i + "].url");
		var t1 = x2.toLowerCase().indexOf("http:\//");
		var t2 = x2.toLowerCase().indexOf(Host);
		if (x1 != "" && x2 == "") { // If item is a title
			n += "\<h5 class=\"MenuHeading\">" + x1 + "\</h5>";
		} else if (x1 == "" && x2 != "") { // If item is a description
			n += x2 + "\<br />";
		} else { // If item is a link
			if (dotSrc != "") { // Add bullet if used
				n += "\<img src=\"" + imgs + dotSrc + "\" alt=\"o\"";
				if (dotOvr != "") {
					n += " name=\"" + eval(boxArray + '.id') + "-" + i + "\"";
				}
				n += " width=\"" + dotWth + "\" height=\"" + dotHgt + "\" /> ";
			}
			n += "\<a class=\"bxlnk\" tabindex=\"0\"";
			if (t1 == 0 && t2 != 0 && newWindow && window.open) { // If link is external
				n += " href=\"javascript:newWin('" + x2 + "');\"";
			} else { // If link is internal
				n += " href=\"" + x2 + "\"";
			}
			if (dotOvr != "") {
				n += " onmouseover=\"Chng('" + eval(boxArray + '.id') + "-" + i + "','1');boxOver('');\"";
				n += " onfocus=\"Chng('" + eval(boxArray + '.id') + "-" + i + "','1');boxOver('');\"";
				n += " onmouseout=\"Chng('" + eval(boxArray + '.id') + "-" + i + "','0');boxOut('');\"";
				if (i == N) {
					n += " onblur=\"Chng('" + eval(boxArray + '.id') + "-" + i + "','0');";
					n += "hideLayer('" + eval(boxArray + '.id') + "');\"";
				} else {
					n += " onblur=\"Chng('" + eval(boxArray + '.id') + "-" + i + "','0');boxOut('');\"";
				}
			} else {
				n += " onmouseover=\"boxOver('');\"";
				n += " onfocus=\"boxOver('');\"";
				n += " onmouseout=\"boxOut('');\"";
				if (i == N) {
					n += " onblur=\"hideLayer('" + eval(boxArray + '.id') + "');\"";
				} else {
					n += " onblur=\"boxOut('');\"";
				}
			}
			n += ">" + x1 + ".\</a>\<br />";
		}
		n += "\</td>\</tr>";
	}

	n += "\</table>\</td>\</tr>\</table>\</td>\</tr>\</table>\</div>";
	return n;
}

function newWin(z) { // Open new window script
	var xWin = 0, yWin = 0; // Default window position, top-left corner
	var web = z; // Web document to load
	if (!web) web = Host + "/"; // Default document is Host if no address defined
	if (window.screen) { // Center window, if centering is supported
		xWin = eval( (window.screen.width / 2) - (winWidth / 2) - 10 );
		yWin = eval( (window.screen.height / 2) - (winHeight / 2) - 50 );
	}	
	var optns = "directories=0,toolbar=" + winToolbar; // Browser bars
	optns += ",location=" + winLocation + ",status=" + winStatus; // Browser bars
	optns += ",menubar=" + winMenubar + ",resizable=" + winResizable; // Browser bars
	optns += ",scrollbars=" + winScrollbars; // Browser bars
	optns += ",width=" + winWidth + ",height=" + winHeight; // New window size
	optns += ",left=" + xWin + ",top=" + yWin; // Window position (omAll)
	optns += ",screenX=" + xWin + ",screenY=" + yWin; // Window position (omLyr)
	var remote = window.open(web,"popWin",optns);
	remote.focus();
}

function mouseTracker(e) {
    e = e || window.Event || window.event;
    window.pageX = parseInt(e.pageX,10) || e.clientX;
    window.pageY = parseInt(e.pageY,10) || e.clientY;
}

function setMouseTracker() {
    if (document.captureEvents) {
        document.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
    }
    document.onmousemove = this.mouseTracker;
    document.onmouseup = this.hideMenu;
}

 document.onmousemove = mouseTracker;
