function htmlEditorSurroundText(text1, text2, textarea)
{
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	} else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;
		textarea.value = begin + text1 + selection + text2 + end;
		if (textarea.setSelectionRange) {
			if (selection.length == 0) textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}
function htmlEditorGetSelectedText( textarea ) {
	var selection = "";
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		selection = textarea.caretPos.text;
	} else if (typeof(textarea.selectionStart) != "undefined") {
		selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
	}
	return selection;

}
function htmlEditorPreview(text) {
	text = text.replace(/ *$/,"");

	text = text.replace(/\[hr\]/g,"<hr />");
	text = text.replace(/\[\/hr\]/g,"");
	text = text.replace(/\[(b|i|u|center|h1|h2)\]/g,"<$1>");
	text = text.replace(/\[\/(b|i|u|center|h1|h2)\]/g,"</$1>");

	text = text.replace(/\[email\](.+?)\[\/email\]/g,"<a href=\"mailto:$1\">$1</a>");
	text = text.replace(/\[url\](http:\/\/)*(\S+?)\[\/url\]/g,"<a href=\"http://$2\">$2</a>");
	text = text.replace(/\[url href="(http:\/\/)?(\S+?)"\](.+?)\[\/url\]/g,"<a href=\"http://$2\">$3</a>");

	text = text.replace(/\n{3,}/g,"\n\n");
	text = text.replace(/\n\n/g,"</p><p>");
	text = text.replace(/\n/g,"<br />");
	text = "<p>"+text+"</p>";
	text = text.replace(/<p><\/p>/g,"");
	//alert(text);
	return text;
}
/**
 *	This function creates all the buttons and their actions for toolbar.
 *	It assumes, that textarea field is sibling node of toolbar and preview divs.
 *	i.e.:
 *
 * 			<div class="htmlEditor">
 *				<div id="preview"></div>
 *				<div id="toolbar"></div>
 *				<textarea id="text"></textarea>
 *			</div>
 */
function htmlEditorSetup( text ) {
	var createButton = function(formant) {
		var link = document.createElement( 'a' );
		var img = document.createElement( 'img' ); 
		img.src = 'documents/htmlEditor/'+formant+'.gif';
		link.appendChild(img);
		img.onmouseover = function() {
			this.style.backgroundImage = 'url(documents/htmlEditor/hoverbg.gif)';
		};
		img.onmouseout = function() {
			this.style.backgroundImage = 'url(documents/htmlEditor/bg.gif)';
		};
		img.onmouseout();
		link.href = 'javascript:void(0);';
		return link;
	};
	var toolbar = getChildrenWithId( text.parentNode, "toolbar" )[0];
	var preview = getChildrenWithId( text.parentNode, "preview" )[0];
	text.onselect = text.onchange = text.onclick = text.onkeyup = function() {
		if (typeof(text.createTextRange) != 'undefined')
					text.caretPos = document.selection.createRange().duplicate();
	}
	var formants = [ 'b','i','u','h1','h2','center','hr','url','email' ];
	for( var i = 0 ; i < formants.length; i++ ) {
		var link = createButton( formants[i] );
		link.formant = formants[i];
		link.onclick = function() {
			if( this.formant == "url" ) htmlEditorSurroundText("["+this.formant+" href=\""+htmlEditorGetSelectedText(text)+"\"]","[/"+this.formant+"]",text);
			else htmlEditorSurroundText("["+this.formant+"]","[/"+this.formant+"]",text);
		};
		toolbar.appendChild(link);
	}
	var img = document.createElement( 'img' ); 
	img.src = 'documents/htmlEditor/divider.gif';
	toolbar.appendChild(img);


	var previewButton = createButton( 'preview' );
	previewButton.onclick = function() {
		if( ! preview ) {
			preview = document.createElement("div");
			preview.id = "preview";
		}
		if( ! preview.parentNode ) toolbar.insertBefore(preview,toolbar.firstChild);
		var oldPreviewString = preview.innerHTML;
		preview.innerHTML = htmlEditorPreview(text.value);
		var newPreviewString = preview.innerHTML;
		if( oldPreviewString == newPreviewString ) preview.style.display = (preview.style.display == 'none' ) ? '' : 'none';
		else {
			preview.style.display = '';
		}
	};
	toolbar.appendChild( previewButton );
}
