/* ACTIVE MENU

*** NOTES ***
[2006-02-28 bsweeney]
Please refer to the documentation at http://www.project2061.org/includes/CodeLibrary/FloatMenu/floatMenu.manual.htm
*/

function menu_bwcheck () {
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.safari=this.agent.indexOf("Safari")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}

function menu_addEvent (obj, evType, fn, useCapture) {
	// Cross-browser code to add an event handler to a DOM object
	// http://www.scottandrew.com/weblog/articles/cbs-events
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

function menu_getStyle (el,styleProp) {
	// Cross-browser code to check the style value for an element
	// http://www.quirksmode.org/dom/getstyles.html
	
	// *** MOD BEGIN ~ 2006-01-19 ~ BSWEENEY ***
	// Modified to use object reference rather than ID text
	// var x = document.getElementById(el);
	var x = el;
	// *** MOD END ***
	
	var y = null;
	if (window.getComputedStyle) {
		y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
	} else if (x.currentStyle) {
		y = eval('x.currentStyle.' + styleProp);
	}
	return y;
}

function findPosX (obj) {
	// Cross-browser code to find the x position of an object
	// http://www.quirksmode.org/js/findpos.html
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY (obj) {
	// Cross-browser code to find the position y position of an object
	// http://www.quirksmode.org/js/findpos.html
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


function menu_setmenupos () {
	// Scroll event for the window object. First clear our any current 
	// instance of menu_settle() then call menu_settle() to bring the menu 
	// back into view on IE6-.
	clearTimeout(menu_settleID);
	menu_settle();
}

function menu_settle () {
	/* Declare local variables */
	var objMenu;
	var docScroll;
	var menuStyleTop;
	
	/* Initialize the variable objMenu to the menu container using the appropriate access method. 
	Otherwise exit the function (fallback for incomplete browser support). */
	if (menu_bw.dom) {
		objMenu = document.getElementById("menu");
	} else if (menu_bw.ie) {
		objMenu = document.all.menu;
	} else {
		return true;
	}
	
	/* For comparison with menu after repositioning to determine if function will be called again. */
	menuStyleTop = parseInt(objMenu.style.top);
	
	/* Determine the current scroll from the top of the page (using various browser methods). 
	Otherwise set the scroll to zero and exit the function. */
	if (window.pageYOffset) {
		docScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		docScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		docScroll = document.body.scrollTop;
	} else {
		docScroll = 0;
		return true;
	}
	
	/* Reposition the menu based on whether or not the page is scrolled beyond the control point
	(the value of absoluteTop minus the value of the offset). */
	if (docScroll > (menu_absoluteTop-menu_offset)) {
		/* If the absoluteTop and offsetAtTop are not the same then the menu is positioned based 
		on a container object and absoluteTop needs to be taken into account when performing the 
		position calculation. Subtract this value from docScroll to determine the current window 
		offset from the top of the container object. */
		if (menu_absoluteTop != menu_offsetAtTop) {
			docScroll -= menu_absoluteTop;
		}
		objMenu.style.top = parseInt(objMenu.style.top) + Math.ceil((docScroll + menu_offset - parseInt(objMenu.style.top)) * .05) + 'px';
	} else {
 		objMenu.style.top = parseInt(objMenu.style.top) + Math.floor((menu_offsetAtTop - parseInt(objMenu.style.top)) * .05) + 'px';
	}
	
	/* Call the settle function only if the menu is still moving to its resting point. */
	if (menuStyleTop != parseInt(objMenu.style.top)) {
		menu_settleID = setTimeout("menu_settle()", 15);
	}
}

function menu_init () {
	/* Declare local variables */
	var objMenu;
	var docScroll;
	
	/* Initialize the variable objMenu to the menu container using the appropriate access method. 
	Otherwise exit the function (fallback for incomplete browser support). */
	if (menu_bw.dom) {
		objMenu = document.getElementById("menu");
	} else if (menu_bw.ie) {
		objMenu = document.all.menu;
	} else {
		return true;
	}
	
	/* If the variable menuSelect has been set to something other than -1 (as in 0+) modify the 
	class of the list item associated with that index so that it is highlighted. */
	if (menuSelect > -1) {
		araTags = objMenu.getElementsByTagName("li");
		if (araTags.length >= menuSelect) {
			if (araTags[menuSelect].className.length > 0) {
				araTags[menuSelect].className += " selected";
			} else {
				araTags[menuSelect].className += "selected";
			}
		}
	}
	
	/* Get the current style values for top and left and set the JS style object properties to 
	these values. */
	objMenu.style.top = parseInt(menu_getStyle(objMenu, "top")) + 'px';
	objMenu.style.left = parseInt(menu_getStyle(objMenu, "left")) + 'px';
	
	/* Populate the offsetAtTop and absoluteTop values. */
	menu_offsetAtTop = parseInt(menu_getStyle(objMenu, "top"));
	menu_absoluteTop = findPosY(objMenu);
	
	if (menu_bw.dom && !menu_bw.ie) {
		/* Browsers other than IE6- support fixed positioning. Use this rather than the "sliding" menu 
		code to keep the menu on the screen. */
		objMenu.style.top = menu_absoluteTop + 'px';
		objMenu.style.left = findPosX(objMenu) + 'px';
		objMenu.style.position = 'fixed';
	} else if (menu_bw.ie) {
		/* Browser events are needed to control the position of the menu if the window view changes. 
		The pertinent events are onscroll and onresize. */
		menu_addEvent(window, 'scroll', menu_setmenupos, false);
		menu_addEvent(window, 'resize', menu_setmenupos, false);
		/* Start the menu position control function. */
		menu_setmenupos();
	}
}

var menu_bw=new menu_bwcheck();
var menu_settleID = 0; // variable to hold the timeout object
var menu_absoluteTop = 0; // absolute highest position (on the page) the menu can be displayed. also absolute position of the menu at start. set by menu_init().
var menu_offsetAtTop = 0; // x value of the menu when the page is above the float point (absolutetop-offset).
menu_addEvent(window, 'load', menu_init, false);

/* BEGIN USER-DEFINED VARIABLES */
var menu_offset = 50; // offset of the menu from the top of the currently visible area
var menuSelect = -1; // item in the menu to be highlighted (zero-based index)
/* END USER-DEFINED VARIABLES */
