// ---- content panel paging toolbar helper class ---- //
Ext.override(Ext.PagingToolbar, {

    // standard initializer
    initComponent: function() {
        Ext.PagingToolbar.superclass.initComponent.apply(this, arguments);

        //this.m_linkItems = new Array();
    },


    // private
    onRender: function(ct, position) {
        Ext.PagingToolbar.superclass.onRender.call(this, ct, position);

        this.first = this.addButton({
            tooltip: this.firstText,
            iconCls: "x-tbar-page-first",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["first"])
        });
        this.prev = this.addButton({
            tooltip: this.prevText,
            iconCls: "x-tbar-page-prev",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["prev"])
        });
        this.next = this.addButton({
            tooltip: this.nextText,
            iconCls: "x-tbar-page-next",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["next"])
        });
        this.last = this.addButton({
            tooltip: this.lastText,
            iconCls: "x-tbar-page-last",
            disabled: true,
            handler: this.onClick.createDelegate(this, ["last"])
        });

        if (this.displayInfo) {
            this.displayEl = Ext.fly(this.el.dom).createChild({ cls: 'x-paging-info' });
        }
        if (this.dsLoaded) {
            this.onLoad.apply(this, this.dsLoaded);
        }
    },

    // private
    updateInfo: function() {
        if (this.displayEl) {
            var count = this.store.getCount();
            var msg = String.format(
                this.displayMsg,
                this.cursor + 1,
                this.cursor + count,
                this.store.getTotalCount());
            
            this.displayEl.update(msg);
        }
    },

    // private
    onLoad: function(store, r, o) {
        if (!this.rendered) {
            this.dsLoaded = [store, r, o];
            return;
        }
        this.cursor = o.params ? o.params.start : 0;
        var d = this.getPageData(), ap = d.activePage, ps = d.pages;

        this.first.setDisabled(ap == 1);
        this.prev.setDisabled(ap == 1);
        this.next.setDisabled(ap == ps);
        this.last.setDisabled(ap == ps);

        //this.deletePageLinkItems();
        //this.insertPageLinkItems(store);

        this.updateInfo();
        this.fireEvent('change', this, d);
    },

    /*
    // private
    deletePageLinkItems: function() {
    for (var i = 0, len = this.m_linkItems.length; i < len; i++) {
    this.m_linkItems[i].destroy();
    }
    },

    // private
    insertPageLinkItems: function(store) {

        var linkItemIndex = 2;
    var totalItems = store.getTotalCount();
    var activePage = Math.ceil((this.cursor + this.pageSize) / this.pageSize);
    var totalPages = totalItems < this.pageSize ? 1 : Math.ceil(totalItems / this.pageSize);

        var iStartPage = Math.max(1, activePage - 3);
    var iEndPage = Math.min(activePage + 3, totalPages);

        if (iStartPage >= 2) {
    // add '...' - clickable
    var linkItem = this.insertLinkItem(linkItemIndex, {
    text: "...",
    href: "#",
    disabled: false,
    handler: this.onClick.createDelegate(this, ["prev_dots", 3])
    });

            this.m_linkItems.push(linkItem);
    linkIndex++;
    }

        for (var iPage = iStartPage; iPage <= iEndPage; iPage++) {
    if (iPage == activePage) {
    // add iPage number - disabled
    linkItem = this.insertLinkItem(linkItemIndex, {
    text: "" + iPage,
    href: "#",
    disabled: true,
    handler: this.onClick.createDelegate(this, ["page", activePage - iPage])
    });
    }
    else {
    // add iPage number - clickable
    linkItem = this.insertLinkItem(linkItemIndex, {
    text: "" + iPage,
    href: "#",
    disabled: false,
    handler: this.onClick.createDelegate(this, ["page", activePage - iPage])
    });
    }

            this.m_linkItems.push(linkItem);
    linkItemIndex++;
    }

        if (iEndPage < totalPages) {
    // add '...' - clickable
    linkItem = this.insertLinkItem(linkItemIndex, {
    text: "...",
    href: "#",
    disabled: false,
    handler: this.onClick.createDelegate(this, ["next_dots", iEndPage])
    });

            this.m_linkItems.push(linkItem);
    }
    },

    insertLinkItem: function(index, linkItem) {
    if (Ext.isArray(linkItem)) {
    var linkItems = [];
    for (var i = 0, len = linkItem.length; i < len; i++) {
    linkItems.push(this.insertLinkItem(index + i, linkItem[i]));
    }
    return linkItems;
    }
    if (!(linkItem instanceof T.LinkItem)) {
    linkItem = new T.LinkItem(linkItem);
    }
    var td = document.createElement("td");
    this.tr.insertBefore(td, this.tr.childNodes[index]);
    this.initMenuTracking(linkItem);
    linkItem.render(td);
    this.items.insert(index, linkItem);
    return linkItem;
    },*/

    // private
    onLoadError: function() {
        if (!this.rendered) {
            return;
        }
    },

    // private
    readPage: function(d) {
        return false;
    },

    // private
    beforeLoad: function() {
    },

    // private
    onClick: function(which, page) {
        var store = this.store;
        switch (which) {
            case "first":
                this.doLoad(0);
                break;
            case "prev":
                this.doLoad(Math.max(0, this.cursor - this.pageSize));
                break;

            /*case "prev_dots":
            this.doLoad(Math.max(0, this.cursor - (3 * this.pageSize)));
            break;
            case "page":
            this.doLoad(Math.max(0, this.cursor - (3 * this.pageSize)));
            break;
            case "next_dots":
            this.doLoad(Math.max(0, this.cursor + (3 * this.pageSize)));
            break;*/ 

            case "next":
                this.doLoad(this.cursor + this.pageSize);
                break;
            case "last":
                var total = store.getTotalCount();
                var extra = total % this.pageSize;
                var lastStart = extra ? (total - extra) : total - this.pageSize;
                this.doLoad(lastStart);
                break;

            default:
                break;
        }
    }
});

/*
var T = Ext.Toolbar;

T.LinkItem = function(t) {
    el.on('click', this.onFocus, this);
    T.LinkItem.superclass.constructor.call(this, s);
};
Ext.extend(T.LinkItem, T.Item, {
    enable: Ext.emptyFn,
    disable: Ext.emptyFn,
    focus: Ext.emptyFn,

    // private
    onRender: function(ct, position) {
        var el = document.createElement("a");
        el.href = this.href || "#";
        el.innerHTML = t.text ? t.text : t; 
    }
});
Ext.reg('tblink', T.LinkItem);
*/