﻿/*
** 2009-6-16 write for www.90jy.com
** Author:      linxi5167@hotmail.com
** Version:     1.0.090618
*/


function Pager(pageSize, recordCount, destPage, _condition, callBack) {
    this.pagesize = pageSize;
    this.recordcount = recordCount;
    this.destpage = destPage;
    this.temppage = this.destpage;
    this.callback = callBack;
    this.condition = _condition;

    this.interval = 5;
    this.absdiff = (this.interval - 1) / 2;

    this.pagecount = Math.ceil(this.recordcount / this.pagesize);
    if (this.destpage > this.pagecount)
        this.destpage = this.pagecount;

    this.create = function() {
        return function() {
            this.renderHtml.apply(this, arguments);
        }
    }


    if (this.pagecount == 0)
        this.pagecount = 1;

    if (this.destpage == 0)
        this.destpage = 1;
    
    
}

Pager.prototype.renderHtml = function() {
    var _container = arguments[0];
    _container.innerHTML = "";
    _container.appendChild(document.createTextNode(String.format("共{0}条数据 {1} / {2}页 ",
                this.recordcount, this.destpage, this.pagecount)));

    // 第一页
    var firstA = document.createElement("A");

    firstA.appendChild(document.createTextNode("首页"));
    firstA.setAttribute("href", "javascript:void(0)");
    firstA.onclick = pagerDelegate(this, this.callback, { "mode": "first" });

    _container.appendChild(firstA);
    _container.appendChild(document.createTextNode(" "));

    // 上一页
    var previousA = document.createElement("A");

    previousA.appendChild(document.createTextNode("上一页"));
    previousA.setAttribute("href", "javascript:void(0)");
    previousA.onclick = pagerDelegate(this, this.callback, { "mode": "previous" });

    _container.appendChild(previousA);
    _container.appendChild(document.createTextNode(" "));

    // 下一页
    var nextA = document.createElement("A");

    nextA.appendChild(document.createTextNode("下一页"));
    nextA.setAttribute("href", "javascript:void(0)");
    nextA.onclick = pagerDelegate(this, this.callback, { "mode": "next" });

    _container.appendChild(nextA);
    _container.appendChild(document.createTextNode(" "));

    // 末页
    var lastA = document.createElement("A");

    lastA.appendChild(document.createTextNode("末页"));
    lastA.setAttribute("href", "javascript:void(0)");
    lastA.onclick = pagerDelegate(this, this.callback, { "mode": "last" });

    _container.appendChild(lastA);
    _container.appendChild(document.createTextNode(" "));

}

