var i=0;
var titlename=commtitlename;
var varname='';
var dyarr=''; //调用的数据数组
var commnums=300;
var commnum=300;
var replytconf='';
var isdel='';
var ispage='';//false/true
var isuser='';//是否登录评论
var jsconbuttoncla='pl_tjbutton';
var jsconborder='#000';
var conaddrs='top';
var level='';
var twolevel='';
var portitle='';
var checkcode='false';
var sorttype='';
var trend='';
var nums='';
var hnums='';
var isdel='';
var showtrendid='';
var showcomment='';
var mod_replay='';
var fun_replay='';
var file_replay='';
var muserid='';//作者id
function rmd(imgurl){
	i++;
    $('#'+imgurl).attr('src','/comment/Lib/yanzheng.php?id=='+i);
}

function setmsg(){
	document.getElementById("msg").innerHTML='';
}

///添加回复html
function reply_dyn(id,rid,uid,name,username,sort,showcomid,sanjiaoclass,mod,fun,file,showmid){
	muserid=uid;
	mod_replay=mod;
	fun_replay=fun;
	file_replay=file
	$('.comment_tex_shuru1').show()
	/*$('#commentadd_'+id).hide();*/
	$('.comment_replycontss').html("");
	var html="";
	html+='<div class="comment_tex_shuru1"><span class="'+sanjiaoclass+'"></span>';
    html+='<div><textarea class="comment_textarea" onkeypress="checkreply_dyn(this,event,'+id+','+rid+','+uid+',\''+sort+'\',\''+name+'\',\''+username+'\',\''+showcomid+'\',\''+mod+'\',\''+fun+'\',\''+file+'\','+showmid+')" onfocus=replyshowcode(this,"","replychicktext_dyn(this,\''+name+'\','+id+','+showmid+')",\''+name+'\',\''+username+'\') name="content"   id="contre">'+commReplyText+'</textarea>';
	html+='<div class="comment_user"><b id="msgre" style="float:left;" class="comment_red"></b><span class="comment_yanzre" style="display:none;float:left;"> <b>'+Code+'：</b><input type="text" id="checkcodere" onfocus=showprom("contre") onBlur="removeprom()" onkeypress="checkreply_dyn(this,event,'+id+','+rid+',\''+sort+'\',\''+name+'\',\''+username+'\',\''+showcomid+'\',\''+mod+'\',\''+fun+'\',\''+file+'\','+showmid+')" name="checkcode" maxlength="4" size="4" class="input_input" style="width:60px;"><img  align="absmiddle" alt="'+Code+'" hspace="5" id="randimgre"  height="" border=0></span></div></div>';
    html+='</div>';
	$('#commentadd_'+showmid).hide();
	$("#reply"+showmid).html(html);
	$('#contre').focus();
	$('#contre').css("height","35px");
	
}
function addcommentajax(mid,uid,sort,showcomid,showaddcomm){
	var html='';
html+='<span class="comm_ajax_sanBg"></span>';
html+='<textarea style="width:500px;" onblur="chicktext_dyn(this,'+mid+',true)" onfocus=showcode_dyn(this,\'\',"chicktext_dyn(this,'+mid+',true)") name="content" rows=5 id="cont_ajax_'+mid+'" class="comment_textarea" onkeypress="check_dyn(this,event,'+mid+','+uid+',\''+sort+'\',\''+showcomid+'\',true)">'+commText+'</textarea>'; 
html+='<input id="showtrendid" type="hidden"><input id="showall_yes_no" type="hidden" value="false">';
html+='<div id="msg_ajax_'+mid+'"></div>';
$('#'+showaddcomm).html(html);
	
}
/***********
**** 验证方法
************/
function showcode_dyn(thiscon,fun,blurfun){
	onfocusmouse(thiscon,fun,blurfun);
	//alert('fdsaf')
	var con=thiscon.value;
	if($.trim(con)==commText)
	thiscon.value='';
	$('#'+thiscon.id).css("height","35px");
	if($(".comment_yanz").is(":hidden") && checkcode=='true'){
		$(".comment_yanz").show();
		rmd('randimg');
	}
}
///显示回复的验证码
function replyshowcode(thiscon,fun,blurfun,name,username){
	onfocusmouse(thiscon,fun,blurfun);
	var con=$('#contre').val();
	var replyname=Replyuser+name+"："
	if($.trim(con)==commReplyText || $.trim(con)==replyname)
	$('#contre').val(replyname)
	if($(".comment_yanzre").is(":hidden")  && checkcode=='true'){
		$(".comment_yanzre").show();
		rmd('randimgre');
	}
}


