W.texicon=function(a){
	var id=(a.id?' id="'+a.id+'"':'')
	return '<div style="text-align:center;background-color:#BBB;border-top:#FFF solid 2px;border-right:#EEE solid 2px;border-bottom:#777 solid 2px;padding:2px;border-left:#888 solid 2px;'+(a.sty||'')+'" class="dib fl '+(a.cls||'')+' arrow"'
		+' onmouseover="this.style.borderColor=\'yellow\';"'
		+' onmouseup="this.style.borderColor=\'yellow\';"'
		+' onmouseout="this.style.borderTopColor=\'#FFF\';this.style.borderRightColor=\'#EEE\';this.style.borderBottomColor=\'#777\';this.style.borderLeftColor=\'#888\';"'
		+' onmousedown="this.style.borderTopColor=\'#777\';this.style.borderRightColor=\'#888\';this.style.borderBottomColor=\'#fff\';this.style.borderLeftColor=\'#EEE\';'+(a.down||'')+'return false;"'
		+'>'
		+(a.value||a.label)+'</div>'
}; 

W.riched={
	inputs:{},defore:'black',deback:'white',
	clipboard:'',
	colbox:function(n){
		var safes='',safecols=[0,3,6,9,'C','F'],r,g,b,scl=safecols.length;
		r=scl;while(r--){g=scl;while(g--){b=scl;while(b--){
			safes+="<span onmousedown=\"riched.inputs['"+n+"'].com(event.altKey ?(document.selection?'backcolor':'hilitecolor'):'forecolor',this.style.backgroundColor);heidi('cbx_"+n+"');return false;\""
			+' style="background-color:#'+safecols[r]+safecols[r]+safecols[g]+safecols[g]+safecols[b]+safecols[b]
			+'">&nbsp;</span>'}
			}safes+='<Br/>'
		}
		return texicon({id:'clbx',value:'colour',cls:'prl',sty:"top:-2px;",down:"heidi('cbx_"+n+"',2);"})
			+'<br/><div id="cbx_'+n+'" style="display:none"><div class="prl"><DIV style="font-size:80%;padding:4px;">hold Alt to set background</DIV>'
			+'<DIV id="RTcols_'+n+'" style="line-height:80%;" unselectable="on" class="arrow">'+safes+eD+eD+eD
	},
	make:function(o){
		riched.inputs[o.name]=new riched.txtarea(o);
		if(!WD.designMode){
			dr('<span>'+riched.inputs[o.name].html+'</span>',o.home)
			return {};
		}
		riched.inputs[o.name].draw(o.home)
		return riched.inputs[o.name]
	},
	txtarea:function (o){for(var oi in o)this[oi]=o[oi];return this},
	but:function(o,n){for(var oi in o)this[oi]=o[oi];
		if(!this.html)this.html=this.com;
		this.owner=n;this.index=riched.inputs[n].buts.length;
		return this
	}
};
riched.but.prototype={
	com:'',
	arg:'',
	html:'',
	draw:function(){
		var diz="riched.inputs['"+this.owner+"'].buts['"+this.index+"']",
			id=this.com2||this.com,
			com='if('+(this.condition||'1').replace(/diz/g,diz)+')'
				+(/\W/.test(this.com)
				?this.com
				: // "a.set('"+(this.html)+"');"+
					"riched.clipboard=riched.inputs['"+this.owner+"'].com('"
					+(this.com||'Select')+"',"+diz+".arg);"
			);
		return {id:'rdF_'+id,tip:id,value:this.html,down:com}
		//(this.pretext||'').replace(/diz/g,diz)
		//	+texicon({id:'RE'+id,tip:id,value:this.html,down:com,dib:true})
	}
};
riched.makelink=function(red){
	var link=prompt('URL? (leave blank to unlink)');
	if(link)riched.inputs[red].cd().execCommand('createLink',false, link);
	else riched.inputs[red].cd().execCommand('Unlink',false,'');
};
riched.txtarea.prototype={
	rng:0,name:'',html:'',bgcol:'#FFFFFF',fgcol:'#000000',cssclass:'',buts:[],
	gethtml:fzz("a=dge('red_'+this.name)."+(document.selection?'':"contentWindow.document.body.")+"innerHTML;b=a.replace(/<[^>]+>/g,'').replace(/\\s+/g,'');return b?a.replace(/<div>\\s*<\\/div>/g,''):'';"),
	draw:function(into){
		var coms=[
			{com:'Bold',html:'&nbsp;<b>B</b>&nbsp;'},
			{com:'Italic',html:'&nbsp;<i>I</i>&nbsp;'},
			{com:'Underline',html:'&nbsp;<u>U</u>&nbsp;'},
			{com:'justifyleft',html:'<img src="client/red/left just.gif">'},
			{com:'justifycenter',html:'<img src="client/red/centre.gif">'},
			{com:'justifyright',html:'<img src="client/red/right just.gif">'},
			{com:'FormatBlock',com2:'Paragraph',arg:'<p>',html:'normal'},
			{com:'FormatBlock',com2:'Preformatted',arg:'<pre>',html:'<code>code</code>'},
			{com:'FormatBlock',com2:'Heading 1',arg:'<h1>',html:'<b style="font-size:110%">Head 1</b>'},
			{com:'FormatBlock',com2:'Heading 2',arg:'<h2>',html:'<b>Head 2</b>'},
			{com:'insertunorderedlist',html:'&nbsp;&bull;&nbsp;'},
			{com:'outdent',html:'&larr;'},
			{com:'indent',html:'&rarr;'}
			//{com:'Link',arg:'CreateLink',html:'link'}
			//	{com:"riched.inputs['"+this.name+"'].ranger('hello');",html:'image'}
		];
		if(0||this.links)coms.push(
			{com:'CreateLink',
				condition:'diz.arg',html:'link',
				pretext:'<input type="text" size="6" value="" onblur="if(this.value)diz.arg=this.value;">'
		});
		if(!this.buts.length&&!this.pre){
			for(i in coms)
				this.buts.push(new riched.but(coms[i],this.name))
		}		
		var h='',r=[];
		this.butht='<DIV class="'+this.cssclass+' p2">';
		for(i in this.buts)r.push(this.buts[i].draw())
		if(0)this.butht+='<div class="prl" style="float:right">'
			+texicon({id:'inp_colr',genus:'onoff',value:'colour',down:"heidi('cpkinp',2);",stern:true,opaque:true})
			+'<div id="cpkinp" class="pab z1" style="display:none">'+colur.picker(2)+eD
			+eD;
		for(i in r){
			morg(r[i],{sound:31,col:'88AADD',solo:true})
			this.butht+=texicon(r[i]);
		}
		this.butht+=texicon({id:'mklink',value:'link',down:"riched.makelink('"+this.name+"');"});
		this.butht+=eD+riched.colbox(this.name);
		if(!dge(into))return;
	 	if(document.selection){
			dr('<div id="red_'+this.name+'" class="cansel wide" contenteditable="true">'
				+this.html+eD,into)
		}else{
			dr(h+'<iframe id="red_'+this.name+'" name="red_'+this.name+'" frameborder="0"'
		 		+' class="prl wide high cb cansel" style="border:0px;"></iframe>',into)
		 	var h=this.html;if(this.pre)h='<pre>'+h+'</pre>';
			var red=this.cd();red.open();red.write('<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'
				+'<html id="red_'+this.name+'" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
				+'<style>body{margin:0px;padding:0px;font-family:Arial,Helvetica}\n'
				+(this.style||'')+'\n</style>'
				+'<body class="cansel">'
				+'<div>'+h+'</div></body></html>\n');
			red.close();
//			if(this.cd().body.spellcheck)this.cd().body.spellcheck=false;
		}
		setTimeout("if(riched.inputs['"+this.name+"'].cd()){riched.inputs['"+this.name+"'].cd().designMode='On';riched.inputs['"+this.name+"'].cd().body.spellcheck=false}",100);
	},
	// store range of current selection
	ranger:function(txt){
		if(!dge('red_'+this.name))return '';
		if(!txt)return document.selection
			?this.cd().selection.createRange().parentElement()
			:this.cw().getSelection().getRangeAt(0).commonAncestorContainer;
		var sel,rng,htm;
		if(document.selection){
			// in IE its easy to set + get the HTML of a selection
			sel=this.cd().selection;
			if(sel!==null){
				rng=sel.createRange();this.rng=rng;
				if(txt){try{rng.pasteHTML(txt);rng.collapse(true)}catch(e){}}else txt=rng.htmlText;
			}
		}else{
			// this gecko code is nasty, but it works...
			sel=this.cw().getSelection();
			if(sel!==null){
				rng=sel.getRangeAt(sel.rangeCount-1).cloneRange();
				this.rng=rng;
				if((htm=txt)){
					txt=document.createElement('span');
					txt.innerHTML=htm;
				}else{
					txt=rng.extractContents();
					htm=getInnerHTML(txt);
				}
				rng.insertNode(txt);
				//this.cd().execCommand('insertHTML', txt);

				// now remove any empty/unnecessary tags
				txt=htm.replace(/<(script|style|meta)[^>\w]?[^>]*>.+?<\/\1>/ig,'');
				txt=txt.replace(/<span>([^<]+)<\/span>/ig,'$1');
				txt=txt.replace(/<(abbr|b|big|cite|code|em|i|kbd|s|span|strike|strong|u|xmp)( [^>]+)?>\s*<\/\1>/ig,'');
				txt=txt.replace(/<\/(abbr|b|big|cite|code|em|i|kbd|s|strike|strong|u|xmp)><\1>/ig,'');
			}
		}
		return sel==null?'':txt;
	},
	cw:function(){
		if(nav.OP)return dge('red_'+this.name).contentDocument.window;
		return document.selection?dge('red_'+this.name):dge('red_'+this.name).contentWindow},
	cd:function(){if(nav.OP)return dge('red_'+this.name).contentDocument;return this.cw().document},
	com:function(c, option){
		if(c!='Select')try{this.cd().execCommand(c,false,option)}catch(e){}
		return this.ranger();
	}
};

