﻿//创建框架
	var ubb = document.createElement('div');
	$(ubb)
	.attr('id', 'ubb')
	.css({
		display : 'none',
		visibility: 'visible',
		background: 'white'
	});
		var ubbcode= "<img src='"+plugin_url+"images/bold.gif' style='cursor: pointer' onclick='insert_ubb(\"b\")' />";
		ubbcode +="<img src='"+plugin_url+"images/italicize.gif' style='cursor: pointer' onclick='insert_ubb(\"i\")' />";
		ubbcode +="<img src='"+plugin_url+"images/underline.gif' style='cursor: pointer' onclick='insert_ubb(\"u\")' />";
		ubbcode +="<img src='"+plugin_url+"images/center.gif' style='cursor: pointer' onclick='insert_ubb(\"center\")' />";
		ubbcode +="<img src='"+plugin_url+"images/code.gif' style='cursor: pointer' onclick='insert_ubb(\"code\")' />";
		ubbcode +="<img src='"+plugin_url+"images/quote.gif' style='cursor: pointer' onclick='insert_ubb(\"quote\")' />";
		ubbcode +="<img src='"+plugin_url+"images/image.gif' style='cursor: pointer' onclick='insert_ubb(\"img\")' />";
		ubbcode +="<img src='"+plugin_url+"images/url.gif' style='cursor: pointer' onclick='insert_ubb(\"url\")' />";
		ubbcode +="<img src='"+plugin_url+"images/email.gif' style='cursor: pointer' onclick='insert_ubb(\"email\")' />";
		ubbcode += " <span style='font-size:10px;'>点击图标若无反应，请直接输入UBB代码。</span><br/>"
	$(ubb).html(ubbcode);
$(function() {
	$('#comment').before($(ubb));
	$(ubb).show();
});
//插入
function insert_ubb(cmd) {
	var cmd;
	$('#comment').parseHtml('['+cmd+']'+'[/'+cmd+']');
}

//JQ插件
$.fn.extend({
	selection: function() {
		var txt = '';
		var doc = this.get(0).document;
		if (doc) {
			var sel = doc.selection.createRange();
				if (sel.text.length > 0)
					txt = sel.text;
		}
		else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
			var s = this.get(0).selectionStart;
			var e = this.get(0).selectionEnd;
			if (s != e) {
				txt = this.get(0).value.substring(s, e);
			}
		}
		return $.trim(txt);
	},
	parseHtml: function(t) {
		var doc = this.get(0).document;
		if (doc) {
			this.get(0).focus();
			doc.selection.createRange().collapse;				
			this.get(0).document.selection.createRange().text = t;
		}
		else if (this.get(0).selectionStart || this.get(0).selectionStart == '0') {
			var s = this.get(0).selectionStart;
			var e = this.get(0).selectionEnd;
			var val = this.get(0).value;
			var start = val.substring(0,s);
			var end = val.substring(e);
			this.get(0).value = start + t + end;
		} 
	}
})
