var BannerRotator = Class.create({
	initialize: function(container, imageList, options) {
		this.opts = {};
		this.imageList = imageList;
		this.container = container;
		this.setOptions(options);
		if( !( this.imageList && this.imageList.length > 0) ) {
			alert("Can't rotate an empty image list");
			return;
		}
		this.currentIndex = 0;
		this.imgCache = {};
		this.start();
	}

	,setOptions: function(opts) {
		if(!opts) opts = {};
		this.opts["delay"] = opts["delay"] ? opts["delay"] : 2000;
	}

	,start: function() {

		this.container.innerHTML = "";
		this.container.appendChild( this.getImg(this.currentIndex) );
		setInterval(this.rotate.bind(this), this.opts["delay"]);
	}

	,rotate: function() {
		this.currentIndex += 1;
		if( this.currentIndex >= this.imageList.length ) {
			this.currentIndex = 0;
		}
		var nextImg = this.getImg(this.currentIndex);
		var currImg = this.container.firstChild;
		//this.container.replaceChild( nextImg, this.container.firstChild);

		new Effect.Opacity(this.container,
			{duration: 0.75,
			 from: 1.0, to: 0.0,
			 afterFinish: (function() {
				this.container.replaceChild( nextImg, currImg );
				new Effect.Opacity(this.container,
					{duraction: 0.71,
					 from: 0.0, to: 1.0});

			 }).bind(this)
			});

	}

	,getImg: function(ix) {
		var img = null;
		if(this.imgCache[ix]) {
			img = this.imgCache[ix];
		}
		else {
			var imgSrc = this.imageList[this.currentIndex];
			img = new Image();
			img.src = imgSrc;
			this.imgCache[ix] = img;
		}
		return img;
	}

});
