function Face(id) {
	this.ojb = $("#" + id);
	this.oid = id;
	this.create()
}
// 头像路径
var faceUrl = "jwapp/common/image/face/png/";
// 定义头像数量
var faceNums = 81;
var strNames =["(开心)","(不爽)","(大哭)","(生气)","(矛盾)","(失望)","(保密)","(尴尬)","(喜悦)",
			   "(装酷)","(漠然)","(伤心)","(讽刺)","(晕菜)","(微笑)","(吐舌)","(惊讶)","(狡黠)",
			   "(暗爽)","(男生)","(女孩)","(呆子)","(秘密)","(左拥)","(右抱)","(爱慕)","(心碎)",    
			   "(礼物)","(心意)","(红唇)","(赞美)","(鄙视)","(鼓掌)","(欧克)","(狗屎)","(关注)",  
			   "(晴天)","(乌云)","(闪电)","(彩虹)","(雨伞)","(星星)","(月亮)","(金钱)","(警车)",
			   "(汽车)","(飞机)","(走路)","(岛屿)","(聚会)","(香烟)","(洋酒)","(啤酒)","(咖啡)",
			   "(蛋糕)","(匹萨)","(意面)","(盘子)","(时钟)","(睡着)","(音符)","(灵感)","(足球)",
			   "(思考)","(玫瑰)","(天使)","(魔鬼)","(兔子)","(青蛙)","(山羊)","(绵羊)","(虫子)",
			   "(狗脸)","(猫脸)","(鬼脸)","(电脑)","(电影)","(电话)","(电邮)","(相机)","(手机)"];
//var strNames = ["(微笑)", "(撇嘴)", "(色)", "(发呆)", "(得意)", "(流泪)", "(害羞)", "(睡)",
//		"(尴尬)", "(呲牙)", "(惊讶)", "(冷汗)", "(抓狂)", "(偷笑)", "(可爱)", "(傲慢)", "(困)",
//		"(冷汗)", "(大兵)", "(咒骂)", "(折磨)", "(衰)", "(擦汗)", "(抠鼻)", "(鼓掌)", "(坏笑)",
//		"(左哼哼)", "(右哼哼)", "(鄙视)", "(委屈)", "(阴险)", "(亲亲)", "(可怜)", "(爱情)",
//		"(飞吻)", "(跳跳)", "(回头)", "(献吻)", "(左太极)"];
Face.prototype.create = function() {
	var oid = this.oid;
	var my_face = $("body").find("[name=my_face_" + oid + "]");
	var msg = "<style>.faceContent{width:336px; position:absolute;border:1px solid #aaa;border-top:none;display:;z-index:9999; text-align:center;padding:2px;background:#fff;} .faceContent a img{float:left;cursor:pointer;margin:1px 1px; border:#cacaca 1px solid}  .faceContent a:hover img{border:1px solid #f51d69}   #faceTitle{height:22px; width:36px; position:absolute; background:url("
			+ faceUrl
			+ "first.png) no-repeat center center #fff;border:1px solid #aaa;border-bottom:none;'}</style>"
	msg += "<div class='faceContent' id='faceContent_" + oid + "'></div>";
	if (my_face.length == 0) {
		var _div = "<span name='my_face_" + oid + "'></span>";
		$("body").append($(_div).html(msg));
	} else {
		my_face.html(msg);
	}
	$("#faceContent_" + oid).html("");
	var _str = "";
	for (var i = 1; i <= faceNums; i++) {
		var str = faceUrl + "F_" + i + ".png";
		_str += "<a href='javascript:void(0)'><img src=" + str + " fn='"
				+ strNames[i - 1] + "' title='"
				+ strNames[i - 1].replace(/\(/g, "").replace(/\)/g, "")
				+ "' /></a>";
	}
	$("#faceContent_" + oid).html(_str);
}

Face.prototype.close = function(id) {
	if (!id) {
		$("#my_face_" + this.oid).html("");
	} else {
		$("#my_face_" + id).html("");
	}
}

function removeFace(id) {
	$("#faceContent_" + id).hide();
	// $("#my_face_"+id).html("");
}

