window.onload = globalsInit;

var isIE = navigator.appName.indexOf("Microsoft") > -1;

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
var isIE6 = false;
if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version == '6')
{
    isIE6 = true;
}

function idle() { }

function globalsInit() {
	rolloversInit();
	rightnavInit();
	headerFormInit();
	clicktipsInit();
	bodyexpInit();
}

function rolloversInit() {
	if (!document.getElementById) return;
	
	var arrPreload = new Array();
	var strSrcTmp;
	var arrImages = document.getElementsByTagName('img');
	
	for (var i=0; i<arrImages.length; i++) {
		if (arrImages[i].className.indexOf("imgOver") == 0) {
			var src = arrImages[i].src;
			var imgtype = src.substring(src.lastIndexOf("."), src.length);
			if ( src.match("_on.gif")==null )
			    var altsrc = src.replace(imgtype, "_on" + imgtype);
			else
			    var altsrc = src;

			arrImages[i].setAttribute('altsrc', altsrc);
			arrPreload[i] = new Image();
			arrPreload[i].src = altsrc;
			
			arrImages[i].onmouseover = function() {
				strSrcTmp = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('altsrc'));
			}

			arrImages[i].onmouseout = function() {
				if (!strSrcTmp) 
				   strSrcTmp = this.getAttribute('src').replace("_on" + imgtype, imgtype);
				this.setAttribute('src', strSrcTmp);
				     
			}

		}
	}
}

var arrRightnav = new Array()

function rightnavInit() {
	var arrBoxes = document.getElementsByTagName('div');
	
	for (var i=0; i<arrBoxes.length; i++) {
		if (arrBoxes[i].className == "rightnavBox") {
			if (document.getElementById(arrBoxes[i].id.replace("_closed","_open")).className !== "rightnavBox_active") {
				arrRightnav[arrRightnav.length] = { Name: arrBoxes[i].id.replace("_closed",""), Open: false }
				arrBoxes[i].style.display = "block";
				arrBoxes[i].onclick = function() {
					rightnavRefresh(this.id.replace("_closed",""));
				}
			}
		}
	}

	var arrImages = document.getElementsByTagName('img');
	
	for (var i=0; i<arrImages.length; i++) {
		if (arrImages[i].className == "rightnavBtns_open") {
			arrImages[i].onclick = function() {
				rightnavRefresh(this.parentNode.id.replace("_open",""));
			}
		}
	}
}

function rightnavRefresh(strRightnav) {
	for (var i=0; i<arrRightnav.length; i++) {
		currId = arrRightnav[i].Name + ((arrRightnav[i].Open) ? "_open" : "_closed");
		document.getElementById(currId).style.display = "none";
		if (arrRightnav[i].Name == strRightnav) {
			arrRightnav[i].Open = (arrRightnav[i].Open) ? false : true;
		}
	}

	for (var i=0; i<arrRightnav.length; i++) {
		currId = arrRightnav[i].Name + ((arrRightnav[i].Open) ? "_open" : "_closed");
		document.getElementById(currId).style.display = "block";
	}
}

var arrBodyexp = new Array()

