function Class() { }
Class.prototype.construct = function() {};
Class.__asMethod__ = function(func, superClass) {
  return function() {
      var currentSuperClass = this.$;
      this.$ = superClass;
      var ret = func.apply(this, arguments);
      this.$ = currentSuperClass;
      return ret;
  };
};

Class.extend = function(def) {
  var classDef = function() {
      if (arguments[0] !== Class) { this.construct.apply(this, arguments); }
  };

  var proto = new this(Class);
  var superClass = this.prototype;

  for (var n in def) {
      var item = def[n];

      if (item instanceof Function) {
          item = Class.__asMethod__(item, superClass);
      }

      proto[n] = item;
  }

  proto.$ = superClass;
  classDef.prototype = proto;

  //Give this new class the same static extend method
  classDef.extend = this.extend;
  return classDef;
};

var PageList = Class.extend({
    construct: function(pageIndex,totalPage,totalSize, callbackFunction)
    {
        this.pageIndex=pageIndex;
        this.totalPage=totalPage;
        this.total=totalSize;
        this.callbackFunction=callbackFunction;
        this.tailNumber=8;
        this.headNumber=8;
        this.width=3;
        this.twoend=2;
        if (this.pageIndex-1>0)
            this.prev=this.pageIndex-1;
        else
            this.prev=0;

        if (this.pageIndex<this.totalPage)
            this.next=this.pageIndex+1;
        else
            this.next=0;
        this.class_prefix="p"
    },

    displayList: function()
    {
        var result = "";
        if (this.total==0 || this.pageIndex<0 || this.pageIndex > this.totalPage)
        {
            result += "<div align='center' class='"+this.class_prefix+"_total'><div class='"+this.class_prefix+"_bar'>" + notfound + "</div></div>";
        }
        else if (this.totalPage<=1)
        {
            result += "<div align='center' class='"+this.class_prefix+"_total'><div class='"+this.class_prefix+"_bar'>" + page_total + " (" + this.total+")</div></div>";
        }
        else
        {
            result="<div class='"+this.class_prefix+"_bar' align='center'><nobr>";
            result += this.getPrevPageNum() + "&nbsp;&nbsp;";
            
            
            if (this.totalPage>(this.twoend*2+this.width*2+1))
            {
                result += this.getHead()
                result += this.getCurPageNum(this.pageIndex);
                result += this.getTail();
            }
            else
            {
                for (var i=1;i<=this.totalPage;i++)
                {
                    if (i!=this.pageIndex)
                    {
                        result+=this.getPageNum(i);
                    }
                    else
                    {
                        result+=this.getCurPageNum(i);
                    }
                }
            }
			
            result += "&nbsp;&nbsp;" + this.getNextPageNum() ;

            result += "</nobr>";

            result += "<div align='center' class='"+this.class_prefix+"_total'>"+ page_total + " (" + this.total+")</div></div>";
        }
       
        return result;
    },

    getHead:function()
    {
        var result = "";
        if (this.pageIndex-this.headNumber>1)
        {
            var start=(this.pageIndex-this.width<this.totalPage-this.headNumber?this.pageIndex-this.width:this.totalPage-this.headNumber)
            for (var i=1;i<=this.twoend;i++)
            {
                result += this.getPageNum(i);
            }

            if (start>this.twoend+1)
                result += "<a class='nolink'>&nbsp;...&nbsp;</a>" ;


            for ( var i= start; i < this.pageIndex ; i++ )
            {
                result += this.getPageNum(i);
            }
        }
        else
        {
            for ( var i=1 ; i < this.pageIndex ; i++ )
            {
                result += this.getPageNum(i);
            }
        }

        return result;
    },

    getTail:function()
    {
        var result = "";

        if (this.pageIndex+this.tailNumber<this.totalPage)
        {
            var end=(this.pageIndex+this.width<this.headNumber?this.headNumber:this.pageIndex+this.width)
            for ( var i=this.pageIndex+1 ; i <= end ; i++ )
            {
                result += this.getPageNum(i);
            }

            if (end<this.totalPage-this.twoend)
            result += "<a class='nolink'>&nbsp;...&nbsp;</a>" ;

            for (var i=this.totalPage-this.twoend+1;i<=this.totalPage;i++)
            {
                result += this.getPageNum(i);
            }
        }
        else
        {
            for ( var i=this.pageIndex+1 ; i <= this.totalPage ; i++ )
            {
                result += this.getPageNum(i);
            }
        }

        return result;
    },

    getPageNum:function(index)
    {
        var f = this.callbackFunction + "("+index+")";
        return "<a href=\"#\" onclick=\""+f+";return false\" class='"+this.class_prefix+"_num'>" + index + "</a>";
    },

    getCurPageNum:function(index)
    {
        return "<a class='"+this.class_prefix+"_curpage'><font color='red'><b>" + index + "</b></font></a>";
    },

    getPrevPageNum:function()
    {
        if (this.prev>0)
        {
            var f = this.callbackFunction + "("+this.prev+")";
            return "<a href=\"#\" onclick=\""+f+";return false\" class='"+this.class_prefix+"_num'>" + prev_page + "</a>";
        }
        else
        {
            return "<a class='nolink'>"+prev_page+"</font></a>";
        }
    },

    getNextPageNum:function()
    {
        if (this.next>0)
        {
            var f = this.callbackFunction + "("+this.next+")";
            return "<a href=\"#\" onclick=\""+f+";return false\" class='"+this.class_prefix+"_num'>" + next_page + "</a>";
        }
        else
        {
            return "<a class='nolink'>"+next_page+"</a>";
        }
    }
});

var SmallPageList = PageList.extend({
    construct: function(pageIndex,totalPage,totalSize, callbackFunction)
    {
        this.pageIndex=pageIndex;
        this.totalPage=totalPage;
        this.total=totalSize;
        this.callbackFunction=callbackFunction;
        this.tailNumber=1;
        this.headNumber=1;
        this.width=1;
        this.twoend=1;

        if (this.pageIndex-1>0)
            this.prev=this.pageIndex-1;
        else
            this.prev=0;

        if (this.pageIndex<this.totalPage)
            this.next=this.pageIndex+1;
        else
            this.next=0;
        this.class_prefix="sp"
    }
});

var BlackPageList = PageList.extend({
    construct: function(pageIndex,totalPage,totalSize, callbackFunction)
    {
        this.pageIndex=pageIndex;
        this.totalPage=totalPage;
        this.total=totalSize;
        this.callbackFunction=callbackFunction;
        this.tailNumber=8;
        this.headNumber=8;
        this.width=3;
        this.twoend=2;

        if (this.pageIndex-1>0)
            this.prev=this.pageIndex-1;
        else
            this.prev=0;

        if (this.pageIndex<this.totalPage)
            this.next=this.pageIndex+1;
        else
            this.next=0;
        this.class_prefix="blackp"
    }
});


