

function ImageFader(imagePaths, container) {
	this.container = container || document.body || document.documentElement;
	this.loadImages(imagePaths);
	this.animate();
};
ImageFader.prototype = {

	speed : 3,
	waitBetween : 700,
	current : 0,
	opacity : 0,
	zIndex : 1,
	
	loadImages : function(paths) {
		this.images = [];
		var self = this;
		for(var i=0; i<paths.length; i++) {
			(function( i ) {
				var img = new Image();
				img.onload = function() {
					self.images[ i ] = img; //add once loaded
				};
				img.className = "fade-image";
				self.setStyle(img);
				self.container.appendChild(img);
				img.src = paths[i];
			})( i );
		}
	},
	
	animate : function() {
		var wait = 100;
		var img = this.images[this.current];
		if(img) {
			if(this.opacity < 1) {
				fading = true;
				this.opacity += (this.speed * 0.01);
				this.setStyle(img);
			}
			else {
				this.current++;
				if(this.current >= this.images.length) this.current = 0; //wrap around
				this.zIndex++;
				this.opacity = 0;
				wait = this.waitBetween;
			}
		}
		
		var self = this;
		setTimeout(function(){self.animate();}, wait);
	},
	
	setStyle : function(img) {
		img.style.display = (this.opacity == 0 ? "none" : "");
		img.style.opacity = img.style.MozOpacity = img.style.KhtmlOpacity = this.opacity;
		img.style.filter = "alpha(opacity=" + (this.opacity * 100) + ")";
		img.style.zIndex = this.zIndex;
	}

};
