//extend d.all and d.layers DOMs to understand standard d.getElementById
if(!document.getElementById) {
	document.getElementById = function(id,contDoc) {
		var d=document; var x=null;
		if(d.layers) {
			if(!contDoc) contDoc=d;
			if(contDoc[id]) x=contDoc[id];
			else {
				var lyrs=contDoc.layers;
				for(var i=0;!x && lyrs && i<lyrs.length;i++) x=d.getElementById(id,lyrs[i].document);
			}
			if(x) x.style = x;
		}
		else if(d.all) x=d.all[id];
		return x;
	}
}

////////////////
/*Popup Menus:*/

var beginTop = 114;
var beginLeft = 0;
var menuWidth = 188;
var menuItemHeight = 28;

function showMenu(aElt,menuID) {
	var d=document;

	var menuElt=d.getElementById(menuID);
	if(!menuElt) return; //gracefully bow out

	if(d._popupMenus) hideMenus(d._popupMenus,menuID);

	var sels = document.getElementsByTagName("select");
	for(var i=0; i<sels.length; i++) {
		sels[i].style.visibility = "hidden";
	}

	menuElt.opener = aElt;

	//Add to list of menus:
	if (!d._popupMenus) d._popupMenus=[];
	var menus = d._popupMenus;
	for (var i=0;i<menus.length;i++) if (menus[i] == menuElt) var isInArray = true;
	if (!isInArray) menus[menus.length] = menuElt;

	//Set listeners:
	menuElt.onmouseover = function() {
		for(var i=0;i<menus.length;i++) if (menus[i].timer) clearTimeout(menus[i].timer);
	}
	aElt.onmouseout = menuElt.onmouseout = function() {
		menuElt.timer = setTimeout("hideMenus(document._popupMenus);",500);
	}

	if(!d.layers && aElt.className.indexOf("open-menu")<0) aElt.className += "open-menu";

	//find where in the hierarchy this menu is, and calculate the correct position:
	var hier = menuID.split("submenu")[1].split("x");
	var menuTop = beginTop;
	for(var i=0; i<hier.length; i++) menuTop += menuItemHeight * (hier[i] - 1); //loop through each level and add to menuTop
	var units = (document.layers)?0:"px";
	menuElt.style.top = menuTop + units;
	menuElt.style.left = (beginLeft + hier.length * menuWidth) + units;
	menuElt.style.visibility = 'visible';
}

function hideMenus(menus,exclude) { //exclude is ID of menu whose parents we don't want to hide
	for(var i=0;i<menus.length;i++)	{
		if (!exclude || exclude.indexOf(menus[i].id) != 0) {
			menus[i].style.visibility = 'hidden';
			if(!document.layers) menus[i].opener.className = menus[i].opener.className.replace("open-menu","");
		}
		if (menus[i].timer) clearTimeout(menus[i].timer);
	}
	if(!exclude) { //if all menus closing, re-show <select> fields:
		var sels = document.getElementsByTagName("select");
		for(var i=0; i<sels.length; i++) {
			sels[i].style.visibility = "visible";
		}
	}
}


////////////////////////////
/*Rollover Image Swapping:*/

//Rollover function: all you need is:
//<a href="..." onmouseover="imgSwap(this,document.imageName);"><img ... /></a>
//where imageName is the name="" on the image.
//Then, if the image src is theImage.gif, make the rollover for that image theImage_over.gif
//The script will know to look for the _over image.

function imgSwap(anch,img) {
	var src = img.src;
	var newSrc = src;

	if(src.indexOf("_over.")<0) {
		//if not already in hover state, change to hover image:
		var dotIdx = src.lastIndexOf("."); //index of last dot in path
		var parts = [src.substring(0,dotIdx), src.substring(dotIdx)];
		newSrc = parts.join("_over");
	} else {
		//change back to original:
		var parts = src.split("_over.");
		newSrc = parts.join(".");
	}
	img.src = newSrc || src;

	//make onmouseout swap again:
	anch.onmouseout=function() { imgSwap(anch,img); }
}



//NS4 resize fix:
if(document.layers) window.onresize = function() {location.reload();};




///////SCROLLER FUNCTIONS (for home page only):


//utilities to convert from numbers to strings w/ px units:
function intToPx(intVal) {
	if (document.layers) return intVal;
	else return intVal+"px";
}
function pxToInt(pxVal) {
	if (document.layers) return pxVal;
	else return parseInt(pxVal);
}

//start the scrolling (called once):
function scrollInit(contentID,containHeight,scrollWait,scrollDist) {
	var x=document.getElementById(contentID);
	x.style.top = intToPx(containHeight);
	x.scrollDist = scrollDist;
	x.onmouseover = function() {scrollPause(x);};
	x.onmouseout = function() {scrollUnpause(x);};
	setInterval( "scrollMove('"+contentID+"',"+containHeight+")" , scrollWait );
}

//scroll it one step (called repeatedly):
function scrollMove(contentID,containHeight) {
	var d=document;
	var x=d.getElementById(contentID);
	if (d.getComputedStyle) var contentHeight = pxToInt(d.getComputedStyle(x).getPropertyValue("height"));
	else if (x.offsetHeight) var contentHeight = pxToInt(x.offsetHeight);
	else if (x.document) var contentHeight = x.document.height;
	else return;
	curTop = pxToInt(x.style.top);
	if (curTop+contentHeight<0) x.style.top = intToPx(containHeight);
	else if (curTop>containHeight) x.style.top = intToPx(0-contentHeight);
	else x.style.top = intToPx(curTop - x.scrollDist);
}

function scrollPause(x) { x.scrollDistResume = x.scrollDist; x.scrollDist = 0; }
function scrollUnpause(x) { x.scrollDist = x.scrollDistResume; }






//////Things to run on load:
function onDocumentLoaded() {
	if(document.getElementById("scrollerContainer")) scrollInit("scrollerContent",80,130,2);
}
window.onload = onDocumentLoaded;
