Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/* disable */
ls_enable = false;

/*nav popups*/
popupStructure = 'lite';
popupMaxWidth = 600;

$.extend(mw.util, {
   pageNameFromUrl: function (str) {
      var re = new RegExp(mw.config.get('wgArticlePath').replace('$1', '(.*?)$'));
      
      if (result = re.exec(str))
         return decodeURIComponent(result[1].replace(/_/g, ' '));
      
      return false;
   },
   
   addPortal: function (id, title, after, links) {
      var after = after || 'div.portal:last', links = links || {};

      if ($('#' + id).length == 0) {  // reuse if exists
         var div = $('<div/>').addClass('portal expanded').attr({id: id}), 
             h5 = $('<h5/>').text(title), body = $('<div/>').addClass('body').show(), ul = $('<ul/>');
      
         $(after).after(div.append(h5).append(body.append(ul)));
      }
      
      $.each(links, function (lid, data) {
         var link = mw.util.addPortletLink(id, data.href || '#', data.text, lid, data.title, false, data.next || false);
         
         if (data.click)
            $(link).click(data.click || $.noop);
      });
   }
});

$.fn.extend({
   scrollTo: function (off) {
      var pos = this.offset();
      window.scrollTo(0, pos.top + 2 + (off || 0));
   }
});
 
function mwCustomDefaultSort () {
 
   if (mw.config.get('wgTitle').match(/^(Flag of|Coat of arms of) ([\w\s]+)/i))
      return RegExp.$2
 
   if (mw.config.get('wgTitle').match(/^([\w]+)\s(van|von)\s([\w\s]+)/i))
      return RegExp.$3 + ", " + RegExp.$1 + " " + RegExp.$2
 
   return "Sort Key";
}

function zeroPad (i) {
   return i < 10 ? '0' + i : i;
}

function resetGalleryImages () {
   $('li.gallerybox').show();
}

function showOnlySVGs () {
   $('li.gallerybox a.image:not([href$=".svg"])').each(function () {
      $(this).parents('.gallerybox').hide();
   });
}

function showNoSVGs () {
   $('li.gallerybox a.image[href$=".svg"]').each(function () {
      $(this).parents('.gallerybox').hide();
   });
}

function showOnlyShouldBeSVG () {
   showNoSVGs();
   
   var api = new mw.Api;
   
   $('li.gallerybox:not(:hidden) a.image').each(function () {
      var li = $(this).parents('.gallerybox'), title = mw.util.pageNameFromUrl($(this).attr('href'));

      api.get({
         action: 'query',
         format: 'json',
         prop: 'templates',
         tltemplates: 'Template:Convert to SVG',
         titles: title
      }, {
         ok: function (data) {
            var pages = data.query.pages || {};
            $.each(pages, function (k, v) {
               if (!(v.templates && v.templates.length === 1)) {
                  li.hide();
               }
            });
         }
      });
   });
}

function showOnlyVVA () {
   showNoSVGs();
   
   var api = new mw.Api;
   
   $('li.gallerybox:not(:hidden) a.image').each(function () {
      var li = $(this).parents('.gallerybox'), title = mw.util.pageNameFromUrl($(this).attr('href'));

      api.get({
         action: 'query',
         format: 'json',
         prop: 'templates',
         tltemplates: 'Template:Vector version available',
         titles: title
      }, {
         ok: function (data) {
            var pages = data.query.pages || {};
            $.each(pages, function (k, v) {
               if (!(v.templates && v.templates.length === 1)) {
                  li.hide();
               }
            });
         }
      });
   });
}

