﻿function Pager(pageSize, recoundCount) {
    this.PageSize = pageSize;
    this.RecoundCount = recoundCount;
    this.TotalPage = Math.ceil(recoundCount / pageSize);
}

Pager.prototype.CurrentPage = 1;

Pager.prototype.TotalPage = 1;

Pager.prototype.HaveQueryString = false;

Pager.prototype.SetCurrentPage = function() {
    if (location.search != '') {
        //        var found = false;
        //从第二个字符后开始取串
        var locationSearch = location.search.substring(1);
        
        var arrQueryString = locationSearch.split("&");
        for (var i = 0; i < arrQueryString.length; i++) {
            var tempArray = arrQueryString[i].split("=");
            if (tempArray[0] == "p") {
                this.CurrentPage = parseInt(tempArray[1]);
                this.HaveQueryString = true;
                break;
            }
        }
        if (!this.HaveQueryString) {
            this.CurrentPage = 1;
        }
    }
    else {
        this.CurrentPage = 1;
    }
}

//Pager.prototype.HaveQueryString = function() {
//    var locationSearch = location.search.substring(1);
//    var arrQueryString = locationSearch.split("&");
//    for (var i = 0; i < arrQueryString.length; i++) {
//        var tempArray = arrQueryString[i].split("=");
//        if (tempArray[0] == "p") {
//            return true;
//            break;
//        }
//    }
//    return false;
//}

Pager.prototype.Bind = function() {
    this.SetCurrentPage();
    var fp = document.getElementById("pF");
    var pp = document.getElementById("pP");
    var np = document.getElementById("pN");
    var lp = document.getElementById("pL");
    if (this.CurrentPage == this.TotalPage) {
        np.style.visibility = 'hidden';
        lp.style.visibility = 'hidden';
    }
    if (this.CurrentPage == 1) {
        fp.style.visibility = 'hidden';
        pp.style.visibility = 'hidden';
    }

    //document.getElementById("pTotalPage").innerText = this.TotalPage;

    document.getElementById("pCurrentPage").innerText = this.CurrentPage;
    document.getElementById("pTotalPage").innerHTML = this.TotalPage;

    var p = this;
    fp.onclick = function() {
        location.href = "?p=1";
    }

    pp.onclick = function() {
        if (location.search == '') {
            location.href = "?p=" + (p.CurrentPage - 1);
        }
        else {
            //?op=asdfasdf
            if (!p.HaveQueryString) {
                location.href = location.search + "&p=" + (p.CurrentPage - 1);
            }
            else {
                location.href = location.search.replace("&p=" + p.CurrentPage, "&p=" + (p.CurrentPage -1)).replace("?p=" + p.CurrentPage, "?p=" + (p.CurrentPage - 1));
            }
        }
    }

    np.onclick = function() {
        if (location.search == '') {
            location.href = "?p=" + (p.CurrentPage + 1);
        }
        else {
            //?op=asdfasdf
            if (!p.HaveQueryString) {
                location.href = location.search + "&p=" + (p.CurrentPage + 1);
            }
            else {
                location.href = location.search.replace("&p=" + p.CurrentPage, "&p=" + (p.CurrentPage + 1)).replace("?p=" + p.CurrentPage, "?p=" + (p.CurrentPage + 1));
            }
        }
    }
    lp.onclick = function() {
        location.href = "?p=" + p.TotalPage;
    }
}
