$(function() {
	//创建表情框架
	var i = 1;
	var smilesdata = '';
	$.each(smiles_array,function(k,n){
		smileimg = plugin_url+"smiles/"+n.url;
		smilesdata += "<img src='"+smileimg+"' style='cursor: pointer;margin:2px;padding:1px;' onclick='insertSmile(\""+n.code+"\")' alt='"+n.code+"' />";
		if(i%10==0 && i!=0)
		smilesdata += "<br/>"
		i++;
	});
	var emotion = document.createElement('div');
		$(emotion)
		.attr('id', 'emotion')
		.css({
			display : 'none',
			visibility: 'visible',
			background: 'white',
			position: 'absolute',
			border: 'solid #ccc 1px',
			padding: '2px',
		});
	var toolbar = document.createElement('div');
		$(toolbar).css({position: 'relative'});
		$(toolbar).html('<a href="javascript:void(0);"><img id="toggleemt" src="'+plugin_url+'btn.gif" /></a>');
		$(toolbar).append($(emotion));
		$(emotion).html(smilesdata);
	
	$('textarea#comment').before($(toolbar));
	$(emotion).hide();
	$('#toggleemt').click(function(){
		$(emotion).toggle();
	});
	$(document).click(function(event) {
		if ($(event.target).attr("id") != "toggleemt" && $(event.target).attr("id") != "emotion") {
			$(emotion).hide();
		}
	});
});
//插入表情
function insertSmile(value) {
	$('textarea#comment').parseHtml(value);
}
//JQ插件
	$.fn.extend({
		parseHtml: function(myValue){
			var $t=$(this)[0];
			if (document.selection) {
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			}
			else 
				if ($t.selectionStart || $t.selectionStart == '0') {
					var startPos = $t.selectionStart;
					var endPos = $t.selectionEnd;
					var scrollTop = $t.scrollTop;
					$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
					this.focus();
					$t.selectionStart = startPos + myValue.length;
					$t.selectionEnd = startPos + myValue.length;
					$t.scrollTop = scrollTop;
				}
				else {
					this.value += myValue;
					this.focus();
				}
		}
	})
