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 b = $(this).attr("id");
        if (b != '' && b.indexOf("TABLON.pregunta_") >= 0 && b.indexOf("TABLON.pregunta_mod_") < 0) {
            var c = b.split('_')[2];
            var d = 'TABLON.respuestas_' + c
        } else if (b != '' && b.indexOf("TABLON.pregunta_mod_") >= 0) {
            var c = b.split('_')[2];
            if (document.getElementById("TABLON.respuestas_" + c) && document.getElementById("TABLON.respuestas_" + c).innerHTML != '') {
                document.getElementById("TABLON.respuestas_mod" + c).innerHTML = document.getElementById("TABLON.respuestas_" + c).innerHTML;
                return
            }
            var d = 'TABLON.respuestas_mod' + c
        } else {
            return
        }
        var e = $('<div class="ta_anuncio"></div>');
        $.ajax({
            type: 'GET',
            url: TABLON.plantillaRespuestas,
            async: false,
            success: function (a) {
                html = a
            }
        });
        $.get(BARRIOSYVECINOS.carpetadominio + "/backend/TABLON.respuestas-anuncio.php", {
            id_anuncio: c,
            r: Math.random()
        }, function (a) {
            if ($(a).find('contenidos').length > 0) {
                $(a).find('contenidos').find('contenido').each(function () {
                    var t = $.template(html);
                    e.append(t, {
                        anuncio: $(this).find('anuncio').text(),
                        enviado_por: $(this).find('enviado_por').text(),
                        fecha: $(this).find('fecha').text(),
                        r: Math.random()
                    })
                })
            }
            if (e.html() != '') {
                document.getElementById(d).innerHTML = e.html()
            }
        })
    })
};
TABLON.paginacion = function (b) {
    b = typeof(b) != 'undefined' ? b : '1';
    $.get(BARRIOSYVECINOS.carpetadominio + "/backend/TABLON.paginacion.php", {
        pag: b,
        r: Math.random()
    }, function (a) {
        $("#TABLON_paginacion_1").html(a);
        $("#TABLON_paginacion_2").html(a)
    })
};
TABLON.responder = function (a, b) {
    if (!BARRIOSYVECINOS.estaLogeado()) {
        alert("Tienes que estar logueado para responder a un anuncio");
        return
    }
    b = b != '' ? 'mod' : '';
    if ($("#TABLON_responder_" + b + a).is(":hidden")) {
        $("#TABLON_responder_" + b + a).show()
    }
};
TABLON.ponerAnuncio = function (a) {
    if (!BARRIOSYVECINOS.estaLogeado()) {
        alert("Tienes que estar logueado para poner un anuncio");
        return
    }
    $("div.responderform").hide();
    if ($("#TABLON_poner_anuncio_" + a).is(":hidden")) {
        $("#TABLON_poner_anuncio_" + a).show()
    }
};
TABLON.inicializaTxt = function (a) {
    if (a.value == TABLON.textoResponder || a.value == TABLON.textoAnuncio) {
        a.value = ''
    }
};
TABLON.enviar = function (b) {
    if (!BARRIOSYVECINOS.estaLogeado()) {
        return
    }
    if (b.name.indexOf('frmPonerAnuncio') >= 0) {
        var c = '';
        var d = b.TABLON_respuesta.value
    } else {
        var c = b.TABLON_id_anuncio.value;
        var d = b.TABLON_respuesta.value
    }
    $.post(BARRIOSYVECINOS.carpetadominio + "/backend/TABLON.enviar-anuncio.php", {
        id_anuncio: c,
        texto: d
    }, function (a) {
        $("#" + b.name).html(a)
    })
};
$(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()
    }
});
