/* ACTIVE MENU

*** NOTES ***
[2005-09-20 bsweeney]
Please refer to the documentation at http://www.project2061.org/includes/CodeLibrary/ActiveMenu/activeMenu.manual.htm
*/

function activeMenu_addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

function activeMenu_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 activeMenu_init (e) {
	if (document.getElementById && document.getElementsByTagName) {
		var i, j, k;
		var objDOMNode;
		var intStoryHeight = 0;
		var intStoryBoxHeight = 0;
		var activeMenuBox = document.getElementById('activeMenuBox');
		
		/* Add the activeMenu class to the surrounding <div>, checking for any previous class name.
		The class name check is used often to avoid removing classes that the user has added or that 
		are added programmatically. Applying this class during menu processing ensures that unsupported 
		browsers do not apply styles defined with interactivity in mind (such as width). */
		activeMenuBox.className += (activeMenuBox.className.length > 0 ? ' ' : '') + 'activeMenu';
		
		/* Temporary array containing the story headlines */
		var araStoryLinks = new Array();
		
		/* Temporary DOM object reference to the definition list */
		objDOMNode = activeMenuBox.getElementsByTagName('dl')[0];
		
		/* This is a fix for Explorer when it is in standards mode. If scrollbars are added to a box they 
		are not included in the width (and height?) calculation of that box. Unfortunately, the scrollbars 
		still affect the box's layout on the page because the width of the scrollbars is added on top of the 
		calculated or assigned width of the box. */
		if (activeMenu_browser.ie6 && document.compatMode && document.compatMode != "BackCompat") {
			objDOMNode.style.width = (parseInt(objDOMNode.offsetWidth) - 25) + 'px';
		}
		
		/* By starting j at -1 we'll be able to sync with the array indexes */
		j = -1;
		
		/* Parse all the child nodes of the definition list. Looking for <dt> (story headline) 
		and <dd> (story detail). */
		for (i = 0; i < objDOMNode.childNodes.length; i++) {
			if (objDOMNode.childNodes[i].nodeName == 'DT') {
				j++;
				/* Add the headline link reference to the temporary array */
				araStoryLinks.push(objDOMNode.childNodes[i].getElementsByTagName('a')[0]);
				objDOMNode.childNodes[i].className += (objDOMNode.childNodes[i].className.length > 0 ? ' ' : '') + 'activeMenuStory'+j;
				/* Add the <dt> reference to the global array holding references to the story <dt>s and <dd>s */
				araStoryDetail.push(objDOMNode.childNodes[i]);
			} else if (objDOMNode.childNodes[i].nodeName == 'DD') {
				objDOMNode.childNodes[i].className += (objDOMNode.childNodes[i].className.length > 0 ? ' ' : '') + 'activeMenuStory'+j;
				/* Add the <dd> reference to the global array holding references to the story <dt>s and <dd>s */
				araStoryDetail.push(objDOMNode.childNodes[i]);
			}
		}
		
		/* Create a new unordered list object. */
		var objDOMList = document.createElement("ul");
		var objDOMListElem;
		
		/* For each headline in the araStoryLinks array create a list item and append it to the UL just created. */
		for (i = 0; i < araStoryLinks.length; i++) {
			objDOMListElem = document.createElement("li");

			objDOMNode = araStoryLinks[i].cloneNode(true)
			/* If this is the last element of the array give it the class "lastChild" so that no border is displayed. */
			i == araStoryLinks.length-1 ? objDOMNode.className += (objDOMNode.className.length > 0 ? ' ' : '') + 'lastChild' : null;
			objDOMNode.id = 'activeMenuLink'+i;
			/* Since we're creating this element we're assuming there won't be any pre-existing mouseover events. 
			With this in mind we can assign the mouseover using the property rather than adding an event listener. 
			This also eases the event processing because we can specify the variables needed to determine which 
			story was highlighted, rather than trying to determine it based on event models that vary from browser 
			to browser. */
			eval("objDOMNode.on"+activeMenu_strEvent+" = function () { activeMenu_change("+i+"); return false; }");
			araStoryList.push(objDOMNode);
			objDOMListElem.appendChild(objDOMNode);
			objDOMList.appendChild(objDOMListElem);
		}
		
		document.getElementById('activeMenuList') ? objDOMNode = document.getElementById('activeMenuList') : objDOMNode = activeMenuBox;
		activeMenu_objInsertBefore.length > 0 && document.getElementById(activeMenu_objInsertBefore) ? objDOMNode.insertBefore(objDOMList, document.getElementById(activeMenu_objInsertBefore)) : objDOMNode.appendChild(objDOMList);
		
		/* If the user set the activeMenu_bolResize2Fit resize boolean to true we need to resize the story window 
		to fit the max height. Currently this only works if no height is set (via CSS) on the DL containing the 
		story items or the height is less than that of the longest story item. */
		if (activeMenu_bolResize2Fit) {
			objDOMNode = activeMenuBox.getElementsByTagName('dl')[0];
			/* intStorySectionHeight stores the value of each DT or DD in a story. */
			var intStorySectionHeight;
			var intDLNodes;
			/* For each story change the display using the activeMenu_change() function and then 
			calculate the height of the story in two ways, based on the height of the DL and 
			on the height of the story DT/DDs. Compare the height of the DL against the height of 
			the DT/DDs to find the minimum height to assign to the DL. */
			for (var intStoryIndex = 0; intStoryIndex < araStoryList.length; intStoryIndex++) {
				activeMenu_change(intStoryIndex);
				/* intStorySectionHeight stores the sum of the height of the DT/DDs contained within a story. 
				intDLNodes stores the number of DT/DDs in a story (used for a fudge factor when calculating 
				the height of story based on the DT/DDs. */
				intStorySectionHeight = 0;
				intDLNodes = 0;
				/* We don't know which nodes are showing, so parse them all looking for a class of "storySelected" */
				for (i = 0; i < objDOMNode.childNodes.length; i++) {
					if ((objDOMNode.childNodes[i].nodeName == 'DT' || objDOMNode.childNodes[i].nodeName == 'DD') && objDOMNode.childNodes[i].className.indexOf('storySelected') != -1) {
						objIMGNodes = objDOMNode.childNodes[i].getElementsByTagName('img');
						/* If the node contains an image then we need to make a determination of the relative area 
						used by the node. This is a percentage of the height of the image based on the area of the 
						image and the area of a box containing the image that covers the width of the containing 
						list. */
						if (objIMGNodes.length > 0) {
							intStorySectionHeight += parseInt(objIMGNodes[0].offsetHeight)*((parseInt(objIMGNodes[0].offsetHeight)*parseInt(objIMGNodes[0].offsetWidth))/(parseInt(objIMGNodes[0].offsetHeight)*parseInt(objDOMNode.offsetWidth)*4));
						} else if (objDOMNode.childNodes[i].offsetHeight > 0) {
							intStorySectionHeight += parseInt(objDOMNode.childNodes[i].offsetHeight);
							intDLNodes++;
						}
					}
				}
				/* Height comparison and display setting. If the sum of the sections is used then a fudge factor 
				is added to account for margins and based on the number of DT/DDs (intDLNodes). */
				intStoryBoxHeight < parseInt(objDOMNode.offsetHeight) ? intStoryBoxHeight = parseInt(objDOMNode.offsetHeight) : null;
				intStoryBoxHeight < intStorySectionHeight ? intStoryBoxHeight = intStorySectionHeight + (intDLNodes*5) : null
			}
			/* Set the height of the DL. */
			intStoryBoxHeight > 0 ? objDOMNode.style.height = (intStoryBoxHeight) + 'px' : null;
		}
		
		/* ActiveMenu has been set up. Now we need to call the first item so that something is visible 
		on screen. The variable activeMenu_activeItem indicates the index of the item to show (zero-based). */
		activeMenu_change(activeMenu_intActiveItem);
	}
}

