//requires prototype.js

//Swapper class
function Swapper(img_elem) {
	this.img_elem = img_elem;
	this.main_img = false;
	this.alt_img = false;
	this.event_listeners_set = false;
}
Swapper.prototype.hasAltImage = function() {
	return this.main_img.src.search(/_off\.(gif|jpg|png)/i) > -1;
}
Swapper.prototype.altImageFile = function() {
	var temp = this.main_img.src;
	return temp.replace('_off','_on');
}
Swapper.prototype.begin = function() {
	this.main_img = new Image();
	this.main_img.src = this.img_elem.src;
	if(this.hasAltImage()) {
		this.alt_img = new Image();
		this.alt_img.src = this.altImageFile();
	}
}
Swapper.prototype.preloaded = function() {
	var done = this.main_img.complete;
	if(typeof(this.alt_img) == 'object')
		done = done && this.alt_img.complete;
	return done;
}
Swapper.prototype.swapImageOn = function() {
	this.img_elem.src = this.alt_img.src;
}
Swapper.prototype.swapImageOff = function() {
	this.img_elem.src = this.main_img.src;
}
Swapper.prototype.setEventListeners = function() {
	if(!this.hasAltImage()) {
		this.event_listeners_set = true;
		return;
	}
	Event.observe(this.img_elem.id,'mouseover',this.swapImageOn.bindAsEventListener(this));
	Event.observe(this.img_elem.id,'mouseout',this.swapImageOff.bindAsEventListener(this));
	//if this is a form input, add focus and blur listeners
	if(this.img_elem.hasClassName('swap_input')) {
		Event.observe(this.img_elem.id,'focus',this.swapImageOn.bindAsEventListener(this));
		Event.observe(this.img_elem.id,'blur',this.swapImageOff.bindAsEventListener(this));
	}
	this.event_listeners_set = true;
}
Swapper.prototype.done = function() {
	return this.event_listeners_set;
}


//SwapperList class
var swap_list;
function checkSwapList() {
	swap_list.timedCheck();
}
function SwapperList(root_id,swap_class) {
	this.root_id = root_id;
	this.swap_class = swap_class;
	this.sched = false;
	this.list = new Array();
}
SwapperList.prototype.finished = function() {
	//return true if all swapper objects are finished
	for(var i = 0; i < this.list.length; i++)  {
		if(!this.list[i].done())
			return false;
	}
	return true;
}
SwapperList.prototype.timedCheck = function() {
	for(var i = 0; i < this.list.length; i++) {
		if(!this.list[i].done() && this.list[i].preloaded())
			this.list[i].setEventListeners();
	}
	if(this.finished()) {
		clearTimeout(this.sched);
		this.sched = false;
	} else
		this.sched = setTimeout('checkSwapList()',200);
}
SwapperList.prototype.start = function() {
	var img_elems = $(this.root_id).getElementsByClassName(this.swap_class);
	for(var i = 0; i < img_elems.length; i++) {
		this.list[i] = new Swapper(img_elems[i]);
		this.list[i].begin();
	}
	this.sched = setTimeout('checkSwapList()',200);
}