//
// ImageHandler Class
//

function ImageHandler() {
	this.onstate = "_on";
	this.atstate = "_at";
	this.atimages = new Array();
	this.swaponmouseout = 1;
	this.inventory = new Function("ih_inventory(this)");
	return this;
}

function ih_inventory(ih) {
	// Iterate through all links in document
	var links = document.getElementsByTagName("A");
	for (var i=0;i<links.length;i++) {
	   // If link is swappable, get underlying image
	   var link = links[i];
	   var swap = link.getAttribute("swap");
	   if (swap && swap=='y' && link.childNodes.length>0)
	      ih_preload(ih,link.childNodes[0]);
	}
	// Iterate through image buttons
	var btns = document.getElementsByTagName("INPUT");
	for (var i=0;i<btns.length;i++) {
	   // If button is swappable, get underlying image
	   var btn = btns[i];
	   var swap = btn.getAttribute("swap");	
	   if (swap && swap=='y') ih_preload(ih,btn);
   }
}

function ih_preload(ih,img) {
	if (img && img.src) {
		// Check for the at state, set and quit
		for (var j=0;j<ih.atimages.length;j++) {
			if (img.src.indexOf(ih.atimages[j])>-1) {
				img.src =  img.src.replace(/(\.\w+)$/i,ih.atstate+"$1");
				return;
			}
		}
		// Store the on and off properties
		img.offsrc = img.src;
		img.onsrc = img.src.replace(/(\.\w+)$/i,ih.onstate+"$1");
		// Preload the rollover state
		var tmp = new Image();
		tmp.src = img.onsrc;
		// Add rollover functions
		img.onmouseover = new Function("ih_swap(this)");
		if (ih.swaponmouseout) img.onmouseout = new Function("ih_swap(this)");
	}
}

function ih_swap(img) {
	// Determine current state of image
	if (img.src==img.onsrc) img.src=img.offsrc
	else img.src=img.onsrc;
}

//
// Main
//
var ih_main= new ImageHandler();

