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.description');
		this.toogleNext = $('slideshow-togglers').getElement('a.next');
		this.tooglePrev = $('slideshow-togglers').getElement('a.prev');
		this.length = this.images.length;
		this.locked = false;
		this.setup();
	},
	setup: function(){
		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,
			wheelStops: false
		}).toElement(this.images[this.options.active]);

		this.slideDescriptionFX = new Fx.Scroll(this.description, {
			transition: Fx.Transitions.Quint.easeInOut,
			wheelStops: false
		}).toElement(this.images[this.options.active]);

		this.toogleNext.addEvent('click', this.next.bindWithEvent(this));
		this.tooglePrev.addEvent('click', this.prev.bindWithEvent(this));

		this.play();
	},
	next: function(e){
		if(e) new Event(e).stop();
		if(this.locked) return false;
		if(this.options.active == this.length-1){
			this.cloneFirst();
			return false;
		}
		if(this.options.active != this.length) this.options.active += 1;
		this.slideImageFX.toElement(this.images[this.options.active]);
		this.slideDescriptionFX.toElement(this.descriptions[this.options.active]);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	prev: function(e){
		new Event(e).stop();
		if(this.locked) return false;
		if(this.options.active == 0){
			this.cloneEnd();
			return false;
		}
		if(this.options.active != 0) this.options.active -= 1;
		this.slideImageFX.toElement(this.images[this.options.active]);
		this.slideDescriptionFX.toElement(this.descriptions[this.options.active]);
		if(e){
			$clear(this.timer);
			this.play();
		}
	},
	cloneFirst: function(){
		if(!this.cloneImageFirst) this.cloneImageFirst = this.images[0].clone().injectAfter(this.images[this.options.active]);
		if(!this.cloneDescriptionFirst) this.cloneDescriptionFirst = this.descriptions[0].clone().injectAfter(this.descriptions[this.options.active]);
		this.slideImageFX.toElement(this.cloneImageFirst).chain(function(){
			this.slideImageFX.set([this.images[0].offsetLeft, 0]);
			this.slideDescriptionFX.set([this.descriptions[0].offsetLeft, 0]);
		}.bind(this));
		this.slideDescriptionFX.toElement(this.cloneDescriptionFirst);
		this.options.active = 0;
	},
	cloneEnd: function(){
		if(!this.cloneImageLast) this.cloneImageLast = this.images[(this.length-1)].clone().injectBefore(this.images[this.options.active]);
		if(!this.cloneDescriptionLast) this.cloneDescriptionLast = this.descriptions[(this.length-1)].clone().injectBefore(this.descriptions[this.options.active]);
		this.slideImageFX.set([this.images[0].offsetLeft, 0]);
		this.slideDescriptionFX.set([this.descriptions[0].offsetLeft, 0]);

		this.slideImageFX.toElement(this.cloneImageLast).chain(function(){
			this.slideImageFX.set([this.images[(this.length-1)].offsetLeft, 0]);
			this.slideDescriptionFX.set([this.descriptions[(this.length-1)].offsetLeft, 0]);
		}.bind(this));
		this.slideDescriptionFX.toElement(this.cloneDescriptionLast);
		this.options.active = (this.length-1);
	},
	play: function(){
		this.timer = this.next.periodical(this.options.delay, this);
	}
});

window.addEvent('domready', function(){
	new Slideshow('gallery-overflow', 'slide-description');
});