var RotateImagesI = [];

function RotateImages(arrayofimages,container) {
	window.RotateImagesI.push(this);
	this.el=document.getElementById(container);
	this.el.style.textAlign='left';
	this.images=arrayofimages;
	this.index=0;

	this.interval=8;

	if (this.images.length <= 1) return false;

	this.initialize = function() {
		this.preload();
		this.loop();
	}

	this.preload = function() {
		for(var i=0;i<=this.images.length;i++) {
			if (this.images[i]) {	
				var p = new Image();
				p.src=this.images[i];
			}
		}
	}

	this.next = function() {
		this.index = (this.index >= this.images.length) ? 0 : this.index+1;
		if (!this.images[this.index]) return this.next();
		this.swap();
	}

	this.getCurrentImg = function() {
		return this.el.getElementsByTagName("IMG")[0];
	}

	this.swap = function() {
		var current_image = this.el.getElementsByTagName("IMG")[0];
		current_image.style.position="absolute";
		if (!current_image || !current_image.remove) {
			this.el.innerHTML="";
		}
		var new_image = this.getNewImg();
		this.el.appendChild(new_image);
		new Effect.Appear(new_image,{duration: 1});
		if (current_image && current_image.remove) {
			new Effect.Fade(current_image,{
				duration: 1,
				afterFinish: function() {
					if (current_image && current_image.parentNode) current_image.parentNode.removeChild(current_image);
				}
			});
		}
	}

	this.loop = function() {
		this.next();
		var c = this;
		if (this.timer) window.clearTimeout(this.timer);
		this.timer=window.setTimeout(function() {
			return c.loop.apply(c);
		},this.interval*1000);
	}

	this.getNewImg = function() {
		var el = document.createElement("img");		
		el.src = this.images[this.index];
		el.style.position="absolute";
		el.style.display="none";
		return el;
	}
}

