Fader = Class.create();  
Object.extend(Fader.prototype, {
	initialize: function(id, options) {
		this._options = {
			tempo: options.tempo,
			height: options.height,
			width: options.width
		}
		this._container = $(id);
		this._container.style.width =  this._options.width+'px';
		this._container.style.height = this._options.height+'px';
		this._container.style.display = 'block';
		this._container.style.position = 'relative';
		this._container.style.overflow = 'hidden';
		this._images = this._container.select('a');
		this.images = [];
		this._timeout = null;
		this.imgOffset = 0;
		this.up = true;
		
		this.init();
		return this;
	},
	init: function() {
		var i = 0;
		this._images.each(function(e) {
			this.images[i] = {elt: e, opacity: 0};
			this.initImage(i);
			i++;
		}.bind(this));
		this.fade();
	},
	initImage: function(i) {
		this.images[i].elt.setOpacity(this.images[i].opacity);
		this.images[i].elt.style.position = 'absolute';
		this.images[i].elt.style.top = 0;
		this.images[i].elt.style.left = 0;
		this.images[i].elt.style.zIndex = i;
	},
	doTempo: function() {
		this.imgOffset = this.up ? this.imgOffset+1 : this.imgOffset-1;
		if (this.imgOffset > this._images.length-1) {
			this.imgOffset = this._images.length-1;
			this.up = !this.up;
		}
		else if (this.imgOffset < 1){
			this.imgOffset = 1;
			this.up = !this.up;
		}
		this._timeout = setTimeout(this.fade.bind(this), this._options.tempo*1000);
	},
	fade: function() {
		if (this.up) Effect.Appear(this.images[this.imgOffset].elt, {duration: .5, afterFinish: this.doTempo.bind(this)});
		else Effect.Fade(this.images[this.imgOffset].elt, {duration: .5, afterFinish: this.doTempo.bind(this)});
	}
});