W.getInnerHTML=function(o){
	var h='';if(o.hasChildNodes()){
		for(var i=0;i<o.childNodes.length;i++){
		   h+=getOuterHTML(o.childNodes[i])
		}
	}return h
};

W.getOuterHTML=function(node){
	var str='',tmp='';
	switch(node.nodeType){
		case 1: // ELEMENT_NODE
			var n=node.attributes;str+='<'+node.nodeName;
			for(var i=0;i<n.length;i++)
				if(n.item(i).nodeValue!=null&&!Bio('moz',n.item(i).nodeName))
					str+=' '+n.item(i).nodeName+'="'+n.item(i).nodeValue+'"';
			if(!node.childNodes.length&&/^(IMG|HR|BR|INPUT)$/.test(node.nodeName))str+='>';
			else str+='>'+getInnerHTML(node)+'</'+node.nodeName+'>';
			break;
		case 3:	// TEXT_NODE
			str+=node.nodeValue;break;
		case 4: // CDATA_SECTION_NODE
			str+='<![CDATA['+node.nodeValue+']]>';break;
		case 5: // ENTITY_REFERENCE_NODE
			str+='&'+node.nodeName+';';break;
		case 8: // COMMENT_NODE
			str+='<!-- '+node.nodeValue+' -->';	break;
	}
	return str;
};

