﻿function HtmlContentSplitPage_GetOffsetTop(e){//获取元素的y坐标
	var offset=e.offsetTop;
	if(e.offsetParent!=null)offset+=HtmlContentSplitPage_GetOffsetTop(e.offsetParent);
	return offset;
}

function HtmlContentSplitPage_ArraySort(x,y){
		return x == y ? 0 : (x > y ? 1 : -1);
}

function HtmlContentSplitPage(BoxID,NavBarBoxID){	
	var objBox=document.getElementById(BoxID);//分页容器对象
	if(objBox==null)return;
	
	var i=0,j=0;	
	var BoxOffsetTop=HtmlContentSplitPage_GetOffsetTop(objBox);//容器绝对y坐标	
	var BoxHeight=BoxOffsetTop+objBox.offsetHeight;
	objBox=null;
	
	//在所有div对象查找分页符对象并存入分页符对象数组	
	var aryAllDiv = document.getElementsByTagName("div");//所有div对象
	if(aryAllDiv.length<1){
		aryAllDiv=null;	
		return;
	}
	var arySplitDiv=new Array(0);//分页符对象数组		
	for(i=0;i<aryAllDiv.length;i++){			
		if(aryAllDiv[i].style.pageBreakAfter!=null && aryAllDiv[i].style.pageBreakAfter.length>0){//此div是分页符
			arySplitDiv[j]=aryAllDiv[i];
			j+=1;
		}
	}
	aryAllDiv=null;	
	
	if(arySplitDiv.length<1){//没有分页符
		arySplitDiv=null;
		return;
	}	
	var total=arySplitDiv.length+1;//总页数
	
	//计算每一个分页符在网页中的绝对y坐标
	var aryTopsList=new Array(0);
	aryTopsList[0]=BoxOffsetTop;
	j=1;
	for(i=0;i<arySplitDiv.length;i++){			
		//测试输出//////////////////////////////////////////////////////////////////		
		//arySplitDiv[i].style.border="#000000 1px solid";
		//arySplitDiv[i].innerText   =HtmlContentSplitPage_GetOffsetTop(arySplitDiv[i]);		
		////////////////////////////////////////////////////////////////////////////		
		aryTopsList[j]=HtmlContentSplitPage_GetOffsetTop(arySplitDiv[i]);
		j+=1;
		
		arySplitDiv[i]=null;
	}	
	arySplitDiv=null;	
	aryTopsList=aryTopsList.sort(HtmlContentSplitPage_ArraySort);	
	
	var aryHeightList=new Array(aryTopsList.length);	
	j=0;
	for(i=1;i<aryTopsList.length;i++){
		aryHeightList[j]=aryTopsList[i]-aryTopsList[i-1];
		j+=1;
	}
	aryHeightList[aryHeightList.length-1]=BoxHeight-aryTopsList[aryTopsList.length-1];	
	
	var n=aryTopsList[0]
	for(i=0;i<aryTopsList.length;i++){
		aryTopsList[i]=aryTopsList[i]-n;		
	}
	
	for(i=0;i<aryTopsList.length;i++){		
		aryTopsList[i]=aryTopsList[i].toString();			
	}
	for(i=0;i<aryHeightList.length;i++){			
		aryHeightList[i]=aryHeightList[i].toString();
	}
	
	HtmlContentSplitPage_do(BoxID,NavBarBoxID,total,1,aryHeightList.join(","),aryTopsList.join(","));		
	aryTopsList=null;
	aryHeightList=null;
}

function HtmlContentSplitPage_do(BoxID,NavBarBoxID,totalPage,currentPage,HeightList,TopsList){	
	var i=0;			
	
	//分页
	var objBox=document.getElementById(BoxID);	
	var aryTopsList=TopsList.split(",");
	var aryHeightList=HeightList.split(",");
	
	for(i=0;i<aryTopsList.length;i++){
		aryTopsList[i]=(new Number(aryTopsList[i]));		
	}	
	for(i=0;i<aryHeightList.length;i++){
		aryHeightList[i]=(new Number(aryHeightList[i]));		
	}	
	objBox.style.height=aryHeightList[currentPage-1];
	objBox.scrollTop=aryTopsList[currentPage-1];
	
	objBox=null;	
	aryTopsList=null;
	aryHeightList=null;
	
	
	//输出导航条
	var objNavBarBox=document.getElementById(NavBarBoxID);
	if(objNavBarBox==null)return;
	
	var strNavBarHtml="";		
	for(i=1;i<=totalPage;i++){
		if(i==currentPage){
			strNavBarHtml+="<td>"+i+"</td>";
		}
		else{
			strNavBarHtml+="<td><a href='#' onClick=\"HtmlContentSplitPage_do('"+BoxID+"','"+NavBarBoxID+"',"+totalPage+","+i+",'"+HeightList+"','"+TopsList+"');\">["+i+"]</a></td>";
		}
	}	
	if(currentPage>1){
		strNavBarHtml="<td><a href='#' onClick=\"HtmlContentSplitPage_do('"+BoxID+"','"+NavBarBoxID+"',"+totalPage+","+(currentPage-1)+",'"+HeightList+"','"+TopsList+"');\">Previours</a></td>"+strNavBarHtml;
	}	
	if(currentPage<totalPage){
		strNavBarHtml=strNavBarHtml+"<td><a href='#' onClick=\"HtmlContentSplitPage_do('"+BoxID+"','"+NavBarBoxID+"',"+totalPage+","+(currentPage+1)+",'"+HeightList+"','"+TopsList+"');\">Next</a></td>";
	}	
	strNavBarHtml="<table><tr>"+strNavBarHtml+"</tr></table>";	
	objNavBarBox.innerHTML=strNavBarHtml;
	
	objNavBarBox=null;
}