// 控制表情区位置
Face.prototype.show = function() {
	var O = this.ojb;
	var oid = this.oid;
	var top = O.offset().top;
	var left = O.offset().left;
	var right = O.width() - $("#faceContent_" + oid).width();
	left = left + right + 2;
	var m = parseFloat(O.css("padding-top"));
	var n = parseFloat(O.css("padding-bottom"));
	var h = O.height();
	$("#faceContent_" + oid).css("top", (top + h + m + n+2) + "px").css("left",
			left + "px").show();
	$("#faceContent_" + oid + " img").unbind("click").bind("click", function() {
				var _v = O.val();
				_v = $(this).attr("fn");
				O.setCaret();
				O.insertAtCaret(_v);
				O.focus();
				removeFace(oid);
				return false;
			});
	$("#faceContent_" + oid).unbind("click").bind("click", function() {
				O.setCaret();
				O.insertAtCaret("");
				O.focus();
				return false;
			});

}

// 控制表情区位置
Face.prototype.alwayshow = function() {
	var O = this.ojb;
	var oid = this.oid;
	var top = O.offset().top;
	var left = O.offset().left;
	var m = parseFloat(O.css("padding-top"));
	var n = parseFloat(O.css("padding-bottom"));
	var h = O.height();
	$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px").css(
			"left", left + "px").show();
	$("#faceContent_" + oid + " img").unbind("click").bind("click", function() {
				var _v = O.val();
				_v = $(this).attr("fn");
				O.setCaret();
				O.insertAtCaret(_v);
				O.focus();
				return false;
			});
	$("#faceContent_" + oid).unbind("click").bind("click", function() {
				O.setCaret();
				O.insertAtCaret("");
				O.focus();
				return false;
			});

}

//控制表情区位置
Face.prototype.forwardlostshow = function(replyTool,_this) {
	var O = this.ojb;
	var oid = this.oid;
	var top = O.offset().top;
	var left = O.offset().left;
	var m = parseFloat(O.css("padding-top"));
	var n = parseFloat(O.css("padding-bottom"));
	var h = O.height();
	$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px").css(
			"left", left + "px").show();
	$("#faceContent_" + oid + " img").unbind("click").bind("click", function() {
		clearTimeout(setTimeObj);
				setTimeFlag = 1;
				var _v = O.val();
				_v = $(this).attr("fn");
				O.setCaret();
				O.insertAtCaret(_v);
				O.focus();
				removeFace(oid);
				addTextAreaBlur(replyTool,_this);
				fixLength_publish(replyTool.find("#reply_textarea"+_this.attr("id")),replyTool.find("#textCount"),replyTool.find("#reply_cut"));
				setTimeout(function(){
					setTimeFlag = 0;
				},200);
				return false;
			});
	$("#faceContent_" + oid).unbind("click").bind("click", function() {
				setTimeFlag = 1;
				O.setCaret();
				O.insertAtCaret("");
				O.focus();
				setTimeout(function(){
					setTimeFlag = 0;
				},200);
				return false;
			});

}

// 控制表情区位置
Face.prototype.forwardshow = function() {
	var O = this.ojb;
	var oid = this.oid;
	var top = O.offset().top;
	var left = O.offset().left;
	var m = parseFloat(O.css("padding-top"));
	var n = parseFloat(O.css("padding-bottom"));
	var h = O.height();
	$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px").css(
			"left", left + "px").show();
	$("#faceContent_" + oid + " img").unbind("click").bind("click", function() {
				var _v = O.val();
				_v = $(this).attr("fn");
				O.setCaret();
				O.insertAtCaret(_v);
				O.focus();
				removeFace(oid);
				return false;
			});
	$("#faceContent_" + oid).unbind("click").bind("click", function() {
				O.setCaret();
				O.insertAtCaret("");
				O.focus();
				return false;
			});

}

function reAlwayshow(id) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			obj.show();
		} else {
			return false;
		}
	} else {
		new Face(id).alwayshow();
	}
}

function reAlwayshow_fresh(id) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			var O = $("#" + id);
			var oid = id;
			var top = O.offset().top;
			var left = O.offset().left;
			var m = parseFloat(O.css("padding-top"));
			var n = parseFloat(O.css("padding-bottom"));
			var h = O.height();
			$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px")
					.css("left", left + "px").show();
			$("#faceContent_" + oid + " img").unbind("click").bind("click",
					function() {
						var _v = O.val();
						_v = $(this).attr("fn");
						O.setCaret();
						O.insertAtCaret(_v);
						O.focus();
						return false;
					});
			$("#faceContent_" + oid).unbind("click").bind("click", function() {
						O.setCaret();
						O.insertAtCaret("");
						O.focus();
						return false;
					});
		} else {
			return false;
		}
	} else {
		new Face(id).alwayshow();
	}
}

