﻿/*
DHTML REPOSITION CODE
This file *should* work for all current common browsers.  It will find the relative x an y coordinates
of each layer's parent element.


Instructions for use

-Each menu is laid out in a div tag in the main body, which has an id="subnav*Layer", where * 
is a unique integer.

-Each menu must have a parent element in the main page, which has an id="subnav*".  The * 
corresponds to the div tag's id *

-Either in a CSS style sheet or a style tag in the main page, each menu must have an id
designation as follows:

	#subnav*Layer { position: absolute; z-index: **; visibility: hidden; display: block; }

	where * equals the menu's number and ** = the menu's number + 1

-Each table cell in the menu needs the following code:

	onmouseover="highlightSubnav(this); return true;" onmouseout="unlightSubnav(this); return true;"

-each table cell in the parent element must have the following code:

	onmouseover="highlightSubnav(this); showSubnav('subnav*Layer'); return true;" onmouseout="unlightSubnav(this); return true;" onclick="location.href='(your link here)';"

-Note that in the event your parent elements are arrayed vertically rather than horizontally, each parent element must be in its own table!

-Set the configuration variables, below.
-this file must be included as a src for the main file in order to work:
	<script type="text/javascript" language="JavaScript" src="/_js/dhtml.js"></script>


*/
	//alert("test1");

	//	Platform flags
	var agt=navigator.userAgent.toLowerCase();
	var use_gebi = (document.getElementById) ? true : false;
	var ie4flag = (document.all) ? true : false;
	var ns4flag = (document.layers) ? true : false;
	var ns6flag = (use_gebi && !document.all) ? true : false;
	var safariflag = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
	var windowsflag = (agt.indexOf('window')!=-1)?true:false;
	var macIEflag = ((agt.indexOf('msie 5')!=-1) && (agt.indexOf('mac')!=-1))?true:false;
	
	var subnavShowing = false;
	var subnavExit = false;
	
	var x = 0;  // These variables are reset by the Get Location functions
	var y = 0;

	//var ChildnavExit=false;	
	
//
//	CONFIGURATION VARIABLES	
//
	var enableDebug = 'no'; 	//options are 'alert', 'status', 'no'
	
	var subnav_layers = 5;		// Number of layers
	var subnav_childmax = 0;	//Number of child layers
	var subnavOffsetX = 128; 	// Offsets are the distance that the menu should offset from the 
	var subnavOffsetY = 150; 		// parent objects - usually the height/width of the parent object and a border, if any.
	
	var childnavOffsetX = 0; 	// Offsets are the distance that the menu should offset from the 
	var childnavOffsetY = 1; 		// parent objects - usually the height/width of the parent object and a border, if any.
	
	var ie4OffsetX = 145;   	// absolute location of the top-left item of the parent objects
	var ie4OffsetY = 65;		// because IE 4 doesn't recognize the DOM parent heirarchy of nested tables
	
	var ChildnavExit=true;