function bodyexpInit() {
/*
	var arrLinks = document.getElementsByTagName('a');
	
	for (var i=0; i<arrLinks.length; i++) {
		if (arrLinks[i].className == "bodyexpLink_closed") {
			var currBox = arrLinks[i].parentNode.parentNode;
			arrBodyexp[arrBodyexp.length] = { Name: currBox.id.replace("_closed",""), Open: false }
			currBox.style.display = "block";
			arrLinks[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_closed",""));
			}
		}
		
		if (arrLinks[i].className == "bodyexpLink_open") {
			arrLinks[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_open",""));
			}
		}
	}

*/
	var arrSpans = document.getElementsByTagName('span');
	for (var i=0; i<arrSpans.length; i++) {
		if (arrSpans[i].className == "bodyexpLink_closed") {
			var currBox = arrSpans[i].parentNode.parentNode;
			arrBodyexp[arrBodyexp.length] = { Name: currBox.id.replace("_closed",""), Open: false }
			currBox.style.display = "block";
			arrSpans[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_closed",""));
			}
			arrSpans[i].onmouseover = function() {
				this.style.color = "#f37e0a";
			}
			arrSpans[i].onmouseout = function() {
				this.style.color = "#095cad";
			}
		}
		
		if (arrSpans[i].className == "bodyexpLink_open") {
			arrSpans[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_open",""));
			}
			arrSpans[i].onmouseover = function() {
				this.style.color = "#f37e0a";
			}
			arrSpans[i].onmouseout = function() {
				this.style.color = "#095cad";
			}
		}

		//if we are opening all of them, create a click function to do so
		if(arrSpans[i].className == "bodyexpLink_openAll") {
			arrSpans[i].onclick = function() {
				doExpandAll(this);
			}
			arrSpans[i].onmouseover = function() {
				this.style.color = "#f37e0a";
			}
			arrSpans[i].onmouseout = function() {
				this.style.color = "#095cad";
			}
		}
	}


	var arrImages = document.getElementsByTagName('img');
	
	for (var i=0; i<arrImages.length; i++) {
		if (arrImages[i].className == "bodyexpImg_closed") {
			var currBox = arrImages[i].parentNode.parentNode;
			arrBodyexp[arrBodyexp.length] = { Name: currBox.id.replace("_closed",""), Open: false }
			currBox.style.display = "block";
			arrImages[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_closed",""));
			}
		}
		
		if (arrImages[i].className == "bodyexpImg_open") {
			arrImages[i].onclick = function() {
				bodyexpRefresh(this.parentNode.parentNode.id.replace("_open",""));
			}
		}
	
		if (arrImages[i].className == "bodyexpArrow_openAll") {
			arrImages[i].onclick = function() {
				doExpandAll(this.nextSibling);
			}
		}
	}

}

function doExpandAll(objLink)
{
		//find out if we should expand.
		var bShouldExpand = objLink.firstChild.data == "expand all" ? true : false;

		//loop through all bodies on the page and change them
		for(var j = 0; j < arrBodyexp.length; j++)
		{
			//change the display of the old one first
			var currId = arrBodyexp[j].Name + ((arrBodyexp[j].Open) ? "_open" : "_closed");
			document.getElementById(currId).style.display = "none";
		
			//set our open flag
			arrBodyexp[j].Open = bShouldExpand;
			
			//set our style based on the flag
			currId = arrBodyexp[j].Name + ((arrBodyexp[j].Open) ? "_open" : "_closed");
			document.getElementById(currId).style.display = "block";
		} 
		
		//make sure we refresh the text
		refreshExpandAllText(bShouldExpand);
}


//////////////////////////////////////////////////////////////////////////////////
//will iterate though links, find the expand all links, and change the text 
//////////////////////////////////////////////////////////////////////////////////
function refreshExpandAllText(bJustExpanded)
{
	//grab all the links
	var arrSpans = document.getElementsByTagName('span');

	//loop through links
	for (var i=0; i<arrSpans.length; i++) 
	{
		//only do this to body open all links
		if(arrSpans[i].className == "bodyexpLink_openAll")
		{	
			//now change the text appropriately
			if(bJustExpanded) {
				arrSpans[i].firstChild.data = "close all";
			} else {
				arrSpans[i].firstChild.data = "expand all";
			}
			arrSpans[i].style.color = "#095cad";
		}
	}
	
	//grab all the orange arrows and swap them
	var arrImages = document.getElementsByTagName('img');
	
	for (var i=0; i<arrImages.length; i++) {
		if (arrImages[i].className == "bodyexpArrow_openAll") {
			var newSrc = (arrImages[i].src.indexOf("_closed") > -1) ? arrImages[i].src.replace("_closed","_open") : arrImages[i].src.replace("_open","_closed");
			if (arrImages[i].src.indexOf("_closed") > -1) {
				arrImages[i].src = newSrc;
				arrImages[i].width = 9;
				arrImages[i].height = 7;
			} else {	
				arrImages[i].src = newSrc;
				arrImages[i].width = 7;
				arrImages[i].height = 9;
			}
		}
	}	
	
}

