var pos = 0;
var pos2 = 0;
var cant;

$(document).ready(function(){
	cant = Math.ceil($("#slider").children().length/2);

	bindThumbs();
	
	$("#arriba").click(function(){
		if(pos > 0){
			pos--;
			$("#slider").animate({top:(-pos*62)+"px"}, 500);
		}
		
		return false;
	});
	
	$("#abajo").click(function(){
		if(pos < (cant-3)){
			pos++;
			$("#slider").animate({top:(-pos*62)+"px"}, 500);
		}
		
		return false;
	});
	
	$("#circulos a").click(function(){
		if($(this).attr("class") != "activo"){
			var href = $(this).attr("href");
			document.getElementById("player").cambiarVideo(href);
			
			$("img", this).attr("src", url_base+"/view/images/circulo.png");
			$("#circulos .activo img").attr("src", url_base+"/view/images/circulito.png");
			$("#circulos .activo").removeClass("activo");
			$(this).addClass("activo");
		}
		
		return false;
	});
	
	$(".mp3").jmp3({
		showfilename: "false",
		width: 190
	});
	
	$(".fancybox").fancybox();
	
	$(".categoria a.open").click(function(){
		$(this).siblings(".productos").slideToggle(function(){
			$(this).find(".slider").css("top", "0px");
		});
		
		return false;
	});
	
	$(".categoria .producto").hover(function(){
		$(".normal", this).hide();
		$(".over", this).show();
	}, function(){
		$(".normal", this).show();
		$(".over", this).hide();
	});
	
	$(".categoria .slider").each(function(){
		var hijos = $(this).children().length;
		if(hijos > 5){
			$(this).parent().css("height", "255px");
		}else{
			$(this).parent().css("height", (hijos*51)+"px");
		}
	});
	
	$(".categoria .abajo").click(function(){
		var hijos = $(this).parent().find(".slider").children().length;
		if(hijos > 5 && pos2 < (hijos-5)){
			pos2++;
			$(this).parent().find(".slider").animate({top: (-pos2*51)+"px"}, 300);
		}
		
		return false;
	});
	
	$(".categoria .arriba").click(function(){
		var hijos = $(this).parent().find(".slider").children().length;
		if(hijos > 5 && pos2 >= 1){
			pos2--;
			$(this).parent().find(".slider").animate({top: (-pos2*51)+"px"}, 300);
		}
		
		return false;
	});
	
	$("#cajas a").hover(function(){
		$("img", this).animate({opacity:1},200);
	}, function(){
		$("img", this).animate({opacity:0},200);
	});
	
	$(".columna a.rojo").click(function(){
		$(this).next(".productos").slideToggle();
	});
	
	$(".ver-mas-click").click(function(){
		$(".contenido").css({height: "118px"});
		$(this).parent().parent().find(".contenido").css({height: "100%"});
		$(".ver-mas").show();
		$(this).parent().parent().find(".ver-mas").hide();
		return false;
	});
});

function bindThumbs(){
	$(".thumb").unbind().click(function(){
		var img = new Image();
		var href = $(this).attr("href");
		$("#preloader").show();
		
		$(img).attr("src", href).load(function(){
			$("#full").html(this);
			$("#preloader").hide();
			bindThumbs();
		});
		
		return false;
	});
}