//检查评论内容
function chicktext_dyn(thiscon,id,yes_ajax){	
	if(yes_ajax==undefined)
		var conid='#cont_'+id;
	else
		var conid='#cont_ajax_'+id;
		
	var cont=$.trim($(conid).val());
	if(cont==''){
		$(conid).css("height","20px");
		$(conid).val(commText);
	}
}
//检查回复内容
function replychicktext_dyn(thiscon,name,id,showmid){
	var cont=$.trim($('#contre').val());
	var replyname=Replyuser+name+"："
	if(cont=='' || cont==replyname){
		$('#contre').val(commReplyText)
		$('.comment_replycontss').html("");
		$('#commentadd_'+showmid).show();
	}
}

///检查评论内容并提交
function check_dyn(thiscon,e,mid,uid,sort,showcomid,yes_ajax,mod,fun,file,showmid){
	 var postvalue=$.trim(thiscon.value);
	 var content= thiscon.value; 
	if(e.ctrlKey  ){
		/*
			if(yes_ajax==undefined)
				var conid='#cont_'+mid;
			else
				var conid='#cont_ajax_'+mid;
		
			 var tagname= $(conid).attr("tagName");
			  if(tagname=='textarea' || tagname=='TEXTAREA'){
				  thiscon.value += "\n";
				 
			  }
			  */
			  thiscon.value += "\n";
			
	}else if(e.keyCode==13){
			if(checkcode=='true')
			var code=document.getElementById("checkcode").value;
			
			var content=thiscon.value;
			var location=window.location.href;
			
			if($.trim(content)==""||$.trim(content)==commText){
				alert(commContentText);
				return ;
			}else if($.trim(content).length>commnum){
				alert(commStrMax+commnum+"!");
				return ;
			}else if(code==""){
				alert(commTextCon+Code+"！");
				return ;
			}else if(isuser){
				alert(commLogin)
				return ;
			}else{
				/*if(sort=="blog"){  
					//加入到群聊提示
					art.dialog( {
						id : "_twalert",
						title : +groupChatTips,
						content :groupChatContent,
						width : '270px',
						height : '60px',
						lock : true
					}, function() {
						var fayan=content;
						fayan.replace(/\n/g, "<br>")
						$.post("/chat/?act=insert",{oid:1,content:to+varname+blogcomment+content},function(data){
							addComment_dyn(mid,uid,sort,content,location,showcomid,yes_ajax,mod,fun,file,showmid);
							if(data==1){
								_alert('"'+commContentError+'"','"'+Tips+'"');
							}
						});
					}, function() {
						addComment_dyn(mid,uid,sort,content,location,showcomid,yes_ajax,mod,fun,file,showmid);
					});
				}else{*/
						addComment_dyn(mid,uid,sort,content,location,showcomid,yes_ajax,mod,fun,file,showmid);
				/*}*/
			}
	}
}