function bodyexpRefresh(strBodyexp) {
	for (var i=0; i<arrBodyexp.length; i++) {
		var currId = arrBodyexp[i].Name + ((arrBodyexp[i].Open) ? "_open" : "_closed");
		document.getElementById(currId).style.display = "none";
		if (arrBodyexp[i].Name == strBodyexp) {
			arrBodyexp[i].Open = (arrBodyexp[i].Open) ? false : true;
		}
	}

	for (var i=0; i<arrBodyexp.length; i++) {
		var currId = arrBodyexp[i].Name + ((arrBodyexp[i].Open) ? "_open" : "_closed");
		document.getElementById(currId).style.display = "block";
	}
}

function headerFormInit() {}

var currClicktip;

function clicktipsInit() {

	var arrAreas = document.getElementsByTagName('area');
	for (var i=0; i<arrAreas.length; i++) {
		if (arrAreas[i].className == "clicktip") {
			arrAreas[i].onclick = function() {
				clicktipDisable();
				var intXoffset = document.getElementById(this.parentNode.id + "Img").offsetLeft;
				var intYoffset = document.getElementById(this.parentNode.id + "Img").offsetTop;
				var arrCoords = this.coords.split(",");
				var intX = Math.round(intXoffset) + Math.round(arrCoords[0]) + (Math.round((arrCoords[2] - arrCoords[0]) / 2)) - ((isIE) ? intXoffset : 0);
				var intY = Math.round(intYoffset) + Math.round(arrCoords[1]) + (Math.round((arrCoords[3] - arrCoords[1]) / 2));
				var objClicktipBox = document.getElementById(this.id + "Box");
				objClicktipBox.style.top = intY + "px";
				objClicktipBox.style.left = intX + "px";
				clicktipEnable(this);
			}
		}
	}
	
	/* DAVID COOPER ADDED FOR GLOSSARY LINKOUT SUPPORT */
	var arrHrefs = document.getElementsByTagName('a');
	for (var i=0; i<arrHrefs.length; i++) {
		if (arrHrefs[i].className == "clicktipCloseBtn") {
			arrHrefs[i].onclick = clicktipClose;
		}

		if (arrHrefs[i].className == "clicktip") {
			arrHrefs[i].onclick = function() {
				clicktipDisable();
				var intX = mousex;
				var intY = mousey;
				var objClicktipBox = document.getElementById(this.id + "Box");
				objClicktipBox.style.top = intY + "px";
				objClicktipBox.style.left = intX + "px";
				clicktipEnable(this);
			}
		}
	}
	/* END >>> DAVID COOPER ADDED FOR GLOSSARY LINKOUT SUPPORT */

	var arrImages = document.getElementsByTagName('img');
	for (var i=0; i<arrImages.length; i++) {
		if (arrImages[i].className == "clicktipCloseBtn") {
			arrImages[i].onclick = clicktipClose;
		}

		if (arrImages[i].className == "clicktipImg") {
			arrImages[i].onclick = function() {
				clicktipDisable();
				var intXoffset = this.offsetLeft;
				var intYoffset = this.offsetTop;
				var intX = Math.round(intXoffset) + Math.round(this.width / 2);
				var intY = Math.round(intYoffset) + Math.round(this.height / 2);
				var objClicktipBox = document.getElementById(this.id + "Box");
				objClicktipBox.style.top = intY + "px";
				objClicktipBox.style.left = intX + "px";
				clicktipEnable(this);
			}
		}
	}
}

function clicktipDisable() {
	if (currClicktip) {
		currClicktip.style.display = "none";
	}
}

