wf.ns('wf.web');
wf.web.PagingBar = wf.extend(wf.BoxComponent, {
    pagesize: 20,
	infoText:"总计 {0} 个记录，共 {1} 页。",
    paramNames: {
        start: 'start',
        limit: 'limit'
    },
    init: function(){
        wf.web.PagingBar.superclass.init.call(this);
        var id = this.el.id;
        if (!this.first) {
            this.first = wf.get(id + "_f");
			this.first.on('click',this.onClick.createDelegate(this, ["first"]));
        }
		if (!this.prev) {
            this.prev = wf.get(id + "_p");
			this.prev.on('click',this.onClick.createDelegate(this, ["prev"]));
        }
		 if (!this.next) {
            this.next = wf.get(id + "_n");
			this.next.on('click',this.onClick.createDelegate(this, ["next"]));
        }
		if (!this.last) {
            this.last = wf.get(id + "_l");
			this.last.on('click',this.onClick.createDelegate(this, ["last"]));
        }
		this.displayEl=wf.get(id+'_info');
		this.field=wf.get(id+"_i");
		this.field.on("keydown", this.onPagingKeydown, this);
        this.field.on("focus", function(){this.dom.select();});
		this.field.dom.value=1;
		
		this.cursor = 0;
        this.bind(this.store);
    },
	onClick : function(which){
        var store = this.store;
        switch(which){
            case "first":
			    if(!this.first.disable){
					 this.doLoad(0);
				}
            break;
            case "prev":
				if (!this.prev.disable) {
					this.doLoad(Math.max(0, this.cursor - this.pagesize));
				}
            break;
            case "next":
				if (!this.next.disable) {
					this.doLoad(this.cursor + this.pagesize);
				}
            break;
            case "last":
				if (!this.last.disable) {
					var total = store.getTotalCount();
					var extra = total % this.pagesize;
					var lastStart = extra ? (total - extra) : total - this.pagesize;
					this.doLoad(lastStart);
				}
            break;
        }
    },
	readPage : function(d){
        var v = this.field.dom.value, pageNum;
        if (!v || isNaN(pageNum = parseInt(v, 10))) {
            this.field.dom.value = d.activePage;
            return false;
        }
        return pageNum;
    },
	onPagingKeydown : function(e){
		var k = e.getKey(), d = this.getPageData(), pageNum;
        if (k == e.RETURN) {
            e.stopEvent();
            if(pageNum = this.readPage(d)){
                pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
                this.doLoad(pageNum * this.pagesize);
            }
        }else if (k == e.HOME || k == e.END){
            e.stopEvent();
            pageNum = k == e.HOME ? 1 : d.pages;
            this.field.dom.value = pageNum;
        }else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN){
            e.stopEvent();
            if(pageNum = this.readPage(d)){
                var increment = e.shiftKey ? 10 : 1;
                if(k == e.DOWN || k == e.PAGEDOWN){
                    increment *= -1;
                }
                pageNum += increment;
                if(pageNum >= 1 & pageNum <= d.pages){
                    this.field.dom.value = pageNum;
                }
            }
        }
	},
	doLoad : function(start){
        var o = {}, pn = this.paramNames;
		var lastOptions=this.store.lastOptions;
		if(lastOptions){
			wf.apply(o,lastOptions.params);
		}
        o[pn.start] = start;
        o[pn.limit] = this.pagesize;
      	this.store.load({params:o});
    },
	bind : function(store){
		store = wf.StoreMgr.lookup(store);
        store.on("beforeload", this.beforeLoad, this);
        store.on("load", this.onLoad, this);
        this.store = store;
	},
	unbind : function(store){
        store = wf.StoreMgr.lookup(store);
        store.un("beforeload", this.beforeLoad, this);
        store.un("load", this.onLoad, this);
        this.store = undefined;
    },
	beforeLoad : function(){
		pn = this.paramNames;
		this.store.baseParams[pn.limit]=this.pagesize;
		this.store.baseParams[pn.start]=1;
    },
	onLoad : function(store, r, o){
       this.cursor = o.params ? o.params[this.paramNames.start] : 0;
	   if(!this.cursor){
	   	  this.cursor=0;
	   }
       var d = this.getPageData(), ap = d.activePage, ps = d.pages;
       this.displayEl.update(String.format(this.infoText, d.total,d.pages));
       this.field.dom.value = ap;
       if(ap==1){
	   	 this.first.addClass('disabled');
		 this.first.disable=true;
		 this.prev.addClass('disabled');
		 this.prev.disable=true;
	   }else{
	   	 this.first.removeClass('disabled');
		 this.prev.removeClass('disabled');
		 this.first.disable=false;
		 this.prev.disable=false;
	   }
	   if (ap == ps) {
	   	 this.next.addClass('disabled');
       	 this.last.addClass('disabled');
		 this.last.disable=true;
		 this.next.disable=true;
	   }else{
	   	 this.next.removeClass('disabled');
       	 this.last.removeClass('disabled');
		 this.last.disable=false;
		 this.next.disable=false;
	   }
    },
	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)
        };
    }
	
});
