//客户端分页功能;
var dPager;

//divContent:	内容节点 id;
//divHead:		内容节点内标题节点 id;
//divFoot:		内容节点内页脚节点 id;
function DHTMLpagenation(divContent,divHead,divFoot,iLength) { with (this)
{
	// client static html file pagenation
	this.divContent=divContent;
	this.Node=document.getElementById(divContent);

	if(document.getElementById(divHead) != null ){
		this.head = document.getElementById(divHead).outerHTML;
		document.getElementById(divHead).outerHTML = '';
	}
	else{
		this.head = '';
	}
	if(document.getElementById(divFoot) != null ){
		this.foot = document.getElementById(divFoot).outerHTML;
		document.getElementById(divFoot).outerHTML = '';
	}
	else{
		this.foot = '';
	}

	this.content=this.Node.innerHTML;
	this.contentLength=this.Node.innerText.length;
	this.perpageLength=iLength; //default perpage byte length.
	this.currentPage=1;
	this.currentNode=null;		//当前页最后的节点对象;
	this.currentNodeStrEndPos=0;	//当前页最后的节点的字符串在该节点的位置;
	//this.regularExp=/.+[\?\&]{1}page=(\d+)/;
	this.regularExp=/\d+/;

	this.divDisplayContent;
	this.contentStyle=null;
	this.strDisplayContent="";
	this.divDisplayPagenation;
	this.strDisplayPagenation="";
	this.showLength=0;	//当前页累计长度;

	//arguments.length==2?perpageLength=arguments[1]:'';

	this.pageSizeCount = this.getPageCount();
	this.initialize();
	return this;
	
}};
DHTMLpagenation.prototype.initialize=function() { with (this)
{
	if(this.contentLength<=this.perpageLength)
	{
		this.strDisplayContent=this.content;
		this.Node.innerHTML=this.head + this.strDisplayContent + this.foot;
		return null;
	}
	
	//var i = this.contentLength / this.perpageLength;
	//if((i - Math.floor(i)) > 0.5){
	//	this.pageSizeCount = Math.ceil(i);
	//}
	//else{
	//	this.pageSizeCount = Math.floor(i);
	//}

	this.goto(this.currentPage);
	this.displayContent();
}};


//设置翻页;
DHTMLpagenation.prototype.displayPage = function()
{
	var str;
	var i;
	var j;
	var k;

	if(1 >= this.pageSizeCount){
		this.strDisplayPagenation = '';
		return;
	}
	
	if(this.currentPage<=10){
		i=1;
	}
	else{
		i=Math.floor((this.currentPage-1)/10)*10+1;
	}
	if(i+9<this.pageSizeCount){
		j=i+9;
	}
	else{
		j=this.pageSizeCount;
	}

	str='<div>';
	str += "<span class='pageSplitInfo'>共 " + this.pageSizeCount + " 页，当前第 " + this.currentPage + "/" + this.pageSizeCount + " 页</span>";
	if(i>10){
		str += '<span class="pageSplitIndex"><a onclick="dPager.goto(1);">&lt;&lt;</a></span>';
		str += '<span class="pageSplitIndex"><a onclick="dPager.goto('+ (i-1) +');">&lt;</a></span>';
	}

	for(k=i;k<=j;k++){
		if (k != this.currentPage){
			str += '<span class="pageSplitIndex"><a onclick="dPager.goto('+k+');">' + k +'</a></span>';
		}
		else{
			str += '<span class="pageSplitSelected"><a onclick="dPager.goto('+k+');">' + k +'</a></span>';
		}
	}

	if(j<this.pageSizeCount){
		str += '<span class="pageSplitIndex"><a onclick="dPager.goto('+ (j+1) +');">&gt;</a></span>';
		str += '<span class="pageSplitIndex"><a onclick="dPager.goto('+ this.pageSizeCount +');">&gt;&gt;</a></span>';
	}
		
	str += "</div>";
	this.strDisplayPagenation = str;
}
	
DHTMLpagenation.prototype.previous=function() { with(this)
{
	this.goto(this.currentPage-1);
}};
DHTMLpagenation.prototype.next=function() { with(this)
{
	this.goto(this.currentPage+1);
}};
DHTMLpagenation.prototype.goto=function(iCurrentPage) { with (this)
{
	startime=new Date();
	if(this.regularExp.test(iCurrentPage))
	{
		//this.currentPage=iCurrentPage;
		this.Node.innerHTML=this.content;
		this.strDisplayContent=getHtml(iCurrentPage);
		//this.strDisplayContent=this.content.substr((this.currentPage-1)*this.perpageLength,this.perpageLength);
	}
	else
	{
		alert("page parameter error!");
	}
	this.displayPage();
	this.displayContent();
}};
DHTMLpagenation.prototype.displayContent=function() { with (this)
{
	this.Node.innerHTML=this.strDisplayContent + this.strDisplayPagenation;
}};
DHTMLpagenation.prototype.change=function(iPerpageLength) { with(this)
{
	if(this.regularExp.test(iPerpageLength))
	{
		this.perpageLength=iPerpageLength;
		this.currentPage=1;
		this.initialize();
	}
	else
	{
		alert("请输入数字");
	}
}};

DHTMLpagenation.prototype.getPageCount=function(){
	var i;
	this.currentPage = 1;
	this.currentNode=this.Node;
	this.currentNodeStrEndPos = 0;
	
	i = this.contentLength / this.perpageLength;
	if((i - Math.floor(i)) > 0.5){
		this.pageSizeCount = Math.ceil(i);
	}
	else{
		this.pageSizeCount = Math.floor(i);
	}
	
	i = 0;	
	do{
		this.showLength=0;
		this.getSubHtml(this.currentNode);
		i++;
	}while(this.currentNode != this.Node);	
	
	return i;
}