function clicktipOpen(ObjectClick, ClickTip)
{
	clicktipDisable();
	var coors = findPos(ObjectClick);
    var intXoffset = coors[0];
    var intYoffset = coors[1];
	var intX = Math.round(intXoffset) + Math.round(ObjectClick.width / 2);
	var intY = Math.round(intYoffset) + Math.round(ObjectClick.height / 2);
	var objClicktipBox = document.getElementById(ClickTip);
	objClicktipBox.style.top = intY + "px";
	objClicktipBox.style.left = intX + "px";
	objClicktipBox.style.display = "block";
	currClicktip = objClicktipBox;

}

function clicktipOpenXml(ObjectClick, tipID)
{
	clicktipDisable();
    //---->>>> Determine the coordinates for the Clicktip to be placed <<<<----
	var intXoffset = document.getElementById(ObjectClick.parentNode.id + "Img").offsetLeft;
	var intYoffset = document.getElementById(ObjectClick.parentNode.id + "Img").offsetTop;
	var arrCoords = ObjectClick.coords.split(",");
	var intX = Math.round(intXoffset) + Math.round(arrCoords[0]) + ((Math.round((arrCoords[2] - arrCoords[0]) / 4))*3)- ((isIE6) ? intXoffset : 0);
	var intY = Math.round(intYoffset) + Math.round(arrCoords[1]) + ((Math.round((arrCoords[3] - arrCoords[1]) / 4))*3);
	
	//---->>>> Assign the control that the Click tip will be populated in <<<<----
	var objClicktipBox = document.getElementById('ClickTipBox');
    
    //---->>>> Call the function that loads the tip from the clicktip XML file <<<<----
    findClicktip(objClicktipBox, tipID);
    
    //---->>>> Assign the coordinates and make the tip visible <<<<----
	objClicktipBox.style.top = intY + "px";
	objClicktipBox.style.left = intX + "px";
	objClicktipBox.style.display = "block";
	currClicktip = objClicktipBox;
    
}

function img_clicktipOpenXML(ObjectClick, tipID)
{
    //---->>>> Determine the coordinates for the Clicktip to be placed <<<<----
	var coors = findPos(ObjectClick);
    var intXoffset = coors[0];
    var intYoffset = coors[1];
//    alert(document.body.clientWidth);
//    alert(document.body.clientWidth - 812);
//    alert((document.body.clientWidth - 812)/2);
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
	var intX = Math.round(intXoffset) + (Math.round(ObjectClick.width / 4)*3) - 180 -Math.round((myWidth - 812)/2);
	var intY = Math.round(intYoffset) + (Math.round(ObjectClick.height / 4)*3) - 115;
	
	//---->>>> Assign the control that the Click tip will be populated in <<<<----
	var objClicktipBox = document.getElementById('ClickTipBox');
	
    //---->>>> Call the function that loads the tip from the clicktip XML file <<<<----
    findClicktip(objClicktipBox, tipID);
    
    //---->>>> Assign the coordinates and make the tip visible <<<<----
	objClicktipBox.style.top = intY + "px";
	objClicktipBox.style.left = intX + "px";
	objClicktipBox.style.display = "block";
	currClicktip = objClicktipBox;
}

