﻿(function($) {
tableMore = function(selector, options) {

    var defaults = {
        limit: 15,
        pagelimit: 100000,
        started: 1,
        insertpager: true,
        dragpager: false,
        fixedwidth: false,
		myID: 'tab1'
    };
    this.options = $.extend(defaults, options);
    this.element = $($(selector).get(0));
    this.initialize();
}

tableMore.version = '1.0';
tableMore.updated = '2011-09-20 15:50';

tableMore.prototype.initialize = function() {
    var self = this;
    this.rows = $('tbody > tr', this.element);
    this.length = this.rows.length;
    this.pages = Math.ceil(this.length / this.options.limit);
    this.pagers = [];

    var divide = function(elements, limit) {
        var divided = [];
        elements.each(function(i) {
            if (i % limit == 0) divided.push([]);
            divided[divided.length - 1].push(this);
        });
        return divided;
    }
    this.rows.divided = divide(this.rows, this.options.limit);

    if (this.options.fixedwidth) $('th', this.element).each(function() {
        $(this).width($(this).width() + 1);
    });
    if (this.options.pagelimit < 0) this.options.pagelimit = this.pages;

    return this.goto(this.options.started);
}

tableMore.prototype.goto = function(page) {
    
    var self = this;
    var options = this.options;

    var page = page < 1 ? 1 : page > this.pages ? this.pages : page;
    var range = this._range(), newrange = this._range(page);
    var from = (page - 1) * this.options.limit, to = page * this.options.limit;

    (this._visiblerows || this.rows).hide();
    this._visiblerows = $(this.rows.divided[page - 1]).show();

    $(this.pagers).each(function() {
        var selected = options.selectedclass;
        var children = $(this).children();
        var length = self.length;

        if (length) {
            children.slice(1, self.pages + 1).hide();
            children.slice(range[0], range[1] + 1).removeClass(selected);
            children.slice(newrange[0], newrange[1] + 1).show();
            children.eq(page).addClass(selected);
        }

        var prev = children.eq(0);
        var next = children.eq(self.pages + 1);

        if (!length || newrange[0] == 1) prev.hide();
        else prev.show();
        if (!length || self.pages <= newrange[1]) next.hide();
        else next.show();
    });
    this.page = page;
    return this;
}

tableMore.prototype.revert = function() {
    var self = this;
    $(this.pagers).empty();
    $(this.rows).show();
    $.each(['pagers', 'page', 'rows', '_visiblerows'], function() {
        delete self[this];
    });
    return this;
}

tableMore.prototype._range = function(page) {
    page = page || this.page;
    var pagelimit = this.options.pagelimit;
    return [Math.ceil(page / pagelimit - 1) * pagelimit + 1,
            Math.ceil(page / pagelimit) * pagelimit];
}

$.fn.extend({
    tablepart: function(options) {
        return new tableMore(this, options);
    }
});
})(jQuery);


