/*
Copyright 2010
Douglas Green
douglasgreen.com
*/

// adjustment for Firefox (Mac only problem?) and back button issue
// calling init() explicitly on the page instead

//window.onload = init;

function init() {
	initRollovers();
}

/* ROLLOVERS
================================= */

// roll over image suffix
// ex: image-on.jpg
var onSfx = "-on";

function initRollovers() {
	for (var i=0; i<document.images.length; i++) {
//		if ((document.images[i].parentNode.tagName == "A") || (document.images[i].className == "over")) {
		if (document.images[i].className == "over") {
			setupRollover(document.images[i]);
		}
	}
}

function setupRollover(image) {
	// image source, path, name, extension
	var imgSrc = image.src;
	// get image directory out of src, from start to last '/'
	var imgPth = imgSrc.slice(0, imgSrc.lastIndexOf("/")+1);
	// get image name out of src, after last '/' and before '.'
	var imgNme = imgSrc.slice(imgSrc.lastIndexOf("/")+1, imgSrc.lastIndexOf("."));
	// get file extension after '.'
	var imgExt = imgSrc.slice(imgSrc.lastIndexOf("."));
	// roll out
	image.out = new Image();
	image.out.src = imgSrc;
	image.onmouseout = rollOut;
	// roll over
	image.over = new Image();
	image.over.src = imgPth + imgNme + onSfx + imgExt;
	image.onmouseover = rollOver;
	// keyboard focus
	image.parentNode.childImg = image;
	image.parentNode.onblur = rollOutChild;
	image.parentNode.onfocus = rollOverChild;
}

function rollOut() {
	this.src = this.out.src;
}

function rollOver() {
	this.src = this.over.src;
}

function rollOutChild() {
	this.childImg.src = this.childImg.out.src;
}

function rollOverChild() {
	this.childImg.src = this.childImg.over.src;
}