/////检查回复内容+
function checkreply_dyn(thiscon,e,mid,rid,uid,sort,name,username,showcomid,mod,fun,file,showmid){
	 var postvalue=$.trim($('#contre').val());
	 var content= $('#contre').val();  
	if(e.ctrlKey && e.keyCode==13 || e.ctrlKey && e.keyCode==10){
			 var tagname= $("#contre").attr("tagName");
			  if(tagname=='textarea' || tagname=='TEXTAREA'){
			 	thiscon.value 	;
			  }
	}else if(e.keyCode==13){
		if(checkcode=='true')
		var code=document.getElementById("checkcodere").value;
		var content=document.getElementById("contre").value;
		var location=window.location.href;
		var replyname=Replyuser+name+"："
		if($.trim(content)==""||$.trim(content)==commReplyText||$.trim(content)==replyname){
			alert(commReplyContentText);
			return ;
		}else if($.trim(content).length>commnums){
			alert(commStrMax+commnums+"!");
			return ;
		}else if(code==""){
			alert(commTextCon+Code+"！");
			return ;
		}else if(isuser){
			alert(commLogin)
			return ;
		}else{
			if(content.indexOf(replyname)<0){
				rid=0
			}
			re=new RegExp(replyname,"g"); 
			var urlname=commReply+'<a href="'+username+'" >@'+name+"</a>:&nbsp;"
			
			var replayurlname='<a href="'+username+'" >'+name+"</a>&nbsp;"
			
			var cons=content.replace(re,urlname); 
			
			addreply(rid,mid,uid,sort,cons,location,showcomid,replayurlname,urlname,mod,fun,file,showmid);
		}
	}
}

//处理添加后的返回值

function rs_dyn(val,mid,sort,content,showcomid,location,urlname,imgurl,msg,msgcon,yes_replay,yes_ajax,showmid){
	if(val==0){
		_alert(commUrlError); 
		return ;
	}
	val=val.split(",");
	if(val[0]==0){
		rmd(imgurl);
		_alert(commCodeError);
		return ;
		
	}else if(val[0]==2){
		_alert(commText30);
		return ;
	}else if(val[0]==3){
		_alert(commContentError);
		return ;
	}else if(val[0]==4){
		_alert(commLogin);
		return ;
	}else if(val[0]==1){
		if(msg!=''&& msg!=undefined)
		document.getElementById(msg).innerHTML=msgcon;
		//dyncommtenshow(mid,sort,content,showcomid,location,urlname,msg,msgcon)
		//rmd(imgurl);
		//setTimeout(setmsg,2000);
		//document.getElementById("checkcode").value='';
		//document.getElementById("cont").value='';
		var dis=$('#dyncomment_'+showmid).attr("display");
		if(dis=='none' || dis==undefined){
			$('#dyncomment_'+showmid).show();
		};
		
		if(yes_ajax!=undefined&&yes_ajax!=''){
			$('#commajax_num_'+showmid).html(parseInt($('#commajax_num_'+showmid).html())+1);
			replayhtmladd(val[1],val[val.length-1],sort,content,location,urlname,yes_ajax,showmid);
		}else{
			
			$('#comment_num_'+showmid).html(parseInt($('#comment_num_'+showmid).html())+1);
			replayhtmladd(val[1],val[val.length-1],sort,content,location,urlname,yes_replay,showmid);
		}
		if(msg!=''&& msg!=undefined)
		document.getElementById(msg).innerHTML='';
		//getTrendComment_dyn(val[1],sort,'','dyncomment_'+mid,10,300);
		
		/*getTrendComment(195756,'blog','6991,7036','commentlisttrend_195756',10,300)*/
		return ;
	}else{
		_alert(commError);
	}
}
/**********
****处理数据
***********/
/////处理返回来的数据
function result_dyn(val,id,sort,mod,fun,comid,file,item,isdel,showcomment,num,hnum,tbconf,showmid,muserid) {
	var content="";
	var s=1;
	//showalltrend='false'
	var vals=eval(val);
	if(val!=''){
		for (var k=0;k<vals.length; k++) {	
			if(k!=vals.length-1 && k!=vals.length-2){
				content+=getHtml(k,vals[k],s,id,mod,fun,comid,file,item,isdel,sort,showcomment,tbconf,showmid,muserid);		
			}else if(k!=vals.length-2){
				getcountnum_syn(id,vals[k]['count_num']);
			}
		}
	}
	if(content==""){		
		var htmls=getDefault();
		$('#commentlist').html(htmls);
	}else{
		content+='';	
		content='<div class="comment_dongtaiBox"><input type="hidden" id="replykey_'+id+'" value='+id+','+sort+','+num+','+hnum+','+item+',300,300,'+mod+','+fun+','+comid+','+file+','+tbconf+' >'+content+'</div>';
		document.getElementById(showcomment).innerHTML=content;
		$("#msg_"+id).html('');
		$("#hadecomm_"+id).show();
		$("#dyncomment_"+id).show();
		$("#showcomm_"+id).hide();
		
	}	
}
//添加数据
function getHtml(k, obj,s,id,mod_replay,fun_replay,comid,file_replay,item,isdel,sort,showcomment,tbconf,showmid,muserid){
	var html='';
	html+='<div class="comment_neirong_huifu" id="comment'+obj['id']+'" style="" ><div class="comment_neir_hui_touxiang">';
	if (obj["uid"] != 0 ) {
		html+='<a href="' + obj["link"] + '" target="_blank" ><img src="' + obj["userpic"] + '" alt="' + obj["user"] + '" border="0"></a>\n';	   
	}else {
		html += '<img src="/images/43/43/'+ obj["userpic"]+'userm"  border="0">\n';
	}
    html+='</div><div class="comment_huifu"><p><span>';
	if(obj["uid"]!=0){
		html+='<a href="'+obj["link"]+'" target="_blank" >'+obj["user"]+' '+obj["getonline"]+'</a>';
	}else{
		html+='<a href="'+obj["link"]+'" target="_blank" >'+commNoUser+'</a>';
	}
	html+='</span>';
	if(obj["p_uid"] != 0 ){
		html+='<span class="comment_biaoti_huifu">'+commReply+'</span><span class="comment_dafu"><a href="'+obj["p_link"]+'">'+obj["p_user"]+'</a></span>';
	}
	html+='<label>'+obj["date"]+'</label></p>';
    html+='<p><label class="comment_huifu_yuju">'+obj["content"]+'</label><label class="comment_huifucaozuo"><a href="javascript:void(0)" onclick="reply_dyn('+id+','+obj['id']+','+muserid+',\''+obj["user"]+'\',\''+obj["link"]+'\',\''+sort+'\',\'\',\'\',\''+mod_replay+'\',\''+fun_replay+'\',\''+file_replay+'\','+showmid+')" >'+commReply+'</a></label>&nbsp;';
	
	if(isdel=='true'){
		html+='<label  class="comment_huifucaozuo"><a  id="huifu_gr" href="javascript:void(0)" usemap="#Map_{%$vol.id%}"  onclick="delcomm_dyn('+id+','+obj['id']+',\''+sort+'\')" >'+commDel+'</a></label></p>';
	}
	 
	/*html+='<div id="reply'+obj['id']+'" class="replycontss"></div>';*/
    html+='</div></div>';
	return html;
}


