﻿String.prototype.Trim = function() {
    return this.replace(/(^s*)|(s*$)/g, "");
}

//Ajax获取信息并自动创建Ajax分页
function getPageList(jMsgCon,jPageCon,url,pageSize,createPage,callBackFunction)
{
	this.msgCon=jMsgCon;
	this.msgTemplate=this.msgCon.html();
	this.pageCon=jPageCon;
	this.url=url;	
	this.createPage=createPage;//是否分页		
	this.pageSize=pageSize;
	var resultData;//返回的数据结果	
	var data;//留言数据
	this.callBackFunction=callBackFunction;
	var thisObj=this;
	//初始化
	
	this.loadData=function(pageIndex)
	{
		ajaxLoding.call(thisObj);
		getData.call(thisObj,pageIndex);
	}
	
	function ajaxLoding()
	{
	    //this.msgCon.html("<div style='height:50px; line-height:50px; text-align:center; font-size:14px;'>数据正在加载中...</div>");
	    if ($('#CommentListContent').size() == 0) {
	        jMsgCon.after('<span id="CommentListContent"></span>');
	    }
	    $('#CommentListContent').html("<div style='height:50px; line-height:50px; text-align:center; font-size:14px;'>数据正在加载中...</div>");
	}
	
	function ajaxLoded()
	{		
	}
	
	function getData(pageIndex)
	{
		pageSize=this.pageSize
		dataFormat=this.dataFormat;	
		callBackFunction=this.callBackFunction;
		var type="getpagelist";
		
		var url2=this.url+"&type="+type+"&page="+pageIndex+"&pageSize="+pageSize+"&callBack=?"
		
		var msgCon=this.msgCon;
		var pageCon=this.pageCon;
		
		$.getJSON(url2,function(data){		 
			setPageBar.call(thisObj,data,url2);			
			callBackFunction(data);
		})
	}
	
	//设置分页导航
	function setPageBar(data,requestUrl)
	{
		var pageHtml=GetPageHtml(data.recordCount,data.pageSize,8,data.currentPage,getData,requestUrl,true);
		if(this.createPage)
		{
			this.pageCon.html()
		}
	}
}

function changePageIndex(pageIndex)
{	
	pageDataObj.loadData(pageIndex);	
}

//分页类
function GetPageHtml(recordCount,pageSize,groupCount,currentPage,requestUrl,allowShow)
{
	var pageHtml="";
	if(currentPage<=0)currentPage=1;
	pageCount=Math.ceil(recordCount/pageSize);//总页数
	if(pageCount==0 && !allowShow)return pageHtml;
	
	var fristNum=currentPage-Math.ceil(groupCount/2);//第一个分页数值	
	if(fristNum<=1)fristNum=1;	
	var endNum=fristNum+groupCount;
	if(endNum>pageCount)endNum=pageCount;
	
	var prePage=currentPage-1; 
	if(prePage<=0)prePage=1;
	var nexPage=currentPage+1;	
	if(nexPage>=pageCount){nexPage=pageCount;}
	if (pageCount >= groupCount && fristNum + groupCount >= endNum) fristNum = endNum - groupCount;
	if (fristNum <= 1) fristNum = 1;
	
	return createPageBar();
	
	function createPageBar()
	{
		pageHtml="共有["+recordCount+"]条记录 "+(currentPage+"/"+pageCount);
		if(currentPage==1)
		{
			pageHtml+=(" &lt; ");
		}
		else
		{
			pageHtml+=(" <span style='cursor:pointer;' onclick='changePageIndex(\""+prePage+"\")'>&lt;</span> ");
		}
		
		for(i=fristNum;i<=endNum;i++)
		{
			pageHtml+=createNumInPage(i);
			if(i!=endNum){pageHtml+="|";}
		}
		
		if(currentPage==pageCount)
		{
			pageHtml+=(" &gt ");
		}
		else
		{
			pageHtml+=(" <span style='cursor:pointer;' onclick='changePageIndex(\""+nexPage+"\")'>&gt;</span> ");
		}
		return pageHtml
	}
	
	function createNumInPage(index)
	{
		if(currentPage==index)
		{
			return " <span style='color:red;'>"+index+"</span> ";	
		}
		else
		{
			return " <span style='cursor:pointer' onclick='changePageIndex(\""+index+"\")'>"+index+"</span> "	
		}
	}
}