var switchTimeout = 6000;
var stopSwitch = false;
var timer;

timer = setTimeout("changeTab()", switchTimeout); //activa el carrousel


//los eventos no pueden asignarse hasta que no este todo el DOM cargado.
$(function(){
	$("li.active").parent().bind("click",function(){
		window.location.href = $("#himagen > a").attr("href");
	});

	$("#hcab").find("li").each(function(){
		$(this).bind("mouseover",function(){
			clearTimeout(timer);
			if($(this).attr("class") != "active") {
				num = parseInt($(this).attr("id").substr(("hcab").length));
				changeTab(num);
			}
			stopSwitch = true;
		});
		
		$(this).bind("mouseout",function(){
			clearTimeout(timer); //borro el anterior
			timer = setTimeout("changeTab()", switchTimeout);
			stopSwitch = false;								 
		});
	});
	
});

function changeTab(activaId){
	
	if(activaId==undefined){
		var count_li = $("#hcab").find("li").length;
		var num = parseInt($("#hcab").find(".active").attr("id").substr(("hcab").length));
		var activaId = num+1;
	
		if(activaId > count_li) activaId = 1;
	}
	
	$h1 = $("li.active > h1"); //selecciona el elemento activo (h1)
	$ch_h1 = $h1.children().clone(); //contenido del h1
	$p = $("li#hcab"+activaId+" > p:first"); //selecciona el elemento q se tiene q activar (p)
	$ch_p = $p.children().clone(); //contenido de p
	
	$h1.children().replaceWith($ch_p); //cambia contenidos. 
	$p.children().replaceWith($ch_h1);

	//cambia la posicion
	$h1.prependTo($("li#hcab"+activaId));
	$p.prependTo($("li.active"));
	
	$("li.active").removeClass("active");
	$("li#hcab"+activaId).addClass("active");
	
	changeImg("hcab"+activaId);
		
	if(!stopSwitch) timer = setTimeout("changeTab()", switchTimeout);

}

function changeImg(id){
	//$("#himagen").children().replaceWith(carrousel[id]);
	$("#himagen").html(carrousel[id]);
}

function changeImg2(){
	$enlace = $("li.active").find("a");
	$("#himagen > a").attr("title",$enlace.attr("title")).attr("href",$enlace.attr("href"));
	juego = $enlace.attr("href").split("/");
	i = juego.length-2;
	
	pos = juego[i].indexOf("-");
	if(pos > 0) juego[i] = juego[i].substring(0,pos); //poker-texas-holdem
	
	src = $("#himagen > a > img").attr("src");
	
	pos1 = src.indexOf("-");
	pos2 = src.indexOf(".jpg");
	new_src = src.replace(src.substring(pos1+1,pos2),juego[i]); //home-poker.jpg

	$("#himagen > a > img").attr("src",new_src).attr("alt",$enlace.attr("title"));
}