﻿var tipoS = 1;
$(function () {
    var inicio = 1;

    $('#home_mural').scrollPagination({

        //'contentPage': '../ajax/scroll.aspx?inicio=' + inicio + '&registros=9', // the page where you are searching for results
        'contentData': {}, // you can pass the children().size() to know where is the pagination
        'scrollTarget': $('#home_mural'), // who gonna scroll? in this example, the full window
        'heightOffset': 10, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'pageSize': 20,
        'tipo': tipoS,
        'beforeLoad': function () { // before load, some function, maybe display a preloader div
            $('#loading').fadeIn();


        },
        'afterLoad': function (elementsLoaded) { // after loading, some function to animate results and hide a preloader div
            $('#loading').fadeOut();


            $(elementsLoaded).fadeInWithDelay();
            //                if ($('#content').children().size() > 100) { // if more than 100 results loaded stop pagination (only for test)
            //                    $('#nomoreresults').fadeIn();
            //                    $('#content').stopScrollPagination();
            //                }
        }
    });

    // code for fade in element by element with delay
    $.fn.fadeInWithDelay = function () {
        var delay = 0;
        return this.each(function () {
            $(this).delay(delay).animate({ opacity: 1 }, 200);
            delay += 100;
        });
    };

});
var inicio = 1;
var dados = ""
function pagination(tipo) {
    inicio = 1;
    $('#home_mural').scrollPagination({
        'tipo': tipo,
        'idDiv': '#contentMural',
        'idDivRolagem': '#home_mural',
        
        'pagina': 'ajax/scrollMural.aspx'
    });

    $('#loading').fadeIn();
    $.post('ajax/scrollMural.aspx?inicio=' + inicio + '&registros=19&tipo='+tipo, {}, function (data) {
        $('#contentMural').slideDown('slow', function () {
            $("#contentMural").append(data);

            inicio = inicio + 20;
            $('#loading').fadeOut();
        });

    });
}
function paginationMesa(tipo) {
    inicio = 1;
    tipoS = tipo

    $('#home_mural').scrollPagination({
        'tipo': tipoS,
        'idDiv': '#contentMural',
        'idDivRolagem': '#home_mural',
        
        'pagina': 'ajax/scrollMural.aspx'
    });

    $('#loading').fadeIn();
    $.post('ajax/scrollMural.aspx?inicio=' + inicio + '&registros=19&tipo=' + tipoS, {}, function (data) {
        $('#contentMural').slideDown('slow', function () {

            $("#contentMural").append(data);

            inicio = inicio + 20;
            $('#loading').fadeOut();
        });
    });
}
function comentar(valor) {

    var resposta = document.getElementById("resp_" + valor);

    if (resposta == null) {
        var ul = document.getElementById("frmComentar_" + valor);
        $(ul).append("<li id='resp_" + valor + "'><input id='txtResp_" + valor + "' type='text' /> <input id='btn_" + valor + "' type='button' value='Enviar' onclick='grava2(" + valor + ");' /></li>");
    }
}
function grava(valor) {

    var campo = document.getElementById("txtResp_" + valor);
    var botao = document.getElementById("btn_" + valor);
    if (campo.value != "") {
        cache: false;

        $(botao).attr("disabled", true);
        $(botao).val("Aguarde...")
        var texto = "";
        texto = campo.value;
        id = valor;
        $.post('ajax/gravaMural.aspx?tipo=1', { texto: texto, id: id }, function (data) {
            if (data != "0") { // verifico se deu certo

                $(botao).attr("disabled", false);
                $(botao).val("Enviar");
                var ulFora = document.getElementById("frmPost_" + valor);
                var ulInt = "#muralInt_" + valor + " li";
                var ulIntLast = "#muralInt_" + valor + " li:last";
                $(ulIntLast).remove();
                $(ulFora).slideDown("slow");
                $(ulFora).append(data)
                //$(ulFora).append("<li id='resp_" + valor + "'><input id='txtResp_" + valor + "' type='text' /> <input id='btn_" + valor + "' type='button' value='Enviar' onclick='grava(" + valor + ");' /></li>");

            }
            else {
                alert('Sistema temporariamente indisponível.')
                $(botao).attr("disabled", false);
                $(botao).val("Enviar")
            }

        });
    }
}
function grava2(valor) {

    var campo = document.getElementById("txtResp_" + valor);
    var botao = document.getElementById("btn_" + valor);
    if (campo.value != "") {
        cache: false;

        $(botao).attr("disabled", true);
        $(botao).val("Aguarde...")
        var texto = "";
        texto = campo.value;
        id = valor;
        $.post('ajax/gravaMural.aspx?tipo=2', { texto: texto, id: id }, function (data) {

            if (data != "0") { // verifico se deu certo

                $(botao).attr("disabled", false);
                $(botao).val("Enviar");
                var ulFora = document.getElementById("mural_" + valor);
                var ulInt = "#mural_" + valor + " li";
                var ulIntLast = "#mural_" + valor + " li:last";
                $(ulIntLast).remove();
                $(ulFora).slideDown("slow");
                $(ulFora).append(data)
                //$(ulFora).append("<li id='resp_" + valor + "'><input id='txtResp_" + valor + "' type='text' /> <input id='btn_" + valor + "' type='button' value='Enviar' onclick='grava(" + valor + ");' /></li>");

            }
            else {
                alert('Sistema temporariamente indisponível.')
                $(botao).attr("disabled", false);
                $(botao).val("Enviar")
            }

        });
    }
}

function coments(valor) {
    cache: false;
    var id = "";
    id = valor;
    $.post('ajax/buscaComentariosMural.aspx', { id: id }, function (data) {
        if (data != "0") { // verifico se deu certo

            var ulFora = document.getElementById("muralInt_" + valor);
            $(ulFora).html(data)

        }
        else {
            alert('Sistema temporariamente indisponível.')
            $(botao).attr("disabled", false);
            $(botao).val("Enviar")
        }

    });
}

function gravaMural() {
    var tudo = "";
    var mural = $('#contentMural').html();
    var mensagem = document.getElementById("txtDscMural").value;

    if (mensagem == "")
    return
    var botao = document.getElementById("btnGravaMural1");
    var txtDscMural = document.getElementById("txtDscMural");
    $(botao).attr("disabled", true);
    $(botao).val("Aguarde...")
    cache: false;

    mensagem = mensagem;
    tipoMural = tipoS;
    
    $.post('ajax/gravaItemMural.aspx', { mensagem: mensagem, tipoMural: tipoMural }, function (data) {
        if (data != "0") { // verifico se deu certo

            mensagem = data;
            tudo = mensagem + mural;
            $('#contentMural').html(tudo);
            $(botao).attr("disabled", false);
            $(botao).val("Enviar");
            txtDscMural.value = "";
        }
        else {
            alert('Sistema temporariamente indisponível.')
            $(botao).attr("disabled", false);
            $(botao).val("Enviar")
            $(botao).attr("disabled", false);
            $(botao).val("Enviar")
            txtDscMural.value = "";
        }

    });


}

