var Pager = function(clickCallback, scope) {
    this.init(clickCallback, scope);
};

Pager.prototype = {

    FIRST_PAGE: 'navBar_fp',
    PREVIOUS_PAGE: 'navBar_pp',
    NEXT_PAGE: 'navBar_np',
    LAST_PAGE: 'navBar_lp',
    PAGE_NUMBER: 'navBar_pn',
	
    init: function(clickCallback, scope) {
		
        this.clickCallback = clickCallback;
        this.clickCallbackScope = scope;
		
        var navBar;
        var i = 1;
        while(  (navBar = Ext.get('navBar' + i)) ) {
		
            var fp = navBar.select('.' + this.FIRST_PAGE);
            var pp = navBar.select('.' + this.PREVIOUS_PAGE);
            var np = navBar.select('.' + this.NEXT_PAGE);
            var lp = navBar.select('.' + this.LAST_PAGE);
            var pn = navBar.select('.' + this.PAGE_NUMBER);
			
            if(fp) fp.on('click', this.navBarClick, this, {
                stopEvent: true
            });
            if(pp) pp.on('click', this.navBarClick, this, {
                stopEvent: true
            });
            if(np) np.on('click', this.navBarClick, this, {
                stopEvent: true
            });
            if(lp) lp.on('click', this.navBarClick, this, {
                stopEvent: true
            });
			
            if(pn) {
                pn.each(function(el, obj, index) {
                    el.on('click', this.navBarClick, this, {
                        stopEvent: true
                    });
                }, this);
            }
			
            i++;
        }
    },
	
    navBarClick: function(evt) {
        var type;
        var tgt = Ext.get(evt.target);
		
        if(tgt.hasClass(this.FIRST_PAGE))
            type = this.FIRST_PAGE;
        else if(tgt.hasClass(this.PREVIOUS_PAGE))
            type = this.PREVIOUS_PAGE;
        else if(tgt.hasClass(this.NEXT_PAGE))
            type = this.NEXT_PAGE;
        else if(tgt.hasClass(this.LAST_PAGE))
            type = this.LAST_PAGE;
        else if(tgt.hasClass(this.PAGE_NUMBER))
            type = this.PAGE_NUMBER;
		
        this.clickCallback.call(this.clickCallbackScope, tgt, type);
    },
	
    update: function(curPage, totPages) {
		
        var navBar, dsp;
        var i = 1;

        while( (navBar = Ext.get('navBar' + i)) ) {
		
            var curPageEl = navBar.select('.navBar_curPage');
            var totPagesEl = navBar.select('.navBar_totPages');
            var pnEl = navBar.select('.navBar_pn');
			
            if(curPageEl)
                curPageEl.update(curPage);
				
            if(totPagesEl)
                totPagesEl.update(totPages);
				
            if(pnEl) {
                var offset = 0;
                var totPn = pnEl.getCount();
				
                var half = Math.round(totPn / 2);
                var pageNr = curPage - half;
                if(pageNr < 1)
                    pageNr = 1;

                pnEl.each(function(el, obj, index) {

                    if(pageNr > totPages) {
                        el.enableDisplayMode();
                        el.hide();
                    }
                    else {
                        el.update(pageNr);

                        if(pageNr == curPage)
                            el.addClass('noLink');
                        else
                            el.removeClass('noLink');

                        el.show();
                    }
                    pageNr++;
					
                }, this);
            }
            i++;
        }
		
        if(curPage == 1) {
            Ext.select('.navBar_fp').hide();
            Ext.select('.navBar_pp').hide();
        }
        else {
            Ext.select('.navBar_fp').show();
            Ext.select('.navBar_pp').show();
        }
		
        if(curPage == totPages) {
            Ext.select('.navBar_lp').hide();
            Ext.select('.navBar_np').hide();
        }
        else {
            Ext.select('.navBar_lp').show();
            Ext.select('.navBar_np').show();
        }

        jQuery('#nav_no_ajax').hide();
        //Ext.select('nav_no_ajax').hide();
    }
		
}
