// JavaScript Document
$(document).ready(function(){

									   
						   $("a[id='deletethread']").click(function(){
						   										
																   var $this=$(this);
																   var threadid=$(this).attr("threadid");
																  var barid = $(this).attr("barid");
																//  	alert(barid+ "--" + threadid);
																   var offsetleft=$this.offset().left;
																   var offsettop=$this.offset().top+$this.innerHeight();
																   $.get("jsp/bar/confirm.jsp",{threadid:threadid},function(data){
																  // 	alert(barid+ "--" + threadid);
																													   var $msgbox=$.msgbox.confirmbox("你要删除这个话题吗？",data,offsetleft,offsettop);
																													   $msgbox.find("#confirm").click(function(){
																																							   $.msgbox.confirmbox("","数据提交中...",offsetleft,offsettop);
																																							   $.post("portlet/bar/deleteThread.do",{threadId:threadid,barId:barid},function(data){
																																																					$.msgbox.confirmbox("","数据提交成功",offsetleft,offsettop);
																																																					
																																																					$.msgbox.closemsgbox(1000,function(){window.location.href="threadList.do?id={relate:'myThread',type='discuss',barId:"+barid+"}"})
																																																					
																																																					})
																																							   return false;
																																							   })
																													   
																													   })
																   
																   
																   
																   return false;
																   })

						   
						   $("#commentlist > li > .author > span > #toptopic").click(function(){
																						//	alert("to do top")
																							var topicid=$(this).attr("threadId");
																							return false;
																							})
						   
						   $("#commentlist > li > .author > span > #besttopic").click(function(){
																							// alert("to do best")
																							 return false;
																							 })
						   
						   
						   $("#commentlist > li > .author > span > #edittopicreply").click(function(){
																								// alert('edit');
																								 //alert('edit');
																								 showeditthreadpopwin(this);
																								 return false;
																								 })
						   
						   $("#commentlist > li > .author > span > #replytopicreply").click(function(){
																								  // alert('rep');
																								   var offsetleft=$(this).offset().left;
														var offsettop=$(this).offset().top+$(this).innerHeight();
						   								
													var mypoint=parseInt($("#mypoint").val());
													//alert(mypoint);
													if(mypoint<0){
													   $.msgbox.confirmbox("","你的银币为负！不能评论！",offsetleft,offsettop);
													} else {
																								   showreplyreplypopwin(this);
																								   return false;
																								   }
																								   })
						   
						   $("#commentlist > li > .author > span > #deletetopicreply").click(function(){
																										//alert('deletepost');
																									deletetopicreplyconfirm(this);
	
																									return false;
																									})
						   $("#addreply").click(function(){
						   								var offsetleft=$(this).offset().left;
														var offsettop=$(this).offset().top+$(this).innerHeight();
						   								
													var mypoint=parseInt($("#mypoint").val());
													//alert(mypoint);
													if(mypoint<0){
													   $.msgbox.confirmbox("","你的银币为负！不能评论！",offsetleft,offsettop);
													} else {
		
														 //$(this).after(" <span id=\"msg\">发表中</span>");
														 var $msg=$(this).next();//$("#msg");
														 var $replylist=$("#commentlist");
														 //window.setTimeout('$("#msg").remove()',3000);
														 var topicid=$(this).attr("threadId");
														 var replycontent=$("#replycontent").val();
														 
														 replycontent=replycontent.replace(/</g,"&lt;");
														replycontent=replycontent.replace(/>/g,"&gt;");
														//alert(replycontent);
														 var user = $("#user").val();
														//alert(topicid);
														 //ajax提交
														 var btitle=$.trim(replycontent);
														 if (btitle.length<1){
												    			$("#addreply").next().html("回复内容不能为空！");
												    			return false;
												    		}else{
												    			$("#addreply").next().html("");
												    		}
													//alert(replycontent)
														 $.ajax({
																url:"portlet/bar/postThread.do",
																type:"POST",
																data:{threadId:topicid,content:replycontent,user:user},//"threadId="+topicid+"&content="+replycontent+"&user="+user,
																success:function(html){
																	if (html!=""){
																			$replylist.append(html);
																			$msg.html("提交成功");
																			$("#replycontent").val("")
																			//要重新绑定事件
																			binddeletetopicreplyclick();
																			bindreplyreplyclick();
																			bindeditreplyclick();																			
																		}else{
																			$msg.html("提交失败")
																		}
																	window.setTimeout(function(){$msg.html("")},1000);
																	},
																error:function(){$msg.html("提交失败")},
																beforeSend:function(){$msg.html("提交中")},
																complete: function(){}
																})
																
																}
														 })
						   
						   })

