//<nowiki>
// begin namespace: foButtons
(function( _public, $, undefined ) {
 
_public.loadButtons = function ()
{

 if (mw.config.get("wgAction") != "edit" &&
     mw.config.get("wgAction") != "submit") {
   return;
 }

// mw.loader.using( ['mediawiki.toolbar'], function () {   // diese Zeile für normale toolbar einsetzen
 mw.loader.using( ['ext.wikiEditor'], function () {   // diese Zeile für erweiterte toolbar einsetzen

 // sorgt dafür, dass eigene Buttons auch mit der erweiterten Bearbeiten-Werkzeugleiste funktionieren
 var toolbox =  document.getElementById("wikiEditor-ui-toolbar") || document.getElementById("toolbar");

  if (!toolbox) {
   setTimeout(foButtons.loadButtons, 300); // wir sind zu früh, in 300 ms noch mal probieren
  } else {
 
   var buttons = newButton("Nebenformen","foButtons.nf()","Nf",'');
   buttons += ' • '+newButton("__TOC__","foButtons.toc()","TOC",'');
   buttons += ' • '+newButton("CH&LI","foButtons.chli()","CH",'');
   buttons += ' • '+newButton("Worttrennung","foButtons.wt()","Wt",'');
   buttons += ' • '+newButton("Aussprache","foButtons.aussprache()","Aussprache",'');
   buttons += ' • '+newButton("Prät.Part.","foButtons.pp()","PP",'');
   buttons += ' • '+newButton("br","foButtons.br()","br",'');
   buttons += ' • '+newButton("Dativ Sg","foButtons.datsg()","DatSg",'');
   buttons += ' • '+newButton("Quellen","foButtons.qu()","Qu",'');
   buttons += ' • '+newButton("Ähnlichkeiten","foButtons.aehnl()","Ähnl",'');
   buttons += ' • '+newButton("Anagramme","foButtons.anagr()","Anagr",'');
   buttons += ' • '+newButton("neues Substantiv","checkpage.neuesSubstantiv()","neuesSubst",'');
   buttons += ' • '+newButton("Abschnitte sortieren","foButtons.sortA()","sortA",'');
   buttons += ' • '+newButton("Ü-Tab sortieren","foButtons.sortT()","sortT",'');
   buttons += ' • '+newButton("Unterschrift","foButtons.sign()","sign",'');
   buttons += ' • '+newButton("Hallo","foButtons.hallo()","hallo",'');
   buttons += ' • '+newButton("Änderungen prüfen und speichern [alt-shift-ä]", "checkpage.checkSave()","check & save",'ä');
   buttons += ' • '+newButton("Verbtabelle ausfüllen", "checkpage.fillDeVerbReg()","Verbtab",'');
   buttons += ' • '+newButton("Verbtabelle nach rechts", "checkpage.fillDeVerbReg(+1)","VT+",'');
   buttons += ' • '+newButton("Verbtabelle nach links", "checkpage.fillDeVerbReg(-1)","VT-",'');
   buttons += ' • '+newButton("save localStorage", "foButtons.save()","save ST",'');
   buttons += ' • '+newButton("load localStorage", "foButtons.load()","load ST",'');
   buttons += ' • '+newButton("* [[]]", "foButtons.stlink()","* [[]]",'');
   buttons += ' • '+newButton("fix Ü-Tabelle", "foButtons.fixTab()","fix Tab",'');

   var div = document.createElement("div");
   div.setAttribute("style", "font-size:11px;font-family:sans-serif");
   div.innerHTML += buttons;
 
   toolbox.parentNode.insertBefore(div,toolbox.nextSibling);
   $(toolbox.parentNode).show();
  
  }
 });
};

// obj ist das element in das eingefügt wird. Z.B.: "#wpTextbox1"
function insertTagNeu(obj,pre,post,peri) {
  $( obj ).textSelection(
    'encapsulateSelection', {
      pre: pre,
      peri: peri,
      post: post
    }
  );
}

function insertTags(pre,post,peri) {
  insertTagNeu("#wpTextbox1",pre,post,peri);
}


function newButton(t,f,l,a){
 if (a.length>0){
  a='accesskey="'+a+'" ';
 }
 return '<a title="'+t+'" '+a+'href="#" onclick="'+f+';return false;">'+l+'</a>';
}


_public.save = function (){
 localStorage.setItem('keyy', 'value');
}

_public.load = function (){
 alert("" + localStorage.getItem('keyy'));
 localStorage.removeItem('keyy');
}


_public.aussprache = function (){
 insertTags('\n{{Aussprache}}\n:{{IPA}} {{Lautschrift|}}\n:{{Hörbeispiele}} {{Audio|}}\n');
}

_public.toc = function (){
 insertTags('__TOC__\n');
}

_public.chli = function (){
 insertTags('\n{{Alternative Schreibweisen}}\n:{{CH&LI}} [['+mw.config.get("wgPageName").replace(/ß/g,"ss")+']]\n');
}

_public.br = function (){
 insertTags('<br />');
}

_public.datsg = function (){
 insertTags("''Dativ Singular:'' ");
}

_public.nf = function (){
 insertTags('\n{{Nebenformen}}\n:[[]]');
}
 
_public.pp = function (){
 insertTags(', {{Prät.}} , {{Part.}} ');
}
 
_public.qu = function (){
 insertTags('\n{{Quellen}}\n');
}

_public.anagr = function (){
 insertTags('\n{{Ähnlichkeiten 1|Anagramme=[[]]}}\n');
 document.getElementById("wpSummary").value = "+Anagramm"; 
}

_public.aehnl = function (){
 insertTags('\n{{Ähnlichkeiten 1|[[]]}}\n');
 document.getElementById("wpSummary").value = "+Ähnlichkeiten"; 
}

_public.sign = function (){
 insertTags(' -- ~~~~');
}
 
_public.wt = function (){
 insertTags('\n{{Worttrennung}}\n:\n');
}

_public.stlink = function (){
 insertTags('* [[',']]');
}


// Ü-Tabelle anpassen einfach
_public.fixTab = function ()
 {
   var tb = document.getElementById("wpTextbox1");
   var txt = tb.value;
   var txta = txt.split("\n{{");
   for (var i = 0; i < txta.length; i++) {
     if (txta[i].startsWith("Ü-Tabelle")) {
       txta[i] = txta[i].replace("Ü-Tabelle|Ü","Ü-Tabelle|1|G=|Ü");
       txta[i] = txta[i].replace("Ü-Tabelle\n","Ü-Tabelle|1|G=\n");
       txta[i] = txta[i].replace("}}*{{","}}\n*{{");
       txta[i] = txta[i].replace("\n* {{","\n*{{");
       txta[i] = txta[i].replace(/\: \[1\] \{\{/g,": {{");
       txta[i] = txta[i].replace(/\: \[2\] \{\{/g,": {{");
       txta[i] = txta[i].replace(/\: \[3\] \{\{/g,": {{");
       txta[i] = txta[i].replace(/\: \[4\] \{\{/g,": {{");
       txta[i] = txta[i].replace(/\: \[5\] \{\{/g,": {{");
       txta[i] = txta[i].replace("\n*{{ems}}: {{Ü|ems|}}","");
     }
   }
   tb.value = txta.join("\n{{");
 }


_public.hallo = function (){
 var a = new Audio('//upload.wikimedia.org/wikipedia/commons//0/0d/CPIDL_German_-_Hallo.ogg');
 var b = new Audio('//upload.wikimedia.org/wikipedia/commons/f/fa/De-Leute.ogg');
 a.onended = hallo2;
 a.play();
 b.play();
}

var hallo2 = function (){
 var a = new Audio('//upload.wikimedia.org/wikipedia/commons//0/0d/CPIDL_German_-_Hallo.ogg');
 a.play();
}

_public.sortA = function (){
 document.editform.wpSummary.value="Abschnitte sortiert";
 checkpage.sortChapters();
}

_public.sortT = function (){
 document.editform.wpSummary.value="Übersetzungen sortiert";
 checkpage.sortTranslations();
}


// end namespace: foButtons
}( window.foButtons = window.foButtons || {}, jQuery ));

// see https://github.com/jquery/jquery-migrate/blob/master/warnings.md#jqmigrate-jquerywindowonload-called-after-load-event-occurred

if (document.readyState == "complete") {
 foButtons.loadButtons();
} else {
 $(window).on("load", foButtons.loadButtons );
}

//</nowiki>