BodyLoad.init();

var lf_gallery = function(gallery_id, space, velocity, static)
{
	this.gallery = gallery_id;
	this.index = 0;
	this.space = space == undefined ? null : space;
	this.velocity = velocity == undefined ? 800 : velocity;
	this.mov = null;
	this.static = static == undefined ? false : static;

	BodyLoad.add(Delegate.create(this, this.init));
}

lf_gallery.prototype.init = function()
{
	this.gallery = typeof this.gallery == "string" ? $(this.gallery) : this.gallery;

	this.bt_prev = $t(".bt_prev", this.gallery)[0];
	this.bt_next = $t(".bt_next", this.gallery)[0];

	this.conteiner = $t("div.conteiner", this.gallery)[0];
	this.ul_images = $t("UL", this.conteiner)[0];
	this.li_images = $t("LI", this.ul_images);
	
	if(this.space == null)
	{
		if(this.li_images.length > 0)
		{
			var li = this.li_images[0];
			this.space = (lf_gallery.getStyleNumber(li, "marginLeft") + lf_gallery.getStyleNumber(li, "marginRight")) / 2;
		}
		else
		{
			this.space = 0;
		}
	}

	for(var i=0; i<this.li_images.length; i++)
	{
		var li = this.li_images[i], _x = 0;

		li._width = li.offsetWidth;
		li._height = li.offsetHeight;
		
		if(i > 0) {
			_x += this.li_images[(i > 0) ? i-1 : 0]._width + this.li_images[(i > 0) ? i-1 : 0]._x;
			if(i > 1) _x -= this.space*2*(i-1);
		}

		li._x = _x + (this.space*2*i);
	}

	Event.add(this.bt_prev, "click", Delegate.create(this, this.prev));
	Event.add(this.bt_next, "click", Delegate.create(this, this.next));

}

lf_gallery.prototype.prev = function()
{
	if(this.index > 0){
		this.index--;
		this.moveTo( ( (this.li_images[this.index]._x ) * -1 ), lf_gallery.getStyleNumber(this.ul_images, "marginLeft"));
	}	
}

lf_gallery.prototype.next = function()
{
	if(this.index < this.li_images.length-1){
		this.index++;
		this.moveTo( ( (this.li_images[this.index]._x ) * -1 ), lf_gallery.getStyleNumber(this.ul_images, "marginLeft"));
	}
}

lf_gallery.prototype.onTweenStart =
lf_gallery.prototype.onTweenEnd =
lf_gallery.prototype.onTweenUpdate = function(x){ this.updateGallery(x); }

lf_gallery.prototype.updateGallery = function(x)
{
	this.ul_images.style.marginLeft = x + "px";
}

lf_gallery.prototype.moveTo = function(endX, initX)
{
	if(this.static) this.updateGallery(endX);
	else this.mov = new effects.Tween(this, initX, endX, this.velocity);
}

lf_gallery.getStyleNumber = function(e, p){	return parseInt(getStyle(e, p).split("px")[0]); }

lf_gallery.create = function(objConf)
{
	return new lf_gallery(objConf.gallery_id, objConf.space, objConf.velocity, objConf.static);
}