$(document).ready(function(){
	var pos = 1;
	$("#galeria").css("width","637px").css("position","relative").css("height","337px").css("overflow","hidden").css("margin ","auto").css("top","6px");
	$("<div />").attr("id","contenedor").css("position","relative").appendTo("#galeria");
	$("#galeria").find(".accionar").each(function(){
		$(this).css("top",(($("#galeria").height()/2)-($(this).height()/2))+"px");
	});
	$.getJSON('js/fotos.php', function(data) {
		$.each(data.fotos, function(index, value){
			$("<div />").addClass("foto").css("background","url('imagenes/galeria/imagenes/" + value.imagen + "') center center no-repeat").css("width",$("#galeria").width()+"px").css("height",$("#galeria").height()+"px").css("cursor","pointer").css("float","left").bind('click',function(){ window.location.href = value.url }).appendTo("#contenedor");
		});
		$("<div />").css("clear","left").appendTo("#contenedor");
		$("#contenedor").css("width",($("#contenedor").find("div").size()*$("#contenedor").find("div:first").width())+"px");
	});
	var tempo = $.timer(7000, function(timer){
		moverDerecha();
	});

	function moverDerecha()
	{
		if(pos < $("#contenedor").find("div").size()-1)
		{
			pos++;
			$("#contenedor").animate({left:'-='+$("#contenedor").find("div:first").width()},1000);
		}
		else
		{
			var position = $("#contenedor .foto:first").position();
			$("#contenedor").animate({"left":position.left},1000);
			pos = 1;
		}
	}
	function moverIzquierda()
	{
		if(pos > 1)
		{
			pos--;
			$("#contenedor").animate({left:'+='+$("#contenedor").find("div:first").width()},1000);
		}
		else
		{
			var position = $("#contenedor .foto:last").position();
			$("#contenedor").animate({"left":-(position.left)},1000);
			pos = $("#contenedor").find("div").size()-1;
		}
	}
	
	$(".mover_izq").click(function(){
		tempo.stop();
		moverIzquierda();
	});
	$(".mover_der").click(function(){
		tempo.stop();
		 moverDerecha();
	});
	
});
