/**
 * 
 */
/**
 * Created by cole on 2017/3/31.
 */
$.fn.extend({
    "initPage":function(listCount,currentPage,fun){
        var maxshowpageitem = $(this).attr("maxshowpageitem");
        page = new Page(maxshowpageitem);
        if(maxshowpageitem!=null&&maxshowpageitem>0&&maxshowpageitem!=""){
            page.maxshowpageitem = maxshowpageitem;
        }
        var pagelistcount = $(this).attr("pagelistcount");
        if(pagelistcount!=null&&pagelistcount>0&&pagelistcount!=""){
            page.pagelistcount = pagelistcount;
        }

        var pageId = $(this).attr("id");
        page.pageId=pageId;
        if(listCount<0){
            listCount = 0;
        }
        if(currentPage<=0){
            currentPage=1;
        }
        page.setPageListCount(pageId,listCount,currentPage,fun);

    }
});
function Page(maxshowpageitem) {
    this.maxshowpageitem = maxshowpageitem;
}
Page.prototype = {
//  "maxshowpageitem":5,
    "pagelistcount":10,
    /**
     * 
     * @param listCount 
     */
    constructor: Page,
    "initWithUl":function(pageId,listCount,currentPage){

        var pageCount = 1;
        if(listCount>0){
            var pageCount = listCount%this.pagelistcount>0?parseInt(listCount/this.pagelistcount)+1:parseInt(listCount/this.pagelistcount);
        }
        var appendStr = this.getPageListModel(pageCount,currentPage);
        $("#"+pageId).html(appendStr);
    },
    /**
     * 
     * @param listCount 
     * @param currentPage 
     */
    "setPageListCount":function(pageId,listCount,currentPage,fun){
//  	console.log(arguments)
//  	console.log($(this),$(this).attr("maxshowpageitem"))
        listCount = parseInt(listCount);
        currentPage = parseInt(currentPage);
        this.initWithUl(pageId,listCount,currentPage);
        this.initPageEvent(pageId,listCount,fun);

    },
    "initPageEvent":function(pageId,listCount,fun){
        var me = this;
        $("#"+pageId +">li[class='pageItem']").on("click",function(){
            if(typeof fun == "function"){
                fun($(this).attr("page-data"));
            }
            me.setPageListCount(pageId,listCount,$(this).attr("page-data"),fun);
        });
    },
    "getPageListModel":function(pageCount,currentPage){
        var prePage = currentPage-1;
        var nextPage = currentPage+1;
        var prePageClass ="pageItem";
        var nextPageClass = "pageItem";
        if(prePage<=0){
            prePageClass="pageItemDisable";
        }
        if(nextPage>pageCount){
            nextPageClass="pageItemDisable";
        }
        var appendStr ="";
        appendStr+="<li class='"+prePageClass+"' page-data='1' page-rel='firstpage'>&lt;&lt;</li>";
        appendStr+="<li class='"+prePageClass+"' page-data='"+prePage+"' page-rel='prepage'>&lt;</li>";
        var miniPageNumber = 1;
        if(currentPage-parseInt(this.maxshowpageitem/2)>0&&currentPage+parseInt(this.maxshowpageitem/2)<=pageCount){
            miniPageNumber = currentPage-parseInt(this.maxshowpageitem/2);
        }else if(currentPage-parseInt(this.maxshowpageitem/2)>0&&currentPage+parseInt(this.maxshowpageitem/2)>pageCount){
            miniPageNumber = pageCount-this.maxshowpageitem+1;
            if(miniPageNumber<=0){
                miniPageNumber=1;
            }
        }
        var showPageNum = parseInt(this.maxshowpageitem);
        if(pageCount<showPageNum){
            showPageNum = pageCount
        }
        for(var i=0;i<showPageNum;i++){
            var pageNumber = miniPageNumber++;
            var itemPageClass = "pageItem";
            if(pageNumber==currentPage){
                itemPageClass = "pageItemActive";
            }

            appendStr+="<li class='"+itemPageClass+"' page-data='"+pageNumber+"' page-rel='itempage'>"+pageNumber+"</li>";
        }
        appendStr+="<li class='"+nextPageClass+"' page-data='"+nextPage+"' page-rel='nextpage'>&gt;</li>";
        appendStr+="<li class='"+nextPageClass+"' page-data='"+pageCount+"' page-rel='lastpage'>&gt;&gt;</li>";
       return appendStr;

    }
}
