var ucap_pageinfo_list_arr = new Array();
//定义页面对象元素，每个开源信息列表都会在该数组中增加一个元素
if( typeof(ucap_pageinfo_list_arr) == "undefined" || ucap_pageinfo_list_arr == null){
	 ucap_pageinfo_list_arr = new Array();
}

function ucap_pageinfo(id){
	this.docObj = null;
	this.nowObj = id;
	//HTML Outline对象
	this.m_objSrcContent = null;
	//HTML Column对象
	this.m_arSingleRec = null;
	//隐藏对象,位于m_arSingleRec下
	this.hiddenObj = null;
	this.hiddenObjArr = new Array();

	//获取分页参数
	this.m_nPageSize   = 5; //常量每页条数
	this.m_nRecSize   = 1; //常量每行条数

	//检索所有的节点
	this.m_arNodes = null;
	//记录数
	this.m_nRecCount   = 0;
	//总页数
	this.m_nPageCount = 0;

	//是否显示历史信息
	this.showHistory = false;

	//页码文字输出区
	this.CP = document.getElementById("CP"+this.nowObj);

	this.m_bFirst    = true;
	this.m_arOutLine = new Array();

	function setObj(){
		this.m_objSrcContent = document.all("OutlineContent"+this.nowObj);
		if(this.m_objSrcContent == null){
			alert( "没有定义Outline区域！分页代码发生异常！\n请检查模板的HTML标签已添加 xmlns:x" );
			return false;
		}
		this.m_arSingleRec = this.m_objSrcContent.all("ColumnContent"+this.nowObj);
		if(this.m_arSingleRec == null){
			alert( "没有定义Column区域！分页代码发生异常！\n请检查模板的HTML标签已添加 xmlns:x" );
			return false;
		}
		if(this.m_arSingleRec.children){
			if(this.m_arSingleRec.children.length>0){
				if(typeof(this.m_arSingleRec.children[0])){
				  this.hiddenObj = this.m_arSingleRec.children[0];
				}
			}
		}else if(this.m_arSingleRec.length){
			for(var x=0;x<this.m_arSingleRec.length;x++){
				this.hiddenObjArr[this.hiddenObjArr.length] = this.m_arSingleRec[x].children[0]; 
			}
		}
		//获取分页参数
		if(this.m_objSrcContent.PageSize && !isNaN(this.m_objSrcContent.PageSize)){
			this.m_nPageSize = parseInt(this.m_objSrcContent.PageSize);
		}
		if(this.m_arSingleRec != null && this.m_arSingleRec.length) {this.m_nRecSize = this.m_arSingleRec.length;}

		//提取数据
		//默认提取所有的数据
		//var sPath = "//INFO";

		if(!this.showHistory){
			//alert("更新之前:"+docObj);
			if(!this.docObj){
				this.docObj = document.getElementById("DocumentsDataSrc"+this.nowObj);
			}
			this.m_arNodes = this.docObj.getElementsByTagName("INFO");
		}
		
		this.m_nRecCount   = this.m_arNodes.length;

		this.m_nPageCount = 0;
		//计算总页数

		if(this.m_nRecCount%this.m_nPageSize>0)
		{
			this.m_nPageCount = (this.m_nRecCount - (this.m_nRecCount%this.m_nPageSize))/this.m_nPageSize +1;
		}else{
			this.m_nPageCount = this.m_nRecCount/this.m_nPageSize;
		}

		this.CP = document.getElementById("CP"+this.nowObj);
	}

	//===========showPageLineNum()页面显示状态条函数==================
	function _showPageLineNum(){
		if(!this.docObj){
			this.docObj = document.getElementById("DocumentsDataSrc"+this.nowObj);
		}
		//var recsnode = docObj.XMLDocument.selectNodes("//RECS");
		var recsnode = this.docObj.getElementsByTagName("RECS");
		var infocount = recsnode[0].getAttribute("totalRecs");
		var infocountObj = document.getElementById("infocount"+this.nowObj);
		if(infocountObj) infocountObj.innerText = infocount;
		var recordcountObj = document.getElementById("recordcount"+this.nowObj);
		if(recordcountObj) recordcountObj.innerText = this.m_nRecCount;
		var totalpageObj = document.getElementById("totalpage"+this.nowObj);
		if(totalpageObj) totalpageObj.innerText = this.CP.value+"/"+this.m_nPageCount;
	}
	//===========规范跳转页码函数;_currentPage 跳转页码输入值===
	function _getCurrPage(_currentPage){
		var cPage =1;
		if( _currentPage<=0 || _currentPage=="")
			cPage =1;
		else if(_currentPage>this.m_nPageCount)
			cPage = this.m_nPageCount;
		else
			cPage = _currentPage;

		return cPage;
	}
	function _toPage(_pageNo){
		if(this.m_nRecCount <= 0) return false;
		//删除原有代码
		this.removeAllOldNodes();
		//设置复制节点
		this.m_bFirst = true;

		var cP = this.getCurrPage(_pageNo);
		var startPos = cP*this.m_nPageSize - this.m_nPageSize;
		var endPos = 0;
		if(cP*this.m_nPageSize>this.m_nRecCount)
			endPos=this.m_nRecCount;
		else
			endPos = cP*this.m_nPageSize;

		var nOutlineCount = 0;
		if(this.hiddenObj!=null){
		   this.hiddenObj.style.display="";
		}
		if(this.hiddenObjArr!=null){
			for(var x=0;x<this.hiddenObjArr.length;x++){
				this.hiddenObjArr[x].style.display="";
			}
		}
		for(var i=startPos; i<endPos; i++){
			var nTemp = this.createItemHTML(i, endPos, nOutlineCount);
			if(nTemp <= 0) break;
			i = i + nTemp;
			i--;
			nOutlineCount++;
		}
		if(this.hiddenObj!=null){
			  this.hiddenObj.style.display="none";
		}
		if(this.hiddenObjArr!=null){
			for(var x=0;x<this.hiddenObjArr.length;x++){
				this.hiddenObjArr[x].style.display="none";
			}
		}
		for( ;!this.m_bFirst && nOutlineCount<this.m_nPageSize; nOutlineCount++)
		{
			if(this.m_arOutLine.length <= nOutlineCount) break;
			//m_arOutLine[nOutlineCount].style.display = "none";
		}
		if(this.CP!=null){ this.CP.value = cP;}
		this.showPageLineNum();
		//设置为已经有节点
		this.m_bFirst  = false;
	}

	//删除原有的节点
	function _removeAllOldNodes(){
		var arTemp = document.all("OutlineClone"+this.nowObj);
		if(!arTemp) return false;
		if(arTemp.length){
			for(var i=arTemp.length-1; i>=0; i--){
				if(arTemp[i].children[0].children[0] && arTemp[i].children[0].children[0].tagName=="LI"){
					arTemp[i].children[0].children[0].removeNode(true);
				}
				arTemp[i].removeNode(true);
			}
		}else{
			arTemp.removeNode(true);
		}
		return true;
	}

	/**
	  * 复制分页节点
	  *
	**/
	function _createItemHTML(_nCurrIndex, _nEndIndex, _nOutlineIndex){
		if(this.m_arNodes.length < _nCurrIndex) return 0;

		var objContent = null;
		if(this.m_bFirst){//创建节点
			//复制Outline节点
			objContent = this.m_objSrcContent.cloneNode(true);
			//设置ID
			objContent.id = "OutlineClone"+this.nowObj;
			this.m_arOutLine[this.m_arOutLine.length] = objContent;
		}else{
			if(_nOutlineIndex >= this.m_arOutLine.length)
			{
				//需要重新复制节点


				//复制Outline节点
				objContent = this.m_objSrcContent.cloneNode(true);
				//设置ID
				objContent.id = "OutlineClone"+this.nowObj;
				this.m_arOutLine[this.m_arOutLine.length] = objContent;
				this.m_bFirst = true;
			}else
				objContent = this.m_arOutLine[_nOutlineIndex];
		}

		//设置Display

		objContent.style.display = "";
		//修改ID
		var arSingleRec = objContent.all("ColumnContent"+this.nowObj);
		if(arSingleRec==null) {arSingleRec = objContent;}
		var recCount = 1;
		if(arSingleRec.length) recCount = arSingleRec.length;
		var i = 0;

		for(i=0; i<recCount; i++){

			//判断当前索引是否有效
			if(this.m_arNodes.length <= (_nCurrIndex+i) || _nCurrIndex+i >= _nEndIndex) break;

			//获取当前的XML节点
			var xmlNode = this.m_arNodes[_nCurrIndex+i];
			//遍历所有属性子节点，从XML中提取数据


			var objSingleRec = null;
			if(recCount>1) objSingleRec = arSingleRec[i];
			else objSingleRec = arSingleRec;
			var arAllField = objSingleRec.all;

			//alert(arAllField.length);    
			objSingleRec.style.display = "";
			var today = new Date();
			var pdateNode = xmlNode.selectSingleNode("PublishedTime");
			var showIMG = false;
			var infoday ;
			if(pdateNode) {
				var tmpDtext=pdateNode.text;
				infoday = new Date(Date.parse(tmpDtext.replace(/-/g,"/")));
			}
			if (infoday){
				if((today.getYear()-infoday.getYear()==0) && (today.getMonth()-infoday.getMonth()==0) && (today.getDate()-infoday.getDate()==0)){
					showIMG = true;
				}
			}
			for(var j=0; j<arAllField.length; j++){
				var sFieldName = arAllField[j].FieldName;
				var sTagName = arAllField[j].tagName;
				var shortTitleLength = 1000;
				//alert("sFieldName:"+sFieldName+"\n"+"sTagName:"+sTagName);  
				if(arAllField[j].ShortLen) shortTitleLength = arAllField[j].ShortLen; 
				if((sTagName=="A" && arAllField[j].href=="about:blank<x:INFOURL></x:INFOURL>") || (sTagName=="A" && arAllField[j].href=="about:<x:INFOURL></x:INFOURL>")) {
					sFieldName = "InfoURL";
				}
				if(sFieldName){
					var sValue = "";
					//获取属性节点


					var xmlNodeTemp = xmlNode.selectSingleNode(sFieldName);
					if(xmlNodeTemp) sValue = xmlNodeTemp.text;
					//图片新闻扩展字段，不能取节点的TEXT，要取XML
					//注释时间-卫生部项目-阿汉--暂不放到产品
					/*if(sTagName=="INFOEXPIMG" && xmlNodeTemp.childNodes(0)) {
						sValue = xmlNodeTemp.childNodes(0).xml;   
					}else if(xmlNodeTemp.childNodes.item(0)){
						sValue = xmlNodeTemp.childNodes.item(0).xml;       
					   }
					}*/
					var reg=new RegExp("&gt;","g"); 
					sValue = sValue.replace(reg,">");
					reg=new RegExp("&lt;","g");
					sValue = sValue.replace(reg,"<");

					  // alert(sValue);  
					switch(sTagName){//暂时只支持四种HTML置标定义数据源


					case "A":
						if(sFieldName=="InfoURL"){
						  var tmpValue = sValue.toLowerCase();
						  if((tmpValue.indexOf("http://")>-1) || (tmpValue.indexOf("/")==0)){
							arAllField[j].href = sValue;
						  }else{
							arAllField[j].href = "/publicfiles/business/htmlfiles/"+sValue;
						  }
						}
						break;
					case "INFOTITLE":
						arAllField[j].innerHTML = sValue;
						arAllField[j].title = sValue; //显示alt
						if(arAllField[j].innerText!=arAllField[j].innerHTML) sValue = arAllField[j].innerText;
						if(arAllField[j].ShortLen && sValue.length>shortTitleLength){
							//带格式截取标题
							//sValue = arAllField[j].innerHTML.replace(arAllField[j].innerText,sValue.substring(0,shortTitleLength)+"..");
							//不带格式截取标题
							arAllField[j].innerHTML = sValue.substring(0,shortTitleLength)+"..";
						}
						// -- zhaozh 2008年9月2日14:37:54 用户普遍反映这个功能不显示，因为不能控制
						//if(showIMG)
							//arAllField[j].innerHTML = arAllField[j].innerHTML + "<IMG border=0 src=\"/publicfiles/business/cmsresources/cmsimages/topnew.gif\">";
						
						break;
					case "INFOSUBTITLE":
						arAllField[j].innerHTML = sValue;
						arAllField[j].title = sValue;

						if(arAllField[j].ShortLen && sValue.length>shortTitleLength){
							arAllField[j].innerHTML = sValue.substring(0,shortTitleLength)+"..";
						}
						break;
					case "INFOWRITER":
						arAllField[j].innerText = sValue;
						break;
					case "INFOSOURCE":
						arAllField[j].innerText = sValue;
						break;
					case "INFOMNUMBER":
						arAllField[j].innerText = sValue;
						break;
					case "INFODATE":
						arAllField[j].innerText = sValue;
						break;
					case "INFOEXPIMG":
						sValue = sValue.replace("&gt;",">");
						sValue = sValue.replace("&lt;","<");
						arAllField[j].innerHTML = sValue;
						break;	
						
					case "INFO_ABSTRACT":    
						arAllField[j].innerText = sValue;
						break; 
		
				case "INFO_EXPSTR2":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR3":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR4":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR5":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR6":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR7":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR8":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR9":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKFILECODE":    
					arAllField[j].innerText = sValue;      
					break; 
			   
			    case "ZWGKOPENDATE":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKADDFILE":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKBUMEN":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKSUOYINHAO":   
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR15":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR16":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKOPENTYPE":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKOPENSHIXIAN":    
					arAllField[j].innerText = sValue;
					break; 
				case "ZWGKOPENSCOPE":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR20":    
					arAllField[j].innerText = sValue;      
					break; 
			
				case "INFO_EXPSTR21":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR22":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR23":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR24":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR25":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR26":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR27":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR28":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR29":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR30":    
					arAllField[j].innerText = sValue;      
					break; 
				case "INFO_EXPSTR31":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR32":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR33":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR34":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR35":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR36":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR37":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR38":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR39":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR40":    
					arAllField[j].innerText = sValue;      
					break; 
				case "INFO_EXPSTR41":    
					arAllField[j].innerText = sValue;
					break; 
				case "INFO_EXPSTR42":    
					arAllField[j].innerText = sValue;      
					break;   
					
				case "INFCATEID":      
					arAllField[j].innerText = sValue;      
					break; 	
								 					
				default:
					//arAllField[j].innerText = sValue;
					break;
					}
				}
			} 
		}
		//将没有填充内容的Column隐藏
		for(; arSingleRec.length && i<arSingleRec.length; i++) arSingleRec[i].style.display = "none";

		if(this.m_bFirst)this.m_objSrcContent.insertAdjacentElement("beforeBegin", objContent);
		return i;
	}

	this.setObj = setObj;
	this.showPageLineNum = _showPageLineNum;
	this.toPage = _toPage;
	this.removeAllOldNodes = _removeAllOldNodes;
	this.getCurrPage = _getCurrPage;
	this.createItemHTML = _createItemHTML;
}

