﻿/*
    title   :       page list bar
    creator :       tklmilk
    date    :       2004-9-29

    example:
        var plb = new PageListBar('plb', 343, 6, '?', 10);
        document.write(plb);
*/

function PageListBar(name, TotalNum, TotalPage, CurrPage, url, listlength)
{
    this.name = name;
    this.TotalNum = TotalNum;                      //total num
    this.TotalPage = TotalPage;                    //total page num
    this.CurrPage = CurrPage;                      //current page
    this.url = url;                                //link
    this.listlength = listlength?listlength:10;                                       //number of page showed

    if(this.TotalPage <=0)
    {
        this.TotalPage = 1;
    }
    if(this.CurrPage > this.TotalPage)
    {
        this.CurrPage = this.TotalPage;
    }
}

PageListBar.prototype.go = function(pagenum)
{
    window.location.href = '?CurrPage=' + pagenum + this.url;
}

PageListBar.prototype.goto = function()
{
    var CurrPage = prompt("请输入跳转到的页号",this.CurrPage);
    if(CurrPage)
    {
        if(CurrPage <= this.TotalPage && CurrPage>=1)
        {
            this.go(CurrPage);
        }
    }
}

PageListBar.prototype.toString = function()
{
    var str = '', pStart = pEnd = 1;

    if(this.TotalPage <= 1)
    {
        pStart = pEnd = 1;
    }else{
        if(this.TotalPage <= this.listlength)
        {
            pStart = 1;
            pEnd = this.TotalPage;
        }else{
            var movestep = Math.round(this.listlength/2);
            if(this.CurrPage > movestep)
            {
                pStart = this.CurrPage - movestep;
                pEnd = this.CurrPage + movestep;
                if(pEnd > this.TotalPage)
                {
                    pStart -= pEnd - this.TotalPage;
                    pEnd = this.TotalPage;
                }

                if(pEnd > this.TotalPage)
                {
                    pEnd = this.TotalPage;
                    pStart -= (pEnd - this.TotalPage);
                }
            }else{
                pStart = 1;
                pEnd = this.listlength;
            }
        }
    }

    for(var i=pStart; i<=pEnd; i++)
    {
        str += '<a href="javascript:' + this.name + '.go(' + i + ');void(0);">' + (i==this.CurrPage?('<b>' + i + '</b>'):i) + '</a>&nbsp;';
    }

    str ='找到' + this.TotalNum + '条记录　共' + this.TotalPage + '页　<a href="javascript:' + this.name + '.go(1);void(0);">首页</a>&nbsp;<a href="javascript:' + this.name + '.go(' + ((this.CurrPage-1)<=1?1:(this.CurrPage-1)) + ');void(0);"' + (this.CurrPage==1?'disabled':'') + '><font face="webdings">3</font></a>&nbsp;' + str + '<a href="javascript:' + this.name + '.go(' + ((this.CurrPage+1)>=this.TotalPage?this.TotalPage:(this.CurrPage+1)) + ');void(0);"' + (this.CurrPage==this.TotalPage?'disabled':'') + '><font face="webdings">4</font></a>&nbsp;<a href="javascript:' + this.name + '.go(' + this.TotalPage + ');void(0);">尾页</a>&nbsp;<a href="javascript:' + this.name + '.goto();void(0);">跳转</a>';

    return str;
}