/**
 * Script permettant la gestion des pages du Pager
 */
var PagerEnv = {};
PagerEnv.Pagers = new Array();
PagerEnv.Pager = function(options)
{
    this.id = options.id;
    this.currentPage = parseInt(options.currentPage);
    this.nbpage = parseInt(options.nbTotPage);
    this.nbVisibleNumber = parseInt(options.nbVisibleNumber)
    this.params = new Object();
    this.b1 = 0;
    this.b2 = 0;

    PagerEnv.Pagers[this.id] = this;
    this.init();
}
PagerEnv.Pager.prototype =
{
    getContent : function()
    {
        $(this.id+'_content').innerHTML = "chargement ...";
        this.params.id = this.id;
        this.params.page = this.currentPage;
        ajax = new Ajax(Copix.getActionURL('toolsbox|pager|getcontent'),{
            'data':this.params,
            'method':'POST',
            'evalScripts':true,
            'update':this.id+'_content'
        });
        ajax.request();
        if(this.nbpage > 1){
            this.getPager();
        }
    },
    getEvent : function()
    {
        if($('pagerFirst'+this.id)){
            $('pagerFirst'+this.id).addEvent('click', function(){
                this.goFirst();
            }.bind(this));
        }
        if($('pagerPrev'+this.id)){
            $('pagerPrev'+this.id).addEvent('click', function(){
                this.goPrev();
            }.bind(this));
        }
        if($('pagerNext'+this.id)){
            $('pagerNext'+this.id).addEvent('click', function(){
                this.goNext();
            }.bind(this));
        }
        if($('pagerLast'+this.id)){
            $('pagerLast'+this.id).addEvent('click', function(){
                this.goLast();
            }.bind(this));
        }
        $$('.pagerNumber'+this.id).each(function(el){
            el.addEvent('click', function(){
                this.goPage(el.getProperty('rel'));
            }.bind(this));
        }.bind(this));
    },
    getPager : function()
    {
        if(this.currentPage > 1){
            $('pagerFirst'+this.id).style.display = '';
            $('pagerPrev'+this.id).style.display = '';
        } else {
            $('pagerFirst'+this.id).style.display = 'none';
            $('pagerPrev'+this.id).style.display = 'none';
        }
        if(this.currentPage < this.nbpage){
            $('pagerLast'+this.id).style.display = '';
            $('pagerNext'+this.id).style.display = '';
        } else {
            $('pagerLast'+this.id).style.display = 'none';
            $('pagerNext'+this.id).style.display = 'none';
        }
        // modification des chiffre du pager
        this.getLimitNumber();
        var limit = this.nbpage + 1;
        //var i = (this.currentPage+this.nbVisibleNumber > limit && this.nbpage > this.nbVisibleNumber)?limit - this.nbVisibleNumber:this.currentPage;
        var i = this.b1;
        $$('.pagerNumber'+this.id).each(function(el){
            var reg1=new RegExp("( Selected)", "g");
            var eclass=el.className.replace(reg1,'');
            if(i != null){
                el.setProperty('rel', i);
                el.innerHTML = i;
                if(i == this.currentPage){
                    el.className = eclass+' Selected';
                }else{
                    el.className = eclass;
                }
                ++i;
            }else{
                if(parseInt(el.getProperty('rel')) == this.currentPage){
                    el.className = eclass+' Selected';
                }else{
                    el.className = eclass;
                }
            }
        }.bind(this));
    },
    getLimitNumber : function(){
        this.b1=1;
        this.b2=this.nbVisibleNumber;
	if(this.currentPage >= this.nbVisibleNumber){
		this.b1 = (this.currentPage-this.nbVisibleNumber) + 2;
		this.b2 = this.currentPage + 1;
	}
	if(this.b2 > this.nbPage){
		this.b2 = this.nbPage;
		this.b1 = (this.b2-this.nbVisibleNumber) + 1;
		if(this.b1 < 1) {
                    this.b1 = 1;
                }
	}
    },
    goFirst : function(){
        this.currentPage = 1;
        this.getContent();
    },
    goPrev : function(){
        if(this.currentPage-1 > 0){
            this.currentPage--;
        }else{
            this.currentPage = 1;
        }
        this.getContent();
    },
    goNext : function(){
        if(this.currentPage+1 <= this.nbpage){
            this.currentPage++;
        }else{
            this.currentPage = this.nbpage;
        }
        this.getContent();
    },
    goLast : function(){
        this.currentPage = this.nbpage;
        this.getContent();
    },
    goPage : function(number){
        this.currentPage = parseInt(number);
        this.getContent();
    },
    refresh : function(){
      this.getContent();
    },
    init : function(){
        if(this.nbpage > 1){
            this.getEvent();
        }
        if(this.currentPage != 1){
            this.goPage(this.currentPage);
        }else{
            this.getContent();
        }
    }
}
PagerEnv.Factory = {
    get : function(id)
    {
        if (PagerEnv.Pagers[id]){
            return PagerEnv.Pagers[id];
        } else {
            var options = new Object();
            if(arguments[1]){
                options = arguments[1];
            }
            options.id = id;
            return PagerEnv.Factory.create(options);
        }
    },
    create : function(options)
    {
        if(options.id){
            return new PagerEnv.Pager(options);
        }else {
            return false;
        }
    }
}
