var Slideshow = new Class({
	Implements: Options,
	options: {
		active: 0,
		delay: 9000
	},
	initialize: function(gallery, options){
		this.setOptions(options);
		this.gallery = $(gallery);
		this.images = this.gallery.getElements('img');
		this.length = this.images.length;
		this.locked = false;
		this.setup();
	},
	setup: function(){
		this.pagination = new Element('div', {'class': 'pagination'});
		//this.pagination.inject(this.gallery);
		this.paginate();
		this.getPositions(this.length);

		this.images.each(function(el, i){
			el.fade('out');
		}.bind(this));
		new Fx.Tween(this.images[this.options.active], {duration: 1000}).start('opacity', [0, 1.0]);

		this.play();
	},
	next: function(e){
		if(e) new Event(e).stop();
		this.pages[this.options.active].removeClass('active');
		new Fx.Tween(this.images[this.options.active], {duration: 1500}).start('opacity', [1.0, 0]);
		(this.options.active == this.length-1) ? this.options.active = 0 : this.options.active += 1;
		this.pages[this.options.active].addClass('active');
		new Fx.Tween(this.images[this.options.active], {duration: 1500}).start('opacity', [0, 1.0]);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	prev: function(e){
		new Event(e).stop();
		this.pages[this.options.active].removeClass('active');
		this.images[this.options.active].fade('out');
		(this.options.active == 0) ? this.options.active = (this.length-1) : this.options.active -= 1;
		this.pages[this.options.active].addClass('active');
		this.images[this.options.active].fade('in');
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	play: function(){
		this.timer = this.next.periodical(this.options.delay, this);
	},
	paginate: function(){
		var pages = '';
		for(var i = 1; i <= this.length; i++){
			pages += '<a href="#">' + (i) + '</a>\n';
		}
		this.pagination.set('html', pages);
		this.pages = this.pagination.getElements('a');
		this.pages.each(function(el, i){
			el.addEvent('click', function(e){
				e.stop();
				this.goto(e, i);
			}.bind(this));
		}.bind(this));
		this.pages[this.options.active].addClass('active');
	},
	goto: function(e, pos){
		e.stop();
		if(this.locked) return false;
		this.pages[this.options.active].removeClass('active');
		this.images[this.options.active].fade('out');
		this.options.active = pos;
		this.pages[this.options.active].addClass('active');
		this.images[this.options.active].fade('in');
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	getPositions: function(size){
		this.imagePositions = [];
		for(var i = 0; i < size; i++){
			this.imagePositions[i] = this.images[i].getPosition(this.gallery);
		}
	}
});