//初始化各种分页变量
function initPage(id,docObj){
	//判断数组中是否存在该变量，如果存在则
	var obj = _getPagefromArr(id);
	if (!( obj != null && typeof(obj)!="undefined" && obj !="undefined"  )){
		obj = new ucap_pageinfo(id);
		if( docObj ){
			obj.docObj = docObj;
		}
		ucap_pageinfo_list_arr.push( obj );
	}else if( docObj ){
		obj.docObj = docObj;
	}
	obj.setObj();
	//默认定位到第一页
	obj.toPage(1);
	//将标志位设回
	obj.m_bFirst  = false;
}

function _getPagefromArr(id){
	//判断数组中是否存在对象
	for( var i=0,k=ucap_pageinfo_list_arr.length;i<k;i++ ){
		if( ucap_pageinfo_list_arr[i] != null && ucap_pageinfo_list_arr[i].nowObj == id ){
			return ucap_pageinfo_list_arr[i];
		}
	}
	return null;
}


//===============goto()直接跳转函数=============================================
function goto(objid){
	var obj = _getPagefromArr(objid);
	if ( obj != null && typeof(obj)!="undefined" && obj !="undefined"  ){
		var cpvObj = document.getElementById("CPV"+objid);
		if(cpvObj){
			obj.toPage(cpvObj.value);
			//设置当前路径
			cpvObj.value = obj.CP.value;
		}
	}
}

//跳转到指定页面
function toPage(objid,pageid){
	if( typeof(pageid) == "undefined" ){return;}
	var obj = _getPagefromArr(objid);
	if ( obj != null && typeof(obj)!="undefined" && obj !="undefined"  ){
		obj.toPage(pageid);
	}
}

function toPrePage(objid){
	var obj = _getPagefromArr(objid);
	if ( obj != null && typeof(obj)!="undefined" && obj !="undefined"  ){
		var k = parseInt(obj.CP.value);
		if( !isNaN( k ) ){
			obj.toPage( k-1 );
		}
	}
}
function toNextPage(objid){
	var obj = _getPagefromArr(objid);
	if ( obj != null && typeof(obj)!="undefined" && obj !="undefined"  ){
		var k = parseInt(obj.CP.value);
		if( !isNaN( k ) ){
			obj.toPage( k+1 );
		}
	}
}
function toLastPage(objid){
	var obj = _getPagefromArr(objid);
	if ( obj != null && typeof(obj)!="undefined" && obj !="undefined"  ){
		obj.toPage( obj.m_nPageCount );		
	}
}
