
/*                                                 
* Comments:商户类型显示                                                                   
* Author：黄孟俊
* QQ: 240713484
* Create Date  
* Modified By                                      
* Modified Date:                                      
* Why & What is modified      
* Company:上海华腾系统软件有限公司                                         
*/

var mchnttype=function(){
	this.name="mchnttype";
	this.typeData=[];  //商户类型
	this.checkedTypeData={};
	this.mchntData=[]; //当前查询结果商户信息列表
	this.type_dtl_panel="#mchnttype_dtl";
	this.mchnt_dtl_panel="#mchntinfo_dtl";
	this.mchntlist_panel="#mchntlist_panel";
	this.dlBody="";
	this.listPanel="";
	this.order="";
	this.pages="#pages";
	this.reqPage=1;
	this.mchntid="";
	this.isList=true;
   
	 

	this.init=function(isList){
		this.isList=isList;
		this.ajaxMchnttype();
	}
	
	this.getCss = function()
	{
	  var csss=0;
	
	$.each(this.typeData,function(entryIndex,entry){
	     var css=1;
		$.each(entry.children,function(entryIndex1,entry1){  
	     css=css+1; 
	 })
	 csss =csss+ Math.ceil((css/12));
	})	
	return csss+1;
	}
	
	
	this.createTypeDiv=function(){ 
		var css3 = this.getCss();
		$(this.type_dtl_panel).html('');
      	 var div;
      	 if(css3<4)
      	 { div=$('<div id="mchnttype_dtl4"></div>').appendTo(this.type_dtl_panel);
      	 }
		else if(css3==4)
		{
       		  div=$('<div id="mchnttype_dtl4"></div>').appendTo(this.type_dtl_panel);
      	 }else if(css3==5)
		{
       		  div=$('<div id="mchnttype_dtl5"></div>').appendTo(this.type_dtl_panel);
      	 }else if(css3==6)
		{
       		  div=$('<div id="mchnttype_dtl6"></div>').appendTo(this.type_dtl_panel);
      	 }else if(css3==7)
		{
       		  div=$('<div id="mchnttype_dtl7"></div>').appendTo(this.type_dtl_panel);
      	 }else if(css3==8)
		{
       		  div=$('<div id="mchnttype_dtl8"></div>').appendTo(this.type_dtl_panel);
      	 }else if(css3==9)
		{
       		  div=$('<div id="mchnttype_dtl9"></div>').appendTo(this.type_dtl_panel);
      	 } 
	  var table=$('<table ></table>').appendTo(div);

		var self=this;
		$.each(this.typeData,function(entryIndex,entry){  
			var tr=$('<tr></tr>').appendTo(table);
			var td1=$('<td vAlign="baseline"></td>').attr({'class':'mchnttype_title'}).appendTo(tr);
			$('<div ></div>').html(entry.name+"").appendTo(td1);
			var td2=$('<td ></td>').appendTo(tr);	
			var span=$('<span></span>').appendTo(td2);
			var a=$('<a style="cursor:pointer"></a>').html('不限').appendTo(span);
			if(entry.checked){
				a.click(function(){
					self.clearCheck(false,entry,-1);
					self.reqPage=1;
					self.ajaxMchntInfo();
				});
			}else{
				span.attr({'class':'mchnttype_check'});
			}	
			
			$.each(entry.children,function(entryIndex1,entry1){  
				var span1=$('<span></span>').appendTo(td2);
				var a1=$('<a style="cursor:pointer;"></a>').html(entry1.name).appendTo(span1);
				
				if(entry1.checked){
					span1.attr({'class':'mchnttype_check'});
				}else{
					a1.hover(function(){
				span1.addClass('mchnttype_check a');
				},function(){
				span1.removeClass('mchnttype_check a');
				}
				);
					a1.click(function(){
						self.clearCheck(true,entry,entryIndex1);
						self.reqPage=1;
						self.ajaxMchntInfo();
					});
				}
				
			});
			
		});
		
		
		var tr=$('<tr></tr>').appendTo(table);
		var td1=$('<td></td>').attr({'class':'mchnttype_title'}).appendTo(tr);
		$('<div></div>').html("名称/地址").appendTo(td1);
		var td2=$('<td></td>').html("<input type=\"text\" id=\"query\"  name=\"query\" class=\"text-mchnt\"  > <input id=\"query_sub\" type=\"submit\" class=\"btn-6\" value=\"搜索\" >").appendTo(tr);
		
		$("#query_sub").click(function(){
			self.reqPage=1;
			self.ajaxMchntInfo();
		});
		
		$("#order_new").click(function(){
			$("#order_new").attr({'class':'mchnt_sort_li_hover_new'});
			$("#order_hot").attr({'class':'mchnt_sort_hot'});
			self.order="addtime";
			self.reqPage=1;
			self.ajaxMchntInfo();
		});
		
		$("#order_hot").click(function(){
			$("#order_hot").attr({'class':'mchnt_sort_li_hover_hot'});
			$("#order_new").attr({'class':'mchnt_sort_new'});
			self.order="commendpoint";
			self.reqPage=1;
			self.ajaxMchntInfo();
		});	
		
	}
	
	
	
	this.createMchntDiv=function(){
		
		$(this.mchnt_dtl_panel).html('');
		var dls="";
		var self=this;
		var count=0;
		dls+="<table style=\"border-collapse:collapse;\" bordercolor=\"#CDCDCD\"  rules=\"rows\" frame=hsides>";
		$.each(this.mchntData,function(entryIndex,entry){
			var dl=self.dlBody;
			if(count%2==0){
			dls+="<tr>";
			
			}
			dls+="<td>";
			dl=dl.replace(/reg:mchntid/g,entry.mchntid);
			dl=dl.replace(/reg:mchntname/g,entry.mchntname);
			dl=dl.replace(/reg:acttype/g,entry.acttype);
			dl=dl.replace(/reg:mchnttel/g,entry.mchnttel);
			dl=dl.replace(/reg:mchntaddr/g,entry.mchntaddr);
			dl=dl.replace(/reg:newstr/g,entry.newstr);
			dl=dl.replace(/reg:mchntdesc/g,entry.mchntdesc2);
			dl=dl.replace(/reg:logourl/g,entry.logourl==null||entry.logourl==""?"default.jpg":entry.logourl); 
			dl=dl.replace(/reg:hot/g,entry.pghtml);
			dl=dl.replace(/reg:others/g,entry.othercount==0?" ":"[查看其它分店]");
			count++;
			dls+=dl;
			dls+="</td>";
			if(count%2==0){
			dls+="</tr>";
			
			}
			//if(count%2==0)dls+="<hr style=\"color:#CDCDCD\"/>";
			
		});
		dls+="</table>";
		$(this.mchnt_dtl_panel).html(dls);
		$(".other").click(function(){
			self.mchntid=$(this).attr("val");
			self.clearAllCheck();
			self.reqPage=1;
			self.ajaxMchntInfo();
		});
		$("#mchntinfo_dtl td").hover(
		
		function(){
				$(this).addClass('mchntinfo_dtl_dl_hover');
				},function(){
				$(this).removeClass('mchntinfo_dtl_dl_hover');
				
				}
		);
	}
	
	
	this.ajaxMchnttype=function(){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/net/common/ntmchnttypes.json",
			dataType:'json',
			success:function(result){
				self.typeData=result;
				self.createTypeDiv();
				if(self.isList)self.ajaxMchntInfo();
			}
		}); 
	}

	this.ajaxMchntInfo=function(){
		var self=this;
		if(!self.isList){
			this.initListPanel();
			self.isList=true;
			$(self.mchntlist_panel).html(this.listPanel);
		}
		
		$.ajax({
			type:"post",
			url: "/smknet/mchnt.do?method=query&"+self.getPars(),
			contentType:'text/html;charset=UTF-8',
			dataType:'json',
			success:function(result){
				self.mchntData=result.entitys;
				self.createMchntDiv();
				self.createAlinks(result.pagesStr);
			}
		}); 
	}
	
	this.createAlinks=function(pagesStr){
		$(this.pages).html(pagesStr);
		var self=this;
		$(this.pages).find("a").click(function(){
			self.reqPage=parseInt($(this).attr('title'));
			self.isPage=true;
			self.ajaxMchntInfo();
		});
	}
	
	this.clearCheck=function(checked,oneData,num){
		oneData.checked=checked;
		$.each(oneData.children,function(entryIndex,entry){  
			entry.checked=(entryIndex==num);
		});
		if(checked){
			this.checkedTypeData[oneData.id]=oneData.children[num];
		}else{
			this.checkedTypeData[oneData.id]=false;
		}
		this.createTypeDiv();
	}
	
	this.clearAllCheck=function(){
		var self=this;
		$.each(this.typeData,function(entryIndex,entry){  
			entry.checked=false;
			$.each(entry.children,function(entryIndex1,entry1){
				entry1.checked=false;
			});
			self.checkedTypeData[entry.id]=false;
		});
		this.createTypeDiv();
		
	}
	
	
	
	this.getPars=function(){
		var subPars={};
		var pars;
		var self=this;
		$.each(this.typeData,function(entryIndex,entry){  
			if(entry.checked){
				$.each(entry.children,function(entryIndex1,entry1){  
					if(entry1.checked){
						pars+="&"+[self.name]+"="+entry1.id;
					}
				});
			}
		});
		if(self.order!=null&&self.order!=""){
			subPars["order"]=self.order;
		}
		var query=$("#query").attr("value");
		if(query!=null&&query!=""){
			subPars["query"]=encodeURIComponent(query);
		}
		if(self.isPage){
			self.isPage=!self.isPage;
		}else{
			self.reqPage=1;
		}
		if(self.mchntid!=null&&self.mchntid!=""){
			subPars["mchntid"]=self.mchntid;
			self.mchntid="";
		}
		subPars["reqPage"]=self.reqPage;
		return pars+"&"+jQuery.param(subPars);
	}
	/**
	this.initDlBody=function(){
		this.dlBody="";
		this.dlBody+="<dl>                                                                                                                        ";
		this.dlBody+="	<dt class=\"fleft\"><div style='vertical-align:middle'><br/><a href=\"/smknet/net/views/reg:mchntid.html\"><img class=\"imgborder_1\" src=\"/smknet/upload/ntmchntinfotb/reg:logourl\" width=\"98px\" height=\"70px\" ></a></div></dt> ";
		this.dlBody+="	<dd>                                                                                                                       ";
		this.dlBody+="		<br/><ul class=\"mchntinfo_dtl_title\">                                                                                       ";
		this.dlBody+="			<li style=\"width:320px;\"><h5>reg:acttype<a href=\"/smknet/net/views/reg:mchntid.html\" >reg:mchntname</a>reg:newstr</h5></li>                                                                                            ";
		this.dlBody+="			<li style=\"text-align:left;width:120px; margin-top:-4px;\">热度：reg:hot</li>                                                                                                       ";
		this.dlBody+="			<li class=\"other\" style=\"cursor:pointer;width:90px;\" val=\"reg:mchntid\">reg:others</li>                                                                                ";
		this.dlBody+="		</ul>                                                                                                                    ";
		this.dlBody+="		                                                                                                    ";
		this.dlBody+="		<ul class=\"mchnt_addr\">                                                                                                ";
		this.dlBody+="			<li style=\"color:#767676\">reg:mchntdesc</li>                                                                                             ";
		this.dlBody+="			<li>地址：reg:mchntaddr</li>                                                                                             ";
		this.dlBody+="			<li>电话：reg:mchnttel</li>                                                                                              ";
		this.dlBody+="		</ul>                                                                                                                 ";
		this.dlBody+="	</dd>                                                                                                                      ";
		this.dlBody+="</dl>                                                                                                                       ";
	}
	**/
	
	this.initDlBody=function(){
		this.dlBody="";
		this.dlBody+="<table aligh=\"center\" style=\"margin-top:15px;margin-bottom:15px; display:inline;\" width=\"350\" border=\"0\"> ";
		this.dlBody+=" <tr> 																";
		this.dlBody+="    <td rowspan=\"5\" width=\"110\" vAlign=\"baseline\"><div style='vertical-align:middle'><br/><a href=\"/smknet/net/views/reg:mchntid.html\"><img class=\"imgborder_1\" src=\"/smknet/upload/ntmchntinfotb/reg:logourl\" width=\"98px\" height=\"70px\" ></a></div></td> 												";
		this.dlBody+="    <td width=\"210\"><h5>reg:acttype<a href=\"/smknet/net/views/reg:mchntid.html\" >reg:mchntname</a>reg:newstr</h5></td>								 ";
		this.dlBody+="  </tr>															 ";
		this.dlBody+="  <tr> 												";
		this.dlBody+="    <td>地址：reg:mchntaddr</td> 												";
		this.dlBody+="  </tr> 													";
		this.dlBody+="  <tr> 													";
		this.dlBody+="    <td>电话：reg:mchnttel</td>											 ";
		this.dlBody+="  </tr>												 ";
		this.dlBody+="  <tr> 										";
		this.dlBody+="    <td class=\"other\" style=\"cursor:pointer;width:90px;\" val=\"reg:mchntid\">reg:others</td> 												";
		this.dlBody+="  </tr> 													";
		this.dlBody+="  <tr> 											";
		this.dlBody+="    <td>热度：reg:hot</td>													 ";
		this.dlBody+="  </tr>													 ";
		this.dlBody+="</table>              													    ";                                                                                                  
	}


	this.initDlBody();
	
	this.initListPanel=function(){
		this.listPanel="";
		if(navigator.appName=='Netscape'){
			this.listPanel+="<div class=\"mchnt_list_title w700 center mt2\" >                 "; 
			this.listPanel+="	<div class=\"fright\">                                           ";
			this.listPanel+="		<dl id=\"firechange\">                                                           ";
			this.listPanel+="			<dd >                                                        ";
			this.listPanel+="			<dt class=\"fleft\">排序：</dt>                              ";
			this.listPanel+="				<ul class=\"mchnt_sort\">                                  ";
			this.listPanel+="					<li id=\"order_new\" class=\"mchnt_sort_new\">&nbsp;&nbsp;&nbsp;最新</li>";
			this.listPanel+="					<li id=\"order_hot\" class=\"mchnt_sort_hot\">&nbsp;&nbsp;&nbsp;最热</li>  ";
			this.listPanel+="				</ul>                                                      ";
			this.listPanel+="			</dd>                                                        ";
			this.listPanel+="		</dl>                                                          ";
			this.listPanel+="	</div>                                                           ";
			this.listPanel+="	<div><strong>特约商户</strong></div>                             ";
			this.listPanel+="</div>                                                            ";
			this.listPanel+="<div class=\"w700 center\" id=\"mchntinfo_dtl\"></div>            ";
			this.listPanel+="<div id=\"pages\" class=\"pages1 mt6 fright\" ></div>             ";
		}else{
			this.listPanel+="<div class=\"mchnt_list_title w700 center mt2\" >                 "; 
			this.listPanel+="	<div class=\"fright\">                                           ";
			this.listPanel+="		<dl id=\"firechange\">                                                           ";
			this.listPanel+="			<dt class=\"fleft\">排序：</dt>                              ";
			this.listPanel+="			<dd >                                                        ";
			this.listPanel+="				<ul class=\"mchnt_sort\">                                  ";
			this.listPanel+="					<li id=\"order_new\" class=\"mchnt_sort_new\">&nbsp;&nbsp;&nbsp;最新</li>";
			this.listPanel+="					<li id=\"order_hot\" class=\"mchnt_sort_hot\">&nbsp;&nbsp;&nbsp;最热</li>  ";
			this.listPanel+="				</ul>                                                      ";
			this.listPanel+="			</dd>                                                        ";
			this.listPanel+="		</dl>                                                          ";
			this.listPanel+="	</div>                                                           ";
			this.listPanel+="	<div><strong>特约商户</strong></div>                             ";
			this.listPanel+="</div>                                                            ";
			this.listPanel+="<div class=\"w700 center\" id=\"mchntinfo_dtl\"></div>            ";
			this.listPanel+="<div id=\"pages\" class=\"pages1 mt6 fright\" ></div>             ";
		}
	}
}




