var imageViewer = new Class({

	sizes: {w:634,h:265},
	fxOptions: {duration:500},
	interval: 6000,

	initialize: function(items,options){
		if(options) for(var o in options) this[o]=options[o];
		this._current = 0;
		this._previous = null;
		this.items = items.setStyle('display', 'none');
		this.items[this._current].setStyle('display','block');
		this.attrs = {
			alpha: ['opacity',0,1,'']
		};
		this.mode = 'alpha';
	},

	walk: function(n,manual){
		if(this._current!==n && !this.disabled){
			this.disabled = true;

			this._previous = this._current;
			this._current = n;
			var a = this.attrs[this.mode].associate(['p','f','t','u']);
			for(var i=0;i<this.items.length;i++){
				if(this._current===i){
					this.items[i].setStyles($extend({'display':'block','z-index':'2'},JSON.decode('{"'+a.p+'":"'+a.f+a.u+'"}')));
				}else if(this._previous===i){
					this.items[i].setStyles({'z-index':'1'});
				}else{
					this.items[i].setStyles({'display':'none','z-index':'0'});
				}
			}
			this.items[n].set('tween',{onComplete:this.onComplete.bind(this)}).tween(a.p,a.f,a.t);
		}
	},

	play: function(wait){
		this.stop();
		if(!wait){
			this.next();
		}
		this.timer = this.next.periodical(this.interval,this,[false]);
	},

	stop: function(){
		$clear(this.timer);
	},

	next: function(manual){
		this.walk(this._current+1<this.items.length ? this._current+1 : 0,manual);
	},

	previous: function(manual){
		this.walk(this._current>0 ? this._current-1 : this.items.length-1,manual);
	},

	onComplete: function(){
		this.disabled = false;
		this.items[this._previous].setStyle('display', 'none');
		if(this.onWalk) this.onWalk(this._current);
	}
});

window.addEvent('domready', function () {
	$('imageviewer').setStyle('display', 'block');
	new imageViewer($$('#imageviewer img)'), {
		mode : 'alpha'
	}).play(true);
});