function reForwardlostshow(id,replyTool,_this) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			var O = $("#" + id);
			var oid = id;
			var top = O.offset().top;
			var left = O.offset().left;
			var m = parseFloat(O.css("padding-top"));
			var n = parseFloat(O.css("padding-bottom"));
			var h = O.height();
			$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px")
					.css("left", left + "px").show();
			$("#faceContent_" + oid + " img").unbind("click").bind("click",
					function() {
						setTimeFlag = 1;
						var _v = O.val();
						_v = $(this).attr("fn");
						O.setCaret();
						O.insertAtCaret(_v);
						O.focus();
						removeFace(oid);
						addTextAreaBlur(replyTool,_this);
						fixLength_publish(replyTool.find("#reply_textarea"+_this.attr("id")),replyTool.find("#textCount"),replyTool.find("#reply_cut"));
						setTimeout(function(){
							setTimeFlag = 0;
						},200);
						return false;
					});
			$("#faceContent_" + oid).unbind("click").bind("click", function() {
						setTimeFlag = 1;
						O.setCaret();
						O.insertAtCaret("");
						O.focus();
						setTimeout(function(){
							setTimeFlag = 0;
						},200);
						return false;
					});
		} else {
			return false;
		}
	} else {
		new Face(id).forwardlostshow(replyTool,_this);
	}
}


function reForwardshow(id) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			var O = $("#" + id);
			var oid = id;
			var top = O.offset().top;
			var left = O.offset().left;
			var m = parseFloat(O.css("padding-top"));
			var n = parseFloat(O.css("padding-bottom"));
			var h = O.height();
			$("#faceContent_" + oid).css("top", (top + h + m + n + 2) + "px")
					.css("left", left + "px").show();
			$("#faceContent_" + oid + " img").unbind("click").bind("click",
					function() {
						var _v = O.val();
						_v = $(this).attr("fn");
						O.setCaret();
						O.insertAtCaret(_v);
						O.focus();
						removeFace(oid);
						return false;
					});
			$("#faceContent_" + oid).unbind("click").bind("click", function() {
						O.setCaret();
						O.insertAtCaret("");
						O.focus();
						return false;
					});
		} else {
			return false;
		}
	} else {
		new Face(id).forwardshow();
	}
}

// 控制表情区位置
Face.prototype.Imshow = function() {
	var O = this.ojb;
	var oid = this.oid;
	var top = O.offset().top;
	var left = O.offset().left;
	var m = parseFloat(O.css("padding-top"));
	var n = parseFloat(O.css("padding-bottom"));
	var h =  $("#faceContent_" + oid).height();
	$("#faceContent_" + oid).css("bottom", "142px").css(
			"left", left + "px").css("border-top","1px solid #AAAAAA").css("position","fixed").show();
	$("#faceContent_" + oid + " img").unbind("click").bind("click", function() {
				var _v = O.val();
				_v = $(this).attr("fn");
				O.setCaret();
				O.insertAtCaret(_v);
				O.focus();
				removeFace(oid);
				return false;
			});
	$("#faceContent_" + oid).unbind("click").bind("click", function() {
				O.setCaret();
				O.insertAtCaret("");
				O.focus();
				return false;
			});

}

function reImshow(id) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			var O = $("#" + id);
			var oid = id;
			var top = O.offset().top;
			var left = O.offset().left;
			var m = parseFloat(O.css("padding-top"));
			var n = parseFloat(O.css("padding-bottom"));
			var h = obj.height();
			$("#faceContent_" + oid).css("bottom", "142px").css(
			"left", left + "px").css("border-top","1px solid #AAAAAA").css("position","fixed").show();
			$("#faceContent_" + oid + " img").unbind("click").bind("click",
					function() {
						var _v = O.val();
						_v = $(this).attr("fn");
						O.setCaret();
						O.insertAtCaret(_v);
						O.focus();
						removeFace(oid);
						return false;
					});
			$("#faceContent_" + oid).unbind("click").bind("click", function() {
						O.setCaret();
						O.insertAtCaret("");
						O.focus();
						return false;
					});
		} else {
			return false;
		}
	} else {
		new Face(id).Imshow();
	}
}


// 重新显示
function reShow(id) {
	var obj = $("#faceContent_" + id);
	if (obj.size() != 0) {
		if (obj.css("display") == "none") {
			var O = $("#" + id);
			var top = O.offset().top;
			var left = O.offset().left;
			var right = O.width() - obj.width();
			left = left + right + 2;
			var m = parseFloat(O.css("padding-top"));
			var n = parseFloat(O.css("padding-bottom"));
			var h = O.height();
			obj.css("top", (top + h + m + n+2) + "px").css("left", left + "px")
					.show();
			$("#faceContent_" + id + " img").unbind("click").bind("click",
					function() {
						var _v = O.val();
						_v = $(this).attr("fn");
						O.setCaret();
						O.insertAtCaret(_v);
						O.focus();
						removeFace(id);
						return false;
					});
			$("#faceContent_" + id).unbind("click").bind("click", function() {
						O.setCaret();
						O.insertAtCaret("");
						O.focus();
						return false;
					});
		} else {
			return false;
		}
	} else {
		new Face(id).show();
	}
}

