
function addWikiEditorBar(textarea) {
  if ((typeof(document["selection"]) == "undefined")
   && (typeof(textarea["setSelectionRange"]) == "undefined")) {
    return;
  }
  
  var toolbar = document.createElement("div");
  toolbar.className = "wikitoolbar";

  function addButton(id, title, fn) {
    var a = document.createElement("a");
    a.href = "#";
    a.id = id;
    a.title = title;
    a.onclick = function() { try { fn() } catch (e) { } return false };
    a.tabIndex = 400;
    toolbar.appendChild(a);
  }

  function encloseSelection(prefix, suffix) {
    textarea.focus();
    var start, end, sel, scrollPos, subst;
    if (typeof(document["selection"]) != "undefined") 
	{
      sel = document.selection.createRange().text;
    } 
	else if (typeof(textarea["setSelectionRange"]) != "undefined") 
	{
      start = textarea.selectionStart;
      end = textarea.selectionEnd;
      scrollPos = textarea.scrollTop;
      sel = textarea.value.substring(start, end);
    }
    if (sel.match(/ $/)) 
	{ // exclude ending space char, if any
      sel = sel.substring(0, sel.length - 1);
      suffix = suffix + " ";
    }
    subst = prefix + sel + suffix;
    if (typeof(document["selection"]) != "undefined") 
	{
      var range = document.selection.createRange().text = subst;
      textarea.caretPos -= suffix.length;
    } 
	else if (typeof(textarea["setSelectionRange"]) != "undefined") 
	{
      textarea.value = textarea.value.substring(0, start) + subst +
                       textarea.value.substring(end);
      if (sel) 
	  {
        textarea.setSelectionRange(start + subst.length, start + subst.length);
      } else 
	  {
        textarea.setSelectionRange(start + prefix.length, start + prefix.length);
      }
      textarea.scrollTop = scrollPos;
    }
  }

  addButton("strong", "fett: '''Beispiel'''", function() { encloseSelection("'''", "'''"); });
  addButton("em", "kursiv: ''Beispiel''", function() { encloseSelection("''", "''"); });
  addButton("ul", "Liste: *", function() { encloseSelection("*", "\n*\n"); });
  addButton("ol", "Aufzählung: #", function() { encloseSelection("#", "\n#\n"); });
  addButton("br", "Zeilenumbruch: <br>", function() { encloseSelection("<br>", ""); });
  addButton("heading1", "Überschrift 1: == Beispiel ==", function() { encloseSelection("== ", " ==\n", "Überschrift"); });
  addButton("heading2", "Überschrift 2: === Beispiel ===", function() { encloseSelection("=== ", " ===\n", "Überschrift"); });
  addButton("heading3", "Überschrift 3: ==== Beispiel ====", function() { encloseSelection("==== ", " ====\n"); });
  addButton("heading4", "Überschrift 4: ===== Beispiel =====", function() { encloseSelection("===== ", " =====\n"); });
  addButton("heading5", "Überschrift 5: ====== Beispiel ======", function() { encloseSelection("====== ", " ======\n"); });
  addButton("link", "Link: [[DocID | Linktext]]", function() { encloseSelection("[[ | ", " ]]"); });
  addButton("extlink", "externer Link: [[http://www.beispiel.com/ | Linktext]]", function() { encloseSelection("[[http://www. | ", " |_blank]]"); });
  addButton("table", "Tabelle: {| |Zelle1 |Zelle2 |}", function() { encloseSelection("{|\n|", "\n|Zelle2\n|}\n"); });
  addButton("tablerow", "Tabellenzeile: |- |Spalte1 |Spalte2 ", function() { encloseSelection("|-\n|", "\n|Spalte2\n"); });
  addButton("illu", "Illustraion: [[ILLU:GALLERY_1 | left | box | Bildbeschreibung]]", function() { encloseSelection("[[ILLU:GALLERY_", " | left | box | Bildbeschreibung ]]"); });
  addButton("img", "Image: [[IMG:Url | right | framed | Bildbeschreibung]]", function() { encloseSelection("[[IMG:", " | right | framed | Bildbeschreibung ]]"); });
  addButton("hr", "Trennlinie: ----", function() { encloseSelection("----\n", ""); });
  //addButton("aright", "rechtsbündig: ======= Beispiel =======", function() { encloseSelection("\n======= ", " =======\n"); });
  //addButton("acenter", "zentriert: ======== Beispiel ========", function() { encloseSelection("\n======== ", " ========\n"); });

  textarea.parentNode.insertBefore(toolbar, textarea);
  var br = document.createElement("br");
  br.style.clear = "left";
  textarea.parentNode.insertBefore(br, textarea);
}

