﻿var ETNGpager = function(pName, dstName, cntPP, cntPS, objArray) {
    this.pName = pName; //分页代码存放位置
    this.dstName = dstName;
    this.curP = 1; //默认当前页为第一页
    this.cntPP = cntPP || 2; //默认每页两条纪录
    this.cntPS = cntPS || 3; //默认每页显示5个分页上下文
    this.objArray = objArray;
    this.items = [];
    this.showPNP = true; /*显示上下页链接*/
    this.showType = true; /*滑动分页*/
    this.result = { pagedata: [], pagebar: '', limit: [0, 0], report: '' };
    this.parse(); /*总纪录数*/
}
ETNGpager.prototype.page = function() {
    this.cntP = Math.ceil(this.cntR / this.cntPP); /*总页数*/
    this.cntS = Math.ceil(this.cntP / this.cntPS); /*总段数*/
    this.curS = Math.ceil(this.curP / this.cntPS); /*当前段*/
    this.preP = this.curP - 1; /*上一页*/
    this.nextP = this.curP + 1; /*下一页*/
    this.preS = this.curS - 1; /*上一段*/
    this.nextS = this.curS + 1; /*下一段*/
    this.startR = (this.curP - 1) * this.cntPP + 1; /*起始纪录*/
    this.endR = (this.curP * this.cntPP > this.cntR) ? this.cntR : this.curP * this.cntPP; /*结束纪录*/
    this.result['pagedata'] = [];
    if (this.showType) {
        this.perSide = Math.floor(this.cntPS / 2);
        this.startP = (this.curP > this.perSide) ? (this.curP - this.perSide) : 1;
        this.endP = (this.startP + this.cntPS) > this.cntP ? this.cntP : (this.startP + this.cntPS);
    } else {
        this.startP = (this.curS - 1) * this.cntPS + 1;
        this.endP = (this.curS * this.cntPS > this.cntP) ? this.cntP : (this.curS * this.cntPS);
    }
    for (var i = this.startP; i <= this.endP; i++) {
        this.result['pagedata'].push((i == this.curP) ? '<span class="curPage">' + i + '</span>' : '<span style="cursor:hand;" onclick="page(' + i + ')">' + i + '</span>');
    }
    if (this.showPNP) {
        if (this.curP > 1) this.result['pagedata'].unshift('<span style="cursor:hand;"  onclick="page(1)"><<</span>&nbsp;&nbsp;<span style="cursor:hand;"  onclick="page(' + (this.curP - 1) + ')"><</span>');
        else this.result['pagedata'].unshift('<span style="color:#999999;"><<</span>&nbsp;&nbsp;<span style="color:#999999;"><</span>');
        if (this.curP < this.cntP) this.result['pagedata'].push('<span style="cursor:hand;" onclick="page(' + (this.curP + 1) + ')">></span>&nbsp;&nbsp;<span style="cursor:hand;" onclick="page(' + this.endP + ')">>></span>');
        else this.result['pagedata'].push('<span style="color:#999999;">></span>&nbsp;&nbsp;<span style="color:#999999;">>></span>');
    }
    this.result['pagebar'] = this.result['pagedata'].join('&nbsp;&nbsp;');
    this.result['limit'] = [this.startR, this.endR];
}
ETNGpager.prototype.parse = function() {
    for (var i = 0; i < this.objArray.length; i++) {
        this.items[this.items.length] = "<td style=\"width:50px;\"><img src='images/m_r1_c2.jpg' /></td><td style=\"width:350px; height:30px\"><a  href='" + this.objArray[i].NewsUrl + "' target='_blank'>" + this.objArray[i].NewsTitle + "</a></td><td>" + this.objArray[i].CreateDate + "</td>";
    }
    this.cntR = this.items.length;
    return this.items.length;
}
ETNGpager.prototype.create = function() {
    this.page();
    document.getElementById(this.dstName).innerHTML = '<table><tr>' + this.items.slice(this.startR - 1, this.endR).join('</tr><tr>') + '</tr></table>';
    document.getElementById(this.pName).innerHTML = '<span class="ctrlPages">' + this.result['pagebar'] + this.result['report'] + '</span>';
}
