function Page(options){
    this.allNums = 0;//在后端读取的总行数
    this.start = 0;
    this.length = options.length || 10;//加载条数
    this.url = options.url; //读取url地址
    this.pageObj = options.pageObj;//翻页按钮容器
    this.showPages =  options.showPages || 6; //显示翻页链接个数
    this.callback = options.callback ;
    this.indexClass = options.indexClass || "page-link";//首页按钮类
    this.prevClass = options.prevClass || "page-link"; //上一页按钮类
    this.nextClass = options.nextClass || "page-link"; //下一页按钮类
    this.endClass = options.endClass || "page-link"; //尾页按钮类
    this.curClass = options.curClass || "page-link"; //当前页按钮类
    this.normalClass = options.normalClass || "page-link"; //普通按钮类
    this.postData = options.postData; //其他post提交数据
}
Page.prototype = {
    constructor:Page,
    /**
     * 计算分页
     * @param page 起始页0,1,2
     */
    calcPage:function (page){
    var pageNums = Math.ceil(this.allNums / this.length); //总页数
        if (pageNums<2) return false;
    var linkHtml = "", endHtml = "", firstHtml = "", that = this;
    var prevPage = (page > 0 ? (page - 1) : 0), nextPage = ( page >= pageNums-1 ? pageNums-1 : page + 1);
    this.pageObj.empty();
    var html = "";
        if (page > 0){
            html = "<a data-start='0' href='javascript:void(0);' class='" + this.indexClass + "' >首页</a>";
            html += "<a data-start='" + prevPage + "' href='javascript:void(0);' class='" + this.prevClass + "' >上一页</a>";
        }

        page++;
        if (page <= this.showPages) { //第一种情况, 当前选择页小于显示页

            for (var i = 1; i <= (this.showPages > pageNums? pageNums: this.showPages); i++) {
                if (i == page) {//处于当前页
                    linkHtml += "<span class='" + this.curClass + "'>" + i + "</span>";
                } else {//正常页
                    linkHtml += "<a href='javascript:void(0);' data-start='" + (i - 1) + "' class='" + this.normalClass + "'>" + i + "</a>";
                }
            }
            if (this.showPages < pageNums) {//超出设定范围
                linkHtml += "<a data-start='" + (pageNums - 1) + "' href='javascript:void(0);' class='" + this.normalClass + "' >...</a>";
            }
        } else if (page > this.showPages && page < pageNums) {//第二种情况
            firstHtml = "<a data-start='0' href='javascript:void(0);' class='" + this.normalClass + "'>...</a>";
            var calcShowEnd = (page + parseInt(this.showPages / 2) ) ; //计算显示结束位置
            var showEnd = calcShowEnd > pageNums? pageNums:calcShowEnd;//实际显示结束位置
            for (var i = (page - Math.ceil(this.showPages / 2)); i <= showEnd; i++) {
                if (i == page) {//处于当前页
                    linkHtml += "<span class='" + this.curClass + "'>" + i + "</span>";
                } else {//正常页
                    linkHtml += "<a href='javascript:void(0);' data-start='" + (i - 1) + "' class='" + this.normalClass + "'>" + i + "</a>";
                }
            }
            if (showEnd < pageNums) {//超出设定范围
                linkHtml += "<a data-start='" + (pageNums - 1)+ "' href='javascript:void(0);' class='" + this.normalClass + "' >...</a>";
            }
        } else {
            firstHtml = "<a data-start='0' href='javascript:void(0);' class='" + this.normalClass + "'>...</a>";
            for (var i = pageNums - this.showPages; i <= pageNums; i++) {
                if (i == page) {//处于当前页
                    linkHtml += "<span class='" + this.curClass + "'>" + i + "</span>";
                } else {//正常页
                    linkHtml += "<a href='javascript:void(0);' data-start='" + (i - 1) + "' class='" + this.normalClass + "'>" + i + "</a>";
                }
            }
        }
        if (page < pageNums){
            endHtml+= "<a data-start='"+nextPage+"' href='javascript:void(0);' class='"+this.nextClass+"'>下一页</a>";
            endHtml+= "<a data-start='"+(pageNums-1)+"' href='javascript:void(0);' class='"+this.endClass+"' >尾页</a>";
        }

        this.pageObj.append(html+firstHtml+linkHtml+endHtml);
        this.pageObj.find("a ").click(function(){
           that.ajaxLoadPage($(this).attr("data-start"));
        });
    },
    /**
     *读取参与者记录函数
     */
    ajaxLoadPage:function (start){
        var that = this;
        $.ajax({
            url:this.url+"loadStart="+start*this.length+"&loadLength="+this.length,
            type:"post",
            async:false,
            dataType:"json",
            data:that.postData,
            success:function(data){
                if (data.state == 0 ){
                    that.allNums = data.amounts;
                    that.calcPage(parseInt(start));
                    that.callback(data);
                }else if (data.state ==1){
                    console.log(data.errmsg);
                }
            }
        });
        return this;
    },
    /**
     * 设置post提交的数据
     * @param data
     * @param type 设置类型，1-替换并且不影响其他参数，2-追加参数, 3-替换和追加(默认),4-强制全部重写
     * @returns {Page}
     */
    setPostData:function(data, type){
        type = type || 3;
        switch (type){
            case 1:
                for(var i in data){
                    if (undefined != this.postData[i]){
                        this.postData[i] = data[i];
                    }
                }
                break;
            case 2:
                for(var i in data){
                    if (undefined == this.postData[i]){
                        this.postData[i] = data[i];
                    }
                }
                break;
            case 3:
                for(var i in data){
                        this.postData[i] = data[i];
                }
                break;
            case 4:
                this.postData = data || this.postData;
            default :
                return this;
        }
        return this;
    },
    setUrl:function(url){
        this.url = url || this.url;
        return this;
    }
};