var imgRotators = [];

function funcPointer(context,method){
  return function(){
    method.apply(context);
  }
}

function ImageSwitch(oImg, iSpeed, forceReload, sPath) {

	this.index = imgRotators.length;
	imgRotators[this.index] = this;
	this.obj = oImg;
	this.speed = iSpeed;
	this.forceReload = forceReload;
	this.path = sPath;
	this.loc = 0;
	this.imgArray = [];
	this.Rotate = ImageSwitchRotate;

	for (var i = 4;i < arguments.length;i++) {
		var img = new Image();
		img.src = this.path + arguments[i];
		this.imgArray[i-4] = img;
	}
	
	this.obj.onload = null;
	this.obj.onerror = funcPointer(this,this.Rotate);
	
	this.timer = setTimeout("imgRotators["+this.index+"].Rotate();",this.speed);
	
}

function ImageSwitchRotate(){
	clearTimeout(this.timer);
	this.loc++;
	if(!this.imgArray[this.loc]){
		this.loc = 0;
	}
	if (this.forceReload){
		sUnique = new Date();
		sUnique = "?" + sUnique.getTime();
	} else {
		sUnique = "";
	}
	this.obj.src = this.imgArray[this.loc].src + sUnique;
	this.timer = setTimeout("imgRotators["+this.index+"].Rotate();",this.speed)
}