﻿function showPages(name) { //初始化属性
    this.name = name; //对象名称
    this.page = 1; //当前页数
    this.rowCount = 1;
    this.pageCount = 1; //总页数
    //   this.argName = 'page'; //参数名
    //    this.showTimes = 1; //打印次数
    this.pageSize = 10; //每页多少条;
    this.beginPageIndex = 1; //显示页数的开始页数
    this.pageListSize = 10; //每个页面上显示多少页
}
showPages.prototype.checkPages = function() { //进行总页数初始化和当前页数和总页数的验证
    if (isNaN(parseInt(this.page))) this.page = 1;
    if (isNaN(parseInt(this.rowCount))) this.rowCount = 1;
    if (this.page < 1) this.page = 1;
    if (this.pageCount < 1) this.pageCount = 1;


    if (this.rowCount > this.pageSize) {
        this.pageCount = parseInt(this.rowCount / this.pageSize);
        if ((this.rowCount % this.pageSize) > 0) this.pageCount = this.pageCount + 1;
    }
    else {
        this.pageCount = 1;
    }



    if (this.page > this.pageCount) this.page = this.pageCount;
    this.page = parseInt(this.page);
    this.pageCount = parseInt(this.pageCount);
    if (this.page > this.pageListSize) {
        this.beginPageIndex = this.page - (this.page / this.pageListSize) + 1;
    }
    else {
        this.beginPageIndex = 1;
    }
}
showPages.prototype.createHtml = function() { //生成html代码
    if (this.rowCount <= this.pageSize) return "";
    var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;

    if (prevPage < 1) {
        strHtml += '<a  title="第一页"><img src="../images/page-first.gif" alt="第一页" /></a> ';
        strHtml += '<a  title="上一页"><img src="../images/page-prev.gif" alt="上一页" /></a>';
    } else {
        strHtml += '<a title="第一页" href="#_" onclick="' + this.createUrl(1) + '" ><img src="../images/page-first.gif" alt="第一页" /></a>';
        strHtml += '<a title="上一页" href="#_"  onclick="' + this.createUrl(prevPage) + '"   ><img src="../images/page-prev.gif" alt="上一页" /></a>';
    }
    if (this.beginPageIndex > 1) {
        strHtml += ' <a href="#_" onclick="' + this.createUrl(this.beginPageIndex - 1) + '"  title="">...</a> ';
    }



    for (var i = 1; i <= this.pageCount; i++) {
        if (i > 0) {
            if (i == this.page) {

                strHtml += '<span> ' + i + '</span>';
            } else {

                strHtml += '<a href="#_" onclick="' + this.createUrl(i) + '" title="' + i + '">' + i + '</a>';
            }
        }
    }

    if (this.pageCount - this.beginPageIndex >= this.pageListSize) {
        strHtml += ' <a href="#_" onclick="' + this.createUrl(this.beginPageIndex + this.pageListSize) + '" title="">...</a> ';
    }


    if (nextPage > this.pageCount) {
        strHtml += '<a  title="下一页"><img src="../images/page-next.gif" alt="下一页" /></a>';
        strHtml += '<a  title="最后一页"><img src="../images/page-last.gif" alt="最后一页" /></a>';
    } else {
        strHtml += '<a href="#_" onclick="' + this.createUrl(nextPage) + '"><img src="../images/page-next.gif" alt="下一页" /></a>';
        strHtml += '<a href="#_" onclick="' + this.createUrl(this.pageCount) + '"><img src="../images/page-last.gif" alt="最后一页" /></a>';
    }
    return strHtml;
}
showPages.prototype.createUrl = function(intPageIndex) { //跳转页面的方法
    var strModel = this.name;
    strModel = strModel.replace("#$PageIndex$#", intPageIndex);
    strModel = strModel.replace("#$PageSize$#", this.pageSize);
    return strModel
}

showPages.prototype.GetHtml = function() { //显示html代码

    this.checkPages();
    return this.createHtml();


}