// 将String转义
function displayHtml(str) {
	if (str != null) {
		// 将字符串转换成数组
		var strArr = str.split('');
		// HTML页面特殊字符显示，空格本质不是，但多个空格时浏览器默认只显示一个，所以替换
		var htmlChar = "&<>";
		for (var i = 0; i < str.length; i++) {
			// 查找是否含有特殊的HTML字符
			if (htmlChar.indexOf(str.charAt(i)) != -1) {
				// 如果存在，则将它们转换成对应的HTML实体
				switch (str.charAt(i)) {
					case '<' :
						strArr.splice(i, 1, '&#60;');
						break;
					case '>' :
						strArr.splice(i, 1, '&#62;');
						break;
					case '&' :
						strArr.splice(i, 1, '&#38;');
				}
			}
		}
		return strArr.join('');
	}
}
// 替换页面中的表情代码为图片
function convertImg(val) {
	if (val != null) {
		for (var i = 0; i < strNames.length; i++) {
			val = val.replace(new RegExp("\\(" + strNames[i] + "\\)", 'gm'),
					"<img  src='" + faceUrl + "F_" + (i + 1) + ".png'"
							+ " title='"
							+ strNames[i].replace(/\(/g, "").replace(/\)/g, "")
							+ "'/>");
		}
		return val;
	}
}

jQuery.extend({
			/**
			 * 清除当前选择内容
			 */
			unselectContents : function() {
				if (window.getSelection)
					window.getSelection().removeAllRanges();
				else if (document.selection)
					document.selection.empty();
			}
		});
