
/**
 * @overview LinksPagingToolbar.js
 * 
 * This paging toolbar displays before and after pages as links
 * The number of pages to display before and after is configurable with the pageNumber variable
 * 
 * @author Laurent Petit
 * @version 1
*/
Ext.namespace("CCA");

CCA.LinksPagingToolbar = Ext.extend(Ext.Toolbar, {
    // Style related stuff - Jesse
    cls:'x_results_bar',
    buttonSelector: "div.x_button_text",
    buttonTemplate: new Ext.Template('<div class="x_pager_button"><div class="x_button_text">{0}</div></div>'),
    //
    
    /**
     * @cfg {Ext.data.Store} store The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
     */
    /**
     * @cfg {Number} pageSize
     * The number of records to display per page (defaults to 20)
     */
    pageSize: 20,
	/**
	 * @cfg {Number} pageNumber
     * The number of pages to display as links before and after the current page
     */
    pageNumber : 1,
    /**
     * Customizable piece of the default paging text (defaults to "First Page")
     * @type String
     */
    firstText : App.t('links_paging_first'),
    /**
     * Customizable piece of the default paging text (defaults to "Previous Page")
     * @type String
     */
    prevText : App.t('links_paging_previous'),
    /**
     * Customizable piece of the default paging text (defaults to "Next Page")
     * @type String
     */
    nextText : App.t('links_paging_next'),
    /**
     * Customizable piece of the default paging text (defaults to "Last Page")
     * @type String
     */
    lastText : App.t('links_paging_last'),
    /**
     * Object mapping of parameter names for load calls (defaults to {start: 'start', limit: 'limit'})
     */
    paramNames : {start: 'start', limit: 'limit'},

    // private
    initComponent : function(){
        this.addEvents(
            /**
             * @event change
             * Fires after the active page has been changed.
             * @param {Ext.PagingToolbar} this
             * @param {Object} changeEvent An object that has these properties:<ul>
             * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
             * returned by the server</div></li>
             * <li><code>activePage</code> : Number <div class="sub-desc">The current page number</div></li>
             * <li><code>pages</code> : Number <div class="sub-desc">The total number of pages (calculated from
             * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
             * </ul>
             */
            'change',
            /**
             * @event beforechange
             * Fires just before the active page is changed.
             * Return false to prevent the active page from being changed.
             * @param {Ext.PagingToolbar} this
             * @param {Object} beforeChangeEvent An object that has these properties:<ul>
             * <li><code>start</code> : Number <div class="sub-desc">The starting row number for the next page of records to
             * be retrieved from the server</div></li>
             * <li><code>limit</code> : Number <div class="sub-desc">The number of records to be retrieved from the server</div></li>
             * </ul>
             * (note: the names of the <b>start</b> and <b>limit</b> properties are determined
             * by the store's {@link Ext.data.Store#paramNames paramNames} property.)
             */
            'beforechange'
        );
        CCA.LinksPagingToolbar.superclass.initComponent.call(this);
        this.cursor = 0;
        this.bind(this.store);
    },

    // private
    onRender : function(ct, position){
        CCA.LinksPagingToolbar.superclass.onRender.call(this, ct, position);
        this.first = this.addButton({
            text: this.firstText,
            disabled: false,
            handler: this.onClick.createDelegate(this, ["first"]),
            buttonSelector: this.buttonSelector,
            template: this.buttonTemplate
        });
        this.prev = this.addButton({
            text: this.prevText,
            disabled: false,
            handler: this.onClick.createDelegate(this, ["prev"]),
            buttonSelector: this.buttonSelector,
            template: this.buttonTemplate
        });
        
		for (var i=this.pageNumber-1; i>=0; i--) {
            this["prev#"+i] = this.addButton({
                hidden: true,
                handler: this.onClick.createDelegate(this, ["prev#"+i]),
                buttonSelector: this.buttonSelector,
                template: this.buttonTemplate,
                text:'prev' + i
            });
		}
    this.activePage = this.addText(String.format('{0}', 1));
    for (var i=0; i<this.pageNumber; i++) {
      this["next#"+i] = this.addButton({
                hidden: true,
                handler: this.onClick.createDelegate(this, ["next#"+i]),
                buttonSelector: this.buttonSelector,
                template: this.buttonTemplate
	        });
		}
        this.next = this.addButton({
            text: this.nextText,
            disabled: false,
            handler: this.onClick.createDelegate(this, ["next"]),
            buttonSelector: this.buttonSelector,
            template: this.buttonTemplate
        });
        this.last = this.addButton({
            text: this.lastText,
            disabled: false,
            handler: this.onClick.createDelegate(this, ["last"]),
            buttonSelector: this.buttonSelector,
            template: this.buttonTemplate
        });

        if(this.dsLoaded){
            this.onLoad.apply(this, this.dsLoaded);
        }
    },

    // private
    onLoad : function(store, r, o){
        if(!this.rendered){
            this.dsLoaded = [store, r, o];
            return;
        }
        this.cursor = o.params ? o.params[this.paramNames.start] : 0;
        var d = this.getPageData(), ap = d.activePage, ps = d.pages;
        this.activePage.el.innerHTML = String.format('{0}', ap);
        this.first.setVisible(ap != 1);
        this.prev.setVisible(ap != 1);
        this.next.setVisible(ap != ps);
        this.last.setVisible(ap != ps);
		for (var i=0; i<this.pageNumber; i++) {
			//this["prev#"+i].setText(ap - (i+1));
			this["prev#"+i].getEl().update(ap - (i+1));
			this["prev#"+i].setVisible(ap - (i+1) > 0);
			//this["next#"+i].setText(ap + (i+1)); //incompatible with using a button template - setText method calls for markup that might not be there
			this["next#"+i].getEl().update(ap + (i+1));
			this["next#"+i].setVisible(ap + (i+1) <= ps);
		}
        this.fireEvent('change', this, d);
    },

    // private
    getPageData : function(){
        var total = this.store.getTotalCount();
        return {
            total : total,
            activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
            pages :  total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
        };
    },

    // private
    onLoadError : function(){
        if(!this.rendered){
            return;
        }
    },


    // private
    beforeLoad : function(){},

    // private
    doLoad : function(start){
        var o = {}, pn = this.store.paramNames;
		//console.log(pn);
        o[pn.start] = start;
        o[pn.limit] = this.pageSize;
        if(this.fireEvent('beforechange', this, o) !== false){
            this.store.load({params:o});
        }
    },

    /**
     * Change the active page
     * @param {Integer} page The page to display
     */
    changePage: function(page){
        this.doLoad(((page-1) * this.pageSize).constrain(0, this.store.getTotalCount()));
    },

    // private
    onClick : function(which){
		var prefix = which.split('#').shift();
		var page = which.split('#').pop();
		var numTest=/(^\d+$)|(^\d+\.\d+$)/
		page = numTest.test(page) ? parseInt(page) : 0;
        var store = this.store;
        switch(prefix){
            case "first":
                this.doLoad(0);
            break;
            case "prev":
                this.doLoad(Math.max(0, this.cursor-(this.pageSize*(page+1))));
            break;
            case "next":
				this.doLoad(this.cursor+(this.pageSize*(page+1)));
            break;
            case "last":
                var total = store.getTotalCount();
                var extra = total % this.pageSize;
                var lastStart = extra ? (total - extra) : total-this.pageSize;
                this.doLoad(lastStart);
            break;
        };
        //scroll to top of window on going to a new page
        var w = window;
        w.scrollTo(0,0);
    },

    /**
     * Unbinds the paging toolbar from the specified {@link Ext.data.Store}
     * @param {Ext.data.Store} store The data store to unbind
     */
    unbind : function(store){
        store = Ext.StoreMgr.lookup(store);
        store.un("beforeload", this.beforeLoad, this);
        store.un("load", this.onLoad, this);
        store.un("loadexception", this.onLoadError, this);
        this.store = undefined;
    },

    /**
     * Binds the paging toolbar to the specified {@link Ext.data.Store}
     * @param {Ext.data.Store} store The data store to bind
     */
    bind : function(store){
        store = Ext.StoreMgr.lookup(store);
        store.on("beforeload", this.beforeLoad, this);
        store.on("load", this.onLoad, this);
        store.on("loadexception", this.onLoadError, this);
		Ext.apply(this.paramNames,store.paramNames);
        this.store = store;
    },

    // private
    onDestroy : function(){
        if(this.store){
            this.unbind(this.store);
        }
        CCA.LinksPagingToolbar.superclass.onDestroy.call(this);
    }
});
Ext.reg('links-paging', CCA.LinksPagingToolbar);