var preLoadingRotator = new Class ({
	_pos: 0,
	_imgTotal: 0,
	_fadeImgOut: 0,
	_fadeImgIn: 0,
	_imgSrcs: [],

	Implements:[Options],

	options: {
		images: [],
		preloader: null,
		period: 0,
		fadeTime: 0
	},

	initialize: function(options) {

		this.setOptions(options);
		this._imgTotal = this.options.images.length-1;

		_imgSrcs = new Array();

		//place image sources into an array for preloading
		this.options.images.each(function (el,l) {
			_imgSrcs[l] = el.src;
		});

		//pre load images from array once complete begin fade and remove preloading div
		var myImages = new Asset.images(_imgSrcs, {
			onComplete: function() {
				this.options.preloader.setStyle('visibility', 'hidden');
				this.options.images[0].setStyle('visibility', 'visible');
				this.options.images[0].setStyle('opacity', 1);

				if (this.options.start !== undefined && this.options.start == true)
					this.rotator = this.fadeImages.periodical(this.options.period, this);

			}.bind(this)
		});
	},

	fadeImages: function() {
		
		//if last image reset to start of sequence
		if (this._pos == this._imgTotal) {
			this._fadeImgOut=this._imgTotal;
			this._fadeImgIn=0;
			this._pos = 0;
		}
		//else move to next image and fade out current
		else {
			this._fadeImgOut=this._pos;
			this._fadeImgIn=this._pos+1;
			this._pos ++;
		}

		//do actual image fading
		var fadeOut = new Fx.Tween(this.options.images[this._fadeImgOut], {duration: this.options.fadeTime});
		var fadeIn = new Fx.Tween(this.options.images[this._fadeImgIn], {duration: this.options.fadeTime});
		fadeOut.start('opacity',[1,0]);
		fadeIn.start('opacity',[0,1]);
	},

	stop: function() {
		$clear(this.rotator);
	},

	start: function() {
		this.fadeImages();
		this.rotator = this.fadeImages.periodical(this.options.period, this);
	},

	setPosition: function(position) {
		this._pos = position;
	}

});
