/*******************************
*
* elcImage.js
* 
* rollover image class
*
* Author: DoublePrime
* References: ELCI Clinique image.js created by Razorfish
* Version: 0.1
*
* Comments
* MLC: allow use of names containing '_on' apart from the state designation
* Invesigate use in 3-position rollovers
*
*******************************/

var imageGroups = new Array;

function elcImage(img,group) {
	img.onload = '';
	this.obj = img;
	this.name = img.name;
	this.defaultSrc = img.src;
	if (group != 'undefined') {
		if (typeof imageGroups[group] == "undefined") {
			imageGroups[group] = new Array();
			imageGroups[group][0] = '';
		}
		imageGroups[group][imageGroups[group].length] = this;
		this.group = imageGroups[group];
		if (this.defaultSrc.lastIndexOf("_on.") > -1) imageGroups[group][0] = this;
	}
	this.initObj(img);
	this.state = 0;	
 	if (img.src.lastIndexOf("_on.") > -1) {
		this.state = 1;
	}
}

elcImage.prototype.initObj = function(img){
	this.type = img.src.substring((img.src.lastIndexOf(".") + 1), img.src.length);
	this.path = this.defaultSrc.substring(0,this.defaultSrc.lastIndexOf("/")+1);
	this.base = img.src.substring((img.src.lastIndexOf("/") + 1), img.src.lastIndexOf((img.src.lastIndexOf("_on") > -1) ? "_" : "."));
	this.off = (this.base + '.' + this.type);
	var cachestring = this.path + this.base + '_on.' + this.type;
	cachestring = cachestring.substring(cachestring.indexOf("/images"),cachestring.length);
	if (bcImageCache.constructor = elcRMap && bcImageCache.exists(cachestring)){
		this.onImg = bcImageCache.fetch(cachestring);
	} else {
		this.onImg = new Image();
		this.onImg.src = wsmlMakeResourceUrl(this.path + this.base + "_on." + this.type);
	}
	cachestring = this.path + this.base + '_on.' + this.type;
	if (bcImageCache.constructor = elcRMap && bcImageCache.exists(cachestring)){
		this.offImg = bcImageCache.fetch(cachestring);
	} else {
		this.offImg = new Image();
		this.offImg.src = wsmlMakeResourceUrl(this.path + this.base + '.' + this.type);
	}
}

/* the rvalue have already been wrapped in a wsmlMakeResourceUrl */
elcImage.prototype.setOn = function(){
	this.obj.src = this.onImg.src;
}

elcImage.prototype.setOff = function(){
	this.obj.src = this.offImg.src;
}

/* 
	warning: this method may need to be altered to wrap
	the rvalue 'url' in a wsmlMakeResourceUrl function
	However, I beleive the url argument passed already has
	been wrapped. Not sure if this function is a 'fixed point'
	function
*/	
elcImage.prototype.setSrc = function(url){
	this.obj.src = url;
	this.defaultSrc = url;
	this.initObj(this.obj);
}

elcImage.prototype.toggle = function(){
	if(this.state == 0) {
		this.state = 1;
		this.setOn();
	} else {
		this.state = 0;
		this.setOff();
	}
}

/* group functions (exclusive on membership) */
elcImage.prototype.toggleGroup = function(){
	for (i=1;i<this.group.length;i++) {
		if (this.name != this.group[i].name) {
			this.group[i].setOff();
		} else {
			this.group[i].setOn();
		}
	}
}

elcImage.prototype.setGroup = function(){
	for (i=1;i<this.group.length;i++) {
		if (this.name != this.group[i].name) {
			this.group[i].setOff();
		} else {
			this.group[i].setOn();
			this.group[0] = this.group[i];
		}
	}
}

elcImage.prototype.resetGroup = function(){
	for (i=1;i<this.group.length;i++) {
		this.group[i].setOff();
	}
	if(typeof this.group[0] == 'object') this.group[0].setOn();
}
