// Assigns generic DOM rollover functions to any images with a class of "swapbtn".
// Images MUST be named as "whatever.ext" (mouseout) and "whatever_on.ext" (mouseover).
// var W3CDOM = (document.createElement && document.getElementsByTagName);

var mouseOvers = new Array();
var mouseOuts = new Array();

window.onload = initBtns;

function btnMouseOver() {
	this.src = mouseOvers[this.number].src;
}

function btnMouseOut() {
	this.src = mouseOuts[this.number].src;
}

function initBtns() {
//	if (!W3CDOM) return;
if (!document.createElement || !document.getElementsByTagName || !document.getElementById) return;
// Collect all images in document and put into new variable named "all_imgs"
var all_imgs = document.getElementsByTagName('img');
  for (var i=0; i<all_imgs.length; i++) {
    // Sort "all_imgs" for any images with class of "swapbtn" 
    var swaps = all_imgs[i]; 
    if (swaps.className &&
        (' ' + swaps.className + ' ').indexOf('swapbtn') != -1) {
        // Set 'onmouseover' and 'onmouseout' to call btnMouseOver() and btnMouseOut()
		swaps.onmouseover = btnMouseOver;
		swaps.onmouseout = btnMouseOut;
		// Find img "src" suffixes for all 'swaps'. Put into a new variable named "suffixes"
		var suffix = swaps.src.substring(swaps.src.lastIndexOf('.'));
		mouseOuts[i] = new Image();
		mouseOuts[i].src = swaps.src;
		mouseOvers[i] = new Image();
		// Move the img "src" nodes (regex changes "whatever.ext" to "whatever_on.ext")
		mouseOvers[i].src = swaps.src.substring(0,swaps.src.lastIndexOf('.')) + "_on" + suffix;
		swaps.number = i;
    }
  }
}
