var TABLON = {};
TABLON.plantillaRespuestas = '/js_comun/plantillasJS/respuestas-anuncio.html';
TABLON.textoResponder = 'Escribe aquí tu respuesta...';
TABLON.textoAnuncio = 'Escribe aquí tu anuncio...';


TABLON.getRespuestas = function() {
	$("div.ta_anuncio").each(function() {
		var attrid = $(this).attr("id");
		if(attrid != '' && attrid.indexOf("TABLON.pregunta_") >= 0 && attrid.indexOf("TABLON.pregunta_mod_") < 0) {
			var id_anuncio = attrid.split('_')[2];
			var id_capa = 'TABLON.respuestas_' + id_anuncio;
		} else if(attrid != '' && attrid.indexOf("TABLON.pregunta_mod_") >= 0) {
			var id_anuncio = attrid.split('_')[2];
			if(document.getElementById("TABLON.respuestas_" + id_anuncio) && document.getElementById("TABLON.respuestas_" + id_anuncio).innerHTML != '') {
				document.getElementById("TABLON.respuestas_mod" + id_anuncio).innerHTML = document.getElementById("TABLON.respuestas_" + id_anuncio).innerHTML;
				return;
			}
			var id_capa = 'TABLON.respuestas_mod' + id_anuncio;
		} else {
			return;	
		}

		var $respuestas = $('<div class="ta_anuncio"></div>');
		// Cargo la plantilla de respuestas de un anuncio
		$.ajax({
			type: 'GET',
			url: TABLON.plantillaRespuestas,
			async: false,
			success: function(data) {
				html = data;
			}
		});
		$.get(DEPORTEBASE.edicion + "/backend/TABLON.respuestas-anuncio.php", 
			{id_anuncio: id_anuncio, r: Math.random()}, 
			function(xmlData) {	
				
				if($(xmlData).find('contenidos').length > 0) {	
					$(xmlData).find('contenidos').find('contenido').each(function() {
						// Modifico la plantilla de respuestas con los datos de esta respuesta
						var t = $.template(html);
						$respuestas.append( t , {
							anuncio: $(this).find('anuncio').text(),
							enviado_por: $(this).find('enviado_por').text(),
							fecha: $(this).find('fecha').text(),
							r: Math.random()
						});
					});
				}
				if($respuestas.html() != '') {
					document.getElementById(id_capa).innerHTML = $respuestas.html();
				}
			}
		);
	});	
}


TABLON.paginacion = function(pag) {
	pag = typeof(pag) != 'undefined' ? pag : '1';
	$.get(DEPORTEBASE.edicion + "/backend/TABLON.paginacion.php", 
		{pag: pag, r: Math.random()}, 
		function(data) {	
			$("#TABLON_paginacion_1").html(data);
			$("#TABLON_paginacion_2").html(data);
		}	
	);
}

TABLON.responder = function(id_anuncio, modulo) {
	
	if(!DEPORTEBASE.estaLogeado()) {
		alert("Tienes que estar logueado para responder a un anuncio");
		document.location.href = DEPORTEBASE.edicion + '/registro/perfil.html';
		return;
	}
	
	modulo = modulo != '' ? 'mod' : '';
	if ($("#TABLON_responder_" + modulo + id_anuncio).is(":hidden")) {
		$("#TABLON_responder_" + modulo + id_anuncio).show();
	}
}

TABLON.ponerAnuncio = function(numcapa) {
	
	if(!DEPORTEBASE.estaLogeado()) {
		alert("Tienes que estar logueado para poner un anuncio");
		document.location.href = DEPORTEBASE.edicion + '/registro/perfil.html';
		return;
	}
	
	$("div.responderform").hide();
	if ($("#TABLON_poner_anuncio_" + numcapa).is(":hidden")) {
		$("#TABLON_poner_anuncio_" + numcapa).show();
	}
}

TABLON.inicializaTxt = function(campo) {
	if(campo.value == TABLON.textoResponder || campo.value == TABLON.textoAnuncio) {
		campo.value = '';
	}
}

TABLON.enviar = function(frm) {
	
	if(!DEPORTEBASE.estaLogeado()) {
		alert("Tienes que estar logueado para poner un anuncio");
		document.location.href = DEPORTEBASE.edicion + '/registro/perfil.html';
		return;
	}
	
	var texto = '';

	if(frm.name.indexOf('frmPonerAnuncio') >= 0) {
		var id_anuncio = '';
		texto = frm.TABLON_respuesta.value;
	} else {
		var id_anuncio = frm.TABLON_id_anuncio.value;
		texto = frm.TABLON_respuesta.value;
	}

	if(texto == '') {
		alert("No has escrito el texto del anuncio");
		return;
	}
	
	$.post(DEPORTEBASE.edicion + "/backend/TABLON.enviar-anuncio.php", 
		{id_anuncio: id_anuncio, texto: texto}, 
		function (data) {
			$("#"+frm.name).html(data);
		}
	);
}


$(document).ready(function() {
	if($(".tablon_anuncios_contenido").length > 0) {
		TABLON.paginacion(jQuery.url.param('pag'));
	}
	if($("div[class=modPortal modulo_tablon_anuncios]").length > 0) {
		TABLON.getRespuestas();
	}
});
