// Simpler rollover script
// Image must have class rollClass assigned to be a rollover
// save additional images for the states with the same name
// as the main image, plus the suffixes used below
// ie.
//   main image is:  image.jpg
//   mouse over is:  image_H.jpg
//   mouse click is: image_A.jpg
// if either of the files are not found, then that state will
// not be used (recommend creating an image for all states, even
// if one is a duplicate of the other -- see below).
// *****************
// **** Netscape 7 or earlier, you must add an image for all states
// **** because i can't get the onerror event to work for those browsers
// **** alternately, set useAct = false;
// *****************
// rev: feb 14 2007

// class assigned to rollover images
var rollClass = "rollover";

// in the suffix, can not use the following characters:
// ./\$^() etc, because they will mess up the regular expression.
// suffix for hover (mouse over) state
var hovSufx = "_H";
// set to false to ignore the hover state
var useHov = true;

// suffix for active (mouse down) state
var actSufx = "_A";
// set to false to ignore the active state
var useAct = false;

// for better compatibility with older browsers (ie5), the
// events must be attached to an <a> tag, not to the image
// itself. Set to false to attach the event to the image.
// If set to true, the script will search for a parent <a> and
//  attach the mouseover event to that.
var useA = true;

// tag name for images
// for HTML, use capitals; for XHTML, i think lower case(?)
var imgTag = 'IMG';
// tag name for links
var linkTag = 'A';

// set up the onload event
addLoadEvent(setRolls);

// set up image array to contain all the images
var imgA = new Array();

function setRolls() {
if (document.images) {
	// get all images that need rollovers
	var rollImgs = getElementsByClass(rollClass,document,imgTag);
	for (var i=0; i<rollImgs.length; i++) {
		// add events to image
		addEvents(rollImgs[i]);
		// get image name and suffix
		var imgN = getParts(rollImgs[i].src);
		imgA[imgN[2]] = new Array();
		// set up array of suffices
		var temp=[,hovSufx,actSufx];
		// if array value is true, it will create a new image,
		// false it will load the same image as the source
		var tempB=[false,useHov,useAct];
		for (var j=0; j<temp.length;j++) {
		// zero contains default
			imgA[imgN[2]][j] = new Image();
			if (tempB[j]) {
				// catch load error and use the default image instead
				if (navigator.appName=="Microsoft Internet Explorer" && navigator.platform=="Win32") { //:KLUDGE: ie windows (need to use onError)
					imgA[imgN[2]][j].onError = noImg;
				} else {
					imgA[imgN[2]][j].onerror = noImg;
				}
				imgA[imgN[2]][j].useImg = imgN[2];
				imgA[imgN[2]][j].imgNo = j;
				
				imgA[imgN[2]][j].src = buildURL(imgN, temp[j]);
			} else {
				imgA[imgN[2]][j].src = rollImgs[i].src;
			}
		}
	}
}
}

function noImg() {
	var imgName=this.useImg;
	imgA[imgName][this.imgNo].src = imgA[imgName][0].src;
}

function buildURL(arry, sufx) {
	return arry[1]+arry[2]+sufx+arry[3]
}

// split the file path into file name, and suffix
// also deletes the rollover suffices from the file name
function getParts(hrfTx) {
	// to remove the suffices
	var y = new RegExp("("+hovSufx+")|("+actSufx+")","g");
	// to split the path into three pieces
	var re = /^(.*\/)([^\/]+)(\.[^\.]+)$/;
	var x = re.exec(hrfTx);
	x[2] = x[2].replace(y, '')
	return x;
}

// add the mouse over and mouse out events to element pNode
function addEvents(pNode) {
	var attNode = pNode;
	if(useA) { // search for parent link node
		while (attNode && !attNode.nodeName.match(new RegExp(linkTag,"gi"))) {
			attNode = attNode.parentNode;
		}
		// check if there was no parent link node
		if (!attNode) attNode = pNode;
	}
	// this REPLACES any mouse events on element attNode
	if (useHov) {
		attNode.onmouseover=showOver;
		attNode.onmouseup=showOver;
	}
	if (useAct) {
		attNode.onmousedown=showClick;
		if (!useHov) attNode.onmouseup=showOut;
	}
	attNode.onmouseout=showOut;
	// set relation for pickup during events (thanks ppk)
	attNode.relatedElement=pNode;
}
// mouse events
function showOver() {
	var imgNode = this.relatedElement;
	swapImg(imgNode, 1);
}
function showOut() {
	var imgNode = this.relatedElement;
	swapImg(imgNode, 0);
}
function showClick() {
	var imgNode = this.relatedElement;
	swapImg(imgNode, 2);
}
function swapImg(iNod, func) {
	var x = getParts(iNod.src);
	// only switch if the image has loaded
	if (imgA[x[2]][func].complete) iNod.src = imgA[x[2]][func].src;
}

// from http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// get elements by class name
// from http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	var j = 0;
	for (var i = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}