//Author: Fantasy
//create-time:08/04/20
//last-modify-time:08/07/14

/**
 *计算字符串长度的函数
 *
 */
function JHshStrLen(sString)
{
   var sStr,iCount,i,strTemp ;

   iCount = 0 ;
   sStr = sString.split("");
    for (i = 0 ; i < sStr.length ; i ++)
     {
         strTemp = escape(sStr[i]);
          if (strTemp.indexOf("%u",0) == -1)
          {
              iCount = iCount + 1 ;
          }
          else
          {
              iCount = iCount + 2 ;
          }
      }

      return iCount ;
}


function shareIt(){
		/*$map['userId']		=	$this->mid;
		$map['module']		=	$_POST['module'];
		$map['recordId']	=	$_POST['recordId'];
		$map['title']		=	$_POST['title'];
		$map['content']		=	getShareContent($map['module'],$map['recordId']);
		$map['info']		=	$_POST['info'];*/

	var userId		=	$("#userId").val();
	var module		=	$("#module").val();
	var recordId	=	$("#recordId").val();
	var title		=	$("#title").val();
	var info		=	$("#stp-tarea").val();

	//alert(userId);
	//alert(module);
	//alert(recordId);
	//alert(title);
	//alert(info);

	$.post(APP+"/Share/insert",{userId:userId,module:module,recordId:recordId,title:title,info:info},function(txt){
		if(txt){
			$("#facebox .content").html("<center><font size='4'>分享成功！</font></center>");
			setTimeout(function(){$.facebox.close();},1500);
		}else{
			$("#facebox .content").html('分享失败！');
			setTimeout(function(){$.facebox.close();},1500);
		}
	});

}

/**
 * 字符串取出空格
 * 
 */
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

/**
 * 复制到剪贴板-ljq-add
 * new
 */
var copytoclip=1;
function copyToClipboard(theField,isalert) {
	var tempval=document.getElementById(theField);//不能用$否则会发生冲突
	if (navigator.appVersion.match(/\bMSIE\b/)){
		tempval.select();
		if (copytoclip==1){
			therange	=	tempval.createTextRange();
			therange.execCommand("Copy");
			if(isalert!=false)alert("复制成功。现在您可以粘贴（Ctrl+v）到Blog 或BBS中了。");
		}
		return;
	}else{
		alert("您使用的浏览器不支持此复制功能，请使用Ctrl+C或鼠标右键。");
		tempval.select();		
	}
}

/**
 * 表情的插入
 *
 */
function insert(num){
	var con = $("#p-cmt-body").val();
	var pic = "[face]"+num+"[/face]";
	$("#p-cmt-body").val(con+pic);
}

/**
 * 提交评论
 *
 */
$(function(){
	$("#ajaxcom").click(function(){
		//提取变量
		var userId	= $("#userId").val();
		var recordId	= $("#recordId").val();
		var module = $("#module").val();
		var content = $("#p-cmt-body").val();
		
		//var info		=	$("#stp-tarea").val();			
		//alert(info);
		//return;
		var dis = content.replace(/\[face\](\d*)\[\/face\]/g,'<img src='+PUBLIC+'/Images/biaoqing/$1.gif />');

		var userImg = $("#userImg").val();
		var userName= $("#userName").val();
		var userProv = $("#userProv").val();


		var  ran=Math.random();
		//检测合法性
		if(userId == ""){alert("登陆后重试!");return false;}	
		if(content.Trim()==""){alert("评论不能为空!");return false;}				
		if(JHshStrLen(content)>200){alert("评论不能超过100个汉字!");return false;}
	
		//提示效果
		$("#flag").html("");
		$("#load").fadeIn();
		$(this).attr("disabled","disabled").css("background","#D4D0C8").val("正在提交...");
		
		//POST提交
		$.post(APP+"/Comment/insert",{userId:userId,recordId:recordId,module:module,content:content,ran:ran},function(txt){
			if(txt){
				
				//清除提示效果
				$("#load").fadeOut();
				if(module=="Exchange")
				{
					$("#ajaxcom").attr("disabled","").css("background","#F3A624").val("立即回复");
					//$("#flag").html('<A HREF="javascript:delFlag()"><font color="green">评论成功</font></A>');
				}
				else
				{
					$("#ajaxcom").attr("disabled","").css("background","#2782D6").val("评 论");
					$("#flag").html('<A HREF="javascript:delFlag()"><font color="green">评论成功</font></A>');		
					setTimeout(function(){$('#flag').remove();},700);
				}
				
				//清除文本域
				$("#p-cmt-body").val('');
				//评论数加一
				var plnum = parseInt($("#plnum").text())+1;
				$("#plnum").text(plnum+" ");

					//显示你刚刚评论的那条
					//根据不同板块的宽度设置
					if(module=="Photo"){
						var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:404px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:404px">'+dis+'</div></div></li>';

					 }
					else if(module=="Idea"){
						var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:404px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:404px">'+dis+'</div></div></li>';

					 }
					else if(module=="Blog"){
					var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:570px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:570px">'+dis+'</div></div></li>';
					 }
					 else if(module=="Exchange"){
						var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:350px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:350px">'+dis+'</div></div><hr/></li>';

					 }
					else if(module=="Map"){
						var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:350px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:350px">'+dis+'</div></div><hr/></li>';

					 }
					else{
					var pl = '<li id=li'+txt+'><div class="post parent-post"><p class="image"><a href="'+APP+'/space/'+userId+'">'+
							 '<img width="48" alt="'+userName+'" src="'+userImg+'"/></a></p><div class="info" style="width:530px"><span class="author">'+						 '<a href="'+APP+'/space/'+userId+'">'+userName+'</a></span><span class="time">刚刚</span>'+                         '<span class="delete"><a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a></span></div>'+                         '<div class="content" style="width:530px">'+dis+'</div></div></li>';
					 }
					 
					 $(pl).appendTo($("#threadlist"));

			}else{
				alert("评论失败!");
			}
		});

	});

});

