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.
var today  = new Date ();

var months = new Array ("January", "February", "March", "April", "May", "June",
                        "July", "August", "September", "October", "November", "December");

function formatDate (d, with_timezone)
{
  if (!with_timezone) {
    return months[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
  } else {
    var tzRaw = d.getTimezoneOffset();
    var tzHour = Math.floor( Math.abs(tzRaw) / 60);
    var tzMin = Math.abs(tzRaw) % 60;
    var tzString = ((tzRaw >= 0) ? "-" : "+")
                 + ((tzHour < 10) ? "0" : "")
                 + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
    return formatDate (d, false) + ' (' + tzString + ')';
  }
}

if (wgUserName == 'Lupo') {
  // Only for me.
  var my_tests = getParamValue ('withAnyJS');
  if (my_tests) importScript (my_tests);
}

/*** Enable third-party functionality in external scripts */
importScript ('MediaWiki:Utilities.js');

if (typeof (GalleryDetails) != 'undefined') {
  importScript ('MediaWiki:GalleryDetails-Tooltips.js');
  // An enhancement of the GalleryDetails extension, which I have enabled as a gadget
  if (wgUserName == 'Lupo') {
    importScript ('User:Lupo/gd.js'); // Additional links (experimental)
  }
}

importScript ('User:Lupo/da.js');              // Deletion request archiver
importScript ('MediaWiki:ThumbnailPurger.js'); // Purge thumbnails (obsolete?)
importScript ('MediaWiki:Gadget-DelReqHandler.js');
// Fast closing of deletion requests. Could also enable the gadget instead...
importScript ('User:Lupo/ImgDelLinks.js');     // Quick deletion links on image pages

if (wgUserName == 'Lupo') {
  // TEST: Checkusage replacement
  importScript ('User:Lupo/testfile.js');
}

if (typeof (uTemplate) == 'undefined') { // Gadget-UserMessages is disabled
  // Replacement for Gadget-UserMessages.js. Use this only if you have the gadget disabled!
  importScript ('User:Lupo/u.js');
  var template_autosave = false; // Don't save automatically... MAY BE CHANGED
}

if (wgUserName == 'Lupo') {
  importScript ('User:Lupo/autocorrect.js');    // Auto-correction of common errors
  importScript ('User:Lupo/uselanghacks.js');   // Support for setting up upload forms
}

/*** Customize third-party functionality: MediaWiki:Extra-tabs.js */

var UploadForm_own_language_first = true;
var UploadForm_page_preview_in_tooltip = true;

if (wgUserName == 'Lupo') { // YOU MAY REMOVE THIS
  // Only for me. If you also want these, feel free to remove the if statement 
  // and it's closing } below.

  // Make Quick-delete (the nominate-for-deletion links in the sidebar) close the windows it opens
  var quick_delete_close_windows = true;

  // I have the gallery details gadget enabled. Make it also close windows it opens.
  var gallery_details_close_windows = true;
  // Same for ImgDelLinks...
  var image_links_close_windows = true;

} // ALSO REMOVE THIS, THEN  

// Extra-tabs customizations
tab_check_usage = 'uses';
tab_categorize = 'cat?';

function additionalImageTabs ()
{
  if (wgNamespaceNumber != 6) return;
  
  makeTab (  makeRawLink (  'desc'
                          , 'http://tools.wikimedia.de/~magnus/whatisthat.php?image='
                           + encodeURIComponent (wgTitle.split (" ").join ("_"))
                           + '&do_it=' + encodeURIComponent ('Do it!')
                          , '_blank')
           , 'ca-whatsthis');
}

// Always insert the form of Gadget-rightsfilter, which I have enabled
window.AutoLoadLogsFilter = true; 

/*** Thumbnail regenerator (obsolete?) */

function generate_thumbnail () {
  var urltitle = encodeURIComponent (wgTitle.split (' ').join ('_'));

  var input = prompt ('Sizes (separate by \';\'): ', '120;180');

  if (input) input = input.replace (/\s/g, '');
  if (!input || input.length == 0) return;
  var sizes = input.split (';');
  for (i = 0; i < sizes.length; i++) {
    if (sizes[i].length > 0) {
      if (/\D/.test (sizes[i]))
        alert (sizes[i] + ' is not a valid thumbnail size. Use only digits. Value ignored.');
      else {
        var X = sajax_init_object();
        var uri = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/thumb.php?w=' + sizes[i] + '&f=' + urltitle;
        X.open ('GET', uri, false);
        X.send (null); // No POST data
      }
    }
  }
}

/*** Edit section 0 link */

function addLeadSectionTab () {
  /* Only add new tab if the page is editable and has section editing links. */
  if (getElementsByClassName (document, '*', 'editsection').length > 0) {
    var edit_tab = document.getElementById ('ca-edit');
    if (edit_tab != null) {
      var href_for_lead = edit_tab.firstChild.getAttribute ('href') + "&section=0";
      var first_title   = getElementsByClassName (document, 'h1', 'firstHeading');
      if (first_title.length > 0) {
        /* We have a "firstHeading": put "[edit]" to its right */
        var edit_span = document.createElement ('span');
        edit_span.setAttribute ('class', 'editsection'); // To get the correct font size
        // floating clashes with other stuff in the top right corner
        edit_span.setAttribute ('style', 'display:inline; float:none;'); 
        edit_span.appendChild (document.createTextNode (' ['));
        edit_span.appendChild (makeRawLink ("edit", href_for_lead));
        edit_span.appendChild (document.createTextNode (']'));
        first_title[0].appendChild (edit_span);
      } else {
        /* No first title found: create a tab */                                   
        var new_tab = document.createElement ('li');
        new_tab.id = 'ca-editlead';
        var link = makeRawLink ("Edit lead", href_for_lead);
        new_tab.appendChild (link);
      
        edit_tab.parentNode.appendChild (new_tab);
      }
    }
  }
}

function setLeadSectionEditSummary ()
{
  /* The edit summary is set to "Lead section" if we're editing only the lead section of
     an article. This is the case iff the URL of the document ends in "&section=0". */

  if ((document.location.href.indexOf ("&section=0") + 10 == document.location.href.length) &&
      (document.editform != null) &&
      (document.editform.wpSummary != null))
  {
     document.editform.wpSummary.value = "/* Lead section */ ";
  }
}

/*** Personal toolbox */

function myPortlet() {
  /* Put together a new portlet and insert it at the top of the left column. */
  /* Note: \xa0 is &nbsp; and \xa9 is &copy; */
  var newportlet = document.createElement('div');
  var content    = document.createElement('div');
  
  newportlet.setAttribute('class', 'portlet');
  newportlet.setAttribute('id', 'p-lupo-links');
  content.setAttribute('class', 'pBody');
  
  content.appendChild (document.createTextNode('ML: '));
  content.appendChild
    (makeRawLink('(en)',
                 'http://mail.wikipedia.org/pipermail/wikien-l/' +
                 (today.getFullYear ()) + '-' + months[today.getMonth()] + '/thread.html#end'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild
    (makeRawLink('(c)',
                 'http://mail.wikipedia.org/pipermail/commons-l/' +
                 (today.getFullYear ()) + '-' + months[today.getMonth()] + '/thread.html#end'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild
    (makeRawLink('(f)',
                 'http://mail.wikipedia.org/pipermail/foundation-l/' +
                 (today.getFullYear ()) + '-' + months[today.getMonth()] + '/thread.html#end'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild
    (makeRawLink('(t)',
                 'http://mail.wikipedia.org/pipermail/wikitech-l/' +
                 (today.getFullYear ()) + '-' + months[today.getMonth()] + '/thread.html#end'));
  content.appendChild (document.createElement('br'));

  content.appendChild (document.createTextNode('Logs: '));
  content.appendChild (makeWikiLink ('P', 'Special:Log/protect'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('B', 'Special:Log/block'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('D', 'Special:Log/delete'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('I', 'Special:Log/upload'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('M', 'Special:Log/move'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('U', 'Special:Log/newusers'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('N', 'Special:Contributions/newbies'));
  content.appendChild (document.createElement('br'));

  content.appendChild (makeWikiLink ('DEL', 'COM:DEL'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('L', 'COM:L'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('AN', 'COM:AN'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('VP', 'COM:VP'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild (makeWikiLink ('Upload', 'Special:Upload?uselang=experienced'));
  content.appendChild (document.createElement('br'));
  
  if (wgUserName == 'Lupo') {
    content.appendChild (makeWikiLink ('simple', 'User:Lupo/Simple Photographs'));
    content.appendChild (document.createTextNode('\xa0|\xa0'));
    content.appendChild (makeWikiLink ('hairy', 'User:Lupo/Hairy copyright'));
    content.appendChild (document.createTextNode('\xa0|\xa0'));
    content.appendChild (makeWikiLink ('bio', 'User:Lupo/resources'));
    content.appendChild (document.createTextNode('\xa0|\xa0'));
  }
  content.appendChild
    (makeRawLink ('css', wgScript + '?title=User:' 
                         + encodeURIComponent (wgUserName)
                         + '/monobook.css&action=edit'));
  content.appendChild (document.createTextNode('\xa0|\xa0'));
  content.appendChild
    (makeRawLink ('js', wgScript + '?title=User:'
                        + encodeURIComponent (wgUserName)
                        + '/monobook.js&action=edit'));
  if (typeof (ImgDelLinks) != 'undefined') {
    var del_lks = ImgDelLinks.create_link ();
    if (del_lks) {
      content.appendChild (document.createElement('hr'));
      content.appendChild (del_lks);
    }
  }
  if (wgNamespaceNumber == 8 && wgUserName == 'Lupo') {
    var tmp = wgPageName.split ('/');
    if (tmp && tmp.length == 2) {
      content.appendChild (document.createElement('hr'));
      content.appendChild (
        makeActiveLink ('uselang hack', 'UseLangHack.do_uselang_hack (false); void 0;'));
      content.appendChild (document.createElement('br'));
      content.appendChild (
        makeActiveLink ('uselang copy', 'UseLangHack.do_uselang_hack_copy (false); void 0;'));
      content.appendChild (document.createElement('br'));
      content.appendChild (
        makeActiveLink ('uselang hack...', 'UseLangHack.do_uselang_hack (true); void 0;'));
      content.appendChild (document.createElement('br'));
      content.appendChild (
        makeActiveLink ('uselang copy...', 'UseLangHack.do_uselang_hack_copy (true); void 0;'));
    }
  } else if (wgUserName == 'Lupo') {
    var tmp = wgPageName.split ('/');
    if (tmp && tmp.length == 2 && tmp[0] == 'Commons:Upload') {
      content.appendChild (document.createElement('hr'));
      content.appendChild (
        makeActiveLink ('uselang hack (full)', 'UseLangHack.do_full_set (false); void 0;'));
      content.appendChild (document.createElement('br'));
      content.appendChild (
        makeActiveLink ('uselang hack...', 'UseLangHack.do_full_set (true); void 0;'));
    }
  }

  var title=document.createElement('h5');
  title.setAttribute ('style', 'text-transform: none;');
  title.appendChild (document.createTextNode (formatDate (today, false)));
  newportlet.appendChild(title);
  newportlet.appendChild(content);
  
  /* Insert the newly created portlet in the left column. */
  var leftcolumn = document.getElementById ('column-one');
  
  leftcolumn.insertBefore (newportlet, leftcolumn.firstChild);
}

/*** Other customizations: bottom tabs */

function bottomTabs() {
  /* Duplicate the top "tabs" at the end of the content area. */
  var tabs = document.getElementById('p-cactions').cloneNode(true);
  /* Give all the named items new ids to avoid id clashes with the existing top "tabs". */
  tabs.id = 'mytabs';
  var listitems = tabs.getElementsByTagName('LI');
  for (i=0;i<listitems.length;i++) {
    if(listitems[i].id) listitems[i].id = 'mytabs-' + listitems[i].id;
  }
  document.getElementById('column-content').appendChild(tabs);
}

function topTabsToRightPlace() {
  /* Remove the top "tabs" from the .portlet side column (why were they ever put there?) and
     put them at the top of the content area, where they belong! (This allows me to use simple
     relative positioning to get a proper layout. I don't have to mess around with absolute
     positioning. This is necessary for my language links at the top to work properly, but it
     is cleaner anyway. */
  var contents  = document.getElementById('column-content');
  var tabs      = document.getElementById('p-cactions');

  tabs.parentNode.removeChild (tabs);
  contents.insertBefore(tabs, contents.firstChild);
}

function rmLogo() {
  /* Physically remove the logo. My CSS doesn't display it anyway, but I don't know whether
     that is sufficient to prevent the browser loading it. */
  var logo = document.getElementById('p-logo');

  if (logo) logo.parentNode.removeChild (logo);
}

function reformatMyPage() {
  rmLogo();
  addLeadSectionTab();
  setLeadSectionEditSummary();
  myPortlet();
  additionalImageTabs ();
  bottomTabs();
  var content = document.getElementById ('bodyContent');
  if (wgCanonicalNamespace == 'Special' &&
      (wgCanonicalSpecialPageName == 'Log'
       || wgCanonicalSpecialPageName == 'Whatlinkshere'
       || wgCanonicalSpecialPageName == 'LinkSearch')) {
    // Add [del] links to image links on some special pages
    var lks = getElementsByTagNameStatic ('a', document.getElementById ('bodyContent'));
    DelReqHandler.addDelKeepLinks (lks, false, false, true);
  } else if (   document.URL.indexOf ('Commons:Deletion_requests') < 0
             && wgPageName != 'Commons:Deletion_requests'
             && document.URL.search (/[\?&]action=(edit|submit|delete)/) < 0) {
    // Add [del] links on galleries (including on categories)
    // On a deletion request, we need not do this, as DelReqHandler will do it anyway.
    // Also don't do this if "&action=edit|submit|delete" is present.
    var galleries = getElementsByClassName (document, 'table', 'gallery');
    if (galleries && galleries.length > 0) {
      for (var i = 0; i < galleries.length; i++) {
        var lks = getElementsByTagNameStatic ('a', galleries[i]);
        DelReqHandler.addDelKeepLinks (lks, false, false, true);
      }
    }
  }
  if (wgCanonicalNamespace == 'Creator' && document.getElementById ('editform')) {
    var t = document.getElementById ('wpTextbox1');
    if (!t.value || t.value.length == 0) {
      t.value = '\{\{Creator\n|Name = \<\!-- Last name, First name --\>\n|Alternative names = ' + wgTitle
              + '\n|Description = \n|Birthdate = \n|Birthloc = \n|Deathdate = \n|Deathloc = '
              + '\n|Workperiod = \n|Workloc = \n\}\}'
              + '\n\<' + 'includeonly\>\[\[Category: by ' + wgTitle + '\]\]\<\/includeonly\>'
              + '\n\<' + 'noinclude\>\n\[\[Category: by ' + wgTitle + '| \]\]'
              + '\n\<\!-- birth/death cats or XXth century YY--\>'
              + '\n\<' + '\/noinclude\>';
    }
  }
//  topTabsToRightPlace();  /* First the top "tabs"... */ // Used only at en-WP, not here
}

hookEvent ('load', reformatMyPage);

 /*jshint scripturl:true*/
mw.util.addPortletLink('p-tb', 'javascript:importScript("MediaWiki:VisualFileChange.js");', 'Perform batch task', 't-AjaxQuickDeleteOnDemand');
/*jshint scripturl:false*/