var ImageRotators = Array();

function ImageRotatorCreate(imgs, caps, id, fadetime, fadesteps, showtime) {
	ImageRotators.push(new ImageRotator(imgs, caps, id, fadetime, fadesteps, showtime));
}

function ImageRotator(imgs, caps, id, fadetime, fadesteps, showtime) {
	// preload images...
	//var temp = Array();
	//var i = 0;
	//var newdiv = document.createElement("div");
	//newdiv.setAttribute("id",id+"_hidden_images");
	//newdiv.setAttribute("class","hidden_images");
	//for(i=0;i<imgs.length;i++) {
	//	var newimg = document.createElement("img");
	//	//newimg.setAttribute("style","display:none;");
	//	newimg.setAttribute("src",imgs[i]);
	//	newimg.setAttribute("class","hidden_image");
	//	newdiv.appendChild(newimg);
	//	//document.getElementById(id).appendChild(newimg);
	//}
	//document.getElementById(id).appendChild(newdiv);

	this.Images = imgs;
	this.Captions = caps;
	this.ID = id;
	this.FadeTimer = null;
	this.FadeTime = fadetime; // 20
	this.FadeSteps = fadesteps; // 10
	this.ShowTime = showtime; // 5000
	this.CurrentImage = 0;
	this.Start();

	return true;
}

ImageRotator.prototype.Start = function() {
	// Set to first image
	

	// Set caption to caption of first image
	document.getElementById(this.ID + "_cap").innerHTML = this.Captions[this.CurrentImage];

	// Start the "show" timer
	var self = this;
	//this.FadeTimer = setTimeout("ImageRotatorFade("+this.FadeSteps+",-1)",this.ShowTime);
	this.FadeTimer = setTimeout(function(){self.Fade(self.FadeSteps,-1)},this.ShowTime);
}

ImageRotator.prototype.Fade = function(s,d) {
	if(d==1 && s>=this.FadeSteps) {
		var self = this;
		this.FadeTimer = setTimeout(function(){self.Fade(self.FadeSteps,-1)},this.ShowTime);
		return;
	}
	if(d==-1 && s<0) {
		this.Next();
		var self = this;
		this.FadeTimer = setTimeout(function(){self.Fade(0,1)},self.FadeTime/self.FadeSteps);
		return;
	}
	document.getElementById(this.ID + "_img").style.opacity = s/this.FadeSteps;
	document.getElementById(this.ID + "_img").style.MozOpacity = s/this.FadeSteps;
	document.getElementById(this.ID + "_img").style.filter = "alpha(opacity="+Math.round(100*(s/this.FadeSteps))+")";
	var self = this;
	self.FadeTimer = setTimeout(function(){self.Fade((s+d),d)},this.FadeTime/this.FadeSteps);
	return;
}

/*
	if(d==1 && s>=self.FadeSteps) {
		self.FadeTimer = setTimeout("ImageRotatorFade("+self.FadeSteps+",-1)",self.ShowTime);
		return;
	}
	if(d==-1 && s<0) {
		self.Next();
		self.FadeTimer = setTimeout("ImageRotatorFade(0,1)",self.FadeTime/self.FadeSteps);
		return;
	}
	document.getElementById(self.ID + "_img").style.opacity = s/self.FadeSteps;
	document.getElementById(self.ID + "_img").style.MozOpacity = s/self.FadeSteps;
	document.getElementById(self.ID + "_img").style.filter = "alpha(opacity="+Math.round(100*(s/self.FadeSteps))+")";
	self.FadeTimer = setTimeout("ImageRotatorFade("+(s+d)+","+d+")",self.FadeTime/self.FadeSteps);
	return;

*/

ImageRotator.prototype.Next = function() {
	if(this.CurrentImage == this.Images.length - 1) {
		this.CurrentImage = 0;
	} else {
		this.CurrentImage++;
	}
	document.getElementById(this.ID + "_img").src = this.Images[this.CurrentImage];
	document.getElementById(this.ID + "_cap").innerHTML = this.Captions[this.CurrentImage];
}