// JavaScript Document
$(document).ready(function(){
//						   $("#uploadpiclink").click(function(){
//															  $("#postblogalbum").hide();
//															  $("#postblogpic").show();
//															  $("#postblogline").show();
//															  })
						   $("#browsepiclink").click(function(){
															  $("#postblogpic").hide();
															  $("#postblogalbum").show();
															  $("#postblogline").show();
															  })
						   
						   
						    //新建日志分类
						   $("#a_addblogclass").click(function(){
																var offsetleft=$(this).offset().left;
																var offsettop=$(this).offset().top+$(this).innerHeight();
																var userId=$("#userId").val();
															
																$.ajax({
																	   url:"portlet/blog/viewAddCategory.do?userId="+userId,
																	   success:function(html){
																		   var $msgbox=$.msgbox.popupwin(150,100,"添加日志分类",html);
																		   $msgbox.find("#addCategroySubmit").click(function(){
																															 var categroyName=$msgbox.find("#categroyName").val();
																															 var parentCategory=$msgbox.find("#parentCategory").val();
																															 var $this=$(this);
																															 if (categroyName==""){
																																 $this.next().html("请输入分类名")
																																 return false
																																 }
																															 $.post("popup/AddBlogCategory.do",{categroyName:categroyName,parentCategroyID:parentCategory},
																																	function(data){
																																		var json=eval('('+data+')');
																																		if (json.error == "1"){
																																			$this.next().html("添加成功");
																																			$this.attr("disabled",true);
																																			$.msgbox.closemsgbox(700,function(){
																																											  $("#subblogclassid").val(json.categroyNameId);
																																											  $("#subblogclass").val(json.categroyName);
																																											  });
																																			}
																																		if (json.error == "0"){$this.next().html("分类重复")}
																																		})
																															 return false;
																															 })

																		   },
																	   error:function(){$.msgbox.msgbox(150,100,"添加日志分类","页面读取失败！")},
																	   beforeSend:function(){$.msgbox.msgbox(150,100,"添加日志分类","页面读取中！")},
																	   complete: function(){}
																	   })
																return false;
																})							   
						   
						   
						   
						   //清空已选择的话题吧
						   $("a[name='a_cleargroup']").click(function(){
															   $(this).parent().find("#groupname").val("");
															    $(this).parent().find("#groupid").val("");
															   })
						   //Michael要求的这种输入法
						   $("a[name='a_selectgroup2']").click(function(){
															   var offsetleft=$(this).offset().left;
															   var offsettop=$(this).offset().top+$(this).innerHeight();
															   var $this=$(this);
																$.ajax({
																	   url:"popup/blogBar.do",
																	   success:function(html){
																		   //$.msgbox.confirmbox("选择话题吧",html,offsetleft,offsettop);
																		   $.msgbox.popupwin(50,100,"选择话题吧",html);
																		   var $group=$("#popwinsearchconditionbox > table > tbody > tr > td > #group");
																		   binddataclass2("popup/blogSubBar.do",$group.val(),"group",$this);
																		   bindeventclasschange2("popup/blogSubBar.do",$group,"group",$this);
																		   bindeventfindclasskeyup();
																		   },
																	   error:function(){$.msgbox.msgbox(150,100,"选择话题吧","页面读取失败！")},
																	   beforeSend:function(){$.msgbox.msgbox(150,100,"选择话题吧","页面读取中！")},
																	   complete: function(){}
																	   })
															   })
						   
						   
						   //初始化删除已经选择的话题吧 在发表日志和活动页面
						   bindeventgroupdelete($("#postcontent> tbody > tr > td > #group > li"));
						   //选择话题吧
						   $("#a_selectgroup").click(function(){
																var offsetleft=$(this).offset().left;
																var offsettop=$(this).offset().top+$(this).innerHeight();
																var $group=$("#postcontent> tbody > tr > td > #group");
																
																if ($group.find("li").length>=3){
																	
																	$.get("html/closeconfirm.asp",function(html){$.msgbox.confirmbox("您最多选择三个话题吧",html,offsetleft,offsettop);})
																	return;
																	} 
																$.ajax({
																	   url:"html/group.html",
																	   success:function(html){
																		   $.msgbox.popupwin(150,100,"选择话题吧",html);
																		   //$.msgbox.confirmbox("选择话题吧",html,offsetleft,offsettop);
																		   var $group=$("#popwinsearchconditionbox > table > tbody > tr > td > #group");
																		   binddataclass("html/group.asp",$group.val(),"group");
																		   bindeventclasschange("html/group.asp",$group,"group");
																		   bindeventfindclasskeyup();
																		   },
																	   error:function(){$.msgbox.msgbox(150,100,"选择话题吧","页面读取失败！")},
																	   beforeSend:function(){$.msgbox.msgbox(150,100,"选择话题吧","页面读取中！")},
																	   complete: function(){}
																	   })															
																		
																		
																	 

																})						   
						   
						   
						  
																 //选择日志分类
						   $("#a_selectblogclass").click(function(){
																var offsetleft=$(this).offset().left;
																var offsettop=$(this).offset().top+$(this).innerHeight();
																
																$.ajax({
																	   url:"popup/blogCategory.do",
																	   cache:false,
																	   success:function(html){
																		   $.msgbox.popupwin(150,100,"选择日志分类",html);
																		   //$.msgbox.confirmbox("选择日志分类",html,offsetleft,offsettop);
																		   binddataclass("popup/blogSubCategory.do",$("#blogclass").val(),"blog");
																		   bindeventclasschange("popup/blogSubCategory.do",$("#blogclass"),"blog");
																		   bindeventfindclasskeyup();
																		   },
																	   error:function(){$.msgbox.msgbox(150,100,"选择日志分类","页面读取失败！")},
																	   beforeSend:function(){$.msgbox.msgbox(150,100,"选择日志分类","页面读取中！")},
																	   complete: function(){}
																	   })
																})
																
						   
						   
						   
						   //编辑回复
						   $("#replylist > li > .author > span > #editblogreply").click(function(){
						        
																							  showeditreplypopwin(this);
																							  })						   
						   //回复日志
						   $("#replylist > li > .author > span > #replyblogreply").click(function(){
						                 
																							  showreplyreplypopwin(this);
																							  })
						   
						   //删除日志回复
						   $("#replylist > li > .author > span > #deleteblogreply").click(function(){
																								   deleteblogreplyconfirm(this);
																								   })
						   
						    //删除日志
						   $("#deleteblogarticle").click(function(){
																  var offsetleft=$(this).offset().left;
																  var offsettop=$(this).offset().top+$(this).innerHeight();
																  var blogid=$(this).attr("blogId");
																  $.ajax({
																		 url:"jsp/blog/deleteblogarticle.jsp",
																		 type:"POST",
																		 data:"blogId="+blogid,
																		 success:function(html){
																			 if (html=="删除成功"){
																				 window.location.href="blog.do";
																				 }
																			 },
																		 error:function(){
																			 $.msgbox.confirmbox("","数据提交失败！",offsetleft,offsettop);
																			 },
																		 beforeSend:function(){
																			 $.msgbox.confirmbox("","数据提交中！",offsetleft,offsettop);
																			 },
																		 complete: function(){}
																		 })
																  })
						   
						   
						   
						   //发表日志回复
						   $("#btblogreply").click(function(){
						      
															doreply(this,$("#txtblogreply").val())
														})
						   
						   
						   
						   
						   })
