/**
* Gallery 1.0
* Compatible Mootools 1.2
* @author IRCF
*/
var Gallery = new Class({
	Implements : Options,
	options : {
		position : 0,
		auto : false,
		previous : null,
		next : null,
		delay : 5000,
		duration : 500
	},
	initialize : function (element,options){
		this.setOptions(options);
		this.element = element;
		this.items = this.element.getElements("ul > li");
		this.width = this.items[0].getStyle('width').toInt();
		this.position = this.options.position;
		this.items.each(function(item,i){
			item.set('tween',{duration:this.options.duration});
			item.setStyle('margin-left',[(i!=this.position)?this.width:0]);
		}.bind(this));
		if (this.options.previous) this.options.previous.addEvent('click', this.previous.bind(this));
		if (this.options.next) this.options.next.addEvent('click', this.next.bind(this));
		if (this.options.auto){
			this.thread = this.next.periodical(this.options.delay,this);
			this.element.addEvents({
				mouseenter : function(){
					$clear(this.thread);
				}.bind(this),
				mouseleave : function(){
					this.thread = this.next.periodical(this.options.delay,this);
				}.bind(this)
			});
		}
		this.element.addEvent('click',this.next.bind(this));
	},
	slideIn : function(item,direction){
		item.setStyle('margin-left',[(2*direction-1)*this.width]);
		item.tween('margin-left',0);
	},
	slideOut : function(item,direction){
		item.tween('margin-left',[(2*direction-1)*this.width]);
	},
	next : function(){
		this.setPosition((this.position+this.items.length-1) % this.items.length);
	},
	previous : function(){
		this.setPosition((this.position+this.items.length+1) % this.items.length);
	},
	setPosition : function(position){
		this.slideOut(this.items[this.position],0);
		this.position = position;
		this.slideIn(this.items[this.position],1);
	}
});