DHTMLpagenation.prototype.getHtml=function(pageIndex){
	var i;
	var strContent='';
	var strFront = '';
	var strEnd = '';	
	var n;
	var nc;
	var ncp;
	var flag;
	var index;
	
	if(pageIndex<1)
		return '';
	if(pageIndex > this.pageSizeCount)
		return '';
	
	this.currentPage = 1;
	this.currentNode=this.Node;
	this.currentNodeStrEndPos = 0;
	for(i=1;i<pageIndex;i++){
		this.showLength=0;
		this.getSubHtml(this.currentNode);
	}
	
	//取节点父节点格式;
	if(this.currentNode != this.Node){
		flag = true;
		nc = this.currentNode;
		n = this.currentNode.parentNode;
		while(n != null && n !=this.Node){
			if(n.nodeName.toUpperCase() == "OL"){
				//记录当前顺序编号;
				index = 1;
				ncp = nc.previousSibling;
				while(ncp){
					index++;
					ncp = ncp.previousSibling;
				}
			}	
			n.start = index;	
			strFront = getFront(n.outerHTML,n.innerHTML) + strFront;
			nc = n;
			n = n.parentNode;
		}
	}	
	
	//取内容;
	this.currentPage = pageIndex;
	this.showLength=0;
	strContent = this.getSubHtml(this.currentNode);
	
	//取尾格式;
	if(flag && this.currentNode != this.Node){
		n = this.currentNode.parentNode;
		while(n != null && n !=this.Node){
			strEnd += getEnd(n.outerHTML,n.innerHTML);
			n=n.parentNode;
		}
	}
	
	this.showLength=0;

	strFront = this.head + strFront;
	strEnd = strEnd + this.foot;
		
	return strFront + strContent + strEnd;
}

DHTMLpagenation.prototype.getSubHtml=function(o){
	var str;
	str=this.getNodeHtml(o);
	
	//如果是最后一页或内容不够,继续取后面的节点内容;
	o = this.currentNode;

	if(this.currentPage == this.pageSizeCount || this.showLength < this.perpageLength){
		while(o != this.Node && this.showLength < this.perpageLength){
			if(o.nextSibling){
				o = o.nextSibling;
				str += this.getNodeHtml(o);
			}
			else{
				o = o.parentNode;
				str += getEnd(o.outerHTML,o.innerHTML);
			}
		}
		if(o == this.Node){
			this.currentNode = this.Node;
		}
	}

	//如果当前节点刚好取完,则移动到下一个节点;
	if(this.showLength >= this.perpageLength && 0 == this.currentNodeStrEndPos ){
		while(o != this.Node){
			if(o.nextSibling){
				o = o.nextSibling;
				break;
			}
			else{
				o = o.parentNode;
			}
		}
		
		this.currentNode = o;
	}
	return str;
}

//获取一个节点的内容;
DHTMLpagenation.prototype.getNodeHtml=function(o){
	var n;
	var str = '';
	var i;
	var strTemp;
	
	if(this.currentPage < this.pageSizeCount && this.showLength >= this.perpageLength)
		return '';
	
	//获取 o 节点内容;
	//nodeType 还不是很清楚,目前只清楚 nodeType=1,3,8的区别
	if(o.nodeType == 8){
		//节点是注释,全加;
		str += o.outerHTML;
	}
	else if(o.nodeType == 3){
		//节点是文本;
		this.currentNode = o;
		if((this.currentPage == this.pageSizeCount) || (this.showLength + o.data.length - this.currentNodeStrEndPos <= this.perpageLength)){
			str += o.data.substring(this.currentNodeStrEndPos);
			this.currentNodeStrEndPos = 0;
			this.showLength += o.data.length;
		}
		else{
			str += o.data.substring(this.currentNodeStrEndPos,this.perpageLength-this.showLength);
			this.currentNodeStrEndPos = (this.perpageLength-this.showLength) + this.currentNodeStrEndPos;
			this.showLength = this.perpageLength;
		}
	}
	else{
		//节点是元素或其它;
		this.currentNode = o;
	
		if( o.nodeName.toUpperCase() == 'TABLE'){
			str += o.outerHTML;
			this.currentNodeStrEndPos = 0;
			this.showLength += o.innerText.length;		
		}
		else{
			if((this.currentPage == this.pageSizeCount) || (this.showLength + o.innerText.length - this.currentNodeStrEndPos <= this.perpageLength)){
				str += o.outerHTML;
				this.currentNodeStrEndPos = 0;
				this.showLength += o.innerText.length;
			}
			else{
				if(o.hasChildNodes()){
					str += getFront(o.outerHTML,o.innerHTML);
					for(i=0;i<o.childNodes.length;i++){
						str += this.getNodeHtml(o.childNodes[i]);
						if(this.showLength >= this.perpageLength){
							break;
						}					
					}
					str += getEnd(o.outerHTML,o.innerHTML);
				}
				else{
					str += getFront(o.outerHTML,o.innerHTML);
					str += o.innerText.substring(this.currentNodeStrEndPos,this.perpageLength-this.showLength);
					str += getEnd(o.outerHTML,o.innerHTML);
					
					this.currentNodeStrEndPos = (this.perpageLength-this.showLength) + this.currentNodeStrEndPos;
					this.showLength = this.perpageLength;					
				}
			}		
		}
	}		

	return str;
}
