var slideShow = new Class({
	Implements: [Options],
	
	options: {
		secondsfade: 700,
		secondspause: 5000,
		autostart: true
	},
	
	initialize: function(items, options){
		this.setOptions(options);
		this.items = items;
		this.items.each(function(item, index){
			item.setStyles({'z-index': index, 'top': 0, 'position': 'relative'});
			if (index > 0){
				item.setOpacity(0);
			}
		});
		this.curropen = this.prevopen = 0;
		if (this.options.autostart) this.startShowing();
	},
	
	startShowing: function(){
		if (!this.options.autostart) this.nextTurn(this);
		this.minterval = this.nextTurn.pass(this).delay(this.options.secondspause);
	},
	
	stopShowing: function(){
		$clear(this.minterval);
	},
	
	nextTurn: function(p){
		//p.stopShowing();
		p.prevopen = p.curropen;
		p.curropen++;
		if (p.curropen >= p.items.length) p.curropen = 0;
		p.turnOver();
	},
	
	prevTurn: function(p){
		//p.stopShowing();
		p.prevopen = p.curropen;
		p.curropen--;
		if (p.curropen < 0) p.curropen = p.items.length-1;
		p.turnOver();
	},
	
	gotoPage: function(num){
		if (num >= 0 && num < this.items.length){
			this.stopShowing();
			this.prevopen = this.curropen;
			this.curropen = num;
			this.turnOver();
		}else{
			console.log(this.items.length);
		}
	},
	
	turnOver: function(){
		var p = this;
		if (this.showIn != undefined){
			this.showIn.cancel();
			this.showOut.cancel();
		}
		this.showIn = new Fx.Morph(p.items[this.curropen], {duration: this.options.secondsfade, transition: Fx.Transitions.Sine.easeOut});
		this.showOut = new Fx.Morph(p.items[this.prevopen], {duration: this.options.secondsfade, transition: Fx.Transitions.Sine.easeOut});
		this.showOut.start({
			'opacity': 0
		}).chain(function(){
			p.items[p.prevopen].setStyle("display", "none");
			p.items[p.curropen].setStyle("display", "block");
			p.showIn.start({
				'opacity': 1
			}).chain(function(){
				p.minterval = p.nextTurn.pass(p).delay(p.options.secondspause);
			});
		});
	}
});