/*******************************
*
* elcImage.js
* 
* rollover image class
*
* Author: DoublePrime
* References: ELCI Clinique image.js created by Razorfish
* Version: 0.2
*
* Comments
*
* For MAC:
* Uses '_on' and '_off' for the on and off states
*
*******************************/

var onstate = "_over";
var offstate = "_off";

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(onstate+".") > -1) imageGroups[group][0] = this;
	}
	this.initObj(img);
	this.state = 0;	
 	if (img.src.lastIndexOf(onstate+".") > -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(onstate) > -1) || (img.src.lastIndexOf(offstate) > -1)) ? "_" : "."));
	this.off = (this.base + offstate + "." + this.type);
	this.onImg = new Image();
	this.onImg.src = this.path + this.base + onstate + '.' + this.type;
	this.offImg = new Image();
	this.offImg.src = this.path + this.base + offstate + '.' + this.type;
}

elcImage.prototype.setOn = function(){
	this.obj.src = this.onImg.src;
}

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

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