/******************************
** Class accepts a HTML list 
** element and adds rollovers
*******************************/

var MenuRollover = Class.create({
	initialize: function(listElement) {
		// Gather the image elements from the list into an array
		this.listParent = $(listElement);
		this.listItems = this.listParent.childElements();
		this.listButtons = new Array;
		this.onImages = new Array;
		this.offImages = new Array;
		if (this.listItems.length > 0) {
			for(i=0; i < this.listItems.length; ++i) {
				this.listButtons.push(this.listItems[i].down('img'));
			}
		}
		
		// Bind the methods as Event Listeners
		this.b_imgOn = this.imgOn.bindAsEventListener(this);
		this.b_imgOff = this.imgOff.bindAsEventListener(this);
		
		// Register the methods to the events
		if (this.listButtons.length > 0) {
			for(i=0; i < this.listButtons.length; ++i) {
				if(!this.listButtons[i].hasClassName('current')){
					this.listButtons[i].observe('mouseover', this.b_imgOn);
					this.listButtons[i].observe('mouseout', this.b_imgOff);
				}
			}
		}
		
		// Preload the rollover images
		if (this.listButtons.length > 0) {
			for(i=0; i < this.listButtons.length; ++i) {
				if(!this.listButtons[i].hasClassName('current')){
					this.offImages[i] = new Image;
					this.offImages[i].src = this.listButtons[i].src;
					this.onImages[i] = new Image;
					this.onImages[i].src = this.listButtons[i].src.replace("/off/","/on/");
				}
			}
		}
	},
	imgOn: function(event) {
		el = event.element();
		el.src= el.src.replace("/off/","/on/");
	},
	imgOff: function(event) {
		el = event.element();
		el.src = el.src.replace("/on/","/off/");
	}
});

/******************************
** The functions below take an 
** arbitrary number of elements
** and coordinate an rollover
** event
*******************************/

function activateRollover() {
	$A(arguments).each(function(el){
		if(typeof(el) == "object" && !Object.isElement(el)) {
			el = el.element();
		} 
		else if (typeof(el) == "string") {
			el = $(el);
		}
		if (typeof(el.src) == "undefined") {
			el = el.down('img');
		}
		
		matches = el.src.match(/(.*)((?:.gif|.jpg|.png).*)/);
	    base = matches[1]; 
	    ext = matches[2];
		
		el.src = base + '_ro' + ext;
	});
}
function deactivateRollover() {
	$A(arguments).each(function(el){
		if(typeof(el) == "object" && !Object.isElement(el)) {
			el = el.element();
		} else if (typeof(el) == "string")  {
			el = $(el);
		}
		if (typeof(el.src) == "undefined") {
			el = el.down('img');
		}
		
		el.src = el.src.replace('_ro','');
	});
}
function preloadRollover() {
	preloads = new Array;
	$A(arguments).each(function(el){
		if(typeof(el) == "object" && !Object.isElement(el)) {
			el = el.element();
		} 
		else if (typeof(el) == "string") {
			el = $(el);
		}
		if (typeof(el.src) == "undefined") {
			el = el.down('img');
		}
		var ext = el.src.substr(el.src.length-4,4);
		var base = el.src.substr(0,el.src.length-4);
		preload = new Image;
		preload.src = base + '_ro' + ext;
		preloads.push(preload);
	});
}