function activeMenu_change (storyID) {
	var i, j, k
	for (i = 0; i < araStoryList.length; i++) {
		if (araStoryList[i].id == 'activeMenuLink'+storyID) {
			araStoryList[i].className += (araStoryList[i].className.length > 0 ? ' ' : '') + 'storySelected';
		} else {
			araStoryList[i].className=araStoryList[i].className.replace(/\s?storySelected/ig, '');
		}
	}
	for (i = 0; i < araStoryDetail.length; i++) {
		if (araStoryDetail[i].className.indexOf('activeMenuStory'+storyID) != -1) {
			araStoryDetail[i].className += (araStoryDetail[i].className.length > 0 ? ' ' : '') + 'storySelected';
		} else if (araStoryDetail[i].className.indexOf('activeMenuStory'+storyID) == -1) {
			araStoryDetail[i].className = araStoryDetail[i].className.replace(/\s?storySelected/ig, '');
		}
	}
	
	// *** MOD BEGIN ~ 2006-01-26 ~ BSWEENEY ***
	// Modification for HS BIO report, set the border color on the DL
	if (storyID == 0) { strHEXColor = '#66985F'; }
	if (storyID == 1) { strHEXColor = '#B3A92E'; }
	if (storyID == 2) { strHEXColor = '#A86318'; }
	if (storyID == 3) { strHEXColor = '#5466A1'; }
	document.getElementById('activeMenuBox').getElementsByTagName('dl')[0].style.borderColor = strHEXColor;
}

var araStoryList = new Array();
var araStoryDetail = new Array();
var activeMenu_browser = new activeMenu_bwcheck();
var activeMenu_evtTimeout
activeMenu_addEvent(window, 'load', activeMenu_init, false);

/* BEGIN USER-DEFINED VARIABLES */
var activeMenu_bolResize2Fit = false;
var activeMenu_bolResetDefault = false;
var activeMenu_intActiveItem = 0;
var activeMenu_strEvent = 'mouseover';
var activeMenu_objInsertBefore = '';
/* END USER-DEFINED VARIABLES */
