var text_input = "文本";
var adv_mode = "UBB Code - 插入代码前会给出相应提示\n\n点击相应按钮将会出现提示框来指导你完成UBB的输入";
var normal_mode = "UBB Code - 立即插入模式\n\n点击相应按钮将会立即输入UBB代码，也可以选定相应内容后点击按钮。";
var email_normal = "请输入Email链接显示文本。\n留空则鼠标悬停时显示Email地址本身。";
var email_normal_input = "请输入Email地址:";
var fontsize_normal = "请输入要应用字体大小的文本内容";
var font_normal = "请输入要应用字体类型的文本内容";
var bold_normal = "请输入要进行加粗的文本内容";
var italicize_normal = "请输入要设置为斜体的文本内容";
var underline_normal = "请输入要加上下划线的文本内容";
var strike_normal = "请输入要加上删除线的文本内容";
var sup_normal = "请输入要标记为上标格式的文本内容";
var sub_normal = "请输入要标记为下标格式的文本内容";
var quote_normal = "请输入要引用的文本:";
var color_normal = "请输入要使用该颜色的文本内容";
var center_normal = "请输入要居中的文本内容";
var link_normal = "请输入鼠标悬停在链接上显示的文本。\n留空则鼠标悬停时显示链接本身。";
var link_normal_input = "请输入链接:";
var image_normal = "请输入图片地址:";
var media_type = "请输入媒体类型(flash=Flash, mp=Windows Media Player, rm=RealPlayer, qt=QuickTime):";
var media_size = "请输入媒体的显示大小(请输入\"宽,高,1[自动播放]\"，比如 400,300,1)。如果留空，那么将使用默认大小:";
var media_url = "请输入媒体地址:";
var code_normal = "请输入代码内容";
var list_normal = "请输入列表项的内容，留空表示输入完成:";
var seperator_normal = "本分隔符用于手动分隔段落、文字区块等。\n不输入此分隔符，系统将会自动对段落进行调整。\n一般文本里只允许一个分隔符出现:";
function ubbEditor(o){
	this.__defmode__ = o.mode;
	if(o.obj)this.__txt__=o.obj;else this.__txt__=document.getElementById(o.objid);
	if(o.tool)this.__tool__=o.tool;else this.__tool__=document.getElementById(o.toolid);
	this.__type__= o.type;
	this.__caller= o.caller;
	if(this.__defmode__)this.advmode=false;else this.advmode=true;
	if(this.__txt__){
		this.__tool__.innerHTML="</a><a href=\"javascript:"+this.__caller+".bold()\"><img src=\"/misc/images/ubbcode/bb_bold.gif\" alt=\"加粗\" border=0/></a><a href=\"javascript:"+this.__caller+".italicize()\"><img src=\"/misc/images/ubbcode/bb_italicize.gif\" alt=\"斜体\" border=0/></a><a href=\"javascript:"+this.__caller+".underline()\"><img src=\"/misc/images/ubbcode/bb_underline.gif\" alt=\"下划线\" border=0/></a><a href=\"javascript:"+this.__caller+".strike()\"><img src=\"/misc/images/ubbcode/bb_strike.gif\" alt=\"删除线\" border=0/></a><a href=\"javascript:"+this.__caller+".superscript()\"><img src=\"/misc/images/ubbcode/bb_sup.gif\" alt=\"上标\" border=0/></a><a href=\"javascript:"+this.__caller+".subscript()\"><img src=\"/misc/images/ubbcode/bb_sub.gif\" alt=\"下标\" border=0/></a><a href=\"javascript:"+this.__caller+".center()\"><img src=\"/misc/images/ubbcode/bb_center.gif\" alt=\"居中\" border=0/></a><a href=\"javascript:"+this.__caller+".hyperlink()\"><img src=\"/misc/images/ubbcode/bb_url.gif\" alt=\"链接\" border=0/></a><a href=\"javascript:"+this.__caller+".email()\"><img src=\"/misc/images/ubbcode/bb_email.gif\" alt=\"邮件地址\" border=0/></a><a href=\"javascript:"+this.__caller+".image()\"><img src=\"/misc/images/ubbcode/bb_image.gif\" alt=\"图片\" border=0/></a><a href=\"javascript:"+this.__caller+".media()\"><img src=\"/misc/images/ubbcode/bb_media.gif\" alt=\"Flash\" border=0/></a><a href=\"javascript:"+this.__caller+".code()\"><img src=\"/misc/images/ubbcode/bb_code.gif\" alt=\"代码\" border=0/></a><a href=\"javascript:"+this.__caller+".quote()\"><img src=\"/misc/images/ubbcode/bb_quote.gif\" alt=\"引用\" border=0/></a><a href=\"javascript:"+this.__caller+".list()\"><img src=\"/misc/images/ubbcode/bb_list.gif\" alt=\"列表\" border=0/></a><a href=\"javascript:"+this.__caller+".seperator()\"><img src=\"/misc/images/ubbcode/bb_seperator.gif\" alt=\"段落分隔\" border=0/></a><br><select style=\"width=120px\" name=\"font\" onfocus=\"this.selectedIndex=0\" onchange=\""+this.__caller+".chfont(this.options[this.selectedIndex].value)\"><option value=\"\" selected=\"selected\">选择字体</option><option value=\"宋体\">宋体</option><option value=\"黑体\">黑体</option><option value=\"楷体\">楷体</option><option value=\"Arial\">Arial</option><option value=\"Courier New\">Courier New</option><option value=\"Impact\">Impact</option><option value=\"Tahoma\">Tahoma</option><option value=\"Times New Roman\">Times New Roman</option><option value=\"Verdana\">Verdana</option></select><select name=\"size\" onfocus=\"this.selectedIndex=0\" onchange=\""+this.__caller+".chsize(this.options[this.selectedIndex].value)\"><option value=\"\" selected=\"selected\">字号</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"12\">12</option><option value=\"16\">16</option><option value=\"18\">18</option><option value=\"24\">24</option><option value=\"28\">28</option><option value=\"32\">32</option><option value=\"36\">36</option></select><select name=\"color\" onfocus=\"this.selectedIndex=0\" onchange=\""+this.__caller+".chcolor(this.options[this.selectedIndex].value)\"><option value=\"\" selected=\"selected\">字体颜色</option><option value=\"White\" style=\"background-color:white;color:white;\">White</option><option value=\"Black\" style=\"background-color:black;color:black;\">Black</option><option value=\"Red\" style=\"background-color:red;color:red;\">Red</option><option value=\"Yellow\" style=\"background-color:yellow;color:yellow;\">Yellow</option><option value=\"Pink\" style=\"background-color:pink;color:pink;\">Pink</option><option value=\"Green\" style=\"background-color:green;color:green;\">Green</option><option value=\"Orange\" style=\"background-color:orange;color:orange;\">Orange</option><option value=\"Purple\" style=\"background-color:purple;color:purple;\">Purple</option><option value=\"Blue\" style=\"background-color:blue;color:blue;\">Blue</option><option value=\"Beige\" style=\"background-color:beige;color:beige;\">Beige</option><option value=\"Brown\" style=\"background-color:brown;color:brown;\">Brown</option><option value=\"Teal\" style=\"background-color:teal;color:teal;\">Teal</option><option value=\"Navy\" style=\"background-color:navy;color:navy;\">Navy</option><option value=\"Maroon\" style=\"background-color:maroon;color:maroon;\">Maroon</option><option value=\"LimeGreen\" style=\"background-color:limegreen;color:limegreen;\">LimeGreen</option></select>&nbsp;<input type=\"radio\" name=\"mode\" value=\"1\" onclick=\""+this.__caller+".chmode(1)\" checked=\"checked\" />基本<input type=\"radio\" name=\"mode\" value=\"0\" onclick=\""+this.__caller+".chmode(0)\" />提示<iframe src=\"/fileupload.html?type="+this.__type__+"&_o_="+this.__txt__.id+"\"  height=32 width=500 scrolling=\"no\" frameborder=\"no\" id=\"frmPageBuffer\" name=\"frmPageBuffer\"></iframe>";
	}
}
ubbEditor.prototype={
	setfocus:function(){this.__txt__.focus();},
	chmode:function(swtch){//切换模式
		if(1==swtch){
			this.advmode=true;
			alert(normal_mode);
		}else{
			this.advmode=false;
			alert(adv_mode);
		}
  },
	storeCaret:function(cursorPosition){if(cursorPosition.createTextRange) cursorPosition.caretPos = document.selection.createRange().duplicate();},
	AddText:function(NewCode){//添加串
		if(document.all){
			this.insertAtCaret(this.__txt__, NewCode);
			this.setfocus();
		}else{
			this.__txt__.value+=NewCode;
			this.setfocus();
		}
	},
	insertAtCaret:function(textEl,text){//选区添加
		if(textEl.createTextRange&&textEl.caretPos){
			var caretPos=textEl.caretPos;
			caretPos.text+=caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
		}else if(textEl){
			textEl.value += text;
		}else{
			textEl.value=text;
		}
	},
	insertUBB:function(o){
		if(!o.prompttype)o.prompttype=text_input;
		if(document.selection&&document.selection.type=="Text") {
			var range=document.selection.createRange();
			range.text=o.s0+range.text+o.s1;
		}else if(this.advmode) {
			AddTxt=o.s0+o.s1;
			this.AddText(AddTxt);
		}else{
			txt=prompt(o.prompt,o.prompttype);
			if(txt!=null){
				AddTxt=o.s0+txt;
				this.AddText(AddTxt);
				this.AddText(o.s1);
			}
		}
	},
	insertUBB1:function(o){
		if(!o.prompttype0)o.prompttype0="";
		if(!o.prompttype1)o.prompttype1="";
		if(!o.nodirect&&document.selection&&document.selection.type=="Text") {
			var range=document.selection.createRange();
			range.text=o.s0+range.text+o.s1;
		}else if(!o.nodirect&&this.advmode) {
			AddTxt=o.s0+o.s1;
			this.AddText(AddTxt);
		}else{
			txt2=prompt(o.prompt0,o.prompttype0);
			if (txt2!=null) {
				txt=prompt(o.prompt1,o.prompttype1);
				if (txt!=null) {
					if (txt2=="") {
						AddTxt=o.s0+txt;
						this.AddText(AddTxt);
						this.AddText(o.s1);
					} else {
						AddTxt=o.s2+txt2+"]"+txt;
						this.AddText(AddTxt);
						this.AddText(o.s1);
					}
				}
			}
		}
	},
	chsize:function(size){//字号
		this.insertUBB({s0:"[size="+size+"]",s1:"[/size]",prompt:fontsize_normal});
	},
	chfont:function(font){
		this.insertUBB({s0:"[font="+font+"]",s1:"[/font]",prompt:font_normal});
	},
	bold:function(){
		this.insertUBB({s0:"[b]",s1:"[/b]",prompt:bold_normal});
	},
	italicize:function(){
		this.insertUBB({s0:"[i]",s1:"[/i]",prompt:italicize_normal});
	},
	underline:function(){
		this.insertUBB({s0:"[u]",s1:"[/u]",prompt:underline_normal});
	},
	strike:function(){
		this.insertUBB({s0:"[s]",s1:"[/s]",prompt:strike_normal});
	},
	superscript:function(){
		this.insertUBB({s0:"[sup]",s1:"[/sup]",prompt:sup_normal});
	},
	subscript:function(){
		this.insertUBB({s0:"[sub]",s1:"[/sub]",prompt:sub_normal});
	},
	chcolor:function(color){
		this.insertUBB({s0:"[color="+color+"]",s1:"[/color]",prompt:color_normal});
	},
	center:function(){
		this.insertUBB({s0:"[align=center]",s1:"[/align]",prompt:center_normal});
	},
	image:function(){
		this.insertUBB({s0:"[img]",s1:"[/img]",prompt:image_normal});
	},
	code:function(){
		this.insertUBB({s0:"[code]",s1:"[/code]",prompt:code_normal});
	},
	quote:function(){
		this.insertUBB({s0:"[quote]",s1:"[/quote]",prompt:quote_normal});
	},
	hyperlink:function(){
		this.insertUBB1({s0:"[url]",s1:"[/url]",s2:"[url=",prompt0:link_normal,prompt1:link_normal_input,prompttype1:"http://"});
	},
	email:function(){
		this.insertUBB1({s0:"[email]",s1:"[/email]",s2:"[email=",prompt0:email_normal,prompt1:email_normal_input,prompttype1:"name@domain.com"});
	},
	media:function () {
		txt=prompt(media_type,"flash");
		while ("flash,mp,rm,qt".indexOf(txt)<0||txt=="") {
			txt=prompt(media_type,"flash");
		}
		this.insertUBB1({s0:"["+txt+"]",s1:"[/"+txt+"]",s2:"["+txt+"=",prompt0:media_size,prompt1:media_url,prompttype0:"400,300,1",prompttype1:"http://",nodirect:1});
	},

	list:function(){
		if(this.advmode){
			AddTxt="\n[list]\n[*]\n[*]\n[*]\n[/list]\n";
			this.AddText(AddTxt);
		}else{
			AddTxt="\n[list]\n";
			txt="1";
			while ((txt!="") && (txt!=null)) {
				txt=prompt(list_normal,"");
				if (txt!="") {
						AddTxt+="[*]"+txt+"\n";
				}
			}
			AddTxt+="[/list]\n";
			this.AddText(AddTxt);
		}
	},
	insertSmilies:function(strCode) {
		strCode = ' ' + strCode + ' ';
		if (this.__txt__.createTextRange && this.__txt__.caretPos) {
		var caretPos = this.__txt__.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? strCode + ' ' : strCode;
		this.__txt__.focus();
		} else {
		this.__txt__.value  += strCode;
		this.__txt__.focus();
		}
	},
	seperator:function () {
		alert(seperator_normal);
		if (this.__txt__.createTextRange && this.__txt__.caretPos) {
			var caretPos = this.__txt__.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? '[---] ' : '[---]';
			this.__txt__.focus();
		} else {
			this.__txt__.value+='[---]';
			this.__txt__.focus();
		}
	}
}