/* Javascript March 1, 2010*/
////////////////////////////////////////////////////////////////////////////////////////////////////
// TextEdit Version 0.40 
// Copyright (c) 2006-2010 Howard Brown/Suvadia.com
////////////////////////////////////////////////////////////////////////////////////////////////////

var BHTextEditFont=[
	[[90,1],function(v){return '<span style="font-family:'+v+'">'+v+'</span>';}],		
	["Arial",''],
	["Sans Serif",''],
	["Tahoma",''],
	["Verdana",''],
	["Courier New",''],
	["Georgia",''],
	["Times New Roman",''],
	["Impact",''],
	["Comic Sans MS",'']];

var BHTextEditSize=[
	[[16,0],function(v){var n=parseInt(v)+1;return v+'&nbsp;<img src="/newlook/img/clear.gif" style="overflow:hidden; height:'+n+'px; width:'+(n)+'px; background-color:#000000;">'}],			 
    ['1',''],
	['2',''],
	['3',''],
	['4',''],
	['5',''],
	['6',''],
	['7','']];

var  BHTextEditColor=[
	[[50,2],function(v){return '<img src="/newlook/img/clear.gif" width="11" height="12" align="absmiddle" style="background-color:'+v+'">&nbsp;'+v}],			 
	['Black',''],
	['Maroon',''],
	['Green',''],
	['Olive',''],
	['Navy',''],
	['Purple',''],
	['Teal',''],
	['Gray',''],
	['Silver',''],
	['Red',''],
	['Lime',''],
	['Yellow',''],
	['Blue',''],
	['Fuchsia',''],
	['Aqua',''],
	['White','']];

BHTextEditButton=[
	['Bold'],
	['Italic'],
	['Underline'],
	['StrikeThrough']];

function BHTextEdit(ocf)
{
	var s,e,h,w,i,a,tbr,div,tb,td,iframe,wrap,f,sk;
	bhUI.createObject('TextEdit',this);
	var obj=typeof(ocf.id)=='string'?bhUI.element(ocf.id):$(ocf.id);
	this.textarea=obj;
	obj.form.onsubmit=new Function(this.Tx+'.event_submit()');
	obj.form.onreset=new Function(this.Tx+'.event_reset()');
	if(typeof(document.designMode)==__UF)return;
	w=obj.offsetWidth;h=obj.offsetHeight;
	wrap=bhUI.createElement();
	wrap.className='TextEdit';
	obj.style.display='none';
	obj.parentNode.insertBefore(wrap,obj);
	div=wrap.addElement();
	var tbr=new BHToolBar({id:div});
	tbr.TextEdit=this;
	
	this.wrap = wrap;
	
	tbr.itemClick=function(tool)
	{
		
		this.TextEdit.setFormat(4+tool,0);
	}
	
	f='/newlook/textedit/skins/default/img/';
	for(i=0;i<BHTextEditButton.length;i++){
		tbr.addButton({flags:0x02,icon:f+BHTextEditButton[i]+'.gif'});}
		
	var addmenu=function(a,tbr)
	{
		var e=tbr.addButton({});
	 	var n=new BHInput({id:e,width:a[0][0][0],
			onclick:function(i){
				this.selectItem(i);this.hide();
				this.TextEdit.setFormat(this.tag[0][0][1],this.tag[1+i][0]);
				return false;}
		});
		var i,fmt=a[0][1];
	 	for(i=1;i<a.length;i++)n.addItem(fmt(a[i][0]));
		n.selectItem(0);n.tag=a;n.TextEdit=tbr.TextEdit;
	};
	addmenu(BHTextEditFont,tbr);
	addmenu(BHTextEditColor,tbr);
	addmenu(BHTextEditSize,tbr);
	tbr.addButton({width:160},{flags:0x02,caption:'HTML',onclick:new Function(this.Tx+".viewHTML();")});
	
	wrap.addElement().innerHTML='<iframe id="_new_iframe_" scrolling="auto" frameborder="0"></iframe>';
	iframe=bhUI.element('_new_iframe_');iframe.id='';
	this.iframe=iframe;
	iframe.allowTransparency=true;
	this.textarea.style.width = iframe.style.width=w+'px';
	this.textarea.style.marginTop=iframe.style.marginTop='2px';
	this.textarea.style.height=iframe.style.height=(h-tbr.wrap.offsetHeight)+'px';
	this.win=iframe.contentWindow;
	this.win.document.designMode='on';
	this.doc=this.win.document;
	this.doc.open('text/html', 'replace');
    this.doc.write('<body>'+(obj.value?obj.value:(bhUI.Browser.IE?'':'<br>'))+'</body>');
	this.doc.close();
 }

BHTextEdit.prototype.viewHTML=function()
{
	if(this.textarea.style.display == ''){
		this.doc.body.innerHTML=this.textarea.value;
		this.textarea.style.display='none';
		this.iframe.style.display='';	
	}else{
		this.textarea.value=this.doc.body.innerHTML;
		this.textarea.style.display='';
		this.iframe.style.display='none';	
		
	}
}

BHTextEdit.prototype.formatId=['fontsize','fontname','foreColor','backcolor',
				   'Bold','Italic',
				   'Underline','strikethrough',
				   'Subscript','Superscript',
				   'Justifyleft','Justifyright','Justifycenter',
				   'InsertUnorderedList','InsertOrderedList',
				   'Indent','Outdent',
				   'InsertImage','InsertTable','CreateLink'];


BHTextEdit.prototype.event_submit=function()
{
	if(typeof(this.doc)!=__UF)
  	{
	  	this.textarea.value=this.doc.body.innerHTML;
   	} 
}

BHTextEdit.prototype.event_reset=function()
{
	if(typeof(this.doc)!=__UF)
  	{
	  	this.doc.body.innerHTML=bhUI.Browser.IE?'':'<br>';
		this.textarea.value='';
   	} 
}

BHTextEdit.prototype.setFormat=function(index,value)
{
    this.win.focus();
	this.doc.execCommand(this.formatId[index],false,value);
	this.win.focus();
}