﻿
jQuery.fn.jPaginacao = function(elems, qtdPorPagina) {

    jQuery(this).append("<input type='hidden' id='totalRegistros' /><input type='hidden' id='totalRegAtual' /><input type='hidden' id='totalRegAtual' />");
    var qtd = 0;
    jQuery("#anterior").hide();
    jQuery(elems).hide();
    jQuery(elems).each(function() {
        if (qtd < qtdPorPagina) {
            qtd++;
            jQuery(this).show();
        }
    })

    if (qtd < qtdPorPagina) {
        $("#proxima").hide();
        $("#anterior").hide();
    }

    jQuery("#anterior").click(function() {
        jQuery("#proxima").show();
        jQuery("#anterior").show();
        var valoresAtuais = jQuery('#totalRegAtual').val();

        if (valoresAtuais == "") {
            valoresAtuais = qtdPorPagina;
        } else {
            valoresAtuais = parseInt(valoresAtuais) - qtdPorPagina;
        }

        if (valoresAtuais < parseInt($(elems).size())) {
            jQuery(elems).hide().filter(":gt(" + parseInt(valoresAtuais - 1) + ")").filter(":lt(" + (qtdPorPagina) + ")").show();
            document.getElementById('totalRegAtual').value = valoresAtuais;
        }
        if (valoresAtuais == 0) {
            jQuery("#anterior").hide();
        }
        return false;
    });

    jQuery("#proxima").click(function() {
        jQuery("#proxima").show();
        jQuery("#anterior").show();
        var valoresAtuais = jQuery('#totalRegAtual').val();

        if (valoresAtuais == "") {
            valoresAtuais = qtdPorPagina;
        } else {
            valoresAtuais = parseInt(valoresAtuais) + qtdPorPagina;
        }

        if (valoresAtuais < parseInt($(elems).size())) {
            jQuery(elems).hide().filter(":gt(" + parseInt(valoresAtuais - 1) + ")").filter(":lt(" + (qtdPorPagina) + ")").show();
            jQuery('#totalRegAtual').val(valoresAtuais);
        }
        if (valoresAtuais + qtdPorPagina >= $(elems).size()) {
            jQuery("#proxima").hide();
        }
        return false;
    });
}
