var LunaGallery = 
{
	items: null,
	gallery: null,

	init: function(gallery)
	{
		this.gallery = gallery;
		this.items = jQuery(this.gallery).find(".item");
		this.gallery.itemsElement =  jQuery(this.gallery).find(".items");
		this.gallery.itemsArea = jQuery(this.gallery).find(".items_area");
		
		this.gallery.buttonLeft =  jQuery(this.gallery).find(".arrow_left");
		this.gallery.buttonRight =  jQuery(this.gallery).find(".arrow_right");
		
		if(!this.items.length)
			return console.info('no items');
		
		this.assignEvents()
		this.setItemsWidth(this.items.innerWidth(), this.items.length);
	},

	assignEvents: function()
	{
		t = this;
		this.gallery.buttonLeft.click(function()
				{
					t.moveLeft()
				}
		);
		this.gallery.buttonRight.click(function()
				{
					t.moveRight()
				}
		);
	},
	
	setItemsWidth: function(itemWidth, itemsCount)
	{
		this.gallery.itemsElement.width = itemWidth * itemsCount;
		this.gallery.itemsElement.itemsCount = itemsCount;
		this.gallery.itemsElement.itemWidth = itemWidth;
		
		this.gallery.itemsArea.width = this.gallery.itemsArea.innerWidth();
		
		this.gallery.itemsElement.css('width', this.gallery.itemsElement.width);
	},
	
	moveLeft: function()
	{
		left = parseInt(this.gallery.itemsElement.css("left"));
		
		if(left < 0)
			if(left+this.items.innerWidth() < 0)
				this.gallery.itemsElement.animate({"left": "+="+this.items.innerWidth()+"px"}, "slow");
			else
				this.moveAndBackTo(left+this.items.innerWidth(), 0);
		else
			this.moveAndBackLeft()
	},
	
	moveRight: function()
	{
		left = parseInt(this.gallery.itemsElement.css("left"));
		
		left = left+this.gallery.itemsElement.width-this.gallery.itemsArea.width;
		if(left > 0)
			if(left-this.items.innerWidth() > 0)
				this.gallery.itemsElement.animate({"left": "-="+this.items.innerWidth()+"px"}, "slow");
			else
			{
				this.moveAndBackTo(parseInt(this.gallery.itemsElement.css("left"))-this.items.innerWidth(),this.gallery.itemsArea.width - this.gallery.itemsElement.width);
			}
		else
			this.moveAndBackRight()
	},
	
	moveAndBackLeft: function()
	{
		left = parseInt(this.items.innerWidth()/3);
		this.gallery.itemsElement.animate({"left": "+="+left+"px"}, "400")
								 .animate({"left": "-="+left+"px"}, "400")
	},
	
	moveAndBackRight: function()
	{
		left = parseInt(this.items.innerWidth()/3);
		this.gallery.itemsElement.animate({"left": "-="+left+"px"}, "400")
								 .animate({"left": "+="+left+"px"}, "400")
	},
	
	moveAndBackTo: function(to, back)
	{		
		this.gallery.itemsElement.animate({"left": to+"px"}, "400")
								 .animate({"left": back+"px"}, "400")
	},
	
	startDrag: function(event)
	{
	
		this.gallery.itemsElement.startClientX = event.clientX;
		this.gallery.itemsElement.startLeft = parseInt(this.gallery.itemsElement.css("left"));
		
		this.gallery.itemsElement.live("mousemove", this.drag.bind(this))
		this.gallery.itemsElement.a = false;
	},
	
	stopDrag: function(event)
	{
		this.gallery.itemsElement.die("mousemove");
	},
	
	drag: function(event)
	{
		var move = this.gallery.itemsElement.startLeft + event.clientX - this.gallery.itemsElement.startClientX ;
		this.gallery.itemsElement.css("left", move+'px');
	}
}

jQuery(document).ready
(
	function()
	{
		var galleries = jQuery(".luna_gallery");

		for(var i = 0; i < galleries.length; i++)
		{
			LunaGallery.init(galleries[i]);
		}
	}
)
