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

         _initItem();
		 
		  CommentShow(1);
		  
		  _initSendComment();  
		 
		 _initLogin();
})


function _initItem(){

     $(".item  li").mouseover(function(){

		  $(".item  li").removeAttr("class");
		
		  var index =  $(".item  li").index(this);
		  
	      $(".disc div").hide();
		  
		  $(this).addClass("selected");
		  
		 if(index==0)
		 {
		     $(".file").show();	 
		 }
         else if(index==1)
		 {
		    $(".interest").show();	 
		 }
		 else if(index==2)
		 {
			 $(".edu").show();	
		 }
		  else if(index==3)
		 {
			 $(".work").show();	 
			 
		 }
		   else if(index==4)
		 {
			 $(".contact").show();	 
			 
		 }
		  
		  
	 })
	
}





/* 留言列表 */   

function CommentShow(page){
		
	  <!-- start -->
	  $.getJSON(
			   "/"+domain+"/userinfo/boardlist/page/"+page,
			   
				 function(msg)
				  {
					  if(msg!="")
					  {
						 var html="";
						 
						 var length = msg.board.length;
						 
						 var item   = msg.board;
 
					     for(i=0;i<length;i++)
						 {
				             if(item[i].svisualize=='')  
							 {
								item[i].svisualize='/images/default/head.jpg';
							 }
							 
				    html+='<div class="comment" id="'+item[i].id+'"><div class="head"><p><a href="/'+item[i].domain+'" target="_blank">';
					
					html+='<img src="'+item[i].svisualize+'" /></a></p><p><a href="/'+item[i].domain+'" target="_blank">'+item[i].username+'</a></p></div>';
				  
				    html+='<div class="commentbody"><ul><li class="time"><b>留言时间：</b>'+item[i].timestamp;
				  
				   if(item[i].lowshow=='1')
				   {
					     html+=' (此留言为悄悄话)';  
				   }
 
				  if(loginStatus=="host")
				  {
				     if(item[i].replay!="")
				     {
						var temp ="编辑回复";
					 }
					 else
					 {
						var temp ="回复"
				     }
					    html+='<span><a href="javascript:void(0);" type="edit">'+temp+'</a> | <a href="javascript:void(0);" type="del">删除</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></div>';
				  
				 }
		
							 setpage(msg.page);
		
							$(".comment").remove();

		                    $(".page").before(html); 
							
							_initcomedit();
				   }
			  });
	  <!-- end --> 
}




//评论的操作

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

// 评论删除

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

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


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

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




function setpage(page){
	
	    var curpage    = page.page;
							
		var totalpage  = page.totalpage;
							
		var totalcount = page.totalcount;
		
		var html;
		
		var count; 
		
		var uppage;
		
		var nextpage;
		
		$(".comment-top font").html(totalcount);
		
	    if(totalpage>1)
		{

                html  = "第 <span class='cur'>"+curpage+"</span> 页/共 <span>"+totalpage+"</span> 页 &nbsp;&nbsp;";

			if(curpage==1)
			{
				html+= ' 首页 上一页 ';
			}
			else
			{
				uppage = curpage-1;
				
			    html+= ' <a page="1">首页</a> <a  page="'+uppage+'">上一页</a>'
		    }
			if(curpage==totalpage)
			{
				html+= '下一页  末页';
			}
			else
			{
				nextpage = curpage+1;
				
			    html+= ' <a  page="'+nextpage+'">下一页</a> <a page="'+totalpage+'">末页</a>'
			}
		}

        if(html!="")
        {
			  $(".page").html(html);
				 
			  $(".page a").click(function(){
													
				  var page = $(this).attr("page");	
	
				  CommentShow(page);
				  
				  window.location.hash="se";
			   
			   })	 
		}
}



function _initLogin()
{
     $(".commentlogin a:first").click(function(){
											   
		var username = $("#user").val();
		
		var passwd   = $("#pw").val();
		
		if(username==""||passwd=="")
		{
			alert("请输入完整的用户名和密码");
		}
		else
		{	
		 <!-- start -->
				$.post(
			   "/login/login",
				{name:username,pass:passwd},
				 function(msg)
					{
					  if(msg=="error")
					  {
						 $("#user").val("");
						 
						 $("#pw").val("");
						 
						 alert("登录失败");
					 }
					 else
					 { 
					    $(".commentlogin").html("登录成功，请发表留言：");
						 
					 }
				});
		 <!-- end -->
		}
											   
	 })
}





function  _initSendComment(){

        $(".sendcomment .send img").click(function(){
				
			  var content = KE.util.getData("content");
			  
			  var low     = document.getElementById("low").checked;
	
			  if(content=="")
			  {
				   alert("请您输入内容"); 
			  }
			  else
			  {
				   <!-- start -->
						$.post(
					   "/"+domain+"/userinfo/board",
						{content:content,low:low},
						 function(msg)
							{
							  if(msg=="nologin")
							  {
								  alert("请您先登录再进行操作");
							  }
							  else
							  {
								  CommentShow(1);
								  
								  clearEditor('content'); 
								  
								  document.getElementById("low").checked=false;
								  
								  window.location.hash="top1";
							  }
					    });
			       <!--  end  -->  
			  }	
		})
}

/* 清空内容 */  

function clearEditor(id) {
	
    KE.g[id].iframeDoc.open();
	
    KE.g[id].iframeDoc.write(KE.util.getFullHtml(id));
	
    KE.g[id].iframeDoc.close();
	
    KE.g[id].newTextarea.value = '';
	
}

