// Global JavaScripts

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* 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);
			}
		}
	}
} 

// To make dropdown menus work in IE6
function sfHover() {
	var sfEls = $("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.setAttribute('hclassname', this.className);
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.getAttribute('hclassname');
		}
	}
}

/* Just add class="preload" to any images in the photo gallersy 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;
		}
	}
}

/* Functions to highlight selected formfields */
function initforms() {
	if (!document.getElementById) return;
	if ($$('.focusable') == '') return;
	
	var aformRows = $$('div.formRow');
	aformRows.each(function(s) {
		s.getElementsBySelector('.focusable').invoke('observe', 'focus', formrowfocus).invoke('observe', 'blur', formrowblur);
	});
}

function formrowfocus() {
	var selectedRow = this.up('.formRow');
	selectedRow.addClassName('formRowSelected');

}

function formrowblur() {
	var selectedRow = this.up('.formRow');
	selectedRow.removeClassName('formRowSelected');
}


addLoadEvent(initforms);
addLoadEvent(preloadImages);
addLoadEvent(sfHover);