var Slideshow = new Class({
	Implements: Options,
	options: {
		active: 0,
		delay: 6000
	},
	initialize: function(gallery, description, options){
		this.setOptions(options);
		this.gallery = $(gallery);
		this.description = $(description);
		this.images = this.gallery.getElements('img');
		this.descriptions = this.description.getElements('div');
		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.cloneImageFirst = false;
		this.cloneDescriptionFirst = false;
		this.cloneImageLast = false;
		this.cloneDescriptionLast = false;

		this.slideImageFX = new Fx.Scroll(this.gallery, {
			onStart: function(){this.locked = true;}.bind(this),
			onComplete: function(){this.locked = false;}.bind(this),
			transition: Fx.Transitions.Quint.easeInOut,
			duration: 700,
			wheelStops: false,
			wait: 'cancel'
		});
		this.slideImageFX.set(this.imagePositions[this.options.active].x, this.imagePositions[this.options.active].y);

		this.slideDescriptionFX = new Fx.Scroll(this.description, {
			transition: Fx.Transitions.Quint.easeInOut,
			duration: 700,
			wheelStops: false,
			wait: 'cancel'
		});
		this.slideDescriptionFX.set(this.descriptionPositions[this.options.active].x, this.descriptionPositions[this.options.active].y);

		this.play();
	},
	next: function(e){
		if(e) new Event(e).stop();
		this.pages[this.options.active].removeClass('active');
		/*if(this.options.active == this.length-1){
			this.cloneFirst();
			return false;
		}*/
		(this.options.active == this.length-1) ? this.options.active = 0 : this.options.active += 1;
		this.pages[this.options.active].addClass('active');
		this.slideImageFX.start(this.imagePositions[this.options.active].x, this.imagePositions[this.options.active].y);
		this.slideDescriptionFX.start(this.descriptionPositions[this.options.active].x, this.descriptionPositions[this.options.active].y);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	prev: function(e){
		new Event(e).stop();
		this.pages[this.options.active].removeClass('active');
		/*if(this.options.active == 0){
			this.cloneEnd();
			return false;
		}*/
		(this.options.active == 0) ? this.options.active = (this.length-1) : this.options.active -= 1;
		this.pages[this.options.active].addClass('active');
		this.slideImageFX.start(this.imagePositions[this.options.active].x, this.imagePositions[this.options.active].y);
		this.slideDescriptionFX.start(this.descriptionPositions[this.options.active].x, this.descriptionPositions[this.options.active].y);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	cloneFirst: function(){
		if(!this.cloneImageFirst) this.cloneImageFirst = this.images[0].clone().inject(this.images[(this.length-1)], 'after');
		if(!this.cloneDescriptionFirst) this.cloneDescriptionFirst = this.descriptions[0].clone().inject(this.descriptions[(this.length-1)], 'after');
		this.slideImageFX.toElement(this.cloneImageFirst).chain(function(){
			this.slideImageFX.set([this.images[0].offsetLeft, 0]);
		}.bind(this));
		this.slideDescriptionFX.toElement(this.cloneDescriptionFirst).chain(function(){
			this.slideDescriptionFX.set([this.descriptions[0].offsetLeft, 0]);
		}.bind(this));
		this.options.active = 0;
		this.pages[this.options.active].addClass('active');
	},
	cloneEnd: function(){
		if(!this.cloneImageLast) this.cloneImageLast = this.images[(this.length-1)].clone().inject(this.images[0], 'before');
		if(!this.cloneDescriptionLast) this.cloneDescriptionLast = this.descriptions[(this.length-1)].clone().inject(this.descriptions[0], 'before');
		this.slideImageFX.set([this.images[0].offsetLeft, this.images[0].offsetTop]);
		this.slideDescriptionFX.set([this.descriptions[0].offsetLeft, this.descriptions[0].offsetTop]);
		this.slideImageFX.toElement(this.cloneImageLast).chain(function(){
			this.slideImageFX.set([this.images[(this.length-1)].offsetLeft, this.images[(this.length-1)].offsetTop]);
		}.bind(this));
		this.slideDescriptionFX.toElement(this.cloneDescriptionLast).chain(function(){
			this.slideDescriptionFX.set([this.descriptions[(this.length-1)].offsetLeft, this.descriptions[(this.length-1)].offsetTop]);
		}.bind(this));
		this.options.active = (this.length-1);
		this.pages[this.options.active].addClass('active');
	},
	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+1) + '</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.options.active = pos;
		this.pages[this.options.active].addClass('active');
		this.slideImageFX.start(this.imagePositions[this.options.active].x, this.imagePositions[this.options.active].y);
		this.slideDescriptionFX.start(this.descriptionPositions[this.options.active].x, this.descriptionPositions[this.options.active].y);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	getPositions: function(size){
		this.imagePositions = [];
		this.descriptionPositions = [];
		for(var i = 0; i < size; i++){
			this.imagePositions[i] = this.images[i].getPosition(this.gallery);
			this.descriptionPositions[i] = this.descriptions[i].getPosition(this.description);
		}
	}
});
