
var isLogin=true;
/*
 * 商户信息页面加载启动�
 * author:黄孟�
 * QQ:240713484
 * date:2009-08-25
 */
$(document).ready(function(){
	htMenuTab("#nav_menu");
	htBtn();
	htSearch();
	htFolding();
	var comment=new htComment();
	comment.init();
	var n=new mchnttype();
	n.init(false);
	access();
	
});

var htFolding=function(){
	$("#btn_folding").click(function(){
		if($("#mchnt_others").hasClass("disable")){
			$("#mchnt_others").removeClass("disable");
			$(this).html("∧收起");
		}else{
			$("#mchnt_others").addClass("disable");
			$(this).html("∨展�开");
		}
	});
}


var access=function(){
	var mchntid=$("#mchntid").html();
	$.ajax({
		type:"POST",
		url: "/smknet/mchnt.do?method=access&mchntid="+mchntid,
		dataType:'json',
		success:function(result){
			if(result.rtnstatus==0){
				$("#hot_panel").html("热度"+result.pointsgradetb.pghtml);
			}else{
				alert(result.rtndesc);
			}
		}
	}); 
}



var htComment=function(){
	this.nmttid="";
	this.pages="#pages";
	this.dlBody="";
	this.replyBody="";
	this.comment_panel="#comments";
	this.commentData=[];
	this.reqPage=1;
	this.curSupportComment;
	this.curReplayComment;
	this.curReportComment;
	this.init=function(){
		this.initDlBody();
		this.initReplyBody();
		this.getNmttid();
		this.getComments();
		this.initCommentForm();
	}
	
	this.initCommentForm=function(){
		var self=this;
		$("#nccontent").click(function(){
			if(!isLogin){
				alert("您还没登录~�?");
				$("#nccontent").blur();
			}
		});
		
		$("#sub_content").click(function(){
			var nccontent=$("#nccontent").val();
			if(nccontent.length>=2&&nccontent.length<=250){
				var pars={};
				pars["ncobj"]=2;
				pars["nctype"]=1;
				pars["ncstate"]=1;
				pars["nmttid"]=self.nmttid;
				pars["nccontent"]=encodeURIComponent(nccontent);
				self.addComment(pars);
			}else{
				alert("字数不符~?");
			}
		});
	}
	
	this.getNmttid=function(){
		this.nmttid=$("#comment_mchntid").attr('title');
	}
	
	this.addComment=function(pars){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/comment.do?method=add&"+jQuery.param(pars),
			dataType:'json',
			success:function(result){
				$("#nccontent").val("");
				switch(result.rtnstatus){
					case 0:
						alert(result.rtndesc);
						self.getComments();
						break;
					case 99:
						alert(result.rtndesc);
						popup_show();
						break;
					default:	
						alert(result.rtndesc);
				}
			}
		}); 
	}
	
	this.supportComment=function(ncid){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/comment.do?method=support&ncid="+ncid,
			dataType:'json',
			success:function(result){
				switch(result.rtnstatus){
					case 0:
						alert(result.rtndesc);
						self.curSupportComment.html("<div></div>支持("+result.supportcount+")");
						break;
					case 99:
						alert(result.rtndesc);
						popup_show();
						break;
					case 60:
						alert(result.rtndesc);
						break;
					default:	
						alert(result.rtndesc);
				}	
			}
		}); 
	}
	
	this.reportComment=function(ncid){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/comment.do?method=report&ncid="+ncid,
			dataType:'json',
			success:function(result){
				switch(result.rtnstatus){
				case 0:
					alert(result.rtndesc);
					break;
				case 99:
					alert(result.rtndesc);
					popup_show();
					break;
				default:	
					alert(result.rtndesc);
				}
			}
		}); 
	}
	
	this.replyComment=function(pars){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/comment.do?method=reply&"+jQuery.param(pars),
			dataType:'json',
			success:function(result){
				switch(result.rtnstatus){
					case 0:
						alert(result.rtndesc);
						self.getComments();
						break;
					case 99:
						alert(result.rtndesc);
						popup_show();
						break;
					default:	
						alert(result.rtndesc);
				}
			}
		}); 
	}
	
	
	this.getComments=function(){
		var self=this;
		$.ajax({
			type:"POST",
			url: "/smknet/comment.do?method=query&reqPage="+self.reqPage+"&nmttid="+self.nmttid,
			dataType:'json',
			success:function(result){
				self.commentData=result.entitys;
				self.createComments();
				self.createAlinks(result.pagesStr);
			}
		}); 
		
	}
	
	this.createComments=function(){
		$(this.comment_panel).html("");
		var dls="";
		var self=this;
		$.each(this.commentData,function(entryIndex,entry){
			var dl=self.dlBody;
			dl=dl.replace(/reg:addtime/g,entry.addtime_format);
			dl=dl.replace(/reg:ncusername/g,entry.ncusername);
			dl=dl.replace(/reg:nccontent/g,entry.nccontent);
			dl=dl.replace(/reg:supportcount/g,entry.supportcount);
			dl=dl.replace(/reg:ncid/g,entry.ncid);
			dls+=dl;
		});
		$(this.comment_panel).html(dls);
		$(this.comment_panel).find("a[name='comment_support']").click(function(){
			self.curSupportComment=$(this);
			self.supportComment($(this).val());
		})
		$(this.comment_panel).find("a[name='comment_reply']").click(function(){
			var ncid=$(this).val();
			$("#replyDiv"+ncid).html(self.replyBody);
			$("#sub_cancel").click(function(){
				$("#replyDiv"+ncid).html("");
			});
			$("#sub_reply").click(function(){
				var replycontent=$("#replycontent").val();
				if(replycontent.length>=2&&replycontent.length<=250){
					var pars={};
					pars["ncobj"]=2;
					pars["nctype"]=2;
					pars["ncstate"]=1;
					pars["reportncid"]=ncid;
					pars["nmttid"]=self.nmttid;
					pars["nccontent"]=encodeURIComponent(replycontent);
					self.replyComment(pars);
				}else{
					alert("字数不符~�?");
				}
				
			});
			
			
		})
		$(this.comment_panel).find("a[name='comment_report']").click(function(){
			self.curReportComment=$(this);
			self.reportComment($(this).val());
		})
	}
	
	this.createAlinks=function(pagesStr){
		$(this.pages).html(pagesStr);
		var self=this;
		$(this.pages).find("a").click(function(){
			self.reqPage=parseInt($(this).val());
			self.getComments();
		});
	}
	
	this.initDlBody=function(){
		this.dlBody+="<dl  class=\"w700\">                                                                                                              ";
		this.dlBody+="	<dt>                                                                                                                           ";
		this.dlBody+="		<div class=\"replys_date fright\">[reg:addtime]</div>                                                                             ";
		this.dlBody+="		<div class=\"replys_user\">&nbsp;&nbsp;&nbsp;                                                                                  ";
		this.dlBody+="				reg:ncusername                                                                                                               ";
		this.dlBody+="		</div>                                                                                                                       ";
		this.dlBody+="	</dt>                                                                                                                          ";
		this.dlBody+="	<dd>&nbsp;&nbsp;&nbsp;&nbsp;reg:nccontent</dd>                                                                                    ";
		this.dlBody+="	<dd>                                                                                                                           ";
		this.dlBody+="	    <div id=\"replyDivreg:ncid\"></div>                                  ";
		this.dlBody+="		<div align=\"right\">                                                                                                          ";
		this.dlBody+="			<ul class=\"dianpin_btn\">                                                                                                   ";
		this.dlBody+="				<li><a class=\"ht_btn3 ht_btn_icon5\" name=\"comment_report\" value=\"reg:ncid\" target=\"_blank\" ><div></div>举报 </a></li>                    ";
		this.dlBody+="				<li><a class=\"ht_btn3 ht_btn_icon6\" name=\"comment_reply\" value=\"reg:ncid\" target=\"_blank\" ><div></div>回复</a></li>   ";
		this.dlBody+="				<li><a class=\"ht_btn4 ht_btn_icon4\" name=\"comment_support\" value=\"reg:ncid\" target=\"_blank\" ><div></div>支持(reg:supportcount)</a></li>   ";
		this.dlBody+="			</ul><br/>	                                                                                                                     ";
		this.dlBody+="		</div>                                                                                                                       ";
		this.dlBody+="	</dd>                                                                                                                          ";
		this.dlBody+="</dl>                                                                                                                           ";
	}
	
	this.initReplyBody=function(){
		this.replyBody+="<p><font class=\"font7 b\"><a name=\"a_comment\" >回复</a></font>(字数为2-250之间)</p>";
		this.replyBody+="<p><textarea  cols=\"40\" rows=\"4\" name=\"replynccontent\" id=\"replycontent\" ></textarea></p>";
		this.replyBody+="<p><input type=\"button\" value=\"提交\" class=\"btn-3\" id=\"sub_reply\" > &nbsp;";
		this.replyBody+="<input type=\"button\" value=\"取消\" class=\"btn-3\" id=\"sub_cancel\" ></p>";
	}
}


var addCollection=function(){
	var mchntid=$("#mchntid").html();
	var ncltype=1;
	$.ajax({
		type:"POST",
		url: "/smknet/collection.do?method=add&nclobjid="+mchntid+"&ncltype="+ncltype,
		dataType:'json',
		success:function(result){
			switch(result.rtnstatus){
			case 0:
				alert(result.rtndesc);
				window.open('/smknet/member.do?method=tocollection','_blank');
				break;
			case 99:
				alert(result.rtndesc);
				popup_show();
				break;
			default:	
				alert(result.rtndesc);
		
			}
		}
	});	
}