function a_clicktipOpenXML(ObjectClick, tipID)
{
	//---->>>> The coordinates for a hyperlink or <a></a> tags is determined by the current mouse coordinates (mousex, mousey) <<<<----
	
	//---->>>> Assign the control that the Click tip will be populated in <<<<----
	var objClicktipBox = document.getElementById('LinkClickTipBox');
	
    //---->>>> Call the function that loads the tip from the clicktip XML file <<<<----
    findClicktip(objClicktipBox, tipID);
    
    //---->>>> Assign the coordinates and make the tip visible <<<<----
	objClicktipBox.style.top = mousey + "px";
	objClicktipBox.style.left = mousex + "px";
	objClicktipBox.style.display = "block";
	currClicktip = objClicktipBox;
}
//---->>>> this will calculate the the top and left coordinates of an object even when buried in other objects <<<<----
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//---->>>> Used to extract a click tip from the clicktip.xml file using the clicktip.xslt to transform in to the HTML clicktip bubble <<<<----
function findClicktip(clicktipBox, tipID)
{
    try {
        var xslt = new ActiveXObject("Msxml2.XSLTemplate");
        var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
        var xslProc;
        xslDoc.async = false;
        xslDoc.resolveExternals = false;
        xslDoc.load("http://" + location.host + "/xml/clicktipIE.xslt");
        xslt.stylesheet = xslDoc;
        var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
        xmlDoc.async = false;
        xmlDoc.resolveExternals = false;
        xmlDoc.load("http://" + location.host + "/xml/clicktip.xml");
        xslProc = xslt.createProcessor();
        xslProc.input = xmlDoc;
//        xslProc.addParameter("tipSource", source);
//        xslProc.addParameter("tipName", name);
        xslProc.addParameter("_tipID", tipID);
        xslProc.transform();
        //---->>>> return the results
        clicktipBox.innerHTML = xslProc.output;
    }
    catch(e) {
        try{
            var xslStylesheet;
            var xsltProcessor = new XSLTProcessor();
            var myDOM;
            var xmlDoc;
            var myXMLHTTPRequest = new XMLHttpRequest();
            myXMLHTTPRequest.open("GET", "http://" + location.host + "/xml/clicktipIE.xslt", false);
            myXMLHTTPRequest.send(null);

            xslStylesheet = myXMLHTTPRequest.responseXML;
            xsltProcessor.importStylesheet(xslStylesheet);

            // load the xml file
            myXMLHTTPRequest = new XMLHttpRequest();
            myXMLHTTPRequest.open("GET", "http://" + location.host + "/xml/clicktip.xml", false);
            myXMLHTTPRequest.send(null);
            xmlDoc = myXMLHTTPRequest.responseXML;

            // set the parameter using the parameter passed to the outputgroup function
//            xsltProcessor.setParameter(null,"tipSource", source);
//            xsltProcessor.setParameter(null,"tipName", name);
            xsltProcessor.setParameter(null, "_tipID", tipID);
            var fragment = xsltProcessor.transformToFragment(xmlDoc,document);
            clicktipBox.innerHTML = "";
            myDOM = fragment; 
            clicktipBox.appendChild(fragment);
        }
        catch(e) {
          alert("Unable to process the clicktip: " + e);
        }
    }
} 

function clicktipEnable(objArea) {
	var objClicktip = document.getElementById(objArea.id + "Box");
	objClicktip.style.display = "block";
	currClicktip = objClicktip;
}

function clicktipClose() {
	if (currClicktip) {
		currClicktip.style.display = "none";
		currClicktip = null;
	}
}

/* **** Object Prototypes **** */
Array.prototype.getElement = arr_getElement;
Array.prototype.getPropValue = arr_getPropValue;

/* search an associative array for a property value, return the index of that element */
function arr_getElement(strSearchPropName, strValue) {
	var intFound = -1;
	for (i=0; i<this.length; i++) {
		if (eval("this[i]." + strSearchPropName) == strValue) {
			intFound=i;
			break;
		}
	}
	return intFound;
}

/* search an associative array for a property value, return another property value from the desired element */
function arr_getPropValue(strSearchPropName, strValue, strReturnPropName) {
	var intFound = -1;
	for (i=0; i<this.length; i++) {
		if (eval("this[i]." + strSearchPropName) == strValue) {
			intFound = i;
		}
	}
	if (intFound > -1) {
		return eval("this[intFound]." + strReturnPropName);
	} else {
		return intFound;
	}
}


/* DAVID COOPER ADDED TO CAPTURE MOUSE POSITION */
var mousex;
var mousey;

if (document.layers) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=mouseTrack;

function mouseTrack(e) {
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		mousex = e.pageX;
		mousey = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		mousex = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		mousey = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}

/* END >>> DAVID COOPER ADDED TO CAPTURE MOUSE POSITION */