Pager.prototype.renderNumberStyleHtml = function() {
    var _container = arguments[0];
    _container.innerHTML = "";

    _container.appendChild(document.createTextNode(String.format("共{0}条 {1}/{2}页 ",
        this.recordcount, this.destpage, this.pagecount)));

    // 第一页
    var firstA = document.createElement("A");

    firstA.appendChild(document.createTextNode("首页"));
    firstA.setAttribute("href", "javascript:void(0)");
    firstA.onclick = pagerDelegate(this, this.callback, { "mode": "first" });

    _container.appendChild(firstA);
    _container.appendChild(document.createTextNode(" "));

    // 上一页
    var previousA = document.createElement("A");

    previousA.appendChild(document.createTextNode("..."));
    previousA.setAttribute("href", "javascript:void(0)");
    previousA.onclick = pagerDelegate(this, this.callback, { "mode": "previous" });

    _container.appendChild(previousA);
    _container.appendChild(document.createTextNode(" "));

    // 此处开始渲染中间页码串
    if (this.destpage + this.absdiff > this.interval && this.destpage + this.absdiff <= this.pagecount)
        this.generateNumsText(this.destpage - this.absdiff, this.destpage + this.absdiff, _container);
    else if (this.destpage + this.absdiff <= this.interval)
        this.generateNumsText(1, this.interval, _container);
    else if (this.destpage + this.absdiff > this.pagecount)
        this.generateNumsText(this.pagecount - this.interval + 1, this.pagecount, _container);

    // 下一页
    var nextA = document.createElement("A");

    nextA.appendChild(document.createTextNode("..."));
    nextA.setAttribute("href", "javascript:void(0)");
    nextA.onclick = pagerDelegate(this, this.callback, { "mode": "next" });

    _container.appendChild(nextA);
    _container.appendChild(document.createTextNode(" "));

    // 末页
    var lastA = document.createElement("A");

    lastA.appendChild(document.createTextNode("末页"));
    lastA.setAttribute("href", "javascript:void(0)");
    lastA.onclick = pagerDelegate(this, this.callback, { "mode": "last" });

    _container.appendChild(lastA);
    _container.appendChild(document.createTextNode(" "));

    // 页码输入框
    var txtGo = document.createElement("INPUT");

    txtGo.setAttribute("type", "text");
    txtGo.setAttribute("name", "gopage");
    txtGo.setAttribute("id", "gopage");
    txtGo.setAttribute("size", "2");
    txtGo.setAttribute("value", this.destpage);
    txtGo.onchange = pagerDelegate(this, this.handleTextChanged, { "objRef": txtGo });

    _container.appendChild(txtGo);
    _container.appendChild(document.createTextNode(" "));

    // Go按钮
    var btnGo = document.createElement("INPUT");

    btnGo.setAttribute("type", "button");
    btnGo.setAttribute("value", "Go");
    btnGo.setAttribute("size", "2");
    btnGo.onclick = pagerDelegate(this, this.callback, { "mode": "inputnums" });

    _container.appendChild(btnGo);
    _container.appendChild(document.createTextNode(" "));

}

Pager.prototype.moveIndicator = function() {
    if (arguments.length != 1 || arguments[0] == null || arguments[0].mode == null)
        return;

    switch (arguments[0].mode) {
        case "first":
            this.destpage = 1;

            break;
        case "previous":
            this.destpage -= 1;

            if (this.destpage < 1)
                this.destpage = 1;

            break;
        case "next":
            this.destpage += 1;

            if (this.destpage > this.pagecount)
                this.destpage = this.pagecount;

            break;
        case "last":
            this.destpage = this.pagecount;

            break;
        case "nums":
            this.destpage = arguments[0].val;

            break;
        case "inputnums":
            this.destpage = this.temppage;

            break;
    }

    if (this.condition && this.condition.destpage)
        this.condition.destpage = this.destpage;
}

Pager.prototype.setRecordCount = function() {
    this.recordcount = arguments[0];

    this.pagecount = Math.ceil(this.recordcount / this.pagesize);
    if (this.destpage > this.pagecount)
        this.destpage = this.pagecount;

    if (this.pagecount == 0)
        this.pagecount = 1;

    if (this.destpage == 0)
        this.destpage = 1;
}

Pager.prototype.setInterval = function() {
    this.interval = arguments[0];
    this.absdiff = (this.interval - 1) / 2;
}

Pager.prototype.generateNumsText = function() {
    var _container = arguments[2];
    if (arguments[0] < 1)
        arguments[0] = 1;
    if (arguments[1] > this.pagecount)
        arguments[1] = this.pagecount;

    for (var i = arguments[0]; i <= arguments[1]; i++) {
        var numsA = document.createElement("A");

        if (i == this.destpage)
            numsA.appendChild(document.createTextNode(String.format("[ {0} ]", i)));
        else
            numsA.appendChild(document.createTextNode(i.toString()));

        numsA.setAttribute("href", "javascript:void(0)");
        numsA.onclick = pagerDelegate(this, this.callback, { "mode": "nums", "val": i });

        _container.appendChild(numsA);
        _container.appendChild(document.createTextNode(" "));
    }
}

Pager.prototype.handleTextChanged = function() {
    this.temppage = arguments[0].objRef.value;
}