$(document).ready(function () {

   // plain upload
   $('#n-uploadbtn a').attr('href', wgScript + '?title=Special:Upload&uploadformstyle=basic');

   // custom upload buttons
   if ($('#wpUploadDescription').length == 1) 
   {
      var d = new Date();
      var now = (d.getFullYear() + '-' + zeroPad(d.getMonth() + 1) + '-' + zeroPad(d.getDate()));
   
      $('#specialchars').empty()
      .append($('<button/>').click(function (e) {
            $('#wpUploadDescription').val("=={{int:filedesc}}==\n\n{{Information\n|Description={{en|1=}}\n|Source=\n|Date=" + now + "\n|Author=[[User:Ch1902|Ch1902]]\n|Permission=\n|other_versions=\n}}\n\n=={{int:license-header}}==\n\n");
            e.preventDefault();
            return false;
         }).html('<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Information_icon.svg/16px-Information_icon.svg.png">')
      )
      .append($('<button/>').click(function (e) {
            insertTags('\'\'', '\'\'', 'Italic')
            e.preventDefault();
            return false;
         }).html('<em>i</em>')
      )
      .append($('<button/>').click(function (e) {
            insertTags('\'\'\'', '\'\'\'', 'Bold')
            e.preventDefault();
            return false;
         }).html('<strong>b</strong>')
      )
      .append($('<button/>').click(function (e) {
            insertTags('[[:File:', ']]', '')
            e.preventDefault();
            return false;
         }).text('[[:File:]]')
      )
      .append($('<button/>').click(function (e) {
            insertTags('[[', ']]', '')
            e.preventDefault();
            return false;
         }).text('[[ ]]')
      )
      .append($('<button/>').click(function (e) {
            insertTags('[[:en:', ']]', '')
            e.preventDefault();
            return false;
         }).text('[[:en:]]')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{', '}}', '')
            e.preventDefault();
            return false;
         }).text('{{ }}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{en|1=', '}}', '')
            e.preventDefault();
            return false;
         }).text('{{en|1=}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('[[Category:', ']]', '')
            e.preventDefault();
            return false;
         }).text('[[Category:]]')
      )
      .append($('<button/>').click(function (e) {
            insertTags("<gallery>\n", "\n</gallery>", "File:|")
            e.preventDefault();
            return false;
         }).text('<gallery>')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{Self|GFDL|Cc-by-sa-3.0|migration=redundant}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{Self|Self|GFDL|Cc-by-sa-3.0}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{Self|GFDL|Cc-by-3.0|migration=redundant}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{Self|Self|GFDL|Cc-by-3.0}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{Self|Cc-zero}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{Self|Cc-zero}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{PD-self}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{PD-self}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{PD-shape}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{PD-shape}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{PD-ineligible}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{PD-ineligible}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{PD-textlogo}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{PD-textlogo}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{trademark}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{trademark}}')
      )
      .append($('<button/>').click(function (e) {
            insertTags('{{insignia}}', '', '')
            e.preventDefault();
            return false;
         }).text('{{insignia}}')
      )
      
      $('#specialchars').insertBefore('#wpUploadDescription');
      $('#wpUploadDescription').css({height: 200});
      
      // not sure where preview went, kludge
      $('#mw-upload-form .mw-htmlform-submit').after($('<button/>').attr('type', 'button').text('Preview').click(function (e) { 
         e.preventDefault();
         $.ajax({
            type: 'POST',
            url: 'api.php',
            data: {action: 'parse', format: 'json', prop: 'text', text: $('#wpUploadDescription').val()},
            dataType: 'json',
            success: function (data) {
               if ($('#mw-preview-holder').length !== 1) {
                  var fs = $('#mw-upload-form fieldset:eq(0)');
                  fs.after('<fieldset id="mw-preview-holder"><legend>Preview</legend><div id="uploadPreview"></div></fieldset>');
               }
               
               $('#uploadPreview').html(data.parse.text['*']).scrollTo(-25);
            }
         });
         return false;
      }));
   }

   // custom toolbox
   if (mw.config.get('wgNamespaceNumber') == 14 || (mw.config.get('wgNamespaceNumber') == 0 && $('ul.gallery').length > 0)) {
      var links = {
         'tb-reset': {
            title: 'Show all hidden gallery images',
            text: 'Unhide files',
            click: function (e) { e.preventDefault(); resetGalleryImages(); return false; }
         },
         'tb-svgs': {
            title: 'Show only SVG files',
            text: 'Show SVG files',
            click: function (e) { e.preventDefault(); resetGalleryImages(); showOnlySVGs(); return false; }
         },
         'tb-nosvgs': {
            title: 'Show no SVG files',
            text: 'Hide SVG files',
            click: function (e) { e.preventDefault(); resetGalleryImages(); showNoSVGs(); return false; }
         },
         'tb-sbsvg': {
            title: 'Show files tagged as ShouldBeSVG',
            text: 'ShouldBeSVG files',
            click: function (e) { e.preventDefault(); resetGalleryImages(); showOnlyShouldBeSVG(); return false; }
         },
         'tb-vva': {
            title: 'Show files tagged as VVA',
            text: 'VVA files',
            click: function (e) { e.preventDefault(); resetGalleryImages(); showOnlyVVA(); return false; }
         }
      };
      
      mw.util.addPortal('p-my', 'My Tools', '#p-tb', links);
   }
});

