var Paginator = function(options) {
    this.data = options.data;
    this.container = options.container;
    this.pageClickCallback = options.pageClickCallback;
    this.maxVisible = options.maxVisible ? options.maxVisible : 10;


    this.renderizar = function() {
        if (!this.data || !this.container) {
            return;
        }

        var container = $(this.container);

        if (container.length == 0) {
            return;
        }

        container.empty();

        if (this.data.PageCount > 0) {
            container.show();
        } else {
            container.hide();
        }

        if (!container.hasClass('pa')) {
            container.addClass('pa');
        }

        this.pages = Math.ceil(this.data.PageCount / this.maxVisible);
        this.actualPage = Math.ceil(this.data.ActualPage / this.maxVisible);
        this.start = this.actualPage * this.maxVisible - this.maxVisible + 1;
        this.end = this.actualPage * this.maxVisible;

        if (this.end > this.data.PageCount)
            this.end = this.data.PageCount;

        this.showPrevious = this.actualPage > 1;
        this.showNext = this.actualPage < this.pages;


        if (this.showPrevious) {
            var previousElement = $('<a>', { href: '#', page: 'previous' });
            previousElement.html('<');
            previousElement.click($.proxy(this.pageClick, this));
            container.append(previousElement);
        }
        
        for (var i = this.start; i <= this.end; i++) {
            var a = $('<a>', { href: '#', page: i });
            a.html(i);

            if (i == this.data.ActualPage) {
                a.addClass('ativo');
            } else {
                a.removeClass('ativo');
            }

            a.click($.proxy(this.pageClick, this));

            container.append(a);
        }

        if (this.showNext) {
            var nextElement = $('<a>', { href: '#', page: 'next' });
            nextElement.html('>');
            nextElement.click($.proxy(this.pageClick, this));
            container.append(nextElement);
        }
    };

    this.pageClick = function(e) {
        e.preventDefault();
        var el = $(e.target);

        var page = el.attr('page');

        if (page == 'next') {
            page = this.end + 1;
        } else if (page == 'previous') {
            page = this.start - 1;
        }

        if (this.pageClickCallback) {
            this.pageClickCallback({
                page: page
            });
        }

        var container = $(this.container);

        $('a[page]', container).removeClass('ativo');
        $('a[page=' + page + ']', container).addClass('ativo');

    }

    this.renderizar()


};