// JavaScript Document

/*******************************
*
* ImageUtils.js
* ImageUtils Class
* 
* Utilizes prototype.js library - /js/extern/prototype.js
* Image Pre-caching and Rollover class
* PNG transparency filters for MSIE 6
* 
* Ver = 1.0
*
* Author: John Hutcheson, control option
*
*******************************/


// Create ImageUtils class using prototype.js 
var ImageUtils = Class.create();

// Add new methods to the ImageUtils class' prototype
ImageUtils.prototype = {
	
	// protype.js initialize - Constructor method
	// arg: offIndicator - optional. If supplied use instead of _off as the off state indicator in image src.
	// arg: overIndicator - optional. If supplied use instead of _over as the over state indicator in image src.
	// imagesOff - Object - hash of image id and image src for the off state
	// imagesOver - Object - hash of image id and image src for the over state
	initialize: function(offIndicator, overIndicator) { 
		//alert("image utils");
		this.offIndicator = offIndicator ? offIndicator + '.' : '_off.';
		this.overIndicator = overIndicator ? overIndicator + '.' : '_over.';
		if (document.images) {
			// Images Off Object Constructor
			this.imagesOff = new Object();
			
			// Images Over Object Object Constructor
			this.imagesOver = new Object();
			
			//Vars for binding the over and out calls as event listeners
			this.boundOver;
			this.boundOut;
			
			this.setHandlers()
		}
	},
	
	
	// Image pre-caching method
	// adds images to the imagesOff and imagesOver hashes for precaching and over and out methods.
	addImage: function(el) {
		if (!el.src) {
			return false;
		}
		var elem = $(el);
		var imgId = elem.id;
		var dimensions = elem.getDimensions();
		var imgWidth = dimensions.width;
		var imgHeight = dimensions.height;
		//alert (imgId + " " + imgWidth);
		if (this.imagesOff && this.imagesOver) {
			if (this.imagesOff[imgId] && this.imagesOver[imgId]) {
				return true;
			} else {
				var srcStringOff = elem.src;
				var srcStringOver = srcStringOff.include(this.overIndicator) ? srcStringOff : srcStringOff.replace(this.offIndicator, this.overIndicator);
				this.imagesOff[imgId] = new Image(imgWidth, imgHeight);
				this.imagesOff[imgId].src = srcStringOff;
				this.imagesOver[imgId] = new Image(imgWidth, imgHeight);
				this.imagesOver[imgId].src = srcStringOver;
				
				return true;
			}
		}
		return false;
	},
	
	
	setHandlers: function () {
		this.boundOver = this.over.bindAsEventListener(this);
		this.boundOut = this.out.bindAsEventListener(this);
		Event.observe (document, 'mouseover', this.boundOver);
		Event.observe (document, 'mouseout', this.boundOut);
	},
	
	// Image Swap - set to over state
	over: function(e, imgId) {
		//alert ("over " + imgId);
		if (e != null) {
			Event.stop(e);
			var elt = $(Event.element(e));
			if (elt.hasClassName("rollover") && this.imagesOver[elt.id]) {
				elt.src  = this.imagesOver[elt.id].src;
			}
		} else {
			if (!imgId) {
				alert("here");
				return;
			}
			$(imgId).src = this.imagesOver[imgId].src;
		}
	},
	// Image Swap - set to off state
	out: function(e, imgId) {
		//alert ("over " + imgId);
		if (e != null) {
			Event.stop(e);
			var elt = $(Event.element(e));
			if (elt.hasClassName("rollover") && this.imagesOff[elt.id]) {
				elt.src  = this.imagesOff[elt.id].src;
			}
		} else {
			if (!imgId) {
				return;
			}
			$(imgId).src = this.imagesOff[imgId].src;
		}
	}
};
