nullFunction = function() {}

re = new Object;
re.containsImage = nullFunction;
re.getImage = nullFunction;
re.setOff = nullFunction;
re.setOver = nullFunction;
re.setOn = nullFunction;
re.setAllOff = nullFunction;
re.lock = nullFunction;
re.lockAll = nullFunction;
re.unlock = nullFunction;
re.unlockAll = nullFunction;
re.setSrc = nullFunction;
re.getSrc = nullFunction;
re.getRandom = nullFunction;
re.preloadArray = nullFunction;
re.isOff = nullFunction;
re.isOver = nullFunction;
re.isOn = nullFunction;
re.isLocked = nullFunction;
re.toString = function() {
	return "[object uninitialized RolloverEngine]";
}


function RolloverEngine() {
	RolloverEngine.images = new ImageCollection();

	this.ready = false;
	this.findImages();
	this.ready = true;

	return this;
}

RolloverEngine.prototype.findImages = function(doc) {
	if (!doc) doc = window.document;

	if (doc.images.length > 0) {
		var img, a, b;
		for (a = 0; a < doc.images.length; a++) {
			img = doc.images[a];
			if (img.name) {
				this.loadImage(img);
			}
		}
	}

	if (doc.layers && doc.layers != null) {
		if (doc.layers.length > 0) {
			for (b = 0; b < doc.layers.length; b++) {
				this.findImages(doc.layers[b].document);
			}
		}
	}
}

RolloverEngine.prototype.loadImage = function(img) {
	if (img && img != null && img.src) {
		lastSlash = img.src.lastIndexOf("/");
		if (lastSlash == -1) lastSlash = 0;
		lastDot = img.src.lastIndexOf(".");
		if (lastDot == -1) lastDot = img.src.length;

		imgLoc = (lastSlash > 0) ? img.src.substring(0, lastSlash + 1) : new String("");
		baseName = img.src.substring(lastSlash + 1, lastDot);
		ext = img.src.substring(lastDot, img.src.length);

		img.off = new Image();
		img.off.src = img.src;
		img.over = new Image();
		
		img.over.src = imgLoc + baseName + "_on" + ext;
		img.on = new Image();
		img.on.src = imgLoc + baseName + "_on" + ext;

		RolloverEngine.images.addImage(img);
	}
}

RolloverEngine.prototype.containsImage = function(strName) {
	if (this.ready) {
		return (strName && strName != null) ? (RolloverEngine.images[strName] && RolloverEngine.images[strName] != null) : false;
	}
}

RolloverEngine.prototype.getImage = function(strName) {
	if (this.ready) {
		return (this.containsImage(strName)) ? RolloverEngine.images[strName] : null;
	}
}

RolloverEngine.prototype.setOff = function(strName) {
	if (this.ready) {
		img = this.getImage(strName);
		if (img && img != null && !img.locked) img.src = img.off.src;
	}
}

RolloverEngine.prototype.setOver = function(strName) {
	if (this.ready) {
		img = this.getImage(strName);
		if (img && img != null && !img.locked) img.src = img.over.src;
	}
}

RolloverEngine.prototype.setOn = function(strName) {
	if (this.ready) {
		img = this.getImage(strName);
		if (img && img != null && !img.locked) img.src = img.on.src;
	}
}

RolloverEngine.prototype.setAllOff = function() {
	if (this.ready) {
		for (a = 0; a < RolloverEngine.images.countImages(); a++) {
			img = RolloverEngine.images.imageAt(a);
			if (!img.locked) img.src = img.off.src;
		}
	}
}

RolloverEngine.prototype.lock = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		if (img && img != null) img.locked = true;
	}
}

RolloverEngine.prototype.lockAll = function() {
	if (this.ready) {
		for (i = 0; i < RolloverEngine.images.countImages(); i++) {
			RolloverEngine.images.imageAt(i).locked = true;
		}
	}
}

RolloverEngine.prototype.unlock = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		if (img && img != null) img.locked = false;
	}
}

RolloverEngine.prototype.unlockAll = function() {
	if (this.ready) {
		for (j = 0; j < RolloverEngine.images.countImages(); j++) {
			RolloverEngine.images.imageAt(j).locked = false;
		}
	}
}

RolloverEngine.prototype.setSrc = function(strName, path) {
	if (this.ready) {
		img = this.getImage(strName);
		if (img && img != null) {
			img.src = path;
		}
	}
}

RolloverEngine.prototype.getSrc = function(strName) {
	if (this.ready) {
		img = this.getImage(strName);
		return img.src;
	}
}

RolloverEngine.prototype.getRandom = function(rndSrcArray) {
	var intRnd = (Math.round(Math.random() * (rndSrcArray.length - 1)));
	var rndImage = new Image();
	rndImage.src = rndSrcArray[intRnd];
	return rndImage.src;
}

RolloverEngine.prototype.preloadArray = function(arrName, imgArr) {
	eval('this.' + arrName + ' = new Array();');
	for (i=0; i < imgArr.length; i++) {	
		eval('this.' + arrName + '[' + i + '] = new Image();');
		eval('this.' + arrName + '[' + i + '].src = imgArr[i];');
	}
}

RolloverEngine.prototype.isOff = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		return (img && img != null && img.src == img.off.src);
	}
}

RolloverEngine.prototype.isOver = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		return (img && img != null && img.src == img.over.src);
	}
}

RolloverEngine.prototype.isOn = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		return (img && img != null && img.src == img.on.src);
	}
}

RolloverEngine.prototype.isLocked = function(strName) {
	if (this.ready) {
		img = RolloverEngine.images[strName];
		return (img && img != null && img.locked && img.locked == true);
	}
}

RolloverEngine.prototype.toString = function() {
	return "[object RolloverEngine]";
}


function ImageCollection() {
	this.images = new Array();
}

ImageCollection.prototype.addImage = function(img) {
	if (img && img != null && img.name) {
		this.images[this.images.length++] = img;
		try {
		    eval("this." + img.name + " = img;");
		} catch(err) {
		}
	}
}

ImageCollection.prototype.imageAt = function(index) {
	return (index >= 0 && index < this.images.length) ? this.images[index] : null;
}

ImageCollection.prototype.countImages = function() {
	return this.images.length;
}

ImageCollection.prototype.toString = function() {
	return "[object ImageCollection]";
}