//	
//	END CONFIGURATION VARIABLES
//


	//debug function
	function doDebug(someOutput) {
		if (enableDebug == 'alert') {
			alert(someOutput);
		} else if (enableDebug == 'status') {
			status = someOutput;
		}
		return true;
	}
	
	
	//subnav positioning high-level mgmt
	function repositionLayers() {
		doDebug('repositionLayers()');
		for (i = 1; i <= subnav_layers; i++) {
			setLayerPosition("subnav" + i);
		}
		return true;
	}
	
	// layer positioning
	function setLayerPosition(myobjectName) {
		doDebug('setLayerPosition(' + myobjectName + ')');
		var mylayerName = myobjectName + "Layer";
		//alert(mylayerName);
		if (getPosition(myobjectName)) {
			tempx = x + subnavOffsetX;
			tempy = y + subnavOffsetY;
			
			// Logic to find position
			if (safariflag) { 
				// now set layer top and left accordingly
				document.getElementById(mylayerName).style.left = (tempx) + "px";
				document.getElementById(mylayerName).style.top = (tempy - 1) + "px";
				}
			else if (use_gebi && ns6flag) {  
				// now set layer top and left accordingly
				document.getElementById(mylayerName).style.left = (tempx) + "px";
				document.getElementById(mylayerName).style.top = (tempy - 1) + "px";
				}
			else if (use_gebi && !windowsflag) {
				//alert(mylayerName);
				// now set layer top and left accordingly
				document.all[mylayerName].style.pixelLeft = (tempx);
				document.all[mylayerName].style.pixelTop = (tempy - 1);
				}
			else if (use_gebi && windowsflag) {
				// now set layer top and left accordingly
				document.all[mylayerName].style.pixelLeft = (tempx);
				document.all[mylayerName].style.pixelTop = (tempy - 1);
				//alert (windowsflag);
				}
			else if (ie4flag) {
				// now set layer top and left accordingly
				document[mylayerName].style.pixelLeft = tempx + 1;
				document[mylayerName].style.pixelTop = tempy;
				}
			else {
				x=0; y=0; return false;
				}
//			alert(tempx + ", " + tempy);
			return true;
		}
	}
	
	// layer hiding and showing
	function showLayer(lay) {
		doDebug('showLayer(' + lay + ')');
		if (ie4flag) {document.all[lay].style.visibility = "visible";}
		if (ns4flag) {document.layers[lay].visibility = "show";}
		if (ns6flag) {document.getElementById(lay).style.visibility="visible";}
		return true;
	}
	
	function hideLayer(lay) {
		doDebug('hideLayer(' + lay + ')');
		if (ie4flag) {document.all[lay].style.visibility = "hidden";}
		if (ns4flag) {document.layers[lay].visibility = "hide";}
		if (ns6flag) {document.getElementById(lay).style.visibility="hidden";}
		return true;
	}
	
	//returns position of object in page
	function getPosition(myobjectName) {
		doDebug('getPosition(' + myobjectName + ')');
		// Logic to find position
		if (use_gebi) {
		doDebug('using gebi...');
			var o=document.getElementById(myobjectName);
			x=setLayerPositionPageOffsetLeft(o);
			y=setLayerPositionPageOffsetTop(o);
			}
		else if (ie4flag) {
		doDebug('usingie4flag...');
			x=setLayerPositionPageOffsetLeft(document.all[myobjectName]);
			y=setLayerPositionPageOffsetTop(document.all[myobjectName]);
			}
		else {
			doDebug("default");
			x=0;
			//y=0;
			return false; 
			}
		return true;
	}
	
	// Functions for IE, mozilla, safari to get position of an object
	function setLayerPositionPageOffsetLeft (el) {
		doDebug('setLayerPositionPageOffsetLeft(' + el + ')');
		if (el != "" && el != null) {
			if (ie4flag && !use_gebi) {
				if (el.offsetLeft) {
					var ol=el.offsetLeft;
					while ((el=el.offsetParent) != null) { 
						ol += el.offsetLeft; 
					}
					ol += ie4OffsetX;
				} else {
					var ol=0;
				}
			} else {
				var ol=el.offsetLeft;
				while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
			}
		} else {
			var ol=0;
		}
		if (macIEflag) {
			var macMarginLeft=parseInt(document.body.currentStyle.marginLeft);
			ol += macMarginLeft;
		}
		return ol;
	}
	
	function setLayerPositionPageOffsetTop (el) {
		doDebug('setLayerPositionPageOffsetTop(' + el + ')');
		if (el != "" && el != null) {
			if (ie4flag && !use_gebi) {
				if (el.offsetTop) {
					var ot=el.offsetTop;
					while((el=el.offsetParent) != null) { ot += el.offsetTop; }
				} else {
					var ot=0;
				}
					ot += ie4OffsetY;
			} else {
				var ot=el.offsetTop;
				while((el=el.offsetParent) != null) { ot += el.offsetTop; }
			}
		} else {
			var ot=0;
		}
		if (macIEflag) {
			var macMarginTop=parseInt(document.body.currentStyle.marginTop);
			ot += macMarginTop;
		}

		return ot;
	}
	
	//subnav hiding and showing high-level mgmt
	function showSubnav(lay) {
		doDebug('showSubnav(' + lay + ') mgmt function');
		for (i = 1; i <= subnav_layers; i++) {
			hideLayer("subnav" + i + "Layer");
		}
		showLayer(lay);
		subnavShowing = true;
		subnavExit = false;
		setTimeout("checkSubnav()",1000);
	doDebug(lay);
		return true;
	}
	
	
	//periodic check to turn off subnav if user has left menu area
	function checkSubnav() {
		doDebug('checkSubnav()');
		if ((subnavShowing) && (subnavExit)) {
			for (i = 1; i <= subnav_layers; i++) {
				hideLayer("subnav" + i + "Layer");
			}
			subnavExit = false;
		} else if (subnavShowing) {
			setTimeout("checkSubnav()",500);
		}
		return true;
	}


	//subnav entry highlight
	function highlightSubnav(someObject) {
		doDebug('highlightSubnav(' + someObject + ')');
		subnavExit=false;
		return true;
	}
	
	//subnav entry unlight
	function unlightSubnav(someObject) {
		doDebug('unlightSubnav(' + someObject + ')');
		subnavExit=true;
		return true;
	}








