/*
*/
function _RichtextEditor(textarea, css){
    this.name=textarea.name+'_rich';
    this.textarea = textarea;
    this.css = css;
    this.type = (_Def(document.designMode))?'rich':'plain';
//    this.type = 'plain';
    this._win = null;
/*--------------------------------------------------------------------------------------*/
    this.Init = function(){
	var editor = this;

	/*creating toolbar*/
	toolbar = document.createElement('div');
	this.toolbar = toolbar;

	function createButton(imgsrc,ev){
	    a = document.createElement('a');
	    a.href = 'javascript:void(0);';
	    img = document.createElement('img');
	    img.src = '/img'+imgsrc;
	    img.border=0;
	    img.align='middle';
	    a.appendChild(img);
	    this.toolbar.appendChild(a);
	    _AddEventListener(a, 'click', function(event){ev(editor,a);});
	}

	createButton('/text_b.gif',function(e){e.formatText('bold','');});
	createButton('/text_i.gif',function(e){e.formatText('italic','');});
	createButton('/text_u.gif',function(e){e.formatText('underline','');});
	createButton('/text_ul.gif',function(e){e.formatText('insertunorderedlist','');});
	createButton('/text_ol.gif',function(e){e.formatText('insertorderedlist','');});
//	createButton('/text_jl.gif',function(e){e.formatText('justifyleft','');});
//	createButton('/text_jc.gif',function(e){e.formatText('justifycenter','');});
//	createButton('/text_jr.gif',function(e){e.formatText('justifyright','');});
//	createButton('/text_jj.gif',function(e){e.formatText('justifyfull','');});

//	createButton('/text_ahref.gif',function(e){e._insertLink();});
	
	this.textarea.parentNode.insertBefore(this.toolbar, this.textarea);
	
	if(this.type == 'rich'){
	    document.writeln('<iframe frameborder=0 name="'+this.name+'" id="'+this.name+'"'+(this.style?'style="'+this.style+'"':'')+'"></iframe>');
//	    document.writeln('<iframe frameborder=0 name="'+this.name+'" id="'+this.name+'"'+(this.style?'style="'+this.style+'"':'')+'src="/files/blank.html"></iframe>');
	    this.iframe = window.frames[this.name];
	    this._win = (this.iframe.window?this.iframe.window:(this.iframe.contentWindow?this.iframe.contentWindow:null));

//	    _Height(this.name,_Height(this.textarea));
//	    _Width(this.name,_Width(this.textarea));

//	    _Top(this.name,_OffsetTop(this.textarea));
//	    _Left(this.name,_OffsetLeft(this.textarea));



	    
	    _Hide(this.textarea);
	    _Show(this.name);

	    if(this.textarea.form){
		var f = this.textarea.form;
		if(typeof f.onsubmit == "function"){
		    var PrevOnSubmit = f.onsubmit;
		    if(typeof f.__prevOnSubmit == "undefined"){
			f.__prevOnSubmit = [];
		    }
		    f.__prevOnSubmit.push(PrevOnSubmit);
		}
		f.onsubmit = function(){
		    if(editor.type == 'rich'){
			editor.rich2plain();
		    }			
		    var a = this.__prevOnSubmit;
		    if(typeof a != "undefined"){
			for(var i in a){
			    a[i]();
			}
		    }
		}

	    }
	    
	    var html = "<html><head><title>Richtext editor</title>";
//	    if(this.css){
//		html += "<link rel=\"stylesheet\" href=\""+this.css+"\" type=\"text/css\">";
//	    }else{
		html += "<style>body {margin:0px;padding:0px;font-family: Verdana, Helvetica, sans-serif;font-size: 70%;background:white;} p{margin:0;padding:0;}</style>";
//	    }
	    html += "</head><body>"+this.textarea.value+"</body></html>";
	    
	    function _iframeinit(){
		try {
		    editor._win.document.open();
		    editor._win.document.write(html);
		    editor._win.document.close();
		    editor._win.document.designMode='on';
		    editor.currentMode = 'rich';
		}catch(e){
		    setTimeout(_iframeinit, 200);
		}
		
	    }
	    _iframeinit();
	    
	    
	}else{
	    this.currentMode = 'plain';
	}

    }

    this.resize = function (){
	if(this.type == 'rich'){
//	    _Height(this.name,_Height(this.textarea));
//	    _Width(this.name,_Width(this.textarea));

//	    _Top(this.name,_OffsetTop(this.textarea));
//	    _Left(this.name,_OffsetLeft(this.textarea));
	    _Hide(this.textarea);
	    _Show(this.name);
	}
    }

/*--------------------------------------------------------------------------------------*/
    this.plain2rich = function (){
	this._win.document.body.innerHTML = compressHTML(this.textarea.value);
    }
/*--------------------------------------------------------------------------------------*/

    this.rich2plain = function (){
	this.textarea.value = compressHTML(this._win.document.body.innerHTML);
    }
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
    this.formatText = function(cmdID, param){
	switch(this.currentMode){
	    case 'rich':
		this._win.focus();
		this.rich_formatText(cmdID,param);
		this._win.focus();
		break;
	    case 'plain':
		this.plain_formatText(cmdID,param);
		break;
	}
    }

/*--------------------------------------------------------------------------------------*/
    this.rich_formatText = function(cmdID,param){
	switch(cmdID){
	    case 'createlink':
		this._win = (this.iframe.contentWindow)? this.iframe.contentWindow:this.iframe.window;
		this._win.document.execCommand('unlink',false,null);
		if(param !=''){
		    this._win.document.execCommand('createlink',false,param);
		}
		break;
	    default:
		this._win.document.execCommand(cmdID,false,param);
	}
    }
/*--------------------------------------------------------------------------------------*/
    this.plain_formatText = function(cmdID,param){
	if (document.selection) {
	    this.textarea.focus();
	    sel = document.selection.createRange();
	    sel.text = this.plain_processcmd(sel.text,cmdID,param);
	}else if(this.textarea.selectionStart || this.textarea.selectionStart == "0"){
	    var startPos = this.textarea.selectionStart;
	    var endPos = this.textarea.selectionEnd;
//	_field.value = _field.value.substring(0, startPos) + _starttag + _field.value.substring(startPos, endPos) + _endtag + _field.value.substring(endPos, _field.value.length);
	    this.textarea.value = this.textarea.value.substring(0, startPos) + this.plain_processcmd(this.textarea.value.substring(startPos, endPos), cmdID, param) + this.textarea.value.substring(endPos, this.textarea.value.length);
	}else{
	    this.textarea.value += this.plain_processcmd('',cmdID,param);
	}
    }
    
    this.plain_processcmd = function (_text,_cmd,param){
	var res = '';
	switch(_cmd){
	    case 'bold':
		res = '<b>'+_text+'</b>';
		break;
	    case 'italic':
		res = '<i>'+_text+'</i>';
		break;
	    case 'underline':
		res = '<u>'+_text+'</u>';
		break;
	    case 'createlink':
		res = '<a href="'+param+'">'+_text+'</a>';
		break;
	    case 'insertunorderedlist':
		var re=new RegExp("\\r?\\n",'g');
		res = '<ul>\n<li>' + _text.replace(re, '</li>\n<li>') + '</li>\n</ul>';
		break;
	    case 'insertorderedlist':
		var re=new RegExp("\\r?\\n",'g');
		res = '<ol>\n<li>' + _text.replace(re, '</li>\n<li>') + '</li>\n</ol>';
		break;
	}

	return res;
    }
/*--------------------------------------------------------------------------------------*/
    this._insertLink = function(){
	this.formatText('createlink',prompt('Enter hyperlink'));
    }

}
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/










function compressHTML(str){
    newStr = str.replace(/\r?\n/g," ");
    newStr = newStr.replace(/\s{2,}/g,' ');
    //tags manipulation
    newStr = newStr.replace(/<strong>/gi,'<b>');
    newStr = newStr.replace(/<\/strong>/gi,'</b>');
    newStr = newStr.replace(/<em>/gi,'<i>');
    newStr = newStr.replace(/<\/em>/gi,'</i>');
    //delete empty tags
//    newStr = newStr.replace(/<([a-z][a-z]*)> *<\/\1>/gi,' ').
//		    replace(/<([a-z][a-z]*)> *<([a-z][^>]*)> *<\/\1>/gi,'<$2>');
//    newStr = newStr.replace(/<([a-z][a-z]*)><\1>/gi,'<$1>').
//		    replace(/<\/([a-z][a-z]*)><\/\1>/gi,'<\/$1>');

    return newStr;
}