//添加动态的评论信息
function getcommdynHtml(username,nickname,mylogo,mid,id,sort,content,location,urlname,yes_replay,showmid){
	var html='';
	content=replacebr(content);
	html+='<div class="comment_neirong_huifu" id="comment'+id+'" style="" ><div class="comment_neir_hui_touxiang">';
	if (username!= '' ) {
		html+='<a href="/'+username+'" target="_blank" ><img src="'+mylogo+'" alt="'+nickname+'" border="0"></a>\n';	   
	}
    html+='</div><div class="comment_huifu"><p><span>';
	if(username!=''){
		html+='<a href="/'+username+'" target="_blank" >'+nickname+' <img src="/images/online1.png" title="'+Online+'" alt="'+Online+'" style="border:0px;" /></a>';
	}else{
		html+='<a href="/#" target="_blank" >'+commNoUser+'</a>';
	}
	html+='</span>';
	
	/*if(isdel==1){
		html+='<span class="comment_chu_zhao" onclick="delcomm('+obj['id']+','+id+',\''+mod+'\',\''+fun+'\',\''+comid+'\',\''+item+'\',\''+file+'\',\''+tbconf+'\')">'+commDel+'</span>';
	}*/
	if(yes_replay){
		html+='<span class="comment_biaoti_huifu">'+commReply+'</span><span class="comment_dafu">'+urlname+'</span>';
	}
	html+='<label>'+just+'</label></p>';
    html+='<p><label class="comment_huifu_yuju">'+content+'</label><label><a href="javascript:void(0)"onclick="reply_dyn('+mid+','+id+',\''+muserid+'\',\''+nickname+'\',\''+username+'\',\''+sort+'\',\'\',\'\',\''+mod_replay+'\',\''+fun_replay+'\',\''+file_replay+'\','+showmid+')" >'+commReply+'</a></label></p>';
	<!--html+='<div id="reply'+obj['id']+'" class="replycontss"></div>';-->
    html+='</div></div>';
	return html;
}
function replacebr(content){
	content=content.replace(/(\n)+/gi,'<br>');
	return content;
}