jQuery.fn.extend({
	/**
	 * 选中内容
	 */
	selectContents : function() {
		$(this).each(function(i) {
			var node = this;
			var selection, range, doc, win;
			if ((doc = node.ownerDocument) && (win = doc.defaultView)
					&& typeof win.getSelection != 'undefined'
					&& typeof doc.createRange != 'undefined'
					&& (selection = window.getSelection())
					&& typeof selection.removeAllRanges != 'undefined') {
				range = doc.createRange();
				range.selectNode(node);
				if (i == 0) {
					selection.removeAllRanges();
				}
				selection.addRange(range);
			} else if (document.body
					&& typeof document.body.createTextRange != 'undefined'
					&& (range = document.body.createTextRange())) {
				range.moveToElementText(node);
				range.select();
			}
		});
	},
	/**
	 * 初始化对象以支持光标处插入内容
	 */
	setCaret : function() {
		if (!$.browser.msie)
			return;
		var initSetCaret = function() {
			var textObj = $(this).get(0);
			textObj.caretPos = document.selection.createRange().duplicate();
		};
		$(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
	},
	/**
	 * 在当前对象光标处插入指定的内容
	 */
	insertAtCaret : function(textFeildValue) {
		var textObj = $(this).get(0);
		if (document.all && textObj.createTextRange && textObj.caretPos) {
			var caretPos = textObj.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ''
					? textFeildValue + ''
					: textFeildValue;
		} else if (textObj.setSelectionRange) {
			var rangeStart = textObj.selectionStart;
			var rangeEnd = textObj.selectionEnd;
			var tempStr1 = textObj.value.substring(0, rangeStart);
			var tempStr2 = textObj.value.substring(rangeEnd);
			textObj.value = tempStr1 + textFeildValue + tempStr2;
			textObj.focus();
			var len = textFeildValue.length;
			textObj.setSelectionRange(rangeStart + len, rangeStart + len);
			textObj.blur();
		} else {
			textObj.value += textFeildValue;
		}
	}
});


$.fn.extend({
	selection : function() {
		var s, e, range, stored_range;
		if (this[0].selectionStart == undefined) {
			var selection = document.selection;
			if (this[0].tagName.toLowerCase() != "textarea") {
				var val = this.val(), range = selection.createRange()
						.duplicate();
				range.moveEnd("character", val.length);
				s = (range.text == "" ? val.length : val
						.lastIndexOf(range.text));
				range = selection.createRange().duplicate();
				range.moveStart("character", -val.length);
				e = range.text.length;
			} else {
				range = selection.createRange();
				stored_range = range.duplicate();
				stored_range.moveToElementText(this[0]);
				stored_range.setEndPoint('EndToEnd', range);
				s = stored_range.text.length - range.text.length;
				e = s + range.text.length;
			}
		} else {
			s = this[0].selectionStart;
			e = this[0].selectionEnd;
		}
		var te = this[0].value.substring(s, e);
		// return {start:s,end:e,text:te}
		return s;
	}
})

//输入框绑定@
function userTip(){
	var _this=this;
	var id=_this.attr("id");
	var tip_div=_this.parent().find("#utips_div_"+id);
	var tip_span1=_this.parent().find("#utips_span1_"+id);
	var tip_span2=_this.parent().find("#utips_span2_"+id);
	var tip_span3=_this.parent().find("#utips_span3_"+id);
	
	//这个是让div的大小和textarea保持住
	function changeSize(){
		var a1 = parseFloat(_this.css("width"));
		var a2 = parseFloat(_this.css("font-size"));
		var a = a1 - (a2 / 2);
		tip_div.css("width", "" + a);
		tip_div.css("height", _this.css("height"));	
	}
	
	
	function shows_tips(){
		var o=_this;
		changeSize();
		//光标所在位置
		var num = o.selection();
		//整个textarea的内容
		var htmlstr = o.val();
		//光标前的内容
		var subHtmlStr = htmlstr.substr(0, num);
		//光标前内容最后一个@符号
		var indxnum = subHtmlStr.lastIndexOf("@");
		//在存在@符号的情况下
		if (indxnum != -1) {
			//取出最后一个@和光标之间的内容
			var subs=subHtmlStr.substr(indxnum+1,num);
			//内容不为空以及不含空格不含转行符号
			if((subs !="")&& (subs.indexOf(" ")==-1) && (subs.indexOf(unescape("%0A"))==-1) &&(subs.length <=8)){
				var my_tips = $("body").find("div[name=my_tips_" + id + "]");
				if (my_tips.length == 0) {
					var _div = "<div name='my_tips_"+ id+ "' style='display:none;position:absolute;z-index:2147483647;'></div>";;
					$("body").append(_div);
				} 
				my_tips = $("body").find("div[name=my_tips_" + id + "]");
				var _http= jQuery.parseJSON(friends.searchCurFriendsAvatar({},subs,edpinfo.user.id_type));
				renderTpl(my_tips, "dcp/apps/twitter/html/twitter_userTip.html", _http);
				
				//绑定选择事件
				my_tips.find("li").each(function(){
					var m=$(this);
					m.unbind("click").bind("click",function(){
						//alert(1);
						var a=htmlstr.substr(0, indxnum);
						var b=htmlstr.substr(num, htmlstr.length);
						var c=m.attr("fname");
						var d=m.attr("id");
						var top=o.scrollTop();
						_this.val(a+"@"+c+"("+d+")"+" "+b);
						o.scrollTop(top);
						my_tips.hide();
						_this.focus();
						var obj=document.getElementById(_this.attr("id"));
						var length = (a+"@"+c+"("+d+")"+" ").length;
						obj.setSelectionRange(length,length);
					});
					
					m.unbind("hover");
					m.hover(function(){
						$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
							var x=$(this);
							x.removeClass("dm_h");	
							x.attr("fx","0");
						});
						m.addClass("dm_h");
						m.attr("fx","1");
						return false;
 					},
 					function(){
   						m.removeClass("dm_h");
   						m.attr("fx","0");
						return false;
					});
				});
				
				
				
				var strs1 = htmlstr.substr(0, indxnum+1);
				strs1 = strs1.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span1.html(strs1);
				var strs3 = htmlstr.substr(indxnum,htmlstr.length);
				strs3 = strs3.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span3.html(strs3);
				tip_div.scrollTop(o.scrollTop());
				//alert(tip_span2.attr("id"));
				var top = tip_span2.offset().top+ parseInt(o.css("line-height"));
				var left = tip_span2.offset().left;
				my_tips.css("top",(top) + "px").css("left", left + "px").show();
				_this.focus();
			}else{
				$("body").find("[name=my_tips_" + id + "]").hide();	
			}
		}else{
			$("body").find("[name=my_tips_" + id + "]").hide();
		}
	}
	_this.unbind(($.browser.opera ? "keypress" : "keydown")).bind(($.browser.opera ? "keypress" : "keydown"),function(e){
		if(($("body").find("[name=my_tips_" + id + "]").css("display")!="none") &&($("body").find("[name=my_tips_" + id + "]").css("display")!=null)){
			var size=$("body").find("[name=my_tips_" + id + "]").find("li").size();
			if(e.keyCode == 40){
				//向下
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				else if(i<size-1){
					i++;	
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 38){
				//向上
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				if(i>0){
					i--;
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 13){
				//enter
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					return true;
				}else{
					$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").click();
				}
				return false;
			}
		}
	});
	//设置哪个选中
	function setSelected(i){
		var li_a=$("body").find("[name=my_tips_" + id + "]").find("li[fid='"+i+"']");
		$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
			var o=$(this);
			o.removeClass("dm_h");	
			o.attr("fx","0");
		});
		li_a.addClass("dm_h");
		li_a.attr("fx","1");
		return false;
	}
	
	_this.unbind(($.browser.opera ? "keypress" : "keyup")).bind(($.browser.opera ? "keypress" : "keyup"),function(e){
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();	
	});
	_this.unbind("click").bind("click",function(e){
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();
	});

	_this.unbind("blur").bind("blur",function(){
		setTimeout(function(){
			$("body").find("[name=my_tips_" + id + "]").hide();
		},500);	
	});
}
//失物招领输入框绑定@
function userTip_Lost(replyTool){
	var _this=this;
	var id=_this.attr("id");
	var tip_div=_this.parent().find("#utips_div_"+id);
	var tip_span1=_this.parent().find("#utips_span1_"+id);
	var tip_span2=_this.parent().find("#utips_span2_"+id);
	var tip_span3=_this.parent().find("#utips_span3_"+id);
	
	//这个是让div的大小和textarea保持住
	function changeSize(){
		var a1 = parseFloat(_this.css("width"));
		var a2 = parseFloat(_this.css("font-size"));
		var a = a1 - (a2 / 2);
		tip_div.css("width", "" + a);
		tip_div.css("height", _this.css("height"));	
	}
	
	
	function shows_tips(){
		var o=_this;
		changeSize();
		//光标所在位置
		var num = o.selection();
		//整个textarea的内容
		var htmlstr = o.val();
		//光标前的内容
		var subHtmlStr = htmlstr.substr(0, num);
		//光标前内容最后一个@符号
		var indxnum = subHtmlStr.lastIndexOf("@");
		//在存在@符号的情况下
		if (indxnum != -1) {
			//取出最后一个@和光标之间的内容
			var subs=subHtmlStr.substr(indxnum+1,num);
			//内容不为空以及不含空格不含转行符号
			if((subs !="")&& (subs.indexOf(" ")==-1) && (subs.indexOf(unescape("%0A"))==-1) &&(subs.length <=8)){
				var my_tips = $("body").find("div[name=my_tips_" + id + "]");
				if (my_tips.length == 0) {
					var _div = "<div name='my_tips_"+ id+ "' style='display:none;position:absolute;z-index:2147483647;'></div>";;
					$("body").append(_div);
				} 
				my_tips = $("body").find("div[name=my_tips_" + id + "]");
				var _http= jQuery.parseJSON(friends.searchCurFriendsAvatar({},subs,edpinfo.user.id_type));
				renderTpl(my_tips, "dcp/apps/twitter/html/twitter_userTip.html", _http);
				//处理笑脸区隐藏情况
				$("[id^=faceContent_reply_]").each(function(){
					if($(this).attr("id")!="faceContent_"+_this.attr("fid")){
						$(this).hide();
					}
				});
				
				//绑定选择事件
				my_tips.find("li").each(function(){
					var m=$(this);
					m.unbind("click").bind("click",function(){
						var a=htmlstr.substr(0, indxnum);
						var b=htmlstr.substr(num, htmlstr.length);
						var c=m.attr("fname");
						var d=m.attr("id");
						var top=o.scrollTop();
						_this.val(a+"@"+c+"("+d+")"+" "+b);
						o.scrollTop(top);
						my_tips.hide();
						_this.focus();
						var obj=document.getElementById(_this.attr("id"));
						var length = (a+"@"+c+"("+d+")"+" ").length;
						obj.setSelectionRange(length,length);
						//计数
						fixLength_publish(_this,replyTool.find("#textCount"),replyTool.find("#reply_cut"));
					});
					
					m.unbind("hover");
					m.hover(function(){
						$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
							var x=$(this);
							x.removeClass("dm_h");	
							x.attr("fx","0");
						});
						m.addClass("dm_h");
						m.attr("fx","1");
						return false;
 					},
 					function(){
   						m.removeClass("dm_h");
   						m.attr("fx","0");
						return false;
					});
				});
				
				
				
				var strs1 = htmlstr.substr(0, indxnum+1);
				strs1 = strs1.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span1.html(strs1);
				var strs3 = htmlstr.substr(indxnum,htmlstr.length);
				strs3 = strs3.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span3.html(strs3);
				tip_div.scrollTop(o.scrollTop());
				//alert(tip_span2.attr("id"));
				var top = tip_span2.offset().top+ parseInt(o.css("line-height"));
				var left = tip_span2.offset().left;
				my_tips.css("top",(top) + "px").css("left", left + "px").show();
				_this.focus();
			}else{
				$("body").find("[name=my_tips_" + id + "]").hide();	
			}
		}else{
			$("body").find("[name=my_tips_" + id + "]").hide();
		}
	}
	_this.unbind(($.browser.opera ? "keypress" : "keydown")).bind(($.browser.opera ? "keypress" : "keydown"),function(e){
		fixLength_publish(_this,replyTool.find("#textCount"),replyTool.find("#reply_cut"));
		if($.trim(_this.val()).length==0){
			setTimeFlag = 0;
			addTextAreaBlur(replyTool,_this);
			reply_to_user_id = "";
			reply_to_user_name = "";
		}
		
		if(($("body").find("[name=my_tips_" + id + "]").css("display")!="none") &&($("body").find("[name=my_tips_" + id + "]").css("display")!=null)){
			var size=$("body").find("[name=my_tips_" + id + "]").find("li").size();
			if(e.keyCode == 40){
				//向下
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				else if(i<size-1){
					i++;	
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 38){
				//向上
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				if(i>0){
					i--;
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 13){
				//enter
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					return true;
				}else{
					$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").click();
				}
				return false;
			}
		}
	});
	//设置哪个选中
	function setSelected(i){
		var li_a=$("body").find("[name=my_tips_" + id + "]").find("li[fid='"+i+"']");
		$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
			var o=$(this);
			o.removeClass("dm_h");	
			o.attr("fx","0");
		});
		li_a.addClass("dm_h");
		li_a.attr("fx","1");
		return false;
	}
	
	_this.unbind(($.browser.opera ? "keypress" : "keyup")).bind(($.browser.opera ? "keypress" : "keyup"),function(e){
		fixLength_publish(_this,replyTool.find("#textCount"),replyTool.find("#reply_cut"));
		if($.trim(_this.val()).length==0){
			setTimeFlag = 0;
			addTextAreaBlur(replyTool,_this);
			reply_to_user_id = "";
			reply_to_user_name = "";
		}
		
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();	
	});
	_this.unbind("click").bind("click",function(e){
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();
	});
}


