﻿var tipoS = 1;
$(function () {
    var inicio = 1;
    
    $('#home_livro').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_livro'), // who gonna scroll? in this example, the full window
        'heightOffset': 200, // how many pixels before reaching end of the page would loading start? positives numbers only please
        'pageSize': 20,
        'tipo': 0,
        'idDiv': '#contentLivro',
        'idDivRolagem': '#home_livro',
        '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 paginationLivro() {
    inicio = 1;
    $('#home_livro').scrollPagination({
        'tipo': 0,
        'idDiv': '#contentLivro',
        'idDivRolagem': '#home_livro',
        'pagina': 'ajax/scrollLivro.aspx'
    });

    $('#loading').fadeIn();
    $.post('ajax/scrollLivro.aspx?inicio=' + inicio + '&registros=19&tipo=0', {}, function (data) {
        $('#contentLivro').slideDown('slow', function () {
            $("#contentLivro").append(data);

            inicio = inicio + 20;
            $('#loading').fadeOut();
        });

    });
}

function gravaLivro() {
        var tudo = "";
        var mural = $('#contentLivro').html();
        var txtNomeL = document.getElementById("txtNomeL").value;
        var txtTelefoneL = document.getElementById("txtTelefoneL").value;
        var txtCidadeL = document.getElementById("txtCidadeL").value;
        var txtEstadoL = document.getElementById("txtEstadoL").value;
        var txtMensagemL = document.getElementById("txtMensagemL").value;

        var botao = document.getElementById("btnLivro");

        cache: false;

        $(botao).attr("disabled", true);
        $(botao).val("Aguarde...")

        $.post('ajax/gravaLivro.aspx', { txtNomeL: txtNomeL, txtTelefoneL: txtTelefoneL, txtCidadeL: txtCidadeL, txtEstadoL: txtEstadoL, txtMensagemL: txtMensagemL
        }, function (data) {
            if (data != "0") { // verifico se deu certo
                var minhaData = new Date();

                $(botao).attr("disabled", false);
                $(botao).val("Enviar");

                var ul = document.getElementById("contentLivro");

                tudo = data + mural;

                $(ul).html(tudo)
                document.getElementById("txtNomeL").value = "";
                document.getElementById("txtTelefoneL").value = "";
                document.getElementById("txtCidadeL").value = "";
                document.getElementById("txtEstadoL").value = "";
                document.getElementById("txtMensagemL").value = "";


                alert("Livro enviado com sucesso!")

            }
            else {
                alert('Sistema temporariamente indisponível.')
                $(botao).attr("disabled", false);
                $(botao).val("Enviar")
            }

        });

    }

    //////////////////////////////////
    function denunciaLivro(idLivro) {
        var ulFora = document.getElementById("livro_" + idLivro);

        $(ulFora).empty();
        
    }
