// Functions called on page load
// REQUIRES /includes/utils.js to be loaded before this file

/* Set the class on each of the topNav list items to include "over" so dropdown 
   behaviour shows in IE so we don't have to set the class on every LI manually.
   Not sure if this is still required in recent versions of IE. */
function startList() {
	startListById("topNav");
	startListByClass("specialties");
	startListByClass("specialties_horizontal");
}
/*  startListById sets onmouseover and onmouseout of all the children 
    of the element identified by Id as specified in setChildClass.
	Does nothing if the browser doesn'e support document.getElementsById
*/
function startListById(Id) {
	var navRoot;
	if (document.all && document.getElementById) {
		navRoot = document.getElementById(Id);
		setChildClass(navRoot);
	}
}
/*  startListByClass sets onmouseover and onmouseout of all UL elements 
    within 'page' element as specified in setChildClass.
	Does nothing if the browser doesn't support document.getElementById
	or element.getElementsByTagName.
*/
function startListByClass(classNm) {
	if (document.all&&document.getElementById) {
		var navRoot = document.getElementById("page");
		if (navRoot && navRoot.getElementsByTagName) {
			var navRoots = navRoot.getElementsByTagName("UL");
			var className;
			for (j=0; j<navRoots.length; j++) {
				className = navRoots[j].getAttribute("className");
				if (className && className.indexOf(classNm)>-1) {
					setChildClass(navRoots[j]);
				}
			}
		}
	}
}
/*  setChildClass sets the onmouseover and onmouseout events on LI
	children of the navroot so that the LI class includes 'over' 
	when the mouse is over the LI and does not include the 'over'
	class when it is not.
*/
function setChildClass(navRoot) {
	var node;
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {
				this.className+=" over";
			}
			node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
			}
		}
	}
}

/* Add startList to the list of functions run when window.onload event 
is triggered, which means this is run on every page that includes this 
javascript file */
addLoadListener(startList);


/* Sets the width of image captions to the same width of 
	the pictures above them so they wrap.
	Returns false if document.getElementsByTagName is not supported */
function setPictureCaptionsWidth()
{
	if (!document.getElementsByTagName) return false;
	var divs=document.getElementsByTagName("div");
	for (i=0; i < divs.length; i++)
	{   // For each imgLeft or imgRight div get the width of the widest image 
		//and set width of all captions to be the same or 100, whichever is larger
		if(divs[i].className=="imgLeft" || divs[i].className=="imgRight")
		{
			var imgWidth = 100, images = divs[i].getElementsByTagName("img");
			// Get width of image. There should only be one image, but check for multiples.
			for (j=0; j < images.length; j++)
				if (imgWidth < images[j].width)
					imgWidth = images[j].width;
			// Set width of caption elements to width of image
			var paras = divs[i].childNodes;
			for (j=0; j < paras.length; j++)
				if(paras[j].className == "caption")
					paras[j].style.width = imgWidth + 'px';
		}
	}
}
/*  Add setPictureCaptionsWidth to the list of functions run with the page is loaded.
	This will be run for any page that includes this .js file */
addLoadListener(setPictureCaptionsWidth);

/*  Preload menu images so the menus look right as soon as you try to 
	use them.  Arguments are a list of img urls.
	This came from Dreamweaver (I think) */
function MM_preloadImages()
{ //v3.0
	if(!document.images) return false;  // Nothing to do
	if(!document.MM_p)  // Check for MM_p array of images to see if it has already been created 
	{  // TODO: Actually, this will fail if the function is called twice with different sets of images
		document.MM_p=new Array();  // Create array for images
		var i;
		var j=document.MM_p.length;
		var args=MM_preloadImages.arguments;  // Using .arguments allows variable number of arguments
		for(i=0; i<args.length; i++)
		{
			if (args[i].indexOf("#")!=0) // TODO: What is the significance of #?
			{   // For each argument create a new image in the document so that the document loads it.
				document.MM_p[j]=new Image; 
				document.MM_p[j++].src=args[i];
			}
		}
	}
}

addLoadListener(MM_preloadImages,"/IMAGES/MenuImages/buttonBackground.gif","/IMAGES/MenuImages/DropdownBorder.gif");