//	EVERYTHING BELOW HERE IS FOR THE SECOND-TIER (CHILD) NAV MENUS.



	//subnav hiding and showing high-level mgmt
	function showChildnav(lay) {
		doDebug('showChildnav(' + lay + ') mgmt function');
		for (i = 101; i <= subnav_childmax+100; i++) {
			hideLayer("subnav" + i + "Layer");
		}
		showLayer(lay);
		childnavShowing = true;
		ChildnavExit = false;
		setTimeout("checkChildnav()",500);
		doDebug(lay);
		return true;
	}
	
	//periodic check to turn off childnav if user has left menu area
	function checkChildnav() {
		doDebug('checkChildnav()');
		if ((childnavShowing) && (ChildnavExit)) {
			for (i = 101; i <= subnav_childmax+100; i++) {
				hideLayer("subnav" + i + "Layer");
			}
			ChildnavExit = false;
		} else if (childnavShowing) {
			setTimeout("checkChildnav()",500);
		}
		return true;
	}

	//Childnav entry highlight
	function highlightChildnav(someObject) {
		doDebug('highlightChildnav(' + someObject + ')');
		ChildnavExit=false;
		return true;
	}
	
	//Childnav entry unlight
	function unlightChildnav(someObject) {
		doDebug('unlightChildnav(' + someObject + ')');
		ChildnavExit=true;
		return true;
	}

	//Childnav positioning high-level mgmt
	function repositionChildLayers() {
		doDebug('repositionChildLayers()');
		for (i = 101; i <= subnav_childmax+100; i++) {
			setChildPosition("subnav" + i);
		}
		return true;
	}

	// layer positioning
	function setChildPosition(myobjectName) {
		doDebug('setChildPosition(' + myobjectName + ')');
		var mylayerName = myobjectName + "Layer";
		if (getPosition(myobjectName)) {
			tempx = x + childnavOffsetX;
			tempy = y + childnavOffsetY;
			
			// Logic to find position
			if (safariflag) { 
				// now set layer top and left accordingly
				document.getElementById(mylayerName).style.left = (tempx) + "px";
				document.getElementById(mylayerName).style.top = (tempy - 10) + "px";
				}
			else if (use_gebi && ns6flag) {  
				// now set layer top and left accordingly
				document.getElementById(mylayerName).style.left = (tempx) + "px";
				document.getElementById(mylayerName).style.top = (tempy - 1) + "px";
				}
			else if (use_gebi && !windowsflag) {
				// now set layer top and left accordingly
				document.all[mylayerName].style.pixelLeft = (tempx);
				document.all[mylayerName].style.pixelTop = (tempy - 1);
				}
			else if (use_gebi && windowsflag) {
				// now set layer top and left accordingly
				//alert(mylayerName);
				document.all[mylayerName].style.pixelLeft = (tempx);
				document.all[mylayerName].style.pixelTop = (tempy - 1);
				//alert (windowsflag);
				}
			else if (ie4flag) {
				// now set layer top and left accordingly
				document[mylayerName].style.pixelLeft = tempx + 1;
				document[mylayerName].style.pixelTop = tempy;
				}
			else {
				x=0; y=0; return false;
				}
			return true;
		}
	}
	