//绑定话题吧消费类删除点击动作
function bindeventgroupdelete(obj){
	$(obj).each(function(){
						 $(this).find("span > #delete").click(function(){
												$(this).parent().parent().remove();
												})
						 })
	}

//绑定话题吧日志小分类点击动作
function bindeventclassclick(type){
	$("#popwinselectclass > li > a").click(function(){
													   switch (type){
														   case "blog":
														   		$("#subblogclassid").val($(this).parent().attr("subblogclassid"));
																$("#subblogclass").val($(this).parent().text());
																$.msgbox.closemsgbox();
																break;
														   case "group":
														   		var $group=$("#postcontent> tbody > tr > td > #group");
																var $selected=$(this);
																var ishave=false;
																var value="";
																
																//$(this).after("<li><span><a href=\"javascript:;\">删除</a><input name=\"groupid\" id=\"groupid\" type=\"hidden\" value=\""+$selected.parent().attr("groupid")+"\" /><input name=\"groupname\" id=\"groupname\" type=\"hidden\" value=\""+$selected.parent().text()+"\" /></span>"+$selected.parent().text()+"</li>");
																$group.find("li").each(function(){
																					 //alert($(this).html());alert($(this).find("#groupname").val());alert($selected.parent().text())
																					 if ($(this).find("#groupname").val()==$selected.parent().text()){//如果没有添加过就添加
																							 ishave=true;
																						 }
																					 })
																if (!ishave){
																		$group=$group.append("<li><span><a href=\"javascript:;\"  id=\"delete\">删除</a><input name=\"groupid\" id=\"groupid\" type=\"hidden\" value=\""+$selected.parent().attr("groupid")+"\" /><input name=\"groupname\" id=\"groupname\" type=\"hidden\" value=\""+$selected.parent().text()+"\" /></span>"+$selected.parent().text()+"</li>");
																		
																		
																		$.msgbox.closemsgbox();
																		bindeventgroupdelete($group.find("li"));
																	}else{
																		alert("您已经选择过同样的话题吧");
																	}
																
														   }
													   })
	}
