var Rotator = new Class(
{
	imgCounter : 0,
	images: new Array(),

	options:
	{
		'baseElement' : null,
		'imageList' : null,
		'duration' : null
	},

	initialize:	function(options)
	{
		this.setOptions(options);
		this.preload();
	},

	preload: function()
	{
		var i = 0;
		for (i = 0; i < this.options.imageList.length; i++)
		{
			var image = new Image(295, 250);
			image.src = this.options.imageList[i];
			this.images[i] = image;
		}
		if (this.options.imageList.length == this.images.length)
		{
			this.build();
		}
	},

	build: function()
	{
		this.options.baseElement.empty();
		this.image = new Element('img', {'class' : 'image'});
		this.overlay = new Element('div', {'class' : 'fader'});
		this.options.baseElement.appendChild(this.image);
		this.options.baseElement.appendChild(this.overlay);
		this.options.baseElement.setStyle('visibility', 'visible');
		this.image.src = this.images[0].src;
		this.imgCounter++;
		this.loadNextItem.delay(this.options.duration,this);
	},

	loadNextItem: function()
	{
		if (this.options.imageList.length > 1)
		{
			this.fx = this.overlay.effect('opacity',
			{
				duration: 500,
				onComplete : function()
				{
					if(this.imgCounter == (this.options.imageList.length))
					{
						this.imgCounter = 0;
					}
					
					this.image.src = this.images[this.imgCounter].src;

					this.imgCounter++;

					this.fx2 = this.overlay.effect('opacity',
					{
						duration: 500
					}).start(1,0)
				}.bind(this)
			}).start(0, 1);

			this.loadNextItem.delay(this.options.duration,this);
		}
		else
		{
			this.image = this.images[0].src;
		}
	}

});
Rotator.implement(new Options, new Events);