function delFlag(){
	$("#flag").html("");
}

/**
 * 删除评论
 *
 */
function doAjax(rel,id){
	var type = $("#"+id).attr("alt");
	//圈人删除
	if(type == 'quan'){
		$('#QC').html("<b><font size=2 color=green>正在提交...</font></b>");
		 var delId="#"+"quan_"+id;
		$.post(rel,{id:id},function(txt){
			if (txt) {
				$(delId).hide("");
				$('#QC').html("<b><font size=2 color=green>删除成功!</font></b>");
				setTimeout(function(){$('#QC').remove();},500);
			}
		});
	}else if(type == "idea"){
		$('#QC').html("<b><font size=2 color=green>正在提交...</font></b>");
		var delId="#idea_"+id;
		$.post(rel,{id:id},function(txt){
			  if(txt == '1'){
				  $(delId).fadeOut('slow');
				  $('#QC').html("<b><font size=2 color=blue>操作成功!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == '-2'){
				  $('#QC').html("<b><font size=2 color=blue>请选择需要删除的idea!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "-1"){
				  $('#QC').html("<b><font size=2 color=blue>不能删除别人的idea!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "0"){
				  $('#QC').html("<b><font size=2 color=blue>删除失败!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "-3"){
				  $('#QC').html("<b><font size=2 color=blue>未知错误!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }	 
		});
	}else if(type == "map"){
		$('#QC').html("<b><font size=2 color=green>正在提交...</font></b>");
		var delId="#map_"+id;
		$.post(rel,{id:id},function(txt){
			  if(txt == '1'){
				  $(delId).fadeOut('slow');
				  $('#QC').html("<b><font size=2 color=blue>操作成功!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == '-2'){
				  $('#QC').html("<b><font size=2 color=blue>请选择需要删除的map!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "-1"){
				  $('#QC').html("<b><font size=2 color=blue>不能删除别人的map!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "0"){
				  $('#QC').html("<b><font size=2 color=blue>删除失败!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }else if(txt == "-3"){
				  $('#QC').html("<b><font size=2 color=blue>未知错误!</font></b>");
				  setTimeout(function(){$('#QC').remove();},500);
			  }	 
		});
	}else {
		//日志，留言的评论删除
		$('#QC').html("<b><font size=2 color=green>正在提交...</font></b>");
		var delId="#"+"li"+id;
		
		$.post(rel,{id:id},function(txt){
			  if(txt){
				$("#p-cmt-body").val('');
				//评论数减一
				var plnum = parseInt($("#plnum").text())-1;
				$("#plnum").text(plnum+" ");
				$(delId).fadeOut('slow');
				$('#QC').html("<b><font size=2 color=blue>操作成功!</font></b>");
				setTimeout(function(){$('#QC').remove();},500);
			}else{
				alert("操作失败!");
				setTimeout(function(){$('#QC').remove();},500);
			}
		});
	}
}	


/**
 * 回复评论
 *
 */

function huifu_pl(id,hf_id){
	//$("#huifu_form").clone(true).appendTo($("#pl_"+id)).show();
	var name,hf_userId;
	if (hf_id) {
		name = $("#name_" + hf_id).text();
		hf_userId = $("#name_" + hf_id).attr("rel");
	}
	else {
		name = $("#name_" + id).text();
		hf_userId = $("#name_" + id).attr("rel");
	}
	$("#hf_userId").val(hf_userId);
	$("#huifu_form_"+id).find('.hf_con').val('回复'+name+':');
	$("#huifu_form_"+id).show();
}

function close_hf(id){
	//$(_this).parent().parent().hide();
	$("#huifu_form_"+id).hide();
}

function send_hf(_this,id){
	var userId	= $("#userId").val();
	var recordId	= $("#recordId").val();
	var module = $("#module").val();
	var content = $(_this).parent().prev().find(".hf_con").val();
	var hf_userId = $("#hf_userId").val();
	
	var userImg = $("#userImg").val();
	var userName= $("#userName").val();
	var userProv = $("#userProv").val();
	if(userProv)  userProv = '('+userProv+')';
	
	var ol_id = "#hf_ol_"+id;
	if(JHshStrLen(content)<20){
		alert('多写几个字吧!^_^');
		return;
	}
	$.post(APP+"/Comment/insert",{id:id,recordId:recordId,module:module,content:content,hf_userId:hf_userId},function(txt){
		if(txt){
			var hf_pl = '<li id="li'+txt+'" style="display:none">\
				<div class="post">\
					<p class="image">\
						<a href="'+APP+'/space/'+userId+'">\
							<img src="'+userImg+'" width="48"/>\
						</a>\
					</p>\
					<div class="info" style="width:530px">\
						<span class="author">\
							<a href="'+APP+'/space/'+userId+'">'+userName+'</a> '+userProv+'\
						</span>\
						<span class="time">刚刚</span>\
						<span class="delete">\
							<a class="Qconfirm" onclick="Qconfirm_show(this.rel,this.id,this.title)" id="'+txt+'" rel="'+APP+'/Comment/delete" href="javascript:void(0)"  title="确定要删除?">删除</a>\
						</span>\
					</div>\
					<div class="content" style="width:530px">\
						'+content+'\
					</div>\
				</div>\
			</li>';
			$(ol_id).append(hf_pl);
			$("#li"+txt).show('slow');
			//$(hf_pl).show('slow');
			$(_this).parent().prev().find(".hf_con").val('');
			$("#huifu_form_"+id).hide();
		}
	});

	
}










