var g_currentPage = 1;
var g_pageCount = 0;
var g_oldPageIndex;
var g_strForumUrl = "http://bbs.top-combine.com/viewthread.php?tid=";


function fnGetFtc(){
   fn$("#dvFtc").html("<table width='100%' height='200px' align='center'><tr><td valign='middle' align='center'> <img src='./siteImages/loading.gif'/><font color='red'>正在加载留言信息...</font></td></tr></table>");  
   g_pageSize = 14;
	fn$.ajax({
		   type:"POST",
		   data:"action=3&pageIndex="+g_currentPage+"&pageSize=" + g_pageSize,                   
		   url:"services/infoList.php",
		   success:function(msg){
			  var xmlDoc  = fn$(msg);
			  var tcsNode = xmlDoc.find("root tcs");			 
			  if(tcsNode == null || tcsNode.size() == 0){
				 g_pageCount = 0;
				 g_currentPage = 0;	
				 fn$("#dvReply").hide();
				 fn$("#dvFtc").html("<table width='100%' height='200px' align='center'><tr><td valign='middle' align='center'><font color='red'>当前不存在留言信息!</font></td></tr></table>");							 
			  }else{
				  var strHtml = ''; 
				  var iIndex = 1;
				  tcsNode.find("tc").each(
					function(){
					    var tcNode = fn$(this);
					    var strTitle = tcNode.find("title").text();
						var str = strTitle;
						if(str.length > 25){
							str = str.substring(0,25) + "...";
						} 
						strHtml += '<li><span>'+ tcNode.find("rdate").text()+'</span><a href="#this" id="p' + (iIndex++) +'" author="'+tcNode.find("author").text()+'" title="' + strTitle 
							 + '" pid="'+tcNode.find("pid").text()+'" file="'+tcNode.find("file").text()+'" onclick="fnLoadContent(this);">' + str + '</a></li>' ;
				   }					
				 );				
				  
				fn$("#dvFtc").html('<ul class="newslist1" id="ulTc">' + strHtml + "</ul>");
				var iPgIndex = parseInt(xmlDoc.find("root pgindex").text()); 
				if(g_currentPage == 1 || g_currentPage != iPgIndex ){
						g_currentPage = iPgIndex;
						g_pageCount = Math.ceil(parseInt(xmlDoc.find("root rcount").text())/g_pageSize);
				}
				fnLoadContent($("p1"));				
			  }
			   fnUpdateNavigation();			  
		   },
		   error:function(){
			 fn$("#pgCtrl1").hide();
			 fn$("#dvFtc").html("<table width='100%' height='200px' align='center'><tr><td valign='middle' align='center'><font color='red'>获得留言信息失败!</font></td></tr></table>");							 
			 		
			 alert("获得留言信息失败，请稍候重试！");  			 
			 g_currentPage = g_oldPageIndex;  
			 fnUpdateNavigation();
		   }  		   
	  }); 	 			  
}
       

function fnUpdateNavigation(){
	if (g_pageCount > 1){
		fn$("#pgCtrl1").show();	
	}else{
		fn$("#pgCtrl1").hide();	
		return;
	}
	 if( g_currentPage <= 1){
	    fn$("#lfst").addClass("unclick").removeClass("prev");
		fn$("#lprev").addClass("unclick").removeClass("prev");
	 }else{
		fn$("#lfst").addClass("prev").removeClass("unclick");
		fn$("#lprev").addClass("prev").removeClass("unclick");
	 }
	 if(g_currentPage >= g_pageCount){
		 fn$("#llast").addClass("unclick").removeClass("prev");
		 fn$("#lnext").addClass("unclick").removeClass("prev");
	 }else{
		fn$("#llast").addClass("prev").removeClass("unclick");
		fn$("#lnext").addClass("prev").removeClass("unclick");		
	 }
	 if(g_pageCount != 0){	   
	   fn$("#btnGo").attr("disabled", false);
	 }else{
		fn$("#btnGo").attr("disabled", true);
	 }		
	 fn$("#sppgindex").html(g_currentPage);
	 fn$("#sppgcount").html(g_pageCount);
}
 
function fnInit(){		
	 fn$("#btnGo").click(
	   function(){
		 fnGotoPage("Custom");	  
	   }
	 );
	 fn$("#lnext").click(
	   function(){
		 fnGotoPage("Next");	  
	   }
	 );
	 fn$("#lprev").click(
	   function(){
		 fnGotoPage("Previous");	  
	   }
	 );
	  fn$("#lfst").click(
	   function(){
		 fnGotoPage("First");	  
	   }
	 );
	 fn$("#llast").click(
	   function(){
		 fnGotoPage("Last");	  
	   }
	 );

	 g_currentPage = 1;
	 fnGetFtc();
}
 

function fnGotoPage(strPage){
  g_oldPageIndex = 	g_currentPage;
  switch(strPage){
	case "Custom":
	   var strPageIndex =  fn$("#txtPage").val();
	   if( strPageIndex == ""){
		 alert("请输入页码！");
		 return;
	   }
	   if(!fnChkData(4,strPageIndex)){
		 alert("页码只能为大于0的整数！");
		 return ;
	   }
	   strPageIndex = parseInt(strPageIndex);
	   if(strPageIndex < 1 || strPageIndex > g_pageCount){
		  alert("当前页码只能为1至" + g_pageCount +"的整数！");
		  return ;
	   }
	   if(strPageIndex ==  g_currentPage) return;
	   g_currentPage = strPageIndex;
	   break;
	 case "Next":
	   g_currentPage++;
	   break;
	 case "Previous":
	   g_currentPage--;
	   break;	 
	 case "First":
	   g_currentPage = 1;
	   break;
	 case "Last":
	   g_currentPage = g_pageCount;
	   break;	 
  }
  fnGetFtc();
}


function fnLoadContent(ctrl){	
  if(ctrl == null) return;
  fn$.ajax({
	   type:"POST",
	   data:"action=4&htmFileName="+ctrl.getAttribute("file"),	 					 
	   url:"services/infoList.php",
	   success:function(msg){
		  msg = msg.replace(/width=[\'|\"](\d)*(px)?[\'|\"]/gi,"");
		  msg = msg.replace(/height=[\'|\"](\d)*(px)?[\'|\"]/gi,"");
		  var obj = fn$("<div>"+msg+"</div>");		 
		  obj.find("img").each(
			  function(){
				 var img = new Image();
				 img.src = fn$(this).attr("src");
				 var iWidth = img.width;
				 if(iWidth > 290){
					 fn$(this).width(290);
					 fn$(this).height(img.height * 290 /iWidth);					 
				 }				
			  }
		 );			 
		 fn$("#dvContent").html(msg);
		 fn$("#spTitle").html(ctrl.getAttribute("title"));
		 fn$("#spName").html(ctrl.getAttribute("author"));
		 fn$("#link").attr("href", g_strForumUrl + ctrl.getAttribute("pid"));		
		 fn$("#dvReply").show();
	   },
	   error:function(){		 
		 alert("下载留言内容失败！");		
	   }  		   
}); 
}	 

 