﻿jQuery.fn.exists = function() { return jQuery(this).length > 0; }


jQuery.fn.habilita = function(Mov) {
    this.unbind();
    this.click(function() { Paging(Mov); return false; });
    this.attr("href", "#");
 }

jQuery.fn.desabilita = function() {
    this.unbind();
    this.click(function() { return false; });
    this.removeAttr("href");
}

var CurrentPage = -1;

function Paging(Mov) {
    var PageSize = 2;
    var as = $('.thumb');
    var PageCount = Math.ceil(as.length / PageSize);
    CurrentPage += Mov;
    $("#currentpage").val(CurrentPage);

    var primeiro = (CurrentPage * PageSize);
    var ultimo = primeiro + PageSize;
    $("td.thumb").hide();
    $("td.thumb").slice(primeiro, ultimo).fadeIn("slow");

    if (PageCount == 1) {
        $("#bt-anterior").hide();
        $("#bt-proximo").hide();
    }
    else {

        if (CurrentPage == 0) {
            $("#bt-anterior a").desabilita();
        }
        else {
            $("#bt-anterior a").habilita(-1);
        }

        if (CurrentPage == PageCount - 1) {
            $("#bt-proximo a").desabilita();
        }
        else {
            $("#bt-proximo a").habilita(1);
        }
         
        
        /*************   CICLICO
        
        var ante = -1;
        var prox = 1;
        if (CurrentPage == 0)
            ante = PageCount-1;
        else if (CurrentPage == PageCount - 1)
            prox = (-1*PageCount)+1;

        $("#bt-anterior a").unbind();
        $("#bt-proximo a").unbind();
        
        $("#bt-anterior a").click(function() { Paging(ante); return false; });
        $("#bt-proximo a").click(function() { Paging(prox); return false; });
        
        **********/
    }
    /*
    if (CurrentPage > 0)
        $("#bt-anterior").show();
    else
        $("#bt-anterior").hide();

    if (CurrentPage < PageCount - 1)
        $("#bt-proximo").show();
    else
        $("#bt-proximo").hide();
        */
}



$(document).ready(function() {
    if ($("#tb-galeria").exists()) {
        Paging(1);    
    }
});


