/*append event as control parameter of (by control,event name,event function)*/
function __event(a,b,g){
	var d="on"+b;
	if(a.addEventListener)a.addEventListener(b,g,false);
	else if(a.attachEvent)a.attachEvent(d,g);
	else{var e=a[d];a[d]=function(){var c=e.apply(this,arguments),j=g.apply(this,arguments);return c==undefined?j:j==undefined?c:j&&c}}
}
/*html code edit control*/
function editor(textarea) {
	function _encode(str) {return str.replace(/&/ig, "&amp;").replace(/</ig, "&lt;").replace(/>/ig, "&gt;").replace(/\x22/ig, "&quot;");}
	function _decode(str) {return str.replace(/&lt;/ig, "<").replace(/&gt;/ig, ">").replace(/&/ig, "&amp;");}
	function _f1(n){var c=["white","buttonhighlight","buttonshadow"]; return "1px solid "+c[n];}
	function _out(e){this.style.border=_f1(0);}
	function _over(e){with (this.style){borderLeft   = borderTop    	= _f1(1);borderRight	= borderBottom	= _f1(2);}}
	function _up(e){with (this.style){borderLeft   	= borderTop    	= _f1(1);borderRight	= borderBottom	= _f1(2);}}
	function _down(e){with (this.style){borderRight  = borderBottom 	= _f1(1);borderLeft		= borderTop		= _f1(2);}}
	function _pic(k){var p= document.createElement("IMG");p.src=this.path+k+this.extname;p.id="editor"+this.index+k;p.style.border=_f1(0);return p;}
	function _btnevent(pic,fn){
			__event(pic,"mouseover",function(){_over.apply(pic)});
			__event(pic,"mouseout",function(){_out.apply(pic)});
			__event(pic,"mousedown",function(){_down.apply(pic)});
			__event(pic,"mouseup",function(){_up.apply(pic)});
			pic.onclick=function(){fn.apply(_2,arguments)}
	}
	function _add(p1,p2){var f=p2?p2:function(){this.cmd(p1);};var p=_pic.call(this,p1);_btnevent(p, f);return p;}
	function _icon(src){var p= document.createElement("IMG");p.src=src;p.style.border=_f1(0);
							_btnevent(p,function(){this.cmd('InsertImage',false,src)});return p;}
	
	this.icon=function(dv,path,ext,start,len){for(;start<len;start++){dv.appendChild(_icon.call(this,path+''+start+ext));}}
	this.picture=function(dv,s){dv.appendChild(_pic.call(this,s));}
	this.extend = function(source) { for (var p in source) {this[p] = source[p];} }
	this.userUI=function(d1,obj){for(var k in obj){d1.appendChild(_add.call(this, k, obj[k]));}}
	this.createUI=function(){var r=arguments[0];for(var i=1;i<arguments.length;i++){var k=arguments[i];r.appendChild(_add.call(this, k, null));}}
	this.createOption=function(d1,obj,fn){
		var r=document.createElement("SELECT");
		__event(r,"change", function(){obj.change.apply(r);});
		for(var k in obj.opt) r.appendChild(fn(obj.opt[k], k));
		d1.appendChild(r);
	}
	this.focus=function(){this._iframe.contentWindow.focus();}
	this.cmd=function(what,bl,opt){
		this.focus();
		var _a,_w=this._iframe.contentWindow.document;
		if(_w.getSelection){_a=_w;}else{_a=_w.selection.createRange();}
		if(opt==null||opt==''){_a.execCommand(what,false,"");} else{_a.execCommand(what,bl,opt);}
	}
	this.addEditStyle=function(o){
		var _doc=this._iframe.contentWindow.document;
		if(!_doc.createStyleSheet){
			var n1=document.createElement("style");
			for(var k in o) n1.appendChild(_doc.createTextNode(k+"{"+o[k]+"}"));
			_doc.body.appendChild(n1);
		}else{
			var s=_doc.createStyleSheet();
			for(var k in o) s.addRule(k, o[k]);
		}
	}
	this.save=function(e){
		var str=_2._iframe.contentWindow.document.body.innerHTML;
			str=str.replace(/<script([\S\s]+?)<\/script>/ig,"");
		try{textarea.value=_encode(str);}catch(e){textarea.innerHTML=str;}
	}
	this.write=function(str){this._iframe.contentWindow.document.body.innerHTML=str;}
	//initialize class
	textarea.style.display = "none";
	var _2=this;
	this._iframe = textarea.parentNode.insertBefore(document.createElement("iframe"), textarea);
	this._iframe.className = "htmlarea";
	var _d=this._iframe.contentWindow.document;
	_d.designMode = "On";
	_d.contentEditable = true;
	_d.open();
	_d.write(_decode(textarea.innerHTML));
	_d.close();
	if (textarea.form) __event(textarea.form,"submit",this.save);
};
//
function winforum(o1,div1,div2,div3){
	var _color={'#ffffff':'白色','#ff0000':'紅色','#00ff00':'綠色','#0000ff':'藍色','#ff00ff':'桃紅色','#00ffff':'淺藍色',
		'#ffff00':'黃色','#000000':'黑色','#70db93':'草綠色','#8c7853':'青銅色','#b87333':'紅銅色','#d19275':'長石色',
		'#cd7f32':'金色','#c0c0c0':'灰色','#9f9f9f':'卡其色','#8e236b':'紫藍色','#ff7f00':'橙色','#db70db':'淡紫色',
		'#bc8f8f':'粉紅色','#eaadea':'梅李色','#d9d9f3':'石英色','#6f4242':'鮭肉色','#8c1717':'鮮紅色','#e6e8fa':'銀色',
		'#d8bfd8':'淡紫色','#adeaea':'藍綠色','#4f2f4f':'紫色','#d8d8bf':'小麥色','#faebd7':'復古白','#f0ffff':'天藍色',
		'#f5f5dc':'米色','#ffe4c4':'淡黃色','#ffebcd':'白杏仁','#8a2be2':'青紫色','#a52a2a':'棕色','#deb887':'硬木頭',
		'#5f9ea0':'軍隊藍','#7ff000':'淡黃綠色','#d2691e':'灰褐色','#ff7f50':'珊瑚色','#6495ed':'瞿麥藍','#fff8dc':'玉蜀黍穗',
		'#dc143c':'鮮紅色','#00008b':'深藍色','#008b8b':'深藍綠色','#b8860b':'深秋麒麟','#a9a9a9':'深灰色','#006400':'墨綠色',
		'#bdb76b':'深卡其色','#8b008b':'暗紫紅色','#556b2f':'暗橄欖綠','#ff8c00':'暗橙色','#9932cc':'暗蘭花','#8b0000':'暗紅色',
		'#e9967a':'暗橙紅色','#8fbc8f':'暗海綠色','#483d8b':'深灰藍色','#2f4f4f':'深藍灰色','#9400d3':'暗紫色','#ff1493':'重粉紅色',
		'#00bfff':'重天空藍','#696969':'暗灰色','#1e90ff':'柔和藍'}
	function createOpt(txt,val){var r=document.createElement('option');r.innerHTML=txt;r.setAttribute("value",val);return r;}
	function createBgOpt(txt,val){var r=createOpt(txt,val);r.style.background=val;;return r;}
	function createColorOpt(txt,val){var r=createOpt(txt,val);r.style.color=val;;return r;}
	function makecolor(name,obj){var r={'':name};for(var k in obj)r[k]=obj[k];return r;}
	editor.call(this, o1);
	var a=this;
	if(null!=div3)a.icon(div3,"/Forum/ICO/",".gif",80,91);
	a.extend({path:"/editor/", extname:".gif", index:1});
	a.createOption(div1,{change:function(){a.cmd('FontName',false,this[this.selectedIndex].value);this.selectedIndex=0;},
		 opt:{'':'设置字体','宋体':'宋体','黑体':'黑体','楷体_GB2312':'楷体','仿宋_GB2312':'仿宋','隶书':'隶书','幼圆':'幼圆',
			'新宋体':'新宋体','细明体':'细明体','Arial':'Arial','Arial Black':'Arial Black','Arial Narrow':'Arial Narrow',
			'Bradley Hand ITC':'Bradley Hand ITC','Brush Script	MT':'Brush Script MT','Century Gothic':'Century Gothic',
			'Comic Sans MS':'Comic Sans MS','Courier':'Courier','Courier New':'Courier New','MS Sans Serif':'MS Sans Serif',
			'Script':'Script','System':'System','Times New Roman':'Times New Roman','Viner Hand ITC':'Viner Hand ITC',
			'Verdana':'Verdana','Wide Latin':'Wide Latin','Wingdings':'Wingdings'}}, createOpt);
	a.createOption(div1,{change:function(){a.cmd('FontSize',false,this[this.selectedIndex].value);this.selectedIndex=0;},
		opt:{'':'设置大小','1':'1号字','2':'2号字','3':'3号字','4':'4号字','5':'5号字','6':'6号字','7':'7号字'}},createOpt);
	a.createOption(div1,{change:function(){a.cmd('ForeColor',false,this[this.selectedIndex].value);this.selectedIndex=0;},
		opt:makecolor('文字颜色',_color)},createColorOpt);
	a.createOption(div1,{change:function(){a.cmd('BackColor',false,this[this.selectedIndex].value);this.selectedIndex=0;},
		opt:makecolor('背景颜色',_color)},createBgOpt);
	
	a.userUI(div1,{LINK:function(){this.cmd('CreateLink',true,'false');}});
	a.createUI(div1,"unlink");
	a.createUI(div2, "BOLD","ITALIC","underline");
	a.picture(div2, "separator");
	a.userUI(div2,{SELECTALL:function(){this.cmd('SelectAll');}});
	a.createUI(div2,"COPY","CUT","PASTE","DELETE");
	a.picture(div2, "separator");
	a.createUI(div2, "InsertHorizontalRule","justifycenter","justifyleft","justifyright");
	a.picture(div2, "separator");
	a.createUI(div2, "InsertUnorderedList", "InsertOrderedList");
	a.picture(div2, "separator");
	a.createUI(div2, "INDENT","OUTDENT");
	a.picture(div2, "separator");
	a.createUI(div2,"subscript","superscript","RemoveFormat");
	a.addEditStyle({body:"margin:6px;font-size:13px;line-height:138%;border:#000000;",img:"border:1px solid red;","div":"font-size:13px;",
					".zj":"background:#EFF8F4;border:1px solid #C4F4DE;padding:8px;"});
}