/* ============================================================================
 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,p_left,p_menu_name) { // 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(document.body.offsetWidth);
			if (p_menu_name=="left") {
				if(document.body.offsetWidth >= 800){ // do phan giai 800/600
					x_left = (document.body.offsetWidth - 780) / 2 + p_left;
				}else{
					x_left = p_left;
				}
			}else {
				if(document.body.offsetWidth >= 800){ // do phan giai 800/600
					x_left = document.body.offsetWidth/2 + 390- 2*p_left;
				}else{
					x_left = document.body.offsetWidth- p_left;
				}
			}
			layerElement.style.left = x_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()",100);
}

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*boxDelay);
}

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,p_row_style,p_row_over_style){ // Build the dynamic box
	var elmtName = boxArray.name;
	var boxWth;
	bdSize = 0;
	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 + "\"";
	n += " width=\"" + boxWth + "\"";
	n += " cellspacing=\"0\" cellpadding=\"0 \"\>";
	for (var i = 0; i <= N; i++ ) { // Process box list
		n += "<tr class='"+p_row_style+"' onmouseover=\"this.className='"+p_row_over_style+"';\" onmouseout=\"this.className='"+p_row_style+"';\"><td>";
		var x1 = eval(boxArray + "[" + i + "].title");
		var x2 = eval(boxArray + "[" + i + "].url");
		
		n += "<a ";
		n += " href=\"" + x2 + "\"";
		
		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></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;