function getcountnum_syn(id,obj){
	if(obj!='undefined'){
		$('#show_num_'+id).html(obj['show_num']);
		$('#comment_num_'+id).html(obj['comment_num']);
		$('#collect_num_'+id).html(obj['collect_num']);
		$('#par_num_'+id).html(obj['par_num']);
		$('#like_num_'+id).html(obj['like_num']);
	}
	
}
//没有数据
function getDefault(){
	var html='<div class="notval" >';
    html+='      <span id="noCommentMsg">'+commNoCurrInfo+titlename+commInfo+'</span>';
	html+='</div>';
	return html;
}

function hideallcomment(id){
	$('#dyncomment_'+id).hide();
	$('#hadecomm_'+id).hide();
	$('#showcomm_'+id).show();
}
function showallcomment(id){
	$('#dyncomment_'+id).show();
	$('#hadecomm_'+id).show();
	$('#showcomm_'+id).hide();
}
function commentmap(id){
	window.location.href='#Map_'+id
	$('#cont_'+id).focus();
	$('#cont_'+id).val("");
}
function hidecomment(conid){
	$(conid).css("height","20px");
	$(conid).val(commText);
	$('#xihuan_btt').focus();
}

/********
*** 读取评论内容
****	id    			文章id
****	sort  			文章类别
****	showtrendid		已读取的评论id
****	showcomment		剩余的评论读取显示的div id
****	num				评论数
****	hnum			回复数未启用
****	item			类别id
****	mod				指模块名称
****	fun				回调函数的方法 名称
****	comid			页面显示评论的id
****	file			模型的存放文件
****	isdel			是否显示删除
****	ispage			是否分页
****	trend			是否动态
*********/
////读取评论内容
function getTrendComment_dyn(isdels,id,sort,showtrendids,showcomments,num,hnum,item,mod,fun,file,uid,showmid,comid,tbconf){
	showtrendid=showtrendids;
	showcomment=showcomments;
	isdel=isdels;
	muserid=uid;
	mod_replay=mod;
	fun_replay=fun;
	file_replay=file;
	$.get("/comment/index.php",{act:"getTrendComment",id:id,sort:sort,showtrendid:showtrendid},function (data){
		if(data=='0'){
			result_dyn("",id,sort,mod,fun,comid,file,item,isdel,showcomment,num,hnum,tbconf,showmid,muserid);
		}else{
			result_dyn(data,id,sort,mod,fun,comid,file,item,isdel,showcomment,num,hnum,tbconf,showmid,muserid);
		}
	});
}
//添加评论
function addComment_dyn(mid,uid,sort,content,location,showcomid,yes_ajax,mod,fun,file,showmid){
	content=$.trim(content,'\n')
	$.post("/comment/index.php?act=addComment",{isuser:true,muserid:uid,mid:mid,sort:sort,content:encodeURI(content),location:location,checkcode:'false',mod:mod,fun:fun,file:file},function (data){
		if(yes_ajax==undefined || yes_ajax==''){
				var conid='#cont_'+showmid;
				var msgid='msg_'+showmid;
		}else{
				var conid='#cont_'+showmid;
				var msgid='msg_'+showmid;
		}
		muserid=uid;
		$(conid).val(commText);
		//hidecomment(conid);
		rs_dyn(data,mid,sort,content,showcomid,location,'','randimg',msgid,commSuccess,'',yes_ajax,showmid);
		
	});
}

