/**
 * SimpleGallery for mootools 1.2.1
 * @license MIT-style license
 * @author Anquetil Manuel <manuel.anquetil [at] baphira.com>
 * @copyright Baphira - http://www.baphira.com
 */
var SimpleGallery = new Class({
	
	Implements: [Options],
	
	options: {
		slideWidth: 487,
		prevBtn: null,
		nextBtn: null
	},
	
	initialize: function(el, options) {
		this.setOptions(options);
		this.gallery = $(el);
		this.scroller = this.gallery.getElement('div');
		this.slides = this.scroller.getElements('ul');
		this.prevBtn = this.options.prevBtn;
		this.nextBtn = this.options.nextBtn;
		
		this.iSelected = 0;
		this.loading = false;
		
		this.effect = new Fx.Morph (this.scroller, {
			transition: Fx.Transitions.Quad.easeInOut,
			duration: 1000
		});
				
		this.prevBtn.addEvent('click', function(e) {
			new Event(e).stop();
			var index = this.iSelected - 1;
			if(index < 0) {
				index = 0;
			}
			this.show(index);
		}.bind(this));
		
		this.nextBtn.addEvent('click', function(e) {
			new Event(e).stop();
			var index = this.iSelected + 1;
			if(index >= this.slides.length) {
				index = this.slides.length-1;
			}
			this.show(index);
		}.bind(this));
	},
	
	show: function(index) {
		if(!this.loading) {
			this.loading = true;
			
			if(this.iSelected < 0) {
				this.iSelected = index;
				this.loading = false;
			} else {
				this.effect.start({
					'margin-left':[-this.iSelected*this.options.slideWidth,-index*this.options.slideWidth]
				}).chain(function() {
					this.iSelected = index;
					this.loading = false;
				}.bind(this));
			}
		}
	}
	
});
