// recuperamos el texto de un comentario por su id
function ampliarComentario(vinculo, tipo){	
	var inicio = vinculo.href.indexOf("?",0)+1;
	var fin = vinculo.href.length;
	var tipoContrario;
	eval(vinculo.href.substring(inicio,fin));
	//alert("men ID: "+comment+"\n\ntipo: "+tipo)
	if (comment != null){
		$.get("php/controlador.php", {"accion":"cargarComentario","comment":comment,"tipo":tipo}, 
			function(respuesta){
				var vinculoHtml = vinculo.parentNode;
				if(respuesta.resultado){
					switch (tipo){
						case "reducir":
							$(vinculoHtml).html(respuesta.texto+"... <a class=\"ampliarComment\" href=\"?comment="+comment+"\">leer m&aacute;s <span>>></span></a>");
							tipoContrario = "ampliar";
						break;
						case "ampliar":
							$(vinculoHtml).html(respuesta.texto+"  <a class=\"reducirComment\" href=\"?comment="+comment+"\">cerrar <span>>></span></a>");
							tipoContrario = "reducir";
						break;
					}
					$(vinculoHtml.lastChild).click(function(){
						ampliarComentario(this, tipoContrario);
						return false;
					});
				}else{
					$(vinculoHtml).html("Error al recuperar el comentario. <br />".respuesta.errorMsg);
				}
			}, "json");
	}
	return false;
}

function controladorSubmitNuevoComentario(){
	$("#formPostear").submit(function(){
		return postearComentario();
	});
}

function postearComentario(){
	googleAnalitycs("/html/postCommentForm_end");
	var comentario = $("#comentario").val();
	if (comentario == ""){
		mensaje = {objeto: {dom: $("#comentario"), iluminar: true}, texto: AFO_postCommentVacio.texto, tiempo: AFO_postCommentVacio.tiempo};
		aviso(mensaje);
	}else if(comentario.length > MAX_LENGTH_COMENTARIOS){
		mensaje = {objeto: {dom: $("#comentario"), iluminar: true}, texto: AFO_comentarioDemasiadoLargo.texto, tiempo: AFO_comentarioDemasiadoLargo.tiempo};
		aviso(mensaje);
	}else{
		mostrarCargador($("#postear"), "posts");
		$.get("php/controlador.php", {"accion":"guardarComentario", "comentario":comentario}, 
			function(respuesta){
				if (respuesta.resultado == true) {				
					cargaComentarios(1);
				}else if (respuesta.resultado=="resultLong"){
					mensaje = {objeto: {dom: $("#comentario"), iluminar: true}, texto: AFO_comentarioDemasiadoLargo.texto, tiempo: AFO_comentarioDemasiadoLargo.tiempo};
					aviso(mensaje);
				}else{
					mensaje = {objeto: {dom: $("#comentario"), iluminar: true}, texto: GEN_errorNoControlado+"<!--"+respuesta.errorMsg+"-->", tiempo: GEN_errorNoControlado.tiempo};
					aviso(mensaje);
				}
				getFormPostComment();
			}, "json");
	}
	return false;
}

function cargaComentarios(pagina){
	var dimensiones = getScrollXY();
	if (dimensiones.y>340){
		$.scrollTo("#titular");
	}
	var nombreIdCarga = "cargandoComentarios";
	$(".pagination").remove();
	$(".postear").remove();
	$("#postear").remove();
	$("#posts").slideToggle(500, function(){
		$("#posts").remove();
		$("#tema_foro").after("<div id=\""+nombreIdCarga+"\"></div>");
		mostrarCargador($("#"+nombreIdCarga+""), "posts");
		$.get("php/controlador.php", {"accion":"getComentarios", "pagina":pagina}, 
			function(respuesta){
				$("#"+nombreIdCarga).remove();
				$("#tema_foro").after(respuesta);
				controladorSubmitNuevoComentario();
			}, "html");
	});
	return false;
}

function getFormPostComment(){
	$.get("php/controlador.php", {"accion":"getFormPostComment"}, function(respuesta){
		$("#postear").html(respuesta);
		controladorSubmitNuevoComentario();
	}, "html");
}