function showreplyreplypopwin(obj){ //显示回复窗口
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
	var replyid=$(obj).attr("threadid");
	var others=encodeURI($(obj).attr("other"));
	//alert(others);
	//alert(replyid);
//	return false;
	$.ajax({
		  type:"POST",
		  url:"jsp/bar/popupPostThreadPage.do?replyid="+replyid+"&others="+others,
		  cache:false,
		  success:function(html){
			  $.msgbox.popupwin(350,120,"回复",html);
			  doreplyreply(obj);
			  },
		  error:function(){
			  $.msgbox.confirmbox("","页面读取失败！",offsetleft,offsettop);
			  },
		  beforeSend:function(){
			  $.msgbox.confirmbox("","页面读取中！",offsetleft,offsettop);
			  },
		  complete: function(){}
		  })
	}
	
function doreply(obj,data){ //回复动作
		data=data.replace(/</g,"&lt;");
		data=data.replace(/>/g,"&gt;");
		var offsetleft=$(obj).offset().left;
		var offsettop=$(obj).offset().top+$(obj).innerHeight();
		var topicid=$(obj).parent().parent().parent().attr("topicid");
		//alert(topicid);
		var replyid=$(obj).parent().parent().parent().attr("replyid");
		
		var action=$(obj).attr("id");
		data=encodeURIComponent(data);
		//alert(action + "回复");
		if (data==""){
			$.msgbox.confirmbox("","请输入内容",offsetleft,offsettop);
		}else{
			var url="";
			var datas = "";
			if (action=="edittopicreply"){
				url="portlet/bar/updateThreadPost.do"
			datas = "threadId="+topicid+"&postId="+replyid+"&content="+data+"&action="+action;
			
			}else{
				var topicid =  $("#threadId").val();
				var others=$("#others").val();
		//alert(others);
				var replycontent = $("#topic_reply").val();
				replycontent=replycontent.replace(/</g,"&lt;");
		replycontent=replycontent.replace(/>/g,"&gt;");
		//var replycontent = encodeURI(replycontent);
		//alert(replycontent);
				url="portlet/bar/postThread.do";//+"&user="+user;
				datas ={threadId:topicid,content:replycontent,utf:1,others:others};// "threadId=" + topicid + "&content="+replycontent+"&utf="+1+"&others="+others;
				//alert(replycontent);
				//alert(datas + " ---");
			}
			$.ajax({
				   url:url,//?blogid="+blogid+"&txtreply="+data, 
				   type:"POST",
				   cache:false,
				   //processData:false,
				   dataType:"html",
				   data:datas, //增加参数，用来区分编辑还是回复
				   success:function(html){
					   $.msgbox.closemsgbox();
					   switch (action){//根据参数，用来区分编辑还是回 复
						   case "edittopicreply": //编辑
							   //$("#commentlist").append(html);
							   //alert(html)
							   $("#topicreply_"+replyid).replaceWith(html);
							   break;
						   case "replytopicreply": //回复
							   $("#commentlist").append(html);
							   break;
						   case "bttopicreply": //回复
							   $("#commentlist").append(html);
							   break;							   
						   }
					    
					   //要重新绑定事件
					   binddeletetopicreplyclick();
					   bindreplyreplyclick();
					   bindeditreplyclick();
					   },
				   error:function(){
					   $.msgbox.confirmbox("","数据提交失败！",offsetleft,offsettop);
					   },
				   beforeSend:function(){
					   $.msgbox.confirmbox("","数据提交中！",offsetleft,offsettop);
					   },
				   complete: function(){
					   //$.msgbox.closemsgbox();
					   }
				   })																
		}	
	}