//输入框绑定@
function userTipAll(uid){
	var _this=this;
	var id=_this.attr("id");
	var tip_div=_this.parent().find("#utips_div_"+id);
	var tip_span1=_this.parent().find("#utips_span1_"+id);
	var tip_span2=_this.parent().find("#utips_span2_"+id);
	var tip_span3=_this.parent().find("#utips_span3_"+id);
	
	//这个是让div的大小和textarea保持住
	function changeSize(){
		var a1 = parseFloat(_this.css("width"));
		var a2 = parseFloat(_this.css("font-size"));
		var a = a1 - (a2 / 2);
		tip_div.css("width", "" + a);
		tip_div.css("height", _this.css("height"));	
	}
	
	
	function shows_tips(){
		var o=_this;
		changeSize();
		//光标所在位置
		var num = o.selection();
		//整个textarea的内容
		var htmlstr = o.val();
		//光标前的内容
		var subHtmlStr = htmlstr.substr(0, num);
		//光标前内容最后一个@符号
		var indxnum = subHtmlStr.lastIndexOf("@");
		//在存在@符号的情况下
		if (indxnum != -1) {
			//取出最后一个@和光标之间的内容
			var subs=subHtmlStr.substr(indxnum+1,num);
			//内容不为空以及不含空格不含转行符号
			if((subs !="")&& (subs.indexOf(" ")==-1) && (subs.indexOf(unescape("%0A"))==-1) &&(subs.length <=8)){
				var my_tips = $("body").find("div[name=my_tips_" + id + "]");
				if (my_tips.length == 0) {
					var _div = "<div name='my_tips_"+ id+ "' style='display:none;position:absolute;z-index:2147483647;'></div>";;
					$("body").append(_div);
				} 
				my_tips = $("body").find("div[name=my_tips_" + id + "]");
				var _http= jQuery.parseJSON(friends.searchCommonFriendsAvatar({},subs,uid,edpinfo.user.id_type));
				renderTpl(my_tips, "dcp/apps/twitter/html/twitter_all_userTip.html", _http);
				
				//绑定选择事件
				my_tips.find("li").each(function(){
					var m=$(this);
					m.unbind("click").bind("click",function(){
						//alert(1);
						var a=htmlstr.substr(0, indxnum);
						var b=htmlstr.substr(num, htmlstr.length);
						var c=m.attr("fname");
						var d=m.attr("id");
						var top=o.scrollTop();
						_this.val(a+"@"+c+"("+d+") "+b);
						o.scrollTop(top);
						my_tips.hide();
						_this.focus();
					});
					
					m.unbind("hover");
					m.hover(function(){
						$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
							var x=$(this);
							x.removeClass("dm_h");	
							x.attr("fx","0");
						});
						m.addClass("dm_h");
						m.attr("fx","1");
						return false;
 					},
 					function(){
   						m.removeClass("dm_h");
   						m.attr("fx","0");
						return false;
					});
				});
				
				
				
				var strs1 = htmlstr.substr(0, indxnum+1);
				strs1 = strs1.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span1.html(strs1);
				var strs3 = htmlstr.substr(indxnum,htmlstr.length);
				strs3 = strs3.replace(/\n/g, '<br/>').replaceAll("  ", " &nbsp;");
				tip_span3.html(strs3);
				tip_div.scrollTop(o.scrollTop());
				//alert(tip_span2.attr("id"));
				var top = tip_span2.offset().top+ parseInt(o.css("line-height"));
				var left = tip_span2.offset().left;
				my_tips.css("top",(top) + "px").css("left", left + "px").show();
				_this.focus();
			}else{
				$("body").find("[name=my_tips_" + id + "]").hide();	
			}
		}else{
			$("body").find("[name=my_tips_" + id + "]").hide();
		}
	}
	
	_this.unbind(($.browser.opera ? "keypress" : "keydown")).bind(($.browser.opera ? "keypress" : "keydown"),function(e){
		if(($("body").find("[name=my_tips_" + id + "]").css("display")!="none") &&($("body").find("[name=my_tips_" + id + "]").css("display")!=null)){
			var size=$("body").find("[name=my_tips_" + id + "]").find("li").size();
			if(e.keyCode == 40){
				//向下
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				else if(i<size-1){
					i++;	
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 38){
				//向上
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					i=0;
					setSelected(i);
					return false;	
				}
				if(i>0){
					i--;
				}
				setSelected(i);
				return false;
			}else if(e.keyCode == 13){
				//enter
				var i=$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid");
				if(i==null){
					return true;
				}else{
					$("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").click();
				}
				return false;
			}
		}
	});
	//设置哪个选中
	function setSelected(i){
		var li_a=$("body").find("[name=my_tips_" + id + "]").find("li[fid='"+i+"']");
		$("body").find("[name=my_tips_" + id + "]").find("li").each(function(){
			var o=$(this);
			o.removeClass("dm_h");	
			o.attr("fx","0");
		});
		li_a.addClass("dm_h");
		li_a.attr("fx","1");
		return false;
	}
	
	_this.unbind(($.browser.opera ? "keypress" : "keyup")).bind(($.browser.opera ? "keypress" : "keyup"),function(e){
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();	
	});
	_this.unbind("click").bind("click",function(e){
		var code=e.keyCode;
		if(code == 38 || code == 40 || code == 13) {
			if(code==13 && ($("body").find("[name=my_tips_" + id + "]").find("li[fx='1']").attr("fid")==null)){
				shows_tips();
			}
			return false;
		}
		shows_tips();
	});

	_this.unbind("blur").bind("blur",function(){
		setTimeout(function(){
			$("body").find("[name=my_tips_" + id + "]").hide();
		},500);	
	});
}


String.prototype.replaceAll = function(s1,s2) {
    return this.replace(new RegExp(s1,"gm"),s2);
}



function subStrs(str, len) {
	if (!str || !len) {
		return '';
	}
	// 预期计数：中文2字节，英文1字节
	var a = 0;
	// 循环计数
	var i = 0;
	// 临时字串
	var temp = '';
	for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 255) {
			// 按照预期计数增加2
			a += 2;
		} else {
			a++;
		}
		// 如果增加计数后长度大于限定长度，就直接返回临时字符串
		if (a > len) {
			return temp+"...";

		}
		// 将当前内容加到临时字符串
		temp += str.charAt(i);
	}
	// 如果全部是单字节字符，就直接返回源字符串
	return str;
}