/* custom edit buttons */
if (mwCustomEditButtons) {
 
   var d = new Date();
   var now = (d.getFullYear() + '-' + zeroPad(d.getMonth() + 1) + '-' + zeroPad(d.getDate()));
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/en/1/12/Button_gallery.png",
      "speedTip": "Insert a picture gallery",
      "tagOpen": "\n<gallery>\n",
      "tagClose": "\n</gallery>",
      "sampleText": "File:Example.jpg|Caption1\nFile:Example.jpg|Caption2"
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/en/6/60/Button_insert_table.png",
      "speedTip": "Insert a table",
      "tagOpen": '{| class="wikitable" border="1"\n|',
      "tagClose": "\n|}",
      "sampleText": "-\n! header 1\n! header 2\n! header 3\n|-\n| row 1, cell 1\n| row 1, cell 2\n| row 1, cell 3\n|-\n| row 2, cell 1\n| row 2, cell 2\n| row 2, cell 3"
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/2/28/Button_info.png",
      "speedTip": "Info Template",
      "tagOpen": "=={{int:filedesc}}==\n\n{{Information\n|Description={{en|1=}}\n|Source=\n|Date=" + now + "\n|Author=[[User:Ch1902|Ch1902]]\n|Permission=\n|other_versions=\n}}\n\n=={{int:license-header}}==\n\n",
      "tagClose": "",
      "sampleText": ""
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/f/f7/Button_shouldbesvg.png",
      "speedTip": "Convert to SVG",
      "tagOpen": "{{Convert to SVG|",
      "tagClose": "}}",
      "sampleText": ""
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/c/c6/Button_vva.png",
      "speedTip": "Vector version available",
      "tagOpen": "{{Vector version available|",
      "tagClose": "}}",
      "sampleText": (mw.config.get('wgNamespaceNumber') == 6 ? mw.config.get('wgTitle').replace(/\.(jpg|jpeg|png|gif)$/gi, '.svg') : "Example.svg")
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/b/b4/Button_category03.png",
      "speedTip": "Category",
      "tagOpen": "[[Category:",
      "tagClose": "]]",
      "sampleText": "Category Name"
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/en/2/23/Button_code.png",
      "speedTip": "Default Sort",
      "tagOpen": "{{DEFAULTSORT:",
      "tagClose": "}}",
      "sampleText": mwCustomDefaultSort()
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/b/b6/Button_category_link.png",
      "speedTip": "Sort Key",
      "tagOpen": "|",
      "tagClose": "",
      "sampleText": mwCustomDefaultSort()
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/e/e2/Button_%C2%A7.png",
      "speedTip": "Summary header",
      "tagOpen": "== {{int:filedesc}} ==",
      "tagClose": "",
      "sampleText": ""
   });
 
   mwCustomEditButtons.push({
      "imageFile": "http://upload.wikimedia.org/wikipedia/commons/5/58/Button_black_copyright.png",
      "speedTip": "License header",
      "tagOpen": "== {{int:license-header}} ==",
      "tagClose": "",
      "sampleText": ""
   });
}