function _DynImageAPI(){
	this.path = "./images/";
	this.ext = "gif";
	this.state = {
		"default" : {
			"out"  : "off",
			"over" : "on",
			"down" : null
		},
		"selected" : { }
	};

	this.instances = 0;
	this.objects = new Object();
	this.compatible = (!!document.images) ? true : false;
}
DynImageAPI = new _DynImageAPI();

function _d_applyEffect(oDynImage, sState){
	if( !oDynImage.isImage ) return false;
	var o = eval(oDynImage.obj);
	// check to make sure you're loading a valid state
	var sType = (this.isDefinedState(sState, oDynImage.state)) ? oDynImage.state : "default";
	// if the state still isn't valid, then stop processing
	if( !this.isDefinedState(sState, sType) ) return false;
	// change the image
	o.src = oDynImage.images[sType + "|" + sState].src;
	return true;
}
_DynImageAPI.prototype.applyEffect = _d_applyEffect;

function _d_getPath(){
	return (this.path.charAt(this.path.length-1) == "/") ? this.path : this.path + "/";
}
_DynImageAPI.prototype.getPath = _d_getPath;

function _d_isDefinedState(sState, sType){
	if( typeof sState == "undefined" ) return false;
	if( typeof sType == "undefined" ) sType = "default";
	var bResult = (!!this.state[sType]);
	if( bResult ) bResult = (!!this.state[sType][sState]);
	return bResult
}
_DynImageAPI.prototype.isDefinedState = _d_isDefinedState;

function _d_setState(sName, sState){
	// loop through all the images and run the rollout() method
	for( k in DynImageAPI.objects ){
		if( this.objects[k].name == sName ){
			this.objects[k].state = sState;
			this.objects[k].rollout();
			break;
		}
	}
}
_DynImageAPI.prototype.setState = _d_setState;

function DynImage(sName, sId, sLayer){
	if( arguments.length == 0 || !DynImageAPI.compatible ) return false;
	
	// if id not provided, assume its the name + id
	this.id = ( !!sId ) ? sId : "id" + sName;
	this.name = sName;
	this.layer =  ( !!sLayer ) ? sLayer : null;
	this.enabled = true;
	this.obj = "document[\"" + this.id + "\"]";
	if( document.layers && this.layer != null ) this.obj = this.layer + "." + this.obj;

	this.images = new Object();
	this.state = "default";
	DynImageAPI.instances++;
	DynImageAPI.objects[DynImageAPI.instances] = this;
	this.pid = DynImageAPI.instances;

	var path = DynImageAPI.getPath(), sImgName = "";
	
	for( sType in DynImageAPI.state ){
		for( sState in DynImageAPI.state[sType] ){
			if( DynImageAPI.isDefinedState(sState, sType) ){
				sImgName = sType + "|" + sState;
				this.images[sImgName] = new Image();
				this.images[sImgName].src = path + this.name + "_" + DynImageAPI.state[sType][sState] + "." + DynImageAPI.ext;
			}
		}
	}
	
	return true;
}
new DynImage(); // initialize an object so prototypes exist in NS3

function _d_rollover(){
	return DynImageAPI.applyEffect(this, "over");
}
DynImage.prototype.rollover = _d_rollover;

function _d_rollout(){
	return DynImageAPI.applyEffect(this, "out");
}
DynImage.prototype.rollout = _d_rollout;

function _d_rolldown(){
	return DynImageAPI.applyEffect(this, "down");
}
DynImage.prototype.rolldown = _d_rolldown;

function _d_isImage(){
	if( !this.enabled || !DynImageAPI.compatible ) return false;
	if( !document[this.id] ) return this.throwError("No image tag with the id \"" + this.id + "\" was found.");
	return true;
}
DynImage.prototype.isImage = _d_isImage;

function _d_throwError(error){
	alert(error);
	this.enabled = false;
	return false;
}
DynImage.prototype.throwError = _d_throwError;