function doreplyreply(obj){//执行回复动作
	$("#submitreplytopicreply").click(function(){
											//alert($("#topic_reply").val());
											
											 var btitle=$.trim($("#topic_reply").val());
														 if (btitle.length<1){
												    			$("#submitreplytopicreply").next().html("回复内容不能为空！");
												    			return false;
												    		}else{
												    			$("#submitreplytopicreply").next().html("");
												    		}
											  doreply(obj,$("#topic_reply").val())
											  })
	}
function showeditthreadpopwin(obj){ //显示编辑回复窗口
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
	var replyid=$(obj).attr("replyid");
	//alert(replyid + 'aaaaaaa');
	$.ajax({
		  url:"portlet/bar/ShowThreadPostWin.do?postId="+replyid,
		  cache:false,
		  success:function(html){
			  $.msgbox.popupwin(350,120,"编辑回复",html);
			  doreplyreply(obj);
			  },
		  error:function(){
		 	 //alert('error');
			  $.msgbox.confirmbox("","页面读取失败！",offsetleft,offsettop);
			  },
		  beforeSend:function(){
			  $.msgbox.confirmbox("","页面读取中！",offsetleft,offsettop);
			  },
		  complete: function(){}
		  })
		 $("#replycontent").val("");
	return false;
	}

function bindeditreplyclick(){ //绑定编辑回复
	$("#commentlist > li > .author > span > #edittopicreply").each(function(){
																	  //alert($(this).attr("blogreplyid"))
																	  $(this).click(function(){
																							 showeditthreadpopwin(this);return false;
																							 })
																	  })
	}	

function bindreplyreplyclick(){//绑定回复话题回复
	$("#commentlist > li > .author > span > #replytopicreply").each(function(){
																	  //alert($(this).attr("blogreplyid"))
																	  //alert('1');
																	  $(this).click(function(){
																							 showreplyreplypopwin(this);return false;
																							 })
																	  })
	}

function binddeletetopicreplyclick(){ //绑定删除话题回复
	$("#commentlist > li > .author > span > #deletetopicreply").each(function(){
																	
																		   $(this).click(function(){
																								  deletetopicreplyconfirm(this);return false;
																								  })
												  })

	}



function deletetopicreplyconfirm(obj){ //删除回复确认
	var topicid=$(obj).parent().parent().parent().attr("topicid");
	var replyid=$(obj).parent().parent().parent().attr("replyid");
	var $thisli=$(obj).parent().parent().parent();
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
 
	$.ajax({
		  url:"portlet/bar/forward.do?ctrlid=topicreply_delete_"+replyid,
		  cache:false,
		  success:function(html){
			  if (replyid!=""){
				  $.msgbox.confirmbox("你要删除这个回复吗？",html,offsetleft,offsettop);
				  }else{
				  $.msgbox.confirmbox("你要删除这个话题吗？",html,offsetleft,offsettop);
				  }
			  
			  dodeletetopicreply(topicid,replyid);
			  },
		  error:function(){
			  $.msgbox.confirmbox("","页面读取失败！",offsetleft,offsettop);
			  },
		  beforeSend:function(){
			  $.msgbox.confirmbox("","页面读取中！",offsetleft,offsettop);
			  },
		  complete: function(){}
		  })
	}
	

function dodeletetopicreply(topicid,replyid){//删除回复动作.click(function(){alert("w33")})str=datastr.split(",")
	 $("#confirm").click(function(){
								  $.post("portlet/bar/deleteThreadPost.do",{topicid:topicid,replyid:replyid});
								  if (replyid!=""){
									  var arraystr=$(this).attr("ctrlid").split("_");
									  $("#commentlist > #topicreply_"+arraystr[2]).remove();
									  }else{
									  window.location.href="topiclist.html";
									  }
								  $.msgbox.closemsgbox();
								  return false;
								  })
	}	