var Gallery = new Class({
	Implements: Options,
	options: {
		active: 0,
		delay: 6000
	},
	initialize: function(gallery, options){
		this.setOptions(options);
		this.container = $(gallery);
		this.gallery = this.container.getElement('div.images');
		this.images = this.gallery.getElements('img');
		this.toogleNext = this.container.getElement('a.next');
		this.tooglePrev = this.container.getElement('a.prev');
		this.pagination = this.container.getElements('ul.pagination a');
		this.length = this.images.length;
		this.locked = false;
		this.setup();
	},
	setup: function(){
		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.setPage(this.options.active);
		this.toogleNext.addEvent('click', this.next.bindWithEvent(this));
		this.tooglePrev.addEvent('click', this.prev.bindWithEvent(this));
		this.pagination.each(function(el, i){
			el.addEvent('click', function(e){
				if(e) new Event(e).stop();
				this.options.active = i;
				this.slideImageFX.toElement(this.images[this.options.active]);
				this.setPage(this.options.active);
			}.bind(this));
		}.bind(this));
	},
	next: function(e){
		if(e) new Event(e).stop();
		if(this.locked) return false;
		if(this.options.active == this.length-1){
			this.options.active = -1;
		}
		if(this.options.active != this.length) this.options.active += 1;
		this.setPage(this.options.active);
		this.slideImageFX.toElement(this.images[this.options.active]);
	},
	prev: function(e){
		new Event(e).stop();
		if(this.locked) return false;
		if(this.options.active == 0){
			this.options.active = (this.length);
		}
		if(this.options.active != 0) this.options.active -= 1;
		this.setPage(this.options.active);
		this.slideImageFX.toElement(this.images[this.options.active]);
	},
	setPage: function(page){
		this.pagination.each(function(el){
			el.removeClass('active');
		});
		this.pagination[page].addClass('active');
	}
});

window.addEvent('domready', function(){
	new Gallery('portfolio-gallery');
});
