﻿// JScript 文件 
// 
// PageNavigate.js (code : UTF-8) 
// base on _ScriptLibray.Asp.Record06
// 2008-6-3 zhang songtao @ lierw.com 40046933 : tianziyizu@163.com

var PageNavigate = new function()
{
    this.rowsCount = 0;
    this.pageSize = 0;
    this.pageCount = 0;
    this.currPage = 0;
    this.pageSign = '';
    this.format = '';
    this.write = _pageNavigate_write;
    this.fixFirstPage = function() { };
    this.fixLastpage = function() { };
}

function _pageNavigate_write()
{
    var Xa1, Xa2, Xa3, Xa4, Xx1, Xx2, html = this.format, page = window.location.pathname, params = window.location.search.replace("?", ""), regx = new RegExp("(&)?" + this.pageSign + "=\\d*", "gi");
    // string : params
    params = params.replace(regx, "");
    if (params.substr(0, 1) != "&") params = "&" + params;
    if (params.substr(params.length - 1) == "&") params = params.substr(0, params.length - 1);
    
    // string : links
    Xa1 = "<a href='" + page + "?" + this.pageSign + "=1" + params + "'>";
    Xa2 = "<a href='" + page + "?" + this.pageSign + "=" + (this.currPage - 1) + params + "'>";
    Xa3 = "<a href='" + page + "?" + this.pageSign + "=" + (this.currPage + 1) + params + "'>";
    Xa4 = "<a href='" + page + "?" + this.pageSign + "=" + this.pageCount + params + "'>";
    if (this.currPage < 2) { this.fixFirstPage(); Xa1 = "<a class='nav_end'>"; Xa2 = "<a class='nav_end'>"; }
    if (this.currPage == this.pageCount) { this.fixLastpage(); Xa3 = "<a class='nav_end'>"; Xa4 = "<a class='nav_end'>"; };
    
    // string : select
    Xx1 = "<select style='font-family:verdana' onchange=\"window.location='" + page + "?" + this.pageSign + "='+this.value+'" + params + "';\"><option value=\"\">↑转到↓</option>";
    for (var i = 0; i < this.pageCount; i++) Xx1 += "<option value='" + i + "'>第 " + i + " 页</option>";
    Xx1 += "</select>";
    Xx2 = "<span><input type='text' size='4' /><input type=button value='转到' onclick=\"window.location='" + page + "?" + this.pageSign + "='+this.parentNode.children[0].value+'" + params + "';\" /></span>";
    
    // replace format
    html = html.replace(/\$n1/gi, this.rowsCount);
    html = html.replace(/\$n2/gi, this.pageCount);
    html = html.replace(/\$n3/gi, this.currPage);
    html = html.replace(/\$n4/gi, this.pageSize);
    html = html.replace(/\$a1/gi, Xa1);
    html = html.replace(/\$a2/gi, Xa2);
    html = html.replace(/\$a3/gi, Xa3);
    html = html.replace(/\$a4/gi, Xa4);
    html = html.replace(/\$ae/gi, "</a>");
    html = html.replace(/\$x1/gi, Xx1);
    html = html.replace(/\$x2/gi, Xx2);
    
    // replace group numbers
    if (-1 != html.indexOf("$z"))
    {
        var spaceLen, spaceStr = "", spacePoint, spacePointEnd;
        if (this.rowsCount > 0)
        {
            spaceLen = html.replace(/(.*\$z)(\d*)(.*)/i, "$2")
            if ("" == spaceLen) spaceLen = 5; else spaceLen = parseInt(spaceLen); 
            if (this.currPage % spaceLen == 0) spacePoint = this.currPage - spaceLen; else spacePoint = Math.abs(this.currPage - (this.currPage % spaceLen));
            spacePointEnd = spacePoint + spaceLen + 1;
            if (spacePoint == 0) spacePoint = 1;
            while (spacePoint <= spacePointEnd)
            {
                if (spacePoint > this.pageCount) break;
                spaceStr += "<a "; if (spacePoint == this.currPage) spaceStr += "id='nav_n_curr' ";
                spaceStr += "href='" + page + "?" + this.pageSign + "=" + spacePoint + params + "'>" + spacePoint + "</a>&nbsp;"
                spacePoint++;
            }
        };
        html = html.replace(/\$z(\d*)/i, spaceStr);
    }
    
    // write html
    document.write(html);
}