function bindeventclassclick2(type,sourceclickobj){
	$("#popwinselectclass > li > a").click(function(){
													//alert(sourceclickobj.parent().find("#groupname").val())
													var inputval=$(this).parent().text();
													var ishave=false;
													sourceclickobj.parent().parent().parent().find("input[name='groupname']").each(function(){
																																			if ($(this).val()==inputval){
																																				ishave=true;
																																				}
																																			})
													
													
													if (!ishave){													  
														sourceclickobj.parent().find("#groupname").val($(this).parent().text());
														sourceclickobj.parent().find("#groupid").val($(this).parent().attr("groupid"));
														$.msgbox.closemsgbox();
														}else{
														alert("您已经选择过同样的话题吧");	
														}
													
													
													})
	}
	
	

//绑定选择话题吧日志大类change动作
function bindeventclasschange(url,obj,type){
	$(obj).change(function(){
						   binddataclass(url,$(obj).val(),type);
						   var $findclass=$("#findclass");
						   $findclass.val("");
						   if ($findclass.val()==""){$findclass.val($findclass.attr("title"))};
						   })
	}
function bindeventclasschange2(url,obj,type,sourceclickobj){
	$(obj).change(function(){
						   binddataclass2(url,$(obj).val(),type,sourceclickobj);
						   var $findclass=$("#findclass");
						   $findclass.val("");
						   if ($findclass.val()==""){$findclass.val($findclass.attr("title"))};
						   })
	}	
	

//绑定改变搜索话题吧日志小类输入框值得动作
function bindeventfindclasskeyup(){
	$("#findclass").focus(function(){$(this).val("")});
	$("#findclass").blur(function(){
								  if ($(this).val()==""){$(this).val($(this).attr("title"))}
								  })
	$("#findclass").keyup(function(){
										  var findsubblogclass=$(this).val();
										  var $popwinselectclass=$("#popwinselectclass > li")
										  $popwinselectclass.each(function(){
																			  if ($(this).text().indexOf(findsubblogclass)<0){$(this).hide();}else{$(this).show();}
																		    })											  
										  })
	}

//载入话题吧日志小分类列表
function binddataclass(url,classid,type){
	$("#popwinsearchresultbox").html("数据读取中...");
	$("#popwinsearchresultbox").load(url,{cid:classid},function(){bindeventclassclick(type)});
	}
function binddataclass2(url,classid,type,sourceclickobj){
	$("#popwinsearchresultbox").html("数据读取中...");
	$("#popwinsearchresultbox").load(url,{cid:classid},function(){bindeventclassclick2(type,sourceclickobj)});
	}
	
	
	

