// Global JavaScripts

/* Function to add routines to page load event */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Just add class="preload" to any images in the rotators or galleries so that they'll be loaded in the background for quicker transitions */
function preloadImages() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'preload') {
			var src = aImages[i].getAttribute('src');
			aPreLoad[i] = new Image();
			aPreLoad[i].src = src;
		}
	}
}

/* Just add class="rollover" to the img tag and make sure your images are named whatever.gif and whatever-over.gif */
/* You can use any kind of images and name them what you like.  The "-over" is the part that matters */
function initrollovers() {
	if (!document.getElementById) return;
	var aPreLoad = new Array();
	var sTempsrc;
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '-over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempsrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempsrc) sTempsrc = this.getAttribute('src').replace('-over'+ftype, ftype);
				this.setAttribute('src', sTempsrc);
			}
		}
	}
} 

function extractSectionName(hrefString) {
	var arr = hrefString.split('/');
	return  (arr.length<2) ? hrefString : arr[3].toLowerCase();
}

function setActiveMenu(arr, crtPage) {
	for (var i=0; i<arr.length; i++) {
		if(extractSectionName(arr[i].href) == crtPage) {
			if (arr[i].parentNode.tagName != "DIV") {
				//arr[i].className = "current";
				arr[i].parentNode.className = "selected";
			}
		}
	}
}

function setNavOnStates() {
	hrefString = document.location.href ? document.location.href : document.location;
	
	if (document.getElementById("mainNav")!=null)
		setActiveMenu(document.getElementById("mainNav").getElementsByTagName("a"), extractSectionName(hrefString));

	if (document.getElementById("leftNav")!=null)
		setActiveMenu(document.getElementById("leftNav").getElementsByTagName("a"), extractSectionName(hrefString));
}


	
addLoadEvent(preloadImages);
addLoadEvent(initrollovers);
addLoadEvent(setNavOnStates);