/*
	oh joy of DOM!

	these two wee functions make the other functions totally portable. 
	now we can have multiple forms on one page, all with javascript
	enabled buttons, all re-using the same functions. coolness.

	Here we simply grab the element that was clicked, then run UP the DOM 
	tree until we meet a <form> element (the parent form) and return 
	that form's textarea element. 

	figuring this out taught me DOM! ;o)
*/

function GetParentForm(e) {	
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target; 
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) { // Safari bug work-around
		targ = targ.parentNode;
	}
	targ = targ.parentNode;
    while (targ.tagName != "FORM") {
		targ = targ.parentNode;
	}
	return targ;
}

// as above, but returns the textarea from the parent form element
// note: passing "e" (event) around everywhere is for firefox. the bugger!
function GetParentFormTextarea(e) {
	return GetParentForm(e).getElementsByTagName('textarea')[0];
}

// text manipulation..

// insert text at current caret position..
// [will replace current selection]

function InsertText(e, text) {

	// grab the textarea element from the current form..
	textarea = GetParentFormTextarea(e);
	StoredUndo = textarea.value;

	// IE...
	if (typeof(textarea.caretPos) != "undefined") { 
		var caretPos = textarea.caretPos;
		caretPos.text = text;
		caretPos.select();
		textarea.focus();

	// Mozish...
	} else if (typeof(textarea.selectionStart) != "undefined") { 
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;
		textarea.value = begin + text + end;
		textarea.scrollTop = scrollPos;
		textarea.setSelectionRange(begin.length + text.length, begin.length + text.length );
		textarea.focus();

	// whateva..
	} else {
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}


// surround the selected text with tStart and tEnd.
function SurroundText(e, tStart, tEnd) {

	// grab the textarea element from the current form..
	textarea = GetParentFormTextarea(e);
	StoredUndo = textarea.value;

	// can a valid text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? tStart + caretPos.text + tEnd + ' ' : tStart + caretPos.text + tEnd;
		caretPos.select();
		StoredSelLength = caretPos.text.length;

	// moz wrap..
	} 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;
		StoredSelLength = selection.length;
		textarea.value = begin + tStart + selection + tEnd + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + tStart.length, newCursorPos + tStart.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + tStart.length + selection.length + tEnd.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;

	// or else slam it on the end again..
	} else {
		textarea.value += tStart + tEnd;
		textarea.focus(textarea.value.length - 1);
	}
}

// the actual buttons and dials.. 

function TAB_Link(e) {
	var linkz  = prompt("Please, type in a valid URL \n(for example: http://www.example.org/)","http://");
	SurroundText(e, "[url=" + linkz + "]", "[/url]");
}
function TAB_Email(e) {
	var email  = prompt("Please, type in a valid e-mail address \n(for example: info@example.org)","");
	SurroundText(e, "[mail=" + email + "]", "[/mail]");
}

function TAB_Bold(e)      { SurroundText(e, '[b]', '[/b]'); }
function TAB_Italic(e)    { SurroundText(e, '[i]', '[/i]'); }
function TAB_Underline(e) { SurroundText(e, '[u]', '[/u]'); }
function TAB_Striked(e)   { SurroundText(e, '[s]', '[/s]'); }
function TAB_Left(e)      { SurroundText(e, '[left]',   '[/left]');   }
function TAB_Center(e)    { SurroundText(e, '[center]', '[/center]'); }
function TAB_Right(e)     { SurroundText(e, '[right]',  '[/right]');  }
function TAB_Smile(e,smile) { InsertText(e, smile); }
