/*******************************
*
* elcImage.js
* 
* rollover image class
*
* Author: DoublePrime
* References: ELCI Clinique image.js created by Razorfish
* Version: 0.2
*
* Comments
* Uses '_on', '_on', and '_off'
*
*******************************/

var imageGroups = new Array;

function elcImage(img,group,over) {
	img.onload = '';
	this.obj = img;
	this.name = img.name;
	this.defaultSrc = img.src;
	if ((group != 'undefined') && (group != null)) {
		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.usesOver = ((typeof(over) != "undefined") && (over != null));
	this.initObj(img);
	this.state = 0;
 	if (img.src.lastIndexOf("_on.") > -1) {
		this.state = 1;
	}
 	if (img.src.lastIndexOf("_over.") > -1) {
		this.state = 2;
	}
}

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) || (img.src.lastIndexOf("_off") > -1) || (img.src.lastIndexOf("_over") > -1)) ? "_" : "."));
	this.off = (this.base + '_off.' + this.type);
	this.offImg = new Image();
	this.offImg.src = this.path + this.base + '_off.' + this.type;
	this.onImg = new Image();
	this.onImg.src = this.path + this.base + "_on." + this.type;
	this.overImg = new Image();
	if (this.usesOver) {
		this.overImg.src = this.path + this.base + "_over." + this.type;
	} else {
		this.overImg.src = this.offImg.src;
	}
	if (this.base == 'lm_3328_89') {
		alert (this.offImg.src);
	}
}

elcImage.prototype.setOff = function(){
	if (this.state != 0) {
		this.obj.src = this.offImg.src;
		this.state = 0;
	}
}

elcImage.prototype.setOn = function(){
	if (this.state != 1) {
		this.obj.src = this.onImg.src;
		this.state = 1;
	}
}

elcImage.prototype.setOver = function(){
	if (this.state != 2) {
		this.obj.src = this.overImg.src;
		this.state = 2;
	}
}

elcImage.prototype.isOff = function() {
	if (this.state == 0) {
		return true;
	} else {
		return false
	}
}

elcImage.prototype.isOn = function() {
	if (this.state == 1) {
		return true;
	} else {
		return false
	}
}

elcImage.prototype.isOver = function() {
	if (this.state == 2) {
		return true;
	} else {
		return false
	}
}

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();
}
