﻿jQuery.fn.exists = function() { return jQuery(this).length > 0; }

jQuery.fn.habilita = function(Mov) {
    this.unbind();
    this.click(function() { $.move_galeria(Mov); return false; });
    this.attr("href", "#");
 }

jQuery.fn.desabilita = function() {
    this.unbind();
    this.click(function() { return false; });
    this.removeAttr("href");
}

jQuery.move_galeria = function(Mov) {
    var PageSize = 4;
    var as = $('.thumb');
    var PageCount = Math.ceil(as.length / PageSize);
    CurrentPage += Mov;

    //var primeiro = (CurrentPage * PageSize);
    var primeiro = CurrentPage;
    var ultimo = primeiro + PageSize;

    $(".thumb").hide();
    $(".thumb").slice(primeiro, ultimo).fadeIn("fast");
        
    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) {
        if (ultimo == as.length) {
            $("#bt-proximo a").desabilita();
        }
        else {
            $("#bt-proximo a").habilita(1);
        }
    }
}

jQuery.fn.galeria = function() {
    return this.each(function() {
        $.move_galeria(1);
    });
};

var CurrentPage = -1;
