(function($, win) {
    $.fn.wrpage = function(options) {
        if (options.pagesize <= 0) {
            return
        }
        var defaults = { //defaults 使我们设置的默认参数。
            pagesize: 10,
            wr_current: 1,
            baseUrl: './',
            cb: function() {}
        };
        var options = $.extend(defaults, options); //将传入参数和默认参数合并
        var size = Number(options.pagesize);
        var cur = Number(options.wr_current)
        var baseUrl = options.baseUrl
        var cb = options.cb
        var em = $(this);

        init(size, cur)

        // 生成要显示的页码：0 代表省略号。
        // 旧实现在 cur>=5 时不输出第 1 页、size==6 时从 2 开始循环，
        // 都会让第 1 页从页码区消失；这里保证第 1 页和最后一页始终在。
        function pageList(size, cur, span) {
            var out = [], i;
            if (size <= span + 2) {          // 页数不多，直接全部列出来
                for (i = 1; i <= size; i++) out.push(i);
                return out;
            }
            var half  = Math.floor(span / 2);
            var start = cur - half;
            var end   = cur + half - 1;
            if (start < 2) { start = 2; end = start + span - 1; }
            if (end > size - 1) { end = size - 1; start = end - span + 1; }
            if (start < 2) start = 2;
            out.push(1);
            if (start > 2) out.push(0);
            for (i = start; i <= end; i++) out.push(i);
            if (end < size - 1) out.push(0);
            out.push(size);
            return out;
        }

        function init(size, cur) {
            var _html = ""
            // 头：首页 + 上一页
            if (cur > 1) {
                _html += '<a href="javascript:;" class="wr_pagefirst bg-highlight color-white">&lt;&lt;</a>'
                _html += '<a href="javascript:;" class="wr_pagepre bg-highlight color-white">&lt;</a>'
            }
            // 中间：页码窗口（第 1 页与最后一页始终在列）
            var list = pageList(size, cur, 6), i, n
            for (i = 0; i < list.length; i++) {
                n = list[i]
                if (n === 0) {
                    _html += '<span class="wr_dotted">...</span>'
                } else if (n == cur) {
                    _html += '<a href="javascript:;" class="curwrpage bg-highlight opacity-50 color-white">' + n + '</a>'
                } else {
                    _html += '<a href="javascript:;" class="wrpage_number bg-highlight color-white">' + n + '</a>'
                }
            }
            // 尾：下一页 + 尾页
            if (cur < size) {
                _html += '<a href="javascript:;" class="wr_pagenext bg-highlight color-white">&gt;</a>'
                _html += '<a href="javascript:;" class="wr_pageend bg-highlight color-white">尾</a>'
            }
            em.append(_html)
        }

        function jumpPage(page){
            window.location.href = baseUrl.replace(/currentPage/, page);
        }

        em.on("click", "a.wrpage_number", function() {
            jumpPage($(this).text());
            em.empty($(this).text())
            init(size, $(this).text());
            cb($(this).text())

        });
        em.on("click", "a.wr_pagefirst", function() {
            jumpPage(1);
            em.empty()
            init(size, 1);
            cb(1)
        });
        em.on("click", "a.wr_pageend", function() {
            jumpPage(size);
            em.empty()
            init(size, size);
            cb(size)
        });
        em.on("click", "a.wr_pagenext", function() {
            var cu = parseInt(em.children("a.curwrpage").text());
            if (!(cu < size)) return;
            jumpPage(cu+ 1);
            em.empty()
            init(size, cu + 1);
            cb(cu + 1)
        });
        em.on("click", "a.wr_pagepre", function() {
            var cu = parseInt(em.children("a.curwrpage").text());
            if (!(cu > 1)) return;
            em.empty()
            jumpPage(cu - 1);
            init(size, cu - 1);
            cb(cu - 1)
        });
    }
})(jQuery, window)
