﻿// FirstPageIndex = 1

var PagerButtonType = {
    _Default : 0 ,
    _FirstPage : -1 , 
    _PrevPage : -2 ,
    _CurrentPage : -3,
    _NextPage : -4 ,
    _LastPage : -5
}

function getUCount() {
    $.ajax({
        type: "GET",
        url: "../Service/GetDownLoadCount.aspx?ab=" + ToolId + "&rnd=" + Math.random(),
        success: function(msg) {
            document.getElementById("spUCount").innerHTML = msg;
        }
    });
}

var PagerCreater = function(fitter, recordCount, pageSize, buttonCount, btnLink) {
    function $id(id) { return (typeof id == "string") ? document.getElementById(id) : id; }
    this.Fitter = $id(fitter);
    this.Init(recordCount, pageSize, buttonCount);
    this.PageIndex = 1;
    this.CurrentClass = "current";
    this.NormalClass = "";
    this.DisableClass = "disabled";
    this.ButtonHref = btnLink ? btnLink : "javascript:void(0)";
    this.OnPageChang = function(dom, index) { this.Load(index); };
    this.OnLoaded = function() {  };
    this.BeforeLoad = function() { return true; };
}

PagerCreater.prototype.Init = function(recordCount, pageSize, buttonCount) {
    this.RecordCount = recordCount;
    this.ButtonCount = buttonCount;
    this.PageSize = pageSize;
    this.PageCount = Math.ceil(recordCount / pageSize); 
}

PagerCreater.prototype.Load = function(pageindex) {
    if (this.RecordCount <= 0) { return; }
    this.PageIndex = pageindex;
    if (this.BeforeLoad() == false) {
        return;
    }
    
    var sender = this;
    var pagesdom = new Array();
    if (this.PageCount <= this.ButtonCount) {
        pagesdom.push(this.GetButtonDom(PagerButtonType._FirstPage, pageindex - 1, '前页', pageindex != 1));
        for (var i = 1; i <= this.PageCount; i++) {
            pagesdom.push(this.GetButtonDom(PagerButtonType._Default, i, i, true));
        }
        pagesdom.push(this.GetButtonDom(PagerButtonType._FirstPage, pageindex + 1, '下页', pageindex != this.PageCount));
    } else {
        var starindex = 0;
        var endindex = 0;
        var halfCount = ((this.ButtonCount % 2 == 0 ? this.ButtonCount : this.ButtonCount - 1) / 2);
        starindex = Math.max(1, pageindex - halfCount);
        endindex = Math.min(this.PageCount + 1, starindex + this.ButtonCount);
        starindex = Math.min(starindex, endindex - this.ButtonCount);
        pagesdom.push(this.GetButtonDom(PagerButtonType._FirstPage, pageindex - 1, '前页', pageindex != 1));
        for (var i = starindex; i < endindex; i++) {
            pagesdom.push(this.GetButtonDom(PagerButtonType._Default, i, i, true));
        }
        pagesdom.push(this.GetButtonDom(PagerButtonType._FirstPage, pageindex + 1, '下页', pageindex != this.PageCount));
    }
    this.Fitter.innerHTML = "";
    for (var item in pagesdom) {
        this.Fitter.appendChild(pagesdom[item]);
    }
    this.OnLoaded();
}

PagerCreater.prototype.GetButtonDom = function(type, pageindex, text, enable) {
    var sender = this;
    var Dom;
    var tagName = "a";
    if (pageindex == this.PageIndex) type = PagerButtonType._CurrentPage;

    if (type == PagerButtonType._CurrentPage) {
        tagName = "span";
    }

    Dom = document.createElement(tagName);
    if (type == PagerButtonType._CurrentPage) {
        Dom.className = this.CurrentClass;
    } else {
        Dom.href = this.ButtonHref;
        if (!enable) {
            Dom.className = this.DisableClass;
        } else {
            Dom.className = this.NormalClass;
            Dom.onclick = function() { sender.OnPageChang(this,pageindex); };
        }
    }

    Dom.innerHTML = text ? text : pageindex;

    return Dom;
}
