var Lojas = function()
{
	var menu_lojas = $("#menu-lojas ul li a");
	var lojas = $("#enderecos .loja");
	
	var w = 550;
	
	var peddingLoja = 0;
	var actLoja = 0;
	
	var indexs = {};
	
	return {
		init: function()
		{
			lojas.find("ul").width((w * lojas.find("ul li").length));
			lojas.find("ul").each(function(){
				indexs[$(this).attr("id")] = 0;
			});
			
			lojas.find(".bt-next").click(this.next);
			lojas.find(".bt-prev").click(this.prev);
			
			menu_lojas.click(this.show);
			
			var firsId = new Number($(menu_lojas.get(0)).attr("id").split("-")[2]);
			this.show(firsId);
		},
		show: function(e)
		{
			menu_lojas.removeClass("check");
	
			if(isNaN(e))
			{
				var id = new Number($(this).attr("id").split("-")[2]);
				$(this).addClass("check");
			}
			else
			{
				var id = e;
				$("#bt-lojas-" + id).addClass("check");
			}
			
			if(actLoja > 0)
			{
				peddingLoja = id;
				$("#loja-" + actLoja).fadeOut("normal", function(){
					$("#loja-" + peddingLoja).fadeIn("normal");
					actLoja = peddingLoja;
				});
			}
			else
			{
				lojas.hide();
				$("#loja-" + id).show();
				actLoja = id;
			}
			
			try {
				Event.cancel(e);
				return false;
			} catch(ex){};
		},
		next: function(e)
		{
			var id = $(this).attr("id").split("-")[2];
			
			Lojas.move(id, 1);
					
			Event.cancel(e);
			return false;
		},
		prev: function(e)
		{
			var id = $(this).attr("id").split("-")[2];
			
			Lojas.move(id, 2);
					
			Event.cancel(e);
			return false;
		},
		move: function(id, dir)
		{
			var imgs = $("#loja-fotos-" + id);
			var index = new Number(indexs[imgs.attr("id")]);
			var max = imgs.find("li").length;

			if(!dir || dir == 1)
			{
				if(index >= (max-1))
				{
					index = 0;
				}
				else
				{
					index++;
				}
			}
			else
			{
				if(index < 1)
				{
					index = (max-1);
				}
				else
				{
					index--;
				}
			}
			
			imgs.animate({
				marginLeft: ((index * w) * -1) + "px"
			});
			
			indexs[imgs.attr("id")] = index;
		}
	};
	
}();

_c( _d(Lojas, Lojas.init) );