/*
    title   :       page list bar
    creator :       tklmilk
	modify	:		seateng 2005-03-30
    date    :       2004-9-29

    example:
        var plb = new PageListBar('plb', 343, 6, '?', 10);
        document.write(plb);
*/
var pos=0;
function PageListBar(name, pagecount, currpage, url, listlength)
{
    this.name = name;
    this.pagecount = pagecount;                                 //total page num
    this.currpage = currpage;                                   //current page
    this.url = url;                                             //link
    this.listlength = listlength?listlength:10;                                       //number of page showed

    if(this.pagecount <=0)
    {
        this.pagecount = 1;
    }
    if(this.currpage > this.pagecount)
    {
        this.currpage = this.pagecount;
    }
	pos++;
}

PageListBar.prototype.go = function(pagenum)
{
    window.location.href = '?CurrPage=' + pagenum + (this.url.charAt(0)=="&"?"":"&") + this.url;
}

PageListBar.prototype.goto = function(getpos)
{
	var currpage;
	for (var i=1; i<=pos; i++)
	{
		if (getpos==i)
		{
			currpage = document.all("PGNumber"+i).value;
			break;
		}
	}
    if(currpage)
    {
        if(currpage <= this.pagecount && currpage>=1)
        {
            this.go(currpage);
        }
    }
}

PageListBar.prototype.toString = function()
{
    var str = '', pStart = pEnd = 1;

    if(this.pagecount <= 1)
    {
        pStart = pEnd = 1;
    }else{
        if(this.pagecount <= this.listlength)
        {
            pStart = 1;
            pEnd = this.pagecount;
        }else{
            var movestep = Math.round(this.listlength/2);
            if(this.currpage > movestep)
            {
                pStart = this.currpage - movestep;
                pEnd = this.currpage + movestep;
                if(pEnd > this.pagecount)
                {
                    pStart -= pEnd - this.pagecount;
                    pEnd = this.pagecount;
                }

                if(pEnd > this.pagecount)
                {
                    pEnd = this.pagecount;
                    pStart -= (pEnd - this.pagecount);
                }
            }else{
                pStart = 1;
                pEnd = this.listlength;
            }
        }
    }

    for(var i=pStart; i<=pEnd; i++)
    {
        if( i==this.currpage )
			str += '<span class="current">'+ i + '</span>';
		else
			str += '<a href="javascript:' + this.name + '.go(' + i + ');void(0);">' + i + '</a>';
    }
	
	var str2 = '';
	
    str2 = '<div class="mmo_page"> <span class="sumpages">'+ this.pagecount + ' pages</span>';
	str2 += '<a href="javascript:' + this.name + '.go(1);void(0);"' + '>first</a>';
	if( this.currpage==1 )
		str2 += '<span class="disabled">&laquo; prev</span>';
	else
		str2 += '<a href="javascript:' + this.name + '.go(' + ((this.currpage-1)<=1?1:(this.currpage-1)) + ');void(0);">&laquo; prev</a>';
		
	str2 += str;
	
	if( this.currpage==this.pagecount )
		str2 += '<span class="disabled">next &raquo; </span>';
	else
		str2 += '<a href="javascript:' + this.name + '.go(' + ((this.currpage+1)>=this.pagecount?this.pagecount:(this.currpage+1)) + ');void(0);">next &raquo; </a>';
	
	str2 += '<a href="javascript:' + this.name + '.go(' + this.pagecount + ');void(0);">end</a> </div>';
	
	str = str2;
	
	
	
	/*str =  'Total <b>'+ this.pagecount + '</b> Pages<a href="javascript:' + this.name + '.go(1);void(0);"'+ (this.currpage==1?'disabled':'') + '><span>First</span></a><a href="javascript:' + this.name + '.go(' + ((this.currpage-1)<=1?1:(this.currpage-1)) + ');void(0);"' + (this.currpage==1?'disabled':'') + '><span>&lt;Prev</span></a>' + str + '<a href="javascript:' + this.name + '.go(' + ((this.currpage+1)>=this.pagecount?this.pagecount:(this.currpage+1)) + ');void(0);"' + (this.currpage==this.pagecount?'disabled':'') + '><span>Next&gt;</span></a><a href="javascript:' + this.name + '.go(' + this.pagecount + ');void(0);"><span>End</span></a>';*/
	return str;
}
