var g_currentPage = 1;
var g_pageCount = 0;
var g_oldPageIndex;
var g_pageSize = 12;
var g_typeId = 0, g_typeName = "";
var g_pageLoc = 0;
function fnGetGoods(){
   fn$("#dvgoods").html("<table width='100%' height='400px' align='center'><tr><td valign='middle' align='center'> <img src='./siteImages/loading.gif'/><font color='red'>正在加载分类<<"+g_typeName+">>下的商品信息...</font></td></tr></table>");  
   g_pageSize = 12;
	fn$.ajax({
		   type:"POST",
		   data:"action=10&pageIndex="+g_currentPage+"&pageSize=" + g_pageSize+"&typeId=" + g_typeId,                   
		   url:"services/infoList.php",
		   success:function(msg){
			  var xmlDoc  = fn$(msg);
			  var spsNode = xmlDoc.find("root sps");			
			  if(spsNode == null || spsNode.size() == 0){
				 g_pageCount = 0;
				 g_currentPage = 0;				 			 
				 fn$("#dvgoods").html("<table width='100%' height='400px' align='center'><tr><td valign='middle' align='center'><font color='red'>分类<<"+g_typeName+">>不存在商品!</font></td></tr></table>");							 
			  }else{
				  var strHtml = "";
				  spsNode.find("sp").each(		
					  function(){                 
					    var spNode = fn$(this);
						
						var strSpUrl = "goods.php?spid=" + spNode.find("id").text();						
						var strName =  spNode.find("name").text();
						strHtml += '<li><a href="'+strSpUrl+'" title="'+strName+'" class="img" target="_blank"><img onload="fnResizeImg(0,this,138,117);" src="'
						              +spNode.find("pic").text()+'" alt="'+strName+'"/></a><div>';
						strHtml += '<a href="'+strSpUrl+'" title="'+strName+'" target="_blank">'+strName+'</a></div></li>';		
						
					}					
				 );		
				fn$("#dvgoods").html('<ul class="plist">' + 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);
				  }									
			  }
			   fnUpdateNavigation();			  
		   },
		   error:function(){
			 fn$("#pgCtrl1").hide();
			 fn$("#dvgoods").html("<table width='100%' height='400px' align='center'><tr><td valign='middle' align='center'><font color='red'>获得分类<<"+g_typeName+">>下的商品信息失败!</font></td></tr></table>");							 
			 		
			 alert("获得分类<<"+g_typeName+">>下的商品信息失败，请稍候重试");  			 
			 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");	  
	   }
	 );
     if(g_ctrlId != ""){
		 fnGetSpByType($(g_ctrlId));
	 }else{
		 fnGetSpByType($("t1"));
	 }	 
}
 

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;	 
  }
  fnGetGoods();
}

function fnGetSpByType(ctrl){
	if(ctrl == null) return;
	if (g_pageLoc ==0){
		fn$("#ulsptype .csptype").removeClass("cselmenu");
		fn$(ctrl).addClass("cselmenu");
		g_currentPage = 1; g_typeId = ctrl.getAttribute("tid") ; g_typeName = ctrl.getAttribute("title");
		
		fnGetGoods();
	}else{
		ctrl.setAttribute("href", "shop.php?id="+ctrl.id);
		ctrl.target="_blank";
	}
	
}