/////添加回复
function addreply(rid,mid,uid,sort,content,location,showcomid,replayurlname,urlname,mod,fun,file,showmid){
	$.post("/comment/index.php?act=addComment",{isuser:true,muserid:uid,rid:rid,mid:mid,sort:sort,content:encodeURI(content),location:location,checkcode:'false',mod:mod,fun:fun,file:file},function (data){
		$("#contre").val(commReplyText)
		re=new RegExp(urlname,"g"); 			 
		var cons=content.replace(re,'');//randimgre   
		rs_dyn(data,mid,sort,cons,showcomid,location,replayurlname,'randimgre','',commSuccessReply,true,'',showmid);																																						
		//rs(data,'','',showcomid,'','randimgre','msgre',commSuccessReply);
	});
}
//删除评论
function delcomm_dyn(mid,id,sort,showmid){
	$.post("/comment/index.php?act=delComment",{id:id,sort:sort,mid:mid},function (data){
		// window.location.reload();
		var vals=eval('('+data+')');
		///alert(vals['num'])
		if(vals['id']!=''){
			var valid=vals['id'];
		
			for (var k=0;k<valid.length; k++) {	
				$("#comment"+valid[k]['id']).hide();
			}
		}
		$("#comment"+id).hide();	
		$("#comment_num_"+showmid).text(parseInt($('#comment_num_'+showmid).text())-parseInt(vals['num']))
		$("#comment_sheng_num"+showmid).text(parseInt($('#comment_num_'+showmid).text())-1)
		
	});
}
//修改收藏数据
function collectnum_dyn(mid,username,sort,fatype,title,showmid){
	var rsalert='';
	//document.URL
	$.post('/passport/index.php?act=favorite',{cid:fatype,title:title,username:username,type:sort,id:mid},function(d){																			   
		if(d=='login'){
			rsalert=lanLogin;
		}else if(d=="again"){
			rsalert=lanFavoriteAgain;
		}else if(d=="success"){
				$.post("/comment/index.php?act=addComment_num",{sort:sort, mid:mid,textnum:'collect_num'},function(d){
				$('#collect_num_'+showmid).text(parseInt($('#collect_num_'+showmid).text())+1);
			});
			//rsalert=lanFavoriteSuccess;
		}else{
			rsalert=lanFrienSyserror;
		}
			if(rsalert!=''&& d!="success")
			alert(rsalert);
	});
	
}
function commend_dyn(name, id,mid,type,comment_num,yes_comment,alertval,sort,username){
	var url='/'+username+'/mood/view_'+sort+'_'+mid+'.html';
	commend(name, id,mid,type,comment_num,yes_comment,alertval,url)
	
}
///赞
function zan_dyn(id,username,sort,zuid,showmid){
	$.get("/comment/gloat.php?act=show&id="+id+"&zuid="+zuid+"&type="+sort,{url:'/'+username+'/mood/view_'+sort+'_'+id+'.html'},function(data){
		if(data!=-1){
			if(data==0)
			{
				_alert(lanzanno)
			}else if(data==2){
				_alert(lanzannozj)
			}else if(data==1){
				var zannum=parseInt($('#like_num_'+showmid).html())+1;
				$('#like_num_'+showmid).html(zannum);
				$.post("/comment/gloat.php?act=sendmail&id="+id,{uid:zuid,type:sort},function(){});
				$.post("/comment/index.php?act=addComment_num",{sort:sort, mid:id,textnum:'like_num'},function(d){});
				//_alert(lanzansucc)
			}else{
				_alert(lanRequestError1)
			}
				
			
		}else{
			_alert(lanLogin)
		}
	})
}
function showMaxPic(v)
	{
		$("#"+v).hide();
		$("#"+v+'_m').show();
	}
	function hideMaxPic(v)
	{
		$("#"+v).show();
		$("#"+v+'_m').hide();
	}
