﻿//PageIndex:当前页,PageCount：页总数,pageSize：页大小,fun函数名（注意函数默认带页参数）
function GetPager(PageIndex,PageCount,pageSize,fun)
{
    var _strsb = "";
    var _intLoopStart = "0";
    var _intLoopEnd = "0";
    var _strTempUrl = "";
    var _strPage = "<div class=\"pageD\">";
        if (PageCount == 0)
        {
            PageCount = 1;
        }
        
        if (PageIndex == 0)
        {
            PageIndex = 1;
        }

        if (PageIndex > PageCount)
        {
            PageIndex = PageCount;
        }

        if (PageIndex < (pageSize/2)+1)
        {
            _intLoopStart = 1;
            _intLoopEnd = _intLoopStart + pageSize - 1;
        }
        else if (PageIndex>PageCount-(pageSize/2))
        {
            _intLoopStart = PageCount - pageSize + 1;
            _intLoopEnd = PageCount;
        }
        else
        {
            _intLoopStart = PageIndex - (pageSize / 2);
            _intLoopEnd = _intLoopStart + pageSize - 1;
        }
        
        if (_intLoopStart <= 0)
        {
            _intLoopStart = 1;
        }
        
        _intLoopStart = QuZheng(_intLoopStart);
        _intLoopEnd = QuZheng(_intLoopEnd);
        for (i = _intLoopStart; i <= _intLoopEnd; i++)
        {
            if (i > PageCount)
            {
                break;
            }
            
            if (i == PageIndex)
            {
                _strsb += i + "&nbsp;&nbsp;";
            }
            else
            {
                _strsb += "<a href=\"javascript:" + fun + "(" + i +")\">" + i + "</a>&nbsp;&nbsp;";
            }
        }
        
        if (PageIndex - 2 > 0)
        {
            _strPage += "<a href=\"javascript:" + fun + "(1)\">首页</a>&nbsp;&nbsp;";
        }
        
        if (PageIndex - 1 > 0)
        {
            _strPage += "<a href=\"javascript:" + fun + "("+(PageIndex - 1)+")\">上一页</a>&nbsp;&nbsp;";
        }
        
        _strPage += _strsb;
        if (PageCount - PageIndex > 0)
        {
            _strPage += "<a href=\"javascript:" + fun + "("+(PageIndex + 1)+")\">下一页</a>&nbsp;&nbsp;";
        }
        
        if (PageCount - PageIndex > 1)
        {
            _strPage += "<a href=\"javascript:" + fun + "("+PageCount+")\">末页</a>&nbsp;&nbsp;";
        }
        
        _strPage +="</div>";
        if (PageCount <= 1)
        {
            return "";
        }
        else
        {
            return _strPage;
        }
}

function QuZheng(Num)
{
    var ret = Math.round(Num);
    if(ret > Num)
    {
        return (ret - 1)
    }
    return Num;
}


