function getSelection(ta)
{
if (document.selection && document.selection.createRange) {
	var range=document.selection.createRange();
	return range.text;
} else if (typeof(ta["setSelectionRange"]) != "undefined") {
	return ta.value.substring(ta.selectionStart, ta.selectionEnd);
}
return false;
}

function replaceSelection(ta, text)
{
if (document.selection && document.selection.createRange) {
    var range=document.selection.createRange().text=text;
    ta.caretPos-=text.length;
	return true;
} else if (typeof(ta["setSelectionRange"]) != "undefined") {
	var sstart, send, scrollPos;

	start=ta.selectionStart;    
	send=ta.selectionEnd;
	scrollPos=ta.scrollTop;

	ta.value=ta.value.substring(0, start)+text+ta.value.substring(send);
	ta.setSelectionRange(start+text.length, start+text.length);
	ta.scrollTop=scrollPos;
	return true;
}
return false;
}

function sortSelection(ta, reverse)
{
var sel, subst;

sel=getSelection(ta);
if (sel==false || sel=="") {
	return;
}

subst=sel.split("\n").sort();
if (reverse)
	subst.reverse();
return replaceSelection(ta, subst.join("\n"));
}

function trimSelection(ta)
{
sel=getSelection(ta);
if (sel==false || sel=="") {
	return;
}
return replaceSelection(ta, jQuery.trim(sel));
}

function doTextSubst(prefix, suffix, stext, rows) {
var subst;
var a=stext.split("\n");

if (typeof(prefix)=='undefined' || prefix==false)
	prefix='';

if (typeof(suffix)=='undefined' || suffix==false)
	suffix='';

if (a.length==1) {
	return prefix+stext+suffix;
}
for (l in a)
	a[l]=(a[l]!='') ? prefix+a[l]+suffix : '';
return a.join("\n");
}

function doSelectionPrompt(ta, prefix, suffix, promptmsg, pre) {
var sel, subst;

ta.focus();
sel=getSelection(ta);
if ((sel=="" || sel==false) && promptmsg!=null) {
	sel=prompt(promptmsg, pre ? pre : "");
	if (!sel) return;
}
return replaceSelection(ta, doTextSubst(prefix, suffix, sel ? sel : " "));
}

function doSelection(ta, prefix, suffix, rows) {
var sel, subst;

ta.focus();
sel=getSelection(ta);
if ((sel==false || sel=="") && typeof(suffix)!='undefined') {
	return;
}
return replaceSelection(ta, doTextSubst(prefix, suffix, sel, rows));
}

function linkSelection(ta, ltype, msg, pre)
{
ta.focus();
sel=getSelection(ta);

link=prompt(msg, pre ? pre : '');
if (!link)
	return;
link=jQuery.trim(link);
switch (ltype) {
	case 1:
		link=link.replace(' ','_');
		subst=doTextSubst('[['+link+' ',']]', sel ? sel : 'Link text goes here');
	break;
	case 0:
	default:
		subst=doTextSubst('['+link+' ',']', sel ? sel : 'Link text goes here');
	break;
}

return replaceSelection(ta, subst);
}

function FormatingToolbar(i,ta)
{
var t=new toolbar('ftoolbar_'+i);
if (t==false)
	return false;
t.add("heading", "Heading level 1","stock_insert-header", function() {doSelection(ta, "=", "=", true);});
t.add("heading", "Heading level 2","stock_insert-header2", function() {doSelection(ta, "==", "==", true);});
t.add("heading", "Heading level 3","stock_insert-header3", function() {doSelection(ta, "===", "===", true);});
t.separator();
t.add("strong", "Mark as Bold text","stock_text_bold", function() {doSelection(ta, "'''", "'''", false);});
t.add("em", "Mark as Italic text", "stock_text_italic", function() {doSelection(ta, "''", "''", false);});
t.add("u", "Underline", "stock_text_underlined", function() {doSelection(ta, "___", "___", false);});
t.separator();
t.add("list_bullet", "Create Bullet List", "stock_list_bullet", function() {doSelection(ta, "* ");});
t.add("list_bullet", "Create Numbered List", "stock_list_enum", function() {doSelection(ta, "# ");});
t.separator();
t.add("sort", "Sort ascending selected rows", "stock_sort-ascending", function() {sortSelection(ta, false);});
t.add("sort", "Sort descending selected rows", "stock_sort-descending", function() {sortSelection(ta, true);});
t.separator();
t.add("link", "Add URL", "stock_insert-url", function() {linkSelection(ta, 0, "URL:", "http://");});
t.add("ilink", "Internal link", "stock_hyperlink", function() {linkSelection(ta, 1, "Pagename:", "");});
t.separator();
t.add("template", "Add template tag", "stock_new-template", function() {doSelectionPrompt(ta, "\n{{template|", "}}\n", "Template:");});
t.add("image", "Add Image", "stock_insert_image", function() {doSelectionPrompt(ta, "[[image:", "]]", "Image:");});
t.add("attach", "Add article attachment tag", "stock_attach", function() {doSelection(ta, "\n{{attachments}}\n");});

return t;
}

function addTextareaEditingToolbars(tc) {
$('.'+tc).each(function(i,e) { t=FormatingToolbar(i,e); t.place(e); });
}

