/* BT */

p_array = new Array();

$(document).ready(function(){
	var url = document.location.href;
	var urlp = url.split("/");
	var url = urlp[2] + "/" + urlp[3];
	$("body").append('<div id="loginform" class="caja-azul" style="display:none;"><form action="/accion.php" method="POST"><input name="a" value="login" type="hidden" /><input name="url" value="' + url + '" type="hidden" /><table border="0"><tr><td align="right" style="text-align:right;">Nick: <input type="texo" name="nick" value="" size="12" id="login_nick" maxlength="16" /><br />Contrase&ntilde;a: <input type="password" name="pass" value="" size="12" maxlength="60" /></td><td valign="center"><input value="Entrar" type="submit" style="height:48px;" /></td></tr></table></form></div>');
	$("#loginformgo").click(function(){
		$("#loginform").slideToggle("fast", function () { $("#login_nick").focus(); });
		return false;
	});
	$("#registrar-b").click(function(){ lanzar_cu(); return false; });
});

function lanzar_cu() {
	$("#ad_bloque").remove();
	$("#cu-form").remove();
	$.post("/accion.php", { a: "registrar" }, function(data){ $("body").prepend(data); $("#cu-form").fadeIn("fast", function () { $("#cu-nick").focus(); }); });
}

function cmf(mode) {
if (mode == "top") { location.href=location.href + "#pi"; } 
$("#cmf").slideToggle("slow", function () { $("#cmf_texto").focus(); });
if (!$("#ac_img").attr("src")) {
	if (user_ID == 0) {
		var reg = confirm("\u00BFPrefieres registrarte antes de comentar?\n\nVentajas:\n1. Tendras un voto de confianza extra +1.\n2. Nadie podra quitarte el nick.\n3. Podras votar a los demas.\n4. Menos publicidad.\n\n");
		if (reg) { lanzar_cu(); $("#cmf").fadeOut(); }
		$("#cmf_b").before('<table border="0"><tr><td><img src="/ac/animal-captcha.php" id="ac_img" onclick="document.getElementById(\'ac_img\').src=\'/ac/animal-captcha.php?\'+Math.random();" style="float:left;" /></td><td valign="top">&iquest;Que animal es? (<a href="http://gonzo.teoriza.com/animal-captcha">Animal Captcha</a>)<br /><input tabindex="4" type="text" id="cmf_captcha" value="" /><br /><em class="gris">Haz clic en la foto para ver otro animal.</em></td></tr></table>');
	}
}
return false;
}


function paginas(accion) {
if (!p_array[p_now]) { p_array[p_now] = $("#cmol").html(); }
if ((accion == "menos") && (p_now > 1)) { p_now = p_now - 1; } else if ((accion == "mas") && (p_now < p_num)) { p_now = p_now + 1; } else { return; }
$(".p_num").html(p_now);
$("#cmol").css("min-height", "700px").empty();
if (p_array[p_now]) {
	botones_pag();
	$("#cmol").html(p_array[p_now]);
} else {
	var p_init = c_num - ((p_num - p_now) * cp_num);
	var p_limit = cp_num*(p_num - p_now);
	botones_pag("off");
	$.post("/accion.php", { a: "pag", post_ID: post_ID, p_limit: p_limit, p_init: p_init }, function(data){ $("#cmol").html(data); botones_pag(); } );
}
$("#cmol").html(p_array[p_now]);
if (!p_array[p_now]) { pageTracker._trackPageview("/com-pag-" + p_now); }
return false;
}


function botones_pag(mode) {
$("input:first").focus();
if ((p_now <= 1) || (mode == "off")) { $(".c_menos").attr("disabled", "disabled"); } else { $(".c_menos").removeAttr("disabled"); }
if ((p_now >= p_num) || (mode == "off")) { $(".c_mas").attr("disabled", "disabled"); } else { $(".c_mas").removeAttr("disabled"); }
}


function enviar_comentario() {
$("input:first").focus();
var nick = $("#cmf_nick").attr("value");
var email = $("#cmf_email").attr("value");
var texto = $("textarea#cmf_texto").val();
var captcha = $("#cmf_captcha").attr("value");

if ((!nick) && (user_ID == 0)) { alert("Escribe tu nick."); } 
else if ((!email) && (user_ID == 0)) { alert("Escribe tu email."); } 
else if (!texto) { alert("Escribe el texto."); } 
else if ((!captcha) && (user_ID == 0)) { alert("Escribe el nombre del animal que aparece en la imagen."); } 
else { 
	$("#cmf_b").attr("disabled", "disabled").attr("value", "Enviando comentario...");
	$.post("/accion.php", { a: "com", nick: nick, email: email, texto: texto, captcha: captcha, post_ID: post_ID }, function(data){
		if (data == "ok") { $("#cmf_b").attr("value", "Enviado correctamente!").after(' <a href="?ok#coment"><b>Ver comentario</b></a>'); pageTracker._trackPageview("/com-enviado"); } else { $("#cmf_b").attr("value", "Error, enviar de nuevo").removeAttr("disabled"); }
	});
}
return false;
}


$("#cmol li").hover(function () {
	var item_ID = $(this).attr("id");
	$(this).css("background", "#DDFFDD");
	$(this).find(".tm").css("display", "inline");
	if (acceso == 'admin') { var acceso_extra = '<span class="img-remove" onclick="votar(3, ' + item_ID + ', 1);"></span> '; } else { var acceso_extra = ''; }
	$(this).find(".voto").before('<span class="voto_c">' + acceso_extra + '<span class="img-add" onclick="votar(2, ' + item_ID + ', 1);"></span><span class="img-del" onclick="votar(2, ' + item_ID + ', -1);"></span></span> ');
}, function () {
	$(this).css("background", "#FFFFFF");
	$(this).find(".voto_c").remove();
	$(this).find(".tm").css("display", "none");
});

function votar(typenum, item_ID, voto) {
if (user_ID == 0) {
	lanzar_cu();
} else {
	if (typenum == 3) {
		$.post("/accion.php", { a: "desaprobcom", com_ID: item_ID }, function(data){ 
			if (data == "ok") {
				$("#cmol #" + item_ID + " p").css("color", "#DDDDDD");
			}
		});
	} else {
		if (typenum == 2) { 
			var type = "com";
			$("#cmol #" + item_ID + " .voto").fadeOut("slow");
		} else if (typenum == 1) { 
			var type = "post"; 
			$("#p" + item_ID).fadeOut("slow");
		}
		$.post("/accion.php", { a: "voto", type: type, item_ID: item_ID, voto: voto }, function(data){ 
			if (data == "ok") {
				if (typenum == 2) { 
					var voto_old = $("#cmol #" + item_ID + " .voto").text();
					var voto_new = eval(voto_old) + eval(voto);
					if (voto_new >= 0) { var voto_new = "+" + voto_new; }
					$("#cmol #" + item_ID + " .voto").text(voto_new).fadeIn("slow");
					pageTracker._trackPageview("/voto-com");
				} else if (typenum == 1) { 
					var voto_old = $("#p" + item_ID).text();
					var voto_new = eval(voto_old) + eval(voto);
					if (voto_new >= 0) { var voto_new = "+" + voto_new; }
					$("#p" + item_ID).text(voto_new).fadeIn("slow");
					pageTracker._trackPageview("/voto-post");
				}
			} else if (data == "rep") {
				if (typenum == 2) { $("#cmol #" + item_ID + " .voto").fadeIn("slow"); } 
				else if (typenum == 1) { $("#p" + item_ID).fadeIn("slow"); }
				alert("Ya votaste correctamente :)");
			}
		});
	}
}
}