/*
 * clientPage 1.0 - a webpage UI client pagination widget written in javascript
 * Last modify:2009-02-10
 * 增加跳转链接及当前记录数的描述
 * 如果无记录数则不显示分页
 */
(function($) {
    $.fn.zzPagination = function(recordTotolCount, opts) {
        opts = jQuery.extend({
            perPageCount: 8,                  //每页显示多少记录
            displayPageCount: 10,              //显示多少页数
            currentPage: 0,                    //当前页页码
            startEdgeCount: 0,                 //前面边缘页显示的数量
            endEdgeCount: 0,                   //后面边缘页显示的数量
            displayLink: "#", //链接显示 
            firstPageText: "首页",              //首页文本
            lastPageText: "尾页",               //尾页文本
            prevPageText: "前页",             //前一页显示的文本
            nextPageText: "后页",             //下一页显示的文本
            jumpLinkButtonImage: '',            //跳转按钮的图片
            ellipseText: "...",                //隐藏的页显示方式
            prevPageAlwaysShow: true,          //前一页总显示
            nextPageAlwaysShow: true,          //后一页总显示
            firstPageAlwaysShow: true,         //首页总显示
            lastPageAlwaysShow: true,          //尾页总显示
            jumpLinkAlwaysShow: true,          //跳转链接是否显示
            pageSizeAlwayShow: true,            //是否显示类似于 "显示第 x-x条" 描述
            displayMark: true,               //显示类似于 1/30 这样的东西
            needPageNumber: true,              //需要中间的页码
            callbackEvent: function() {        //回调函数
                return false;
            }
        },
        opts || {});

        if (recordTotolCount <= opts.perPageCount) {
            return;
        }

        return this.each(function() {
            var parent = $(this);

            function pageSelected(pageId, evt) {
                opts.currentPage = pageId;

                drawPagination();

                var continuePropagation = opts.callbackEvent(pageId, parent);
                if (!continuePropagation) {
                    if (evt.stopPropagation) {
                        evt.stopPropagation();
                    } else {
                        evt.cancelBubble = true;
                    }
                }

                //window.location.hash = pageId+1;

                return continuePropagation;
            }

            //计算总页数
            function getTotalPageCount() {
                return Math.ceil(recordTotolCount / opts.perPageCount);
            }

            //获取2个边缘应该显示的数量
            function getEdgeDisplayCount() {
                var middlePoint = opts.displayPageCount / 2;
                var totalCount = getTotalPageCount();

                var startEdgeCount = (opts.currentPage < middlePoint) ? 0 : Math.max(Math.min(opts.currentPage - middlePoint, totalCount - opts.displayPageCount), 0);

                var endEdgeCount = (opts.currentPage < middlePoint) ? Math.min(totalCount, opts.displayPageCount) : Math.min(totalCount, opts.currentPage + middlePoint);

                return [startEdgeCount, endEdgeCount];
            }
            //绘制分页条
            function drawPagination() {
                function appendSubItem(pageId, showText, className) {
                    var totalCount = getTotalPageCount();
                    pageId = pageId < 0 ? 0 : (pageId < totalCount ? pageId : totalCount - 1);

                    var node;
                    if (pageId == opts.currentPage) {
                        node = $("<span class='selected'>" + (showText) + "</span>");
                    }
                    else {
                        node = $("<a style='cursor:pointer;'>" + (showText) + "</a>");
                        //$(node).attr("href", opts.displayLink);
                        
                        node.bind("click", getCallbackHandler(pageId));
                        node.addClass(className);
                    }

                    parent.append(node);
                }

                function getCallbackHandler(pageId) {
                    return function(evt) {
                        return pageSelected(pageId, evt);
                    }
                }
                function getJumpCallbackHandler(input) {
                    return function(evt) {
                        var pageId = input.val() * 1 - 1;
                        pageId = pageId < 0 ? 0 : (pageId < getTotalPageCount() ? pageId : getTotalPageCount() - 1);
                        return pageSelected(pageId, evt);
                    }
                }

                parent.empty();
                var edgeCount = getEdgeDisplayCount();
                var totalCount = getTotalPageCount();
                //生成当前页码的提示文字
                if (opts.pageSizeAlwayShow) {
                    if ((opts.currentPage + 1) * opts.perPageCount > recordTotolCount) {
                        parent.append($("<span>当前第 <strong>" + (opts.currentPage * opts.perPageCount + 1) + "-" + recordTotolCount + "</strong> 条</span>"));
                    }
                    else {
                        parent.append($("<span>当前第 <strong>" + (opts.currentPage * opts.perPageCount + 1) + "-" + (opts.currentPage + 1) * opts.perPageCount + "</strong> 条</span>"));
                    }
                }
                //生成首页
                if (opts.firstPageAlwaysShow == true) {
                    appendSubItem(0, opts.firstPageText, "prePage");
                }

                //生成前一页
                if (opts.prevPageAlwaysShow == true) {
                    appendSubItem(opts.currentPage - 1, opts.prevPageText, "prePage");
                }

                if (opts.needPageNumber == true) {
                    //生成前边缘页
                    if ((edgeCount[0] > 0) && (opts.startEdgeCount > 0)) {
                        var startEngeCount = Math.min(edgeCount[0], opts.startEdgeCount);

                        for (var i = 0; i < startEngeCount; i++) {
                            appendSubItem(i, i + 1);
                        }
                        if (opts.startEdgeCount < edgeCount[0]) {
                            $("<span>" + opts.ellipseText + "</span>").appendTo(parent)
                        }
                    }

                    //======正常部分生成开始======
                    for (var i = edgeCount[0]; i < edgeCount[1]; i++) {
                        appendSubItem(i, i + 1);
                    }
                    //======正常部分生成完毕======

                    //生成尾边缘页
                    if (edgeCount[1] < totalCount && opts.endEdgeCount > 0) {
                        if (totalCount - opts.endEdgeCount > edgeCount[1]) {
                            $("<span>" + opts.ellipseText + "</span>").appendTo(parent)
                        }
                        var endEdgeCount = Math.max(totalCount - opts.endEdgeCount, edgeCount[1]);
                        for (var i = endEdgeCount; i < totalCount; i++) {
                            appendSubItem(i, i + 1);
                        }
                    }
                }

                //生成下一页
                if (opts.nextPageAlwaysShow == true) {
                    appendSubItem(opts.currentPage + 1, opts.nextPageText, "nxtPage");
                }

                //生成尾页
                if (opts.lastPageAlwaysShow == true) {
                    appendSubItem(totalCount - 1, opts.lastPageText, "nxtPage");
                }

                if (opts.displayMark == true) {
                    $("<span class='pageMarket'>" + (opts.currentPage + 1) + "/" + totalCount + "</span>").appendTo(parent);
                }

                //生成跳转链接
                if (opts.jumpLinkAlwaysShow) {
                    var pageJumpSpan = $('<span class="pageJump"></span>');
                    pageJumpSpan.append('<input class="jumpInput" type="text" size="2" value="' + (opts.currentPage + 1) + '" /> ');
                    var goButton;
                    if (opts.jumpLinkButtonImage == '') {
                        goButton = $('<input class="jumpButton" type="button" value="go" />');
                    }
                    else {
                        goButton = $('<img class="jumpButton" align="center" src="' + opts.jumpLinkButtonImage + '" />');
                    }
                    goButton.bind('click', getJumpCallbackHandler(pageJumpSpan.find('input')));
                    pageJumpSpan.find('.jumpInput').keyup(
                        function(evt) {
                            if (evt.keyCode == 13) {
                                goButton.trigger('click');
                            }
                        }
                    );
                    pageJumpSpan.append(goButton);
                    parent.append(pageJumpSpan);
                }
            }

            drawPagination();
        });
    }
})(jQuery);