﻿function paging(pageCount, pageNo, functionName, pagingType){    
    var html = "";
    var thisPageNum = 5;
    var showPageNum = 9;
    var preClass = "", nextClass = "", preFun = "", nextFun = "";
    var friClass = "", lastClass = "", friFun = "", lastFun = "";
    if(parseInt(pageNo) < 2){
        preClass = "disable";
        friClass = "disable";
    } else {
        preFun = functionName + "(" + (parseInt(pageNo) - 1) + ");";
        friFun = functionName + "(1);";
    }
    if(parseInt(pageNo) > parseInt(pageCount) - 1){
        nextClass = "disable";
        lastClass = "disable";
    } else {
        nextFun = functionName + "(" + (parseInt(pageNo) + 1) + ");";
        lastFun = functionName + "(" + pageCount + ");";
    }
    if(pagingType == 2){
    
        html += "<div class=\"pager\">";
        html += "<strong class=\"" + preClass + "\" title=\"上一页\" onclick=\"" + preFun + "\">上一页</strong>";
        html += "<strong class=\"" + nextClass + "\" title=\"下一页\" onclick=\"" + nextFun + "\">下一页</strong>";
        html += "</div>";
    } else if(pagingType == 1){
        html += "<div class=\"pager\">";
        html += "<strong class=\"" + friClass + "\" title=\"首页\" onclick=\"" + friFun + "\">首页</strong>";
        html += "<strong class=\"" + preClass + "\" title=\"上一页\" onclick=\"" + preFun + "\">上一页</strong>";
        html += "<strong class=\"" + nextClass + "\" title=\"下一页\" onclick=\"" + nextFun + "\">下一页</strong>";
//        html += "<strong class=\"" + lastClass + "\" title=\"尾页\" onclick=\"" + lastFun + "\">尾页</strong>";
        html += "</div>"; 
    } else if(pagingType == 3){
        var beginPageNo = 1;
        var endPageNo = pageCount;
        if(parseInt(pageNo) > parseInt(thisPageNum)){
            beginPageNo = parseInt(pageNo) - (parseInt(thisPageNum) - 1);
        }
        if((parseInt(beginPageNo) + (parseInt(showPageNum) - 1)) > parseInt(pageCount)){
            endPageNo = pageCount;
        } else {
            endPageNo = parseInt(beginPageNo) + (parseInt(showPageNum) - 1);
        }
        if (pageCount > showPageNum && beginPageNo + showPageNum-1 > pageCount)
        {
            beginPageNo = pageCount - (showPageNum - 1);
        }
        if (beginPageNo > 1 && beginPageNo > endPageNo - (showPageNum - 1))
        {
            beginPageNo = 1;
        }
        html += "<div class=\"pager\">";
        html += "<strong class=\"" + friClass + "\" title=\"首页\" onclick=\"" + friFun + "\">首页</strong>";
        html += "<strong class=\"" + preClass + "\" title=\"上一页\" onclick=\"" + preFun + "\">上一页</strong>";
        if(parseInt(beginPageNo) != 1){
            html += "<a class=\"disable\">...</a>";
        }
        for(var i = parseInt(beginPageNo); i <= parseInt(endPageNo); i++){
            if(parseInt(i) == parseInt(pageNo)){
                html += "<span class=\"active\">" + i + "</span>";
            } else {
                html += "<a onclick=\"" + functionName + "(" + i + ");\">" + i + "</a>";
            }
        }
        if(parseInt(pageCount) > parseInt(thisPageNum) && parseInt(pageCount) != parseInt(endPageNo)){
            html += "<a class=\"disable\">...</a>";
        }
        html += "<strong class=\"" + nextClass + "\" title=\"下一页\" onclick=\"" + nextFun + "\">下一页</strong>";
        html += "</div>";
    } else {
        var beginPageNo = 1;
        var endPageNo = pageCount;
        if(parseInt(pageNo) > parseInt(thisPageNum)){
            beginPageNo = parseInt(pageNo) - (parseInt(thisPageNum) - 1);
        }
        if((parseInt(beginPageNo) + (parseInt(showPageNum) - 1)) > parseInt(pageCount)){
            endPageNo = pageCount;
        } else {
            endPageNo = parseInt(beginPageNo) + (parseInt(showPageNum) - 1);
        }
        if (pageCount > showPageNum && beginPageNo + showPageNum-1 > pageCount)
        {
            beginPageNo = pageCount - (showPageNum - 1);
        }
        if (beginPageNo > 1 && beginPageNo > endPageNo - (showPageNum - 1))
        {
            beginPageNo = 1;
        }
        html += "<div class=\"pager\">";
        html += "<strong class=\"" + friClass + "\" title=\"首页\" onclick=\"" + friFun + "\">首页</strong>";
        html += "<strong class=\"" + preClass + "\" title=\"上一页\" onclick=\"" + preFun + "\">上一页</strong>";
        if(parseInt(beginPageNo) != 1){
            html += "<a class=\"disable\">...</a>";
        }
        for(var i = parseInt(beginPageNo); i <= parseInt(endPageNo); i++){
            if(parseInt(i) == parseInt(pageNo)){
                html += "<span class=\"active\">" + i + "</span>";
            } else {
                html += "<a onclick=\"" + functionName + "(" + i + ");\">" + i + "</a>";
            }
        }
        if(parseInt(pageCount) > parseInt(thisPageNum) && parseInt(pageCount) != parseInt(endPageNo)){
            html += "<a class=\"disable\">...</a>";
        }
        html += "<strong class=\"" + nextClass + "\" title=\"下一页\" onclick=\"" + nextFun + "\">下一页</strong>";
//        html += "<strong class=\"" + lastClass + "\" title=\"尾页\" onclick=\"" + lastFun + "\">尾页</strong>";
        html += "<label>" + pageNo + "/" + pageCount + "</label>";
        html += "</div>";
    }
    return html;
}