﻿$(document).ready(function($){


          _initDel();
		  
		  _initCom();
		  
		  _hostopate();  

          _initSubmit();
		 
})



function  _hostopate()
{
	
	  $(".describe h3:first").click(function(){
									  
			  $("#content").focus();				  
	  
	  })
	
	
     $("#host img[@class]").click(function(){
										   
	     var type = $(this).attr("class");
		 
		if(loginStatus=="guest")
		{
		  alert("请您先登录再进行操作");
		}
		else if(loginStatus=="host")
		{
		     if(type=="scrip")
			 {
				  alert("呵呵，请不要给自己传小纸条 ^_^");
			 }
			 else
			 {
				 alert("呵呵，请不要加自己为好友哦 ^_^");
			 }
		}
		else
		{
				if(type=="scrip")
				{
				  scrip();
				}
				else if(type=="addfriend")
				{
					  addFriend();
				}
		}
	 })
}



//传小纸条
function scrip(){
	
	if(sdommin!="")
	{
       //window.location.href = ;	
	   window.open('/'+sdommin+'/manage/scripsend/username/'+username);
	}
}


//加为好友

function addFriend(){
														
       var height  = window.document.body.scrollHeight; 
	   var top     = document.documentElement.scrollTop+200; 
	   var left   = document.body.scrollWidth/2-250;
	   var html;
	   
       if(sdommin==domain)
	   {
		  alert("自己不能加自己为好友");
	   }
	   else
	   {
	   html='<div id="scrip"></div><div class="scripcontent"><div><h3>加为好友</h3>';
	   
	   html+='<p>您要加『'+username+'』为好友吗？</p><h2><textarea>给对方留言...</textarea></h2>';
	   
	   html+='<h4><input type="button" value="加好友" /><input type="button" value="取消" /></h4></div></div>';
	   
	   $("body").append(html);
	   
	   $("#scrip").height(height);

	   $(".scripcontent").css("top",top);
	   
	   $(".scripcontent").css("left",left);
 
	   $(".scripcontent textarea").focus(function(){
	
	   $(this).val("");
	   
	   $(this).unbind("focus");
								 
        })

	    $(".scripcontent input:first").click(function(){
								
			 var content  =   $(".scripcontent textarea").val();	
			 
			 var len      =  calculate_byte(content);
			 
			 if(len>100)
			 {
				 alert("内容最多允许100个中文或 200个字符");
			 }
			 else
			 {
				 if(content=="给对方留言...")
				 {
					 content ="";
				 }					
		   <!-- start -->
				$.post(
			   "/"+domain+"/space/friend",
			  {content:content},
				 function(msg)
					{
			
						if(msg=="refuse")
						{
							alert("自己不能加自己为好友");
					    }
						else if(msg=="login")
						{
						    alert("请你先登录，再发加为好友请求");
						}
						else if(msg==1)
						{
						    alert("好友邀请发送成功，请等待对方确认");
						   
						   $(".scripcontent input:last").click();
						}
						else if(msg=="wait")
						{
						    alert("您已经给TA发送过好友邀请啦，请等待对方确认");
						   
						    $(".scripcontent input:last").click();
						}
						else if(msg=="friend")
						{
						    alert("此用户已经是您的好友 ,无需再添加");
						   
						    $(".scripcontent input:last").click();
						}
					}
				);
	       <!-- end -->
			 }
	   }) 
	  
	   $(".scripcontent input:last").click(function(){
											  
		    $(".scripcontent").remove();
			
			$("#scrip").remove();
	   }) 
	  }
}




function _initSubmit(){

   $("#content").focus(function(){
	
	   $(this).val("");
	   
	   $(this).unbind("focus");
								 
   })



	$("#form1").submit( function(){
		
		var content  =  $("#content").val();
		
		var len      =  calculate_byte(content);
		
		if(content=="")
		{
		  alert("请输入心情随笔内容");
		  
		  $("#content").focus();
		  
		  return false;
		}
		else if(len>400)
		{
			alert("内容最多允许200个中文或 400个字符");
		  
		   return false;
		}

	       return true;
	
	}); 
}


function calculate_byte( sTargetStr ) {
        var sTmpStr, sTmpChar;
        var nOriginLen = 0;
        var nStrLength = 0;
         
        sTmpStr = new String(sTargetStr);
        nOriginLen = sTmpStr.length;

        for ( var i=0 ; i < nOriginLen ; i++ ) {
                sTmpChar = sTmpStr.charAt(i);

                if (escape(sTmpChar).length > 4) {
                        nStrLength += 2;
                } else if (sTmpChar!='\r') {
                        nStrLength ++;
                }
        }
                        return nStrLength;    
}



function _initDel(){
	
	
	$("a[@type=del]").click(function(){
	
	    var id  =  $(this).parent().parent().attr("id");

	    if (confirm("确定删除这条心情随笔吗？"))
		{
			<!-- start -->
					$.post(
				   "/"+domain+"/userinfo/heartdel",
					{id:id},
					 function(msg)
						{
						  if(msg==1)
						  {
                               $(".heards[@id="+id+"]").slideUp();
                          }
						  else if(msg=="login")
						  {
							  alert("请你先登录");
						  }
					 });
			 <!-- end -->
		}	
									  								  
	})
}




function _initCom()
{
	
 	$("a[@type=com]").click(function(){
									 
			var id   =  $(this).parent().parent().attr("id");

		     heartcommentShow(id)
		  
		     showsendcomment(id);
	})
}




function heartcommentShow(id){
		
	  <!-- start -->
	  $.getJSON(
			   "/"+domain+"/userinfo/heartcomment/id/"+id,
			   
				 function(msg)
					{
					  if(msg!="")
					  {
						 var html="";
						 
						 var length = msg.comment.length;
						 
						 var item   = msg.comment
 
					     for(i=0;i<length;i++)
						 {
				             if(item[i].svisualize=='')  
							 {
								item[i].svisualize='/images/default/head.jpg';
							 }
							
html+='<div class="comment" comid="'+item[i].id+'" > <div class="head"><p><a target="_blank" href="/'+item[i].domain+'"><img src="'+item[i].svisualize+'"/></a></p>';
				
		    html+='<p><a target="_blank" href="/'+item[i].domain+'">'+item[i].username+'</a></p></div><div class="commentbody">';
				
			html+='<ul><li class="time"><b>评论时间：</b>'+item[i].timestamp;
				
			 if(loginStatus=="host")
			 {
		    	html+='<span><a stype="edit" href="javascript:void(0);">回复</a> | <a stype="del" href="javascript:void(0);">删除</a></span>';
			 }
				
			html+='</li><li class="content">'+item[i].content+'</li></ul>';
			
			 if(item[i].replay!="")
			 {
				 html+='<ul class="replay"><li class="replaytime"><b>主人回复：</b>'+item[i].replaytime+'</li><li class="replaycontent">'+item[i].replay+'</li></ul>';	
			 }
			     html+='</div><div class="clear"/></div>';
							 
						 }
						 
					       $(".heardcommentshow[@id="+id+"]").html(html); 
					 }
					 
					       _initedit();
					
					      showsendcomment(id);
			  });
	  <!-- end --> 
}



function _initedit()
{
	      $(".comment a[@stype]").click(function(){
																	 
                var type = $(this).attr("stype");
				
				var id   = $(this).parent().parent().parent().parent().parent().attr("comid");
				
				if(type=="del")
				{
					heartcomdel(id);
				}
				else
				{
				    comedit(id);
				}
		   })    
}


// 评论删除

function heartcomdel(id){
	
      if (confirm("删除后不可恢复，你确定要删除吗？"))
	  {
				  <!-- start -->
						$.post(
					   "/"+domain+"/userinfo/heartcommentdel",
						{id:id},
						 function(msg)
							{
							  if(msg=="1")
							  {
								 $(".comment[@comid="+id+"]").slideUp(function(){
																			   
								   $(this).remove();											   
								 
								 });
							  }
							  else
							  {
								alert("请您先登录再进行操作");  
							  }
					  });
			     <!-- end -->
	  }
}


//回复
function comedit(id){
	
	var sum = $(".comment[@comid="+id+"] .replay").size();
	
	var replay = "";
	
	var relpaysum = $(".comment[@comid="+id+"] .replays").size();

   if(relpaysum==0)
   {
		if(sum!=0)
		{
					   replay =  $(".comment[@comid="+id+"] .replaycontent").html();
			 
					   $(".comment[@comid="+id+"] .replay").hide();
		}	
		
	   var html ='<ul class="replays"><li><b>主人回复：</b></li>'
		
		   html+='<li><input type="text" value="'+replay+'" /> <a replay="'+id+'">保存</a> <a canel="'+id+'">取消</a></li></ul>';
		
		   $(".comment[@comid="+id+"] .commentbody").append(html);
		   	   
		   $("a[@replay="+id+"]").click(function(){
														
					 var replay = $(this).parent().find("input").val();
	
					 if(replay=="")
					 {
						alert("请您输入内容");	 
					 }
					 else
					 {
						 <!-- start -->
						  $.post(
						   "/"+domain+"/dream/dreamcommentreplay",
							{id:id,replay:replay},
							 function(msg)
								{
								  if(msg=="1")
								  {
										$(".comment[@comid="+id+"] .replays").remove();
										
										var sum = $(".comment[@comid="+id+"] .replay").size();
										
										if(sum!=0)
										{
											$(".comment[@comid="+id+"] .replaycontent").html(replay);
											
											$(".comment[@comid="+id+"] .replay").show();
										}else
										{
											var  html='<ul class="replay"><li class="replaytime"><b>主人回复：</b></li><li class="replaycontent">'+replay+'</li></ul>';	
			 
											$(".comment[@comid="+id+"] .commentbody").append(html);
										}
								  }
						  });
						  <!-- end --> 
					  }
		  })

          $("a[@canel="+id+"]").click(function(){
																
			    $(this).parent().parent().remove();
				
				$(".comment[@comid="+id+"] .replay").show();
		  })	  		 
   }
}



function showsendcomment(id){
	

        var size = $(".sendcomment[@id="+id+"]").size();
			
			if(simg=="")
			{
			    simg ="/images/default/friend.gif";
			}
			
		    if(size==0)
			{
		        var html='';
				
				html='<div class="sendcomment" id="'+id+'"><div class="head"><img src="'+simg+'"/></div><div class="commentbody">';
				
				html+='<ul><li class="content"><textarea></textarea></li>';
				
				html+='<li><img src="/images/userinfo/save.gif" /> <img src="/images/userinfo/cancel.gif" /></li></ul></div></div>';
				
			    $(".heardcommentshow[@id="+id+"]").append(html);	
				
				_initSendHeartcomment(id);
		  }
}









function _initSendHeartcomment(id){
	
	 $(".sendcomment[@id="+id+"] li img:first").click(function(){
															
				var content  =  $(".sendcomment[@id="+id+"] li textarea").val();
				 
				
				if(loginStatus=="guest")
		        {
					  alert("请您先登录再进行操作");
				}
				else if(content=="")
				{
				      alert("请输入评论内容");	
					
					  $(".sendcomment[@id="+id+"] li textarea").focus();
				}
				else
				{
				   <!-- start -->
						$.post(
					   "/"+domain+"/userinfo/savecomment",
						{content:content,id:id},
						 function(msg)
							{
							  if(msg=="nologin")
							  {
								  alert("请您先登录再进行操作");
							  }
							  else
							  {
								  $(".sendcomment[@id="+id+"] li textarea").val("");
								  
								   heartcommentShow(id);
							  }
					    });
			       <!--  end  -->  
				}
	  })
	 
	 
	  $(".sendcomment[@id="+id+"] li img:last").click(function(){
																											
	      $(".heardcommentshow[@id="+id+"]").empty();

	  })
}
