/**
* jiushixuefeng ajaj xml
*/

var pageObject = null;

var script =  null;
function Page( args ) {

	this.eleId = args.eleId ;
	
	if( !this.eleId ){
		alert( "无法加载文章列表" );
		return ;
	}
	
	
	this.tempHTML = "";  //临时innerHTML
	
	try{
		this.categoryId = args.categoryId;	//当前栏目id
	}catch(e){
		alert( "无法加载文章列表" );
		return;
	}
	
	this.pageSize = 10;	 			//每页显示数量 默认10	
	this.titleDisplayLength = 30; 	//标题显示长度 默认30
	this.localLanguage = args.localLanguage || "zh";				//显示为中文或英文 en zh 默认为中文
	
	try{
		if( args.pageSize && parseInt( args.pageSize ) ){
			this.pageSize = args.pageSize;
		}
	}catch(e){}
	
	try{
		if( args.titleDisplayLength && parseInt( args.titleDisplayLength ) ){
			this.titleDisplayLength = args.titleDisplayLength;
		}
	}catch(e){}
	
	this.init();
	pageObject = this;
}
Page.prototype = {

	//初始化page类
	init : function(){
	
		//默认参数初始化
		this.appUrl = "http://cms.maoji.gov.cn";	//访问路径	http://app.huainan.gov.cn
		
		this.docString = "";			//当前获取文档数据
		this.reTryTimes = 1;			//重复次数
		this.maxReTryTimes = 10;			//最大重复次数
		this.interval = null;			//定期任务
		
		//this.initCoverDiv();			//加载遮罩TODO
	},
	
	//加载文章列表
	loadPage : function( page ){
		var page = page || 1;
		var url = this.appUrl + "/admin/push.jhtml?method=pushItemArticleList&type=xml&categoryId=" + this.categoryId + 
					"&pageNo=" + page + "&num=" + this.pageSize + "&att=pageObject.docString&r=" + Math.random();
		
		if( document.getElementById("pageScript") != null ) 
		{
			document.getElementsByTagName("head")[0].removeChild( document.getElementById("pageScript") );
		}
		script = document.createElement( "script" );	//创建script对象
		script.setAttribute('type', 'text/javascript');
		script.setAttribute("src" , url);				//对象源地址
		script.setAttribute( "id" , "pageScript" );		//设置script的id
		script.defer = true;
		document.getElementsByTagName("head")[0].appendChild( script );

		this.callBack();	//开始回调
	},
	
	//回调数据
	callBack : function(){
		if( this.reTryTimes == this.maxReTryTimes ) {	//如果重试次数超过最大重复次数 则反馈失败信息
			
			alert( " 加载失败.... " ) ;
			
			this.reTryTimes = 1;	//当前重试次数恢复初始化
			
			this.stopInterval();  	//首先停止任务
			
			//this.hideCoverDiv();	//隐藏覆盖层TODO
		}else{	//否则开始查看回调数据
			
			if( this.docString == "" ){	//如果当前回调数据为空,则继续回调			
				
				
				this.startInterval();	//开始回调
				
				this.reTryTimes++;		//当前重试次数++
				
				//this.showCoverDiv();	//显示覆盖层TODO
				
			}else{						//否则开始生成文章列表和分页数据
		
				//this.hideCoverDiv();//隐藏覆盖层TODO
		
				this.reTryTimes = 1; 	//重置重试次数
				
				this.stopInterval(); 	//首先停止任务
				
				this.buildData();	//开始创建列表
						
			}
		}
	},
	
	
	//解析xml , 显示数据
	
	buildData : function() {
		
		var flag = false;
		
		var ele = document.getElementById( this.eleId );	//首先获取当前元素
		
		this.tempHTML = ele.innerHTML;		//先存放在临时innerHTML , 在错误时可以回显
		
		ele.innerHTML = "";					//首先清空内容
		try{
			
			var doc = loadXML ( this.docString );	//使用返回数据创建xml对象
			var root = doc.documentElement;			//当前根节点
			
			var itemsElement = root.getElementsByTagName( "item" ); //获取列表信息
			
			this.buildList( ele , itemsElement );	//生成列表
			
			var pageElement = root.getElementsByTagName( "page" )[0];	//获取分页节点
			
			this.buildPageHTML( ele , pageElement );						//生成分页数据
			
			this.docString = "";
			
			flag = true;
		}catch( e ){
			alert( e );
			alert( "加载失败,请重试..." );
			return ;
		}
		if( ! flag ){
			ele.innerHTML = this.tempHTML;
		}
	},
	
	//生成列表
	buildList : function( ele , itemsElement ){
	
		
		var itemSize = itemsElement.length;
		if( itemSize > 0 ){
			for( itemI = 0 ; itemI < itemSize ; itemI ++ ){
					var item = itemsElement[itemI];
					
					var title = getAttributeValue( item , "title" );			//获取标题
					var url = getAttributeValue( item , "link" );				//获取链接
					var desc = getAttributeValue( item , "description"  );				//获取链接
					var logo = getAttributeValue( item , "logoUrl"  );				//获取图片
					
					var contentDIV_1 = document.createElement( "div" );
					contentDIV_1.className="hotelinfo_col";
					var contentDIV_2 = document.createElement( "div" );	
					contentDIV_2.className="hotelcol_left";
					var contentDIV_3 = document.createElement( "div" );	
					contentDIV_3.className="hotelcol_right";
					
					var contentDIV_4 = document.createElement( "div" );
					contentDIV_4.className="clear";
					//标题链接处理
					
					contentDIV_1.appendChild( contentDIV_2 );
					
					
					var tempHTML = "<span><img src='"+logo+"' width='178' height='114' alt='' /></span>";
					contentDIV_2.innerHTML = tempHTML;
					
					contentDIV_1.appendChild( contentDIV_3 );
					
					tempHTML =  "<h1><span><a href='"+url+"'><img src='/images/more_ic1.gif' width='39' height='11' alt=''/></a><strong>"+title+"</strong></span></h1><p><span>概要介绍：</span>"+desc+"</p>";
					contentDIV_3.innerHTML = tempHTML;
					
					contentDIV_1.appendChild( contentDIV_4 );
					
					ele.appendChild( contentDIV_1 );
			}	
		}else{
			var noContentLI = document.createElement( "li" );
			if( this.localLanguage == 'en' ){
				noContentLI.innerHTML = " no more results.";
			}else{
				noContentLI.innerHTML = "没有显示内容";
			}
		}
		
		
	},
	
	//生成分页
	buildPageHTML : function( ele , pageElement ){
			var pageSize = parseInt( getAttributeValue( pageElement , "size" ) );
			var currentPage = parseInt( getAttributeValue( pageElement , "currentPage" ) );
			var resultCount = parseInt( getAttributeValue( pageElement , "resultCount" ) );
			var totalPage = parseInt( this.getTotalPage( resultCount , pageSize , resultCount ) );
			
			var pageOL = document.createElement( "div" );
			
			//var tempHTML = "<li>";
			var tempHTML = "<div class='pagination hotelinfo_listpage'>";
			
			//buildPage
			if(currentPage != 1 ){
				if( this.localLanguage == 'en' ){
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage(1);'>first</a> | ";
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage(" + (currentPage - 1) + ");'>previous</a> | ";
				}else{
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage(1);'>首页</a> | ";
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage(" + (currentPage - 1) + ");'>上一页</a> | ";
				}
			}
			
			//开始页码
			var startPage = 0;
			//结束页码
			var endPage = 0;
			
			if(totalPage <= 10 ){ //如果页总数小于10 那么开始页码为1 结束页码为开始页码页总数
				startPage = 1;
				endPage = totalPage;
			}else if(( totalPage - currentPage ) <= 5){ //如果总页数-当前页码小于5 那么结束页码 = 总页数 开始页码 = 总页数-9
				endPage = totalPage;
				startPage = endPage - 9;
			}else if(currentPage <= 5){ 	//如果当前页码小于5 那么开始页码 = 1 结束页码=开始页码+9
				startPage = 1;
				endPage = 10;
			}else{	//否则当前开始页码为当前页码-5 结束页码为当前页码+5
				startPage = currentPage - 5;
				endPage = currentPage + 5;
			}
			
			for( pageNum = startPage; pageNum <= endPage ; pageNum++ ){
				if( pageNum != startPage ){
					tempHTML += " | ";
				}
				if(pageNum != currentPage){
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage(" + pageNum + ");'>" + pageNum + "</a>";
				}else{
					tempHTML += "<font color='red'><b>" + pageNum + "</b></font>" ;
				}
			}
			
			if(currentPage != totalPage){
				if( this.localLanguage == 'en' ){
					tempHTML += " | <a href='#' onclick='window.pageObject.loadPage(" + (currentPage + 1) + ");'>next</a> | ";
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage( " + totalPage + " );'>last</a>";
				} else {
					tempHTML += " | <a href='#' onclick='window.pageObject.loadPage(" + (currentPage + 1) + ");'>下一页</a> | ";
					tempHTML += "<a href='#' onclick='window.pageObject.loadPage( " + totalPage + " );'>尾页</a>";
				}
			}		
			tempHTML += "</div>";
			//tempHTML += "</li>";
			pageOL.innerHTML = tempHTML;
			ele.appendChild( pageOL );
	},
	
	getTotalPage : function( resultCount , pageSize , resultCount ) {
		var resultSizePage = 1;
		if (resultCount == 0) {
			resultSizePage = 1;
		} else {
			resultSizePage = (resultCount % pageSize == 0) ? (resultCount / pageSize)
					: (resultCount / pageSize + 1);
		}
		return resultSizePage;
	},
	
	//开始任务
	startInterval : function(){
		if( this.interval == null ){	//如果当前任务没有开始 则开始当前任务
			this.interval = setInterval( "window.pageObject.callBack();" , 500 );  //每半分钟验证一次数据
		}	
	},
	
	//停止当前任务
	stopInterval : function(){
		if( this.interval != null ){	//如果当前任务不为空 则开始当前任务
				clearInterval( this.interval );
			this.interval = null;
		}
	},
	
	//初始化覆盖层TODO
	initCoverDiv : function(){

		this.loadingDiv = document.createElement( "div" );	//创建加载中div
		this.loadingDiv.style.display = "none";				//初始不显示
		this.loadingDiv.style.position = "absolute";		//绝对位置
		document.body.append( this.loadingDiv );			//添加到document中
				
	},
	
	//显示覆盖层TODO
	showCoverDiv : function(){
		if( this.loadingDiv != null && this.loadingDiv.style.display == 'none' ){
			this.loadingDiv.style.display = '';
		}
	},
	
	//隐藏覆盖层TODO
	hideCoverDiv : function(){
	
		if( this.loadingDiv != null ){
			this.loadingDiv.style.display = 'none';
		}
		
	}
}


//common 

var app=navigator.appName;

function isIE (){
	if(app.indexOf('Netscape') != -1)return false;
	return true;
}

function loadXML( xmlString ){
	var xml = null;
	if( isIE()){
		xml = new ActiveXObject("Microsoft.XMLDOM"); 
		xml.loadXML( xmlString );  
	}else{
		xml = new DOMParser().parseFromString( xmlString , 'text/xml' );
	}
	return xml;
}

function getXMLString( xmlDoc ){
	var doc = xmlDoc;
     if( isIE() ){
     	doc = doc.xml;
     }else{
		doc = (new XMLSerializer()).serializeToString(doc);        
     }
     return doc;
}

function getAttributeValue ( node , attributeName ){
	if( node == null ) return '';
	var value = node.getAttribute( attributeName );
	
	if(  value != null && value != 'null')return value;
	else return '';
}


function  subString ( str , length ) { //截取字符串
	if( str == null || str == '' ){
		return '';
	}else if( str.length < length ){
		return str;
	}else{
		return str.substring( 0 , length ) + "...";
	}
}