function doreply(obj,data){ //回复动作
		var offsetleft=$(obj).offset().left;
		var offsettop=$(obj).offset().top+$(obj).innerHeight();
		//var blogid=$(obj).attr("blogid");
		var blogid=$("#itemsId").val();
		var userid=$("#userId").val();
		var types=$("#types").val();
		var fuserid=$("#fuserId").val();
		
		var blogreplyid=$(obj).attr("blogreplyid");
		var action=$(obj).attr("id");
		data=$.trim(data);
		data=data.replace(/</g,"&lt;");
		data=data.replace(/>/g,"&gt;");
		data=encodeURIComponent(data);
	    var mypoint=parseInt($("#mypoint").val());
		
		if(mypoint<-100&&action!="editblogreply"){
		   $.msgbox.confirmbox("","你的银币小于-100！不能评论！",offsetleft,offsettop);
		} else {
		if(userid==""){
		    
		    $.msgbox.confirmbox("","您当前还没有登录，请登录后发表评论！",offsetleft,offsettop);
		} else{
		if (data==""){
			$.msgbox.confirmbox("","请输入内容",offsetleft,offsettop);
		}else{
			$.ajax({
				   url:"portlet/comment/commentAdd.do",//?blogid="+blogid+"&txtreply="+data, 
				   type:"POST",
				   cache:false,
				   processData:false,
				   dataType:"html",
				   data:"blogid="+blogid+"&blogreplyid="+blogreplyid+"&txtreply="+data+"&action="+action+"&userId="+userid+"&types="+types+"&fuserId="+fuserid, //增加参数，用来区分编辑还是回复
				   success:function(html){
					   $.msgbox.closemsgbox();
					   var $html=$(html);
					   if (types=="blog"){$html=$(html).find(".author").addClass("blogarticleauthor").parent().parent()}
					   switch (action){//根据参数，用来区分编辑还是回 复
						   case "editblogreply": //编辑
							   //$("#replylist").append(html);
							  
							   $("#blogreply_"+blogreplyid).replaceWith($html);
							  
							   break;
						   case "replyblogreply": //回复
							   $("#replylist").append($html);
							   break;
						   case "btblogreply": //回复
						       $("#txtblogreply").val("");
							   $("#replylist").append($html);
							   break;							   
						   }
					    
					   //要重新绑定事件
					   binddeleteblogreplyclick();
					   bindreplyreplyclick();
					   bindeditreplyclick();
					   },
				   error:function(){
					   $.msgbox.confirmbox("","数据提交失败！",offsetleft,offsettop);
					   },
				   beforeSend:function(){
					   $.msgbox.confirmbox("","数据提交中！",offsetleft,offsettop);
					   },
				   complete: function(){
					   //$.msgbox.closemsgbox();
					   }
				   })																
		 }	
		
		}
	  }
	}



function deleteblogreplyconfirm(obj){ //删除回复确认
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
	var replyid=$(obj).attr("blogreplyid");
	
	$.ajax({
		  url:"portlet/comment/deleteComment.do?ctrlid=blogreply_delete_"+replyid+"&commentId="+replyid,
		  cache:false,
		  success:function(html){
			  $.msgbox.confirmbox("你要删除这个回复吗？",html,offsetleft,offsettop);
			  dodeleteblogreply();
			  },
		  error:function(){
			  $.msgbox.confirmbox("","页面读取失败！",offsetleft,offsettop);
			  },
		  beforeSend:function(){
			  $.msgbox.confirmbox("","页面读取中！",offsetleft,offsettop);
			  },
		  complete: function(){}
		  })
	}
	
function dodeleteblogreply(){//删除回复动作.click(function(){alert("w33")})str=datastr.split(",")
	 $("#confirm").click(function(){
								  var arraystr=$(this).attr("ctrlid").split("_");
								  $("#replylist > #blogreply_"+arraystr[2]).remove();
								  $.msgbox.closemsgbox();
								  })
	}

function binddeleteblogreplyclick(){ //绑定删除日志回复
	$("#replylist > li > .author > span > #deleteblogreply").each(function(){
																		   $(this).click(function(){
																			  
																								  deleteblogreplyconfirm(this);
																								  })
												  })

	}

function showreplyreplypopwin(obj){ //显示回复窗口
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
	var blogreplyid=$(obj).attr("blogreplyid");
	
	$.ajax({
		  url:"jsp/common/replycolumnreply.jsp?blogreplyid="+blogreplyid,
		  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 doreplyreply(obj){//执行回复动作
	$("#submitreplyblogreply").click(function(){
	                                         
											  doreply(obj,$("#blog_reply").val())
											  })
	}

function bindreplyreplyclick(){//绑定回复日志回复
	$("#replylist > li > .author > span > #replyblogreply").each(function(){
																	
																	  $(this).click(function(){
																							 showreplyreplypopwin(this);
																							 })
																	  })
	}
	

function showeditreplypopwin(obj){ //显示编辑回复窗口
	var offsetleft=$(obj).offset().left;
	var offsettop=$(obj).offset().top+$(obj).innerHeight();
	var blogreplyid=$(obj).attr("blogreplyid");
	//var contents=$("#contents").val();
	
		//alert(contents);
	$.ajax({
		  url:"portlet/comment/updateComment.do?blogreplyid="+blogreplyid,
		  
		  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 doeditreply(obj){//执行编辑回复动作
	$("#submitreplyblogreply").click(function(){
	     
											  doreply(obj,$("#blog_reply").val())
											  })
	}

function bindeditreplyclick(){ //绑定编辑回复
	$("#replylist > li > .author > span > #editblogreply").each(function(){
																	 
																	  $(this).click(function(){
																							 showeditreplypopwin(this);
																							 })
																	  })
	}	
