On November 27th, 2022, the 8,000th article was added to the SuccuWiki!

MediaWiki:Gadget-HotCat.js: Difference between revisions

From SuccuWiki - The Wiki of the Succubi
Jump to navigation Jump to search
(updating HotCat)
mNo edit summary
 
Line 2: Line 2:


/*
/*
  HotCat V2.21
HotCat V2.32


  Ajax-based simple Category manager. Allows adding/removing/changing categories on a page view.
Ajax-based simple Category manager. Allows adding/removing/changing categories on a page view.
  Supports multiple category changes, as well as redirect and disambiguation resolution. Also
Supports multiple category changes, as well as redirect and disambiguation resolution. Also
  plugs into the upload form. Search engines to use for the suggestion list are configurable, and
plugs into the upload form. Search engines to use for the suggestion list are configurable, and
  can be selected interactively.
can be selected interactively.


  Documentation: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat
Documentation: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat
  List of main authors: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat/Version_history
List of main authors: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat/Version_history


  License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)


  Choose whichever license of these you like best :-)
Choose whichever license of these you like best :-)
*/
*/


/*
/*
  This code is MW version safe. It should run on any MediaWiki installation >= MW 1.15. Note: if
This code is MW version safe. It should run on any MediaWiki installation >= MW 1.15. Note
  running on MW >= 1.17 configured with $wgLegacyJavaScriptGlobals != true, it will still force
that HotCat is supposed to run with or without jQuery, and also on older installations that
  publishing the wg* globals in the window object. Note that HotCat is supposed to run with or
do not yet have window.mw. If you use any of these newer features, make sure you qualify them
  without jQuery, and also on older installations that do not yet have window.mediaWiki. If you
by checking whether they exist at all, and by providing some meaningful fallback implementation
  use any of these newer features, make sure you qualify them by checking whether they exist at
if not. To start itself, HotCat uses jQuery(document).ready(). If it doesn't exist, HotCat won't
  all, and by providing some meaningful fallback implementation if not. To start itself, HotCat
start.
  uses either jQuery(document).ready(), if available (preferred), or the old addOnloadHook().
  If neither exists, HotCat won't start.
*/
*/
if (typeof wgAction == 'undefined' && window.mediaWiki && window.mediaWiki.config) { // Compatibility hack
/* jshint ignore:start */ // This old code uses too many coding conventions incompatible with jshint.
  window.wgAction = window.mediaWiki.config.get('wgAction');
(function () {
}
// Support: MW 1.16
if (typeof window.HotCat == 'undefined' && wgAction != 'edit') { // Guard against double inclusions, and inactivate on edit pages
var conf = window.mw ? mw.config.values : window;
 
if (
// Guard against double inclusions (in old IE/Opera element ids become window properties)
(window.HotCat && !window.HotCat.nodeName)
// Not on edit pages
|| conf.wgAction == 'edit'
 
) {
return;
}


// Configuration stuff.
// Configuration stuff.
window.HotCat = {
window.HotCat = {
  isCommonsVersion : true
// Localize these messages to the main language of your wiki.
    // If you copy HotCat to your wiki, you should set this to false!
messages :
 
{cat_removed  : 'removed [[Category:$1]]'
  // Localize these messages to the main language of your wiki.
,template_removed  : 'removed {{[[Category:$1]]}}'
  ,messages :
,cat_added    : 'added [[Category:$1]]'
    { cat_removed  : 'removed [[Category:$1]]'
,cat_keychange: 'new key for [[Category:$1]]: "$2"' // $2 is the new key
    ,template_removed  : 'removed {{[[Category:$1]]}}'
,cat_notFound : 'Category "$1" not found'
    ,cat_added    : 'added [[Category:$1]]'
,cat_exists  : 'Category "$1" already exists; not added.'
    ,cat_keychange: 'new key for [[Category:$1]]: "$2"' // $2 is the new key
,cat_resolved : ' (redirect [[Category:$1]] resolved)'
    ,cat_notFound : 'Category "$1" not found'
,uncat_removed: 'removed {{uncategorized}}'
    ,cat_exists  : 'Category "$1" already exists; not added.'
,separator    : '; '
    ,cat_resolved : ' (redirect [[Category:$1]] resolved)'
,prefix      : ""
    ,uncat_removed: 'removed {{uncategorized}}'
// Some text to prefix to the edit summary.
    ,separator    : '; '
,using        : ' using [[Help:Gadget-HotCat|HotCat]]'
    ,prefix      : ""
// Some text to append to the edit summary. Named 'using' for historical reasons. If you prefer
        // Some text to prefix to the edit summary.
// to have a marker at the front, use prefix and set this to the empty string.
    ,using        : ' using [[Help:Gadget-HotCat|HotCat]]'
,multi_change : '$1 categories'
        // Some text to append to the edit summary. Named 'using' for historical reasons. If you prefer
// $1 is replaced by a number. If your language has several plural forms (c.f. [[:en:Dual (grammatical form)]]),
        // to have a marker at the front, use prefix and set this to the empty string.
// you can set this to an array of strings suitable for passing to mw.language.configPlural().
    ,multi_change : '$1 categories'
// If that function doesn't exist, HotCat will simply fall back to using the last
        // $1 is replaced by a number. If your language has several plural forms (c.f. [[:en:Dual (grammatical form)]]),
// entry in the array.
        // you can set this to an array of strings suitable for passing to mw.language.configPlural().
,commit      : 'Save'
        // If that function doesn't exist, HotCat will simply fall back to using the last
// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
        // entry in the array.
// see localization hook below.
    ,commit      : 'Save'
,ok          : 'OK'
        // Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
        // see localization hook below.
// see localization hook below.
    ,ok          : 'OK'
,cancel      : 'Cancel'
        // Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
        // see localization hook below.
// see localization hook below.
    ,cancel      : 'Cancel'
,multi_error  : 'Could not retrieve the page text from the server. Therefore, your category changes '
        // Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
+'cannot be saved. We apologize for the inconvenience.'
        // see localization hook below.
// Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
    ,multi_error  : 'Could not retrieve the page text from the server. Therefore, your category changes '
// see localization hook below.
                    +'cannot be saved. We apologize for the inconvenience.'
,short_catchange : null
        // Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
// Defaults to '[[' + category_canonical + ':$1]]'. Can be overridden if in the short edit summaries
        // see localization hook below.
// not the standard category name should be used but, say, a shorter namespace alias. $1 is replaced
    ,short_catchange : null
// by a category name.
        // Defaults to '[[' + category_canonical + ':$1]]'. Can be overridden if in the short edit summaries
}
        // not the standard category name should be used but, say, a shorter namespace alias. $1 is replaced
        // by a category name.
    }
  ,category_regexp    : '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]'
  ,category_regexp    : '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]'
  // Regular sub-expression matching all possible names for the category namespace. Is automatically localized
// Regular sub-expression matching all possible names for the category namespace. Is automatically localized
  // correctly if you're running MediaWiki 1.16 or later. Otherwise, set it appropriately, e.g. at the German
// correctly if you're running MediaWiki 1.16 or later. Otherwise, set it appropriately, e.g. at the German
  // Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|[Kk][Aa][Tt][Ee][Gg][Oo][Rr][Ii][Ee]', or at the
// Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|[Kk][Aa][Tt][Ee][Gg][Oo][Rr][Ii][Ee]', or at the
  // Chinese Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|分类|分類'. Note that namespaces are case-
// Chinese Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|分类|分類'. Note that namespaces are case-
  // insensitive!
// insensitive!
  ,category_canonical : 'Category'
  ,category_canonical : 'Category'
  // The standard category name on your wiki. Is automatically localized correctly if you're running
// The standard category name on your wiki. Is automatically localized correctly if you're running
  // MediaWiki 1.16 or later; otherwise, set it to the preferred category name (e.g., "Kategorie").
// MediaWiki 1.16 or later; otherwise, set it to the preferred category name (e.g., "Kategorie").
  ,categories        : 'Categories'
  ,categories        : 'Categories'
  // Plural of category_canonical.
// Plural of category_canonical.
  ,disambig_category  : 'Disambiguation'
  ,disambig_category  : 'Disambiguation'
  // Any category in this category is deemed a disambiguation category; i.e., a category that should not contain
// Any category in this category is deemed a disambiguation category; i.e., a category that should not contain
  // any items, but that contains links to other categories where stuff should be categorized. If you don't have
// any items, but that contains links to other categories where stuff should be categorized. If you don't have
  // that concept on your wiki, set it to null. Use blanks, not underscores.
// that concept on your wiki, set it to null. Use blanks, not underscores.
  ,redir_category    : 'Category redirects'
  ,redir_category    : 'Category redirects'
  // Any category in this category is deemed a (soft) redirect to some other category defined by the first link
// Any category in this category is deemed a (soft) redirect to some other category defined by a link
  // to another category. If your wiki doesn't have soft category redirects, set this to null.
// to another non-blacklisted category. If your wiki doesn't have soft category redirects, set this to null.
  ,links : {change: '(±)', remove: '()', add: '(+)', restore: '(×)', undo: '(×)', down: '()', up: '()'}
    // If a soft-redirected category contains more than one link to another non-blacklisted category, it's considered
  // The little modification links displayed after category names.
    // a disambiguation category instead.
  ,links : {change: '(±)', remove: '(\u2212)', add: '(+)', restore: '(×)', undo: '(×)', down: '(\u2193)', up: '(\u2191)'}
// The little modification links displayed after category names. U+2212 is a minus sign; U+2193 and U+2191 are
// downward and upward pointing arrows. Do not use ↓ and ↑ in the code!
  ,tooltips : {
  ,tooltips : {
    change:  'Modify'
change:  'Modify'
  ,remove:  'Remove'
,remove:  'Remove'
  ,add:    'Add a new category'
,add:    'Add a new category'
  ,restore: 'Undo changes'
,restore: 'Undo changes'
  ,undo:    'Undo changes'
,undo:    'Undo changes'
  ,down:    'Open for modifying and display subcategories'
,down:    'Open for modifying and display subcategories'
  ,up:      'Open for modifying and display parent categories'
,up:      'Open for modifying and display parent categories'
   }
   }
  // The tooltips for the above links
// The tooltips for the above links
  ,addmulti          : '<span>+<sup>+</sup></span>'
  ,addmulti          : '<span>+<sup>+</sup></span>'
  // The HTML content of the "enter multi-mode" link at the front.
// The HTML content of the "enter multi-mode" link at the front.
  ,multi_tooltip      : 'Modify several categories'
  ,multi_tooltip      : 'Modify several categories'
  // Tooltip for the "enter multi-mode" link
// Tooltip for the "enter multi-mode" link
  ,disable            :
  ,disable            :
    function () { // Return true to disable HotCat. HotCat guarantees that the wg* globals exist here.
function () { // Return true to disable HotCat.
      var ns = wgNamespaceNumber;
var ns = conf.wgNamespaceNumber;
      return (  ns < 0   // Special pages; Special:Upload is handled differently
var nsIds = conf.wgNamespaceIds;
              || ns === 10 // Templates
return (  ns < 0     // Special pages; Special:Upload is handled differently
              || ns === 8 // MediaWiki
|| ns === 10 // Templates
              || ns === 6 && wgArticleId === 0 // Non-existing file pages
|| ns === 828 // Module (Lua)
              || ns === 2 && /\.(js|css)$/.test(wgTitle) // User scripts
|| ns === 8   // MediaWiki
              || typeof (wgNamespaceIds) != 'undefined'
|| ns === 6 && conf.wgArticleId === 0 // Non-existing file pages
                && (  ns === wgNamespaceIds['creator']
|| ns === 2 && /\.(js|css)$/.test(conf.wgTitle) // User scripts
                    || ns === wgNamespaceIds['timedtext']
|| nsIds
                    || ns === wgNamespaceIds['institution']
&& (  ns === nsIds['creator']
                    )
|| ns === nsIds['timedtext']
            );
|| ns === nsIds['institution']
    }
  )
  );
}
  ,uncat_regexp : /\{\{\s*([Uu]ncat(egori[sz]ed( image)?)?|[Nn]ocat|[Nn]eedscategory)[^}]*\}\}\s*(<\!--.*?--\>)?/g
  ,uncat_regexp : /\{\{\s*([Uu]ncat(egori[sz]ed( image)?)?|[Nn]ocat|[Nn]eedscategory)[^}]*\}\}\s*(<\!--.*?--\>)?/g
  // A regexp matching a templates used to mark uncategorized pages, if your wiki does have that.
// A regexp matching a templates used to mark uncategorized pages, if your wiki does have that.
  // If not, set it to null.
// If not, set it to null.
  ,existsYes    : '//upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png'
  ,existsYes    : '//upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png'
  ,existsNo    : '//upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png'
  ,existsNo    : '//upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png'
  // The images used for the little indication icon. Should not need changing.
// The images used for the little indication icon. Should not need changing.
  ,template_regexp    : '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]'
  ,template_regexp    : '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]'
  // Regexp to recognize templates. Like "category" above; autolocalized for MW 1.16+, otherwise set manually here.
// Regexp to recognize templates. Like "category" above; autolocalized for MW 1.16+, otherwise set manually here.
  // On the German Wikipedia, you might use '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]|[Vv][Oo][Rr][Ll][Aa][Gg][Ee]'.
// On the German Wikipedia, you might use '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]|[Vv][Oo][Rr][Ll][Aa][Gg][Ee]'.
  ,template_categories : {}
  ,template_categories : {}
  // a list of categories which can be removed by removing a template
// a list of categories which can be removed by removing a template
  // key: the category without namespace
// key: the category without namespace
  // value: A regexp matching the template name, again without namespace
// value: A regexp matching the template name, again without namespace
  // If you don't have this at your wiki, or don't want this, set it to an empty object {}.
// If you don't have this at your wiki, or don't want this, set it to an empty object {}.
  ,engine_names : {
  ,engine_names : {
    searchindex : 'Search index'
searchindex : 'Search index'
  ,pagelist    : 'Page list'
,pagelist    : 'Page list'
  ,combined    : 'Combined search'
,combined    : 'Combined search'
  ,subcat      : 'Subcategories'
,subcat      : 'Subcategories'
  ,parentcat  : 'Parent categories'
,parentcat  : 'Parent categories'
   }
   }
  // Names for the search engines
// Names for the search engines
  ,capitalizePageNames : true
  ,capitalizePageNames : true
  // Set to false if your wiki has case-sensitive page names. MediaWiki has two modes: either the first letter
// Set to false if your wiki has case-sensitive page names. MediaWiki has two modes: either the first letter
  // of a page is automatically capitalized ("first-letter"; Category:aa == Category:Aa), or it isn't
// of a page is automatically capitalized ("first-letter"; Category:aa == Category:Aa), or it isn't
  // ("case-sensitive"; Category:aa != Category:Aa). It doesn't currently have a fully case-insensitive mode
// ("case-sensitive"; Category:aa != Category:Aa). It doesn't currently have a fully case-insensitive mode
  // (which would mean Category:aa == Category:Aa == Category:AA == Category:aA)
// (which would mean Category:aa == Category:Aa == Category:AA == Category:aA)
  // HotCat tries to set this correctly automatically using an API query. It's still a good idea to manually
// HotCat tries to set this correctly automatically using an API query. It's still a good idea to manually
  // configure it correctly; either directly here if you copied HotCat, or in the local configuration file
// configure it correctly; either directly here if you copied HotCat, or in the local configuration file
  // MediaWiki:Gadget-HotCat.js/local_defaults if you hotlink to the Commons-version, to ensure it is set even
// MediaWiki:Gadget-HotCat.js/local_defaults if you hotlink to the Commons-version, to ensure it is set even
  // if that API query should fail for some strange reason.
// if that API query should fail for some strange reason.
  ,upload_disabled : false
  ,upload_disabled : false
  // If upload_disabled is true, HotCat will not be used on the Upload form.
// If upload_disabled is true, HotCat will not be used on the Upload form.
  ,blacklist : null
  ,blacklist : null
  // Single regular expression matching blacklisted categories that cannot be changed or
// Single regular expression matching blacklisted categories that cannot be changed or
  // added using HotCat. For instance /\bstubs?$/ (any category ending with the word "stub"
// added using HotCat. For instance /\bstubs?$/ (any category ending with the word "stub"
  // or "stubs"), or /(\bstubs?$)|\bmaintenance\b/ (stub categories and any category with the
// or "stubs"), or /(\bstubs?$)|\bmaintenance\b/ (stub categories and any category with the
  // word "maintenance" in its title.
// word "maintenance" in its title.


  // Stuff changeable by users:
// Stuff changeable by users:
  ,bg_changed : '#F8CCB0'
  ,bg_changed : '#F8CCB0'
  // Background for changed categories in multi-edit mode. Default is a very light salmon pink.
// Background for changed categories in multi-edit mode. Default is a very light salmon pink.
  ,no_autocommit : false
  ,no_autocommit : false
  // If true, HotCat will never automatically submit changes. HotCat will only open an edit page with
// If true, HotCat will never automatically submit changes. HotCat will only open an edit page with
  // the changes; users must always save explicitly.
// the changes; users must always save explicitly.
  ,del_needs_diff : false
  ,del_needs_diff : false
  // If true, the "category deletion" link "(-)" will never save automatically but always show an
// If true, the "category deletion" link "(-)" will never save automatically but always show an
  // edit page where the user has to save the edit manually. Is false by default because that's the
// edit page where the user has to save the edit manually. Is false by default because that's the
  // traditional behavior. This setting overrides no_autocommit for "(-)" links.
// traditional behavior. This setting overrides no_autocommit for "(-)" links.
  ,suggest_delay : 100
  ,suggest_delay : 100
  // Time, in milliseconds, that HotCat waits after a keystroke before making a request to the
// Time, in milliseconds, that HotCat waits after a keystroke before making a request to the
  // server to get suggestions.
// server to get suggestions.
  ,editbox_width : 40
  ,editbox_width : 40
  // Default width, in characters, of the text input field.
// Default width, in characters, of the text input field.
  ,suggestions : 'combined'
  ,suggestions : 'combined'
  // One of the engine_names above, to be used as the default suggestion engine.
// One of the engine_names above, to be used as the default suggestion engine.
  ,fixed_search : false
  ,fixed_search : false
  // If true, always use the default engine, and never display a selector.
// If true, always use the default engine, and never display a selector.
  ,use_up_down : true
  ,use_up_down : true
  // If false, do not display the "up" and "down" links
// If false, do not display the "up" and "down" links
  ,list_size : 5
  ,list_size : 5
  // Default list size
// Default list size
  ,single_minor : true
  ,single_minor : true
  // If true, single category changes are marked as minor edits. If false, they're not.
// If true, single category changes are marked as minor edits. If false, they're not.
  ,dont_add_to_watchlist : false
  ,dont_add_to_watchlist : false
  // If true, never add a page to the user's watchlist. If false, pages get added to the watchlist if
// If true, never add a page to the user's watchlist. If false, pages get added to the watchlist if
  // the user has the "Add pages I edit to my watchlist" or the "Add pages I create to my watchlist"
// the user has the "Add pages I edit to my watchlist" or the "Add pages I create to my watchlist"
  // options in his or her preferences set.
// options in his or her preferences set.
,shortcuts : null
,addShortcuts :
function (map) {
if (!map) return;
window.HotCat.shortcuts = window.HotCat.shortcuts || {};
for (var k in map) {
if (!map.hasOwnProperty (k) || typeof k != 'string') continue;
var v = map[k];
if (typeof v != 'string') continue;
k = k.replace (/^\s+|\s+$/g, "");
v = v.replace (/^\s+|\s+$/g, "");
if (k.length === 0 || v.length === 0) continue;
window.HotCat.shortcuts[k] = v;
}
}
};
};


(function () { // Local scope to avoid polluting the global namespace with declarations
// More backwards compatibility. We have a few places where we test for the browser: once for
// Safari < 3.0, twice for WebKit (Chrome or Safari, any versions), twice for IE <= 6, and
// once for IE < 8.
var ua = navigator.userAgent.toLowerCase();
var is_ie6 = /msie ([0-9]{1,}[\.0-9]{0,})/.exec(ua) !== null && parseFloat(RegExp.$1) <= 6.0;
var is_ie_lt8 = /msie ([0-9]{1,}[\.0-9]{0,})/.exec(ua) !== null && parseFloat(RegExp.$1) < 8.0;
var is_webkit = /applewebkit\/\d+/.test(ua) && ua.indexOf ('spoofer') < 0;
// And even more compatbility. HotCat was developed without jQuery, and anyway current jQuery
// (1.7.1) doesn't seem to support in jquery.getJSON() or jQuery.ajax() the automatic
// switching from GET to POST requests if the query arguments would make the uri too long.
// (IE has a hard limit of 2083 bytes, and the servers may have limits around 4 or 8kB.)
//    Anyway, HotCat is supposed to run on wikis without jQuery, so we'd have to supply some
// ajax routines ourselves in any case. We can't rely on the old sajax_init_object(), newer
// MW versions (>= 1.19) might not have it.
var getJSON = (function () {
function getRequest () {
var request = null;
try {
request = new window.XMLHttpRequest();
} catch (anything) {
if (window.ActiveXObject) {
try {
request = new window.ActiveXObject('Microsoft.XMLHTTP');
} catch (any) {
}
} // end if IE
} // end try-catch
return request;
}
 
return function (settings) {
var req = getRequest();
if (!req && settings && settings.error) settings.error (req);
if (!req || !settings || !settings.uri) return req;
var uri = armorUri (settings.uri);
var args = settings.data || null;
var method;
if (args && uri.length + args.length + 1 > 2000) {
// We lose caching, but at least we can make the request
method = 'POST';
req.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
} else {
method = 'GET';
if (args) uri += '?' + args;
args = null;
}
req.open (method, uri, true);
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 || !req.responseText || !(/^\s*[\{\[]/.test(req.responseText))) {
if (settings.error) settings.error (req);
} else {
if (settings.success) settings.success (eval ('(' + req.responseText + ')'));
}
};
req.setRequestHeader ('Pragma', 'cache=yes');
req.setRequestHeader ('Cache-Control', 'no-transform');
req.send (args);
return req;
};
})();
 
function armorUri (uri) {
// Avoid protocol-relative URIs, IE7 has a bug with them in Ajax calls
if (uri.length >= 2 && uri.substring(0, 2) == '//') return document.location.protocol + uri;
return uri;
}
 
function LoadTrigger (needed) {
this.queue = [];
this.toLoad = needed;
}
LoadTrigger.prototype = {
register : function (callback) {
if (this.toLoad <= 0) {
callback (); // Execute directly
} else {
this.queue[this.queue.length] = callback;
}
},
 
loaded : function () {
if (this.toLoad > 0) {
this.toLoad--;
if (this.toLoad === 0) {
// Run queued callbacks once
for (var i = 0; i < this.queue.length; i++) this.queue[i]();
this.queue = [];
}
}
}


  // Backwards compatibility stuff. We want HotCat to work with either wg* globals, or with mw.config.get().
};
  // Our "solution" is to publish the wg* globals if they're not already published.
 
  if (window.mediaWiki && window.mediaWiki.config) {
var setupCompleted = new LoadTrigger(1);
    var globals = window.mediaWiki.config.get();
// Used to run user-registered code once HotCat is fully set up and ready.
    if (globals && globals !== window) {
HotCat.runWhenReady = function (callback) {setupCompleted.register(callback);};
      for (var k in globals) window[k] = globals[k];
 
      window.mediaWiki.config = new window.mediaWiki.Map(true); // Make config point to window again.
var loadTrigger = new LoadTrigger(2);
    }
// Used to delay running the HotCat setup until /local_defaults and localizations have been loaded.
    globals = null;
 
  }
function load (uri) {
  // More backwards compatibility. We have four places where we test for the browser: once for
var head = document.getElementsByTagName ('head')[0];
  // Safari < 3.0, once for WebKit (Chrome or Safari, any versions), and twice for IE <= 6.
var s = document.createElement ('script');
  var ua = navigator.userAgent.toLowerCase();
s.setAttribute ('src', armorUri(uri));
  var is_ie6 = /msie ([0-9]{1,}[\.0-9]{0,})/.exec(ua) != null && parseFloat(RegExp.$1) <= 6.0;
s.setAttribute ('type', 'text/javascript');
  var is_webkit = /applewebkit\/\d+/.test(ua) && ua.indexOf ('spoofer') < 0;
var done = false;
  // And even more compatbility. HotCat was developed without jQuery, and anyway current jQuery
  // (1.7.1) doesn't seem to support in jquery.getJSON() or jQuery.ajax() the automatic
  // switching from GET to POST requests if the query arguments would make the uri too long.
  // (IE has a hard limit of 2083 bytes, and the servers may have limits around 4 or 8kB.)
  //    Anyway, HotCat is supposed to run on wikis without jQuery, so we'd have to supply some
  // ajax routines ourselves in any case. We can't rely on the old sajax_init_object(), newer
  // MW versions (>= 1.19) might not have it.
  var getJSON = (function () {
    function getRequest () {
      var request = null;
      try {
        request = new window.XMLHttpRequest();
      } catch (anything) {
        if (window.ActiveXObject) {
          try {
            request = new window.ActiveXObject('Microsoft.XMLHTTP');
          } catch (any) {
          }
        } // end if IE
      } // end try-catch
      return request;
    }


    return function (settings) {
function afterLoad () {
      var req = getRequest();
if (done) return;
      if (!req && settings && settings.error) settings.error (req);
done = true;
      if (!req || !settings || !settings.uri) return req;
s.onload = s.onreadystatechange = s.onerror = null; // Properly clean up to avoid memory leaks in IE
      var uri = armorUri (settings.uri);
if (head && s.parentNode) head.removeChild (s);
      var args = settings.data || null;
loadTrigger.loaded();
      var method;
}
      if (args && uri.length + args.length + 1 > 2000) {
        // We lose caching, but at least we can make the request
        method = 'POST';
        req.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
      } else {
        method = 'GET';
        if (args) uri += '?' + args;
        args = null;
      }
      req.open (method, uri, true);
      req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 || !req.responseText || !(/^\s*[\{\[]/.test(req.responseText))) {
          if (settings.error) settings.error (req);
        } else {
          if (settings.success) settings.success (eval ('(' + req.responseText + ')'));
        }        
      };
      req.setRequestHeader ('Pragma', 'cache=yes');
      req.setRequestHeader ('Cache-Control', 'no-transform');
      req.send (args);
      return req;
    };
  })();


  function armorUri (uri) {
s.onload = s.onreadystatechange = function () { // onreadystatechange for IE, onload for all others
    // Avoid protocol-relative URIs, IE7 has a bug with them in Ajax calls
if (done) return;
    if (uri.length >= 2 && uri.substring(0, 2) == '//') return document.location.protocol + uri;
if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
    return uri;
afterLoad ();
  }
}
};
s.onerror = afterLoad; // Clean up, but otherwise ignore errors
head.insertBefore (s, head.firstChild); // appendChild may trigger bugs in IE6 here
}


  function LoadTrigger (needed) {
function loadJS (page) {
    this.queue = [];
load (conf.wgServer + conf.wgScript + '?title=' + encodeURIComponent (page) + '&action=raw&ctype=text/javascript');
    this.toLoad = needed;
}
  };
  LoadTrigger.prototype = {
    register : function (callback) {
      if (this.toLoad <= 0) {
        callback (); // Execute directly
      } else {
        this.queue[this.queue.length] = callback;
      }
    },


    loaded : function () {
function loadURI (href) {
      if (this.toLoad > 0) {
var url = href;
        this.toLoad--;
if (url.substring (0, 2) == '//') {
        if (this.toLoad === 0) {
url = window.location.protocol + url;
          // Run queued callbacks once
} else if (url.substring (0, 1) == '/') {
          for (var i = 0; i < this.queue.length; i++) this.queue[i]();
url = conf.wgServer + url;
          this.queue = [];
}
        }
load (url);
      }
}
    }


  };
// Load local configurations, overriding the pre-set default values in the HotCat object above. This is always loaded
// from the wiki where this script is executing, even if this script itself is hotlinked from the Commons. This can
// be used to change the default settings, or to provide localized interface texts for edit summaries and so on.
loadJS ('MediaWiki:Gadget-HotCat.js/local_defaults');


  var setupCompleted = new LoadTrigger(1);
// Load localized UI texts. These are the texts that HotCat displays on the page itself. Texts shown in edit summaries
  // Used to run user-registered code once HotCat is fully set up and ready.
// should be localized in /local_defaults above.
  HotCat.runWhenReady = function (callback) {setupCompleted.register(callback);};
if (conf.wgUserLanguage != 'en') {
// Lupo: somebody thought it would be a good idea to add this. So the default is true, and you have to set it to false
// explicitly if you're not on the Commons and don't want that.
if (typeof window.hotcat_translations_from_commons == 'undefined') {
window.hotcat_translations_from_commons = true;
}
// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
if (window.hotcat_translations_from_commons && conf.wgServer.indexOf('//commons') < 0) {
loadURI ('//commons.wikimedia.org/w/index.php?title='
+ 'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage
+ '&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400'
);
} else {
// Load translations locally
loadJS ('MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage);
}
} else {
loadTrigger.loaded();
}


  var loadTrigger = new LoadTrigger(2);
// No further changes should be necessary here.
  // Used to delay running the HotCat setup until /local_defaults and localizations have been loaded.


  function load (uri) {
// The following regular expression strings are used when searching for categories in wikitext.
    var head = document.getElementsByTagName ('head')[0];
var wikiTextBlank  = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]+';
    var s = document.createElement ('script');
var wikiTextBlankRE = new RegExp (wikiTextBlank, 'g');
    s.setAttribute ('src', armorUri(uri));
// Regexp for handling blanks inside a category title or namespace name.
    s.setAttribute ('type', 'text/javascript');
// See http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/Title.php?revision=104051&view=markup#l2722
    var done = false;
// See also http://www.fileformat.info/info/unicode/category/Zs/list.htm
//  MediaWiki collapses several contiguous blanks inside a page title to one single blank. It also replace a
// number of special whitespace characters by simple blanks. And finally, blanks are treated as underscores.
// Therefore, when looking for page titles in wikitext, we must handle all these cases.
//  Note: we _do_ include the horizontal tab in the above list, even though the MediaWiki software for some reason
// appears to not handle it. The zero-width space \u200B is _not_ handled as a space inside titles by MW.
var wikiTextBlankOrBidi = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200B\\u200E\\u200F\\u2028-\\u202F\\u205F\\u3000]*';
// Whitespace regexp for handling whitespace between link components. Including the horizontal tab, but not \n\r\f\v:
// a link must be on one single line.
//  MediaWiki also removes Unicode bidi override characters in page titles (and namespace names) completely.
// This is *not* handled, as it would require us to allow any of [\u200E\u200F\u202A-\u202E] between any two
// characters inside a category link. It _could_ be done though... We _do_ handle strange spaces, including the
// zero-width space \u200B, and bidi overrides between the components of a category link (adjacent to the colon,
// or adjacent to and inside of "[[" and "]]").


    function afterLoad () {
// First auto-localize the regexps for the category and the template namespaces.
      if (done) return;
var formattedNamespaces = conf.wgFormattedNamespaces;
      done = true;
var namespaceIds = conf.wgNamespaceIds;
      s.onload = s.onreadystatechange = s.onerror = null; // Properly clean up to avoid memory leaks in IE
if (formattedNamespaces) {
      if (head && s.parentNode) head.removeChild (s);
function autoLocalize (namespaceNumber, fallback) {
      loadTrigger.loaded();
function create_regexp_str (name)
    }
{
if (!name || name.length === 0) return "";
var regex_name = "";
for (var i = 0; i < name.length; i++){
var initial = name.substr (i, 1);
var ll = initial.toLowerCase ();
var ul = initial.toUpperCase ();
if (ll == ul){
regex_name += initial;
} else {
regex_name += '[' + ll + ul + ']';
}
}
return regex_name
.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1')
.replace (wikiTextBlankRE, wikiTextBlank);
}


    s.onload = s.onreadystatechange = function () { // onreadystatechange for IE, onload for all others
fallback = fallback.toLowerCase();
      if (done) return;
var canonical  = formattedNamespaces["" + namespaceNumber].toLowerCase();
      if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
var regexp    = create_regexp_str (canonical);
        afterLoad ();
if (fallback && canonical != fallback) regexp += '|' + create_regexp_str(fallback);
      }
if (namespaceIds) {
    };
for (var cat_name in namespaceIds) {
    s.onerror = afterLoad; // Clean up, but otherwise ignore errors
if (  typeof cat_name == 'string'
    head.insertBefore (s, head.firstChild); // appendChild may trigger bugs in IE6 here
&& cat_name.toLowerCase() != canonical
  }
&& cat_name.toLowerCase() != fallback
&& namespaceIds[cat_name] == namespaceNumber)
{
regexp += '|' + create_regexp_str(cat_name);
}
}
}
return regexp;
}


  function loadJS (page) {
if (formattedNamespaces['14']) {
    load (wgServer + wgScript + '?title=' + encodeURIComponent (page) + '&action=raw&ctype=text/javascript');
HotCat.category_canonical = formattedNamespaces['14'];
  }
HotCat.category_regexp = autoLocalize (14, 'category');
}
if (formattedNamespaces['10']) {
HotCat.template_regexp = autoLocalize (10, 'template');
}
}


  function loadURI (href) {
// Utility functions. Yes, this duplicates some functionality that also exists in other places, but
    var url = href;
// to keep this whole stuff in a single file not depending on any other on-wiki Javascripts, we re-do
    if (url.substring (0, 2) == '//') {
// these few operations here.
      url = window.location.protocol + url;
function bind (func, target) {
    } else if (url.substring (0, 1) == '/') {
var f = func, tgt = target;
      url = wgServer + url;
return function () { return f.apply (tgt, arguments); };
    }
}
    load (url);
function make (arg, literal) {
  }
if (!arg) return null;
return literal ? document.createTextNode (arg) : document.createElement (arg);
}
function param (name, uri) {
if (typeof uri == 'undefined' || uri === null) uri = document.location.href;
var re = new RegExp ('[&?]' + name + '=([^&#]*)');
var m = re.exec (uri);
if (m && m.length > 1) return decodeURIComponent(m[1]);
return null;
}
function title (href) {
if (!href) return null;
var script = conf.wgScript + '?';
if (href.indexOf (script) === 0 || href.indexOf (conf.wgServer + script) === 0 || conf.wgServer.substring(0, 2) == '//' && href.indexOf (document.location.protocol + conf.wgServer + script) === 0) {
// href="/w/index.php?title=..."
return param ('title', href);
} else {
// href="/wiki/..."
var prefix = conf.wgArticlePath.replace ('$1', "");
if (href.indexOf (prefix) !== 0) prefix = conf.wgServer + prefix; // Fully expanded URL?
if (href.indexOf (prefix) !== 0 && prefix.substring(0, 2) == '//') prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
if (href.indexOf (prefix) === 0)
return decodeURIComponent (href.substring (prefix.length));
}
return null;
}
function hasClass (elem, name) {
return (' ' + elem.className + ' ').indexOf (' ' + name + ' ') >= 0;
}
function capitalize (str) {
if (!str || str.length === 0) return str;
return str.substr(0, 1).toUpperCase() + str.substr (1);
}
function wikiPagePath (pageName) {
// Note: do not simply use encodeURI, it doesn't encode '&', which might break if wgArticlePath actually has the $1 in
// a query parameter.
return conf.wgArticlePath.replace('$1', encodeURIComponent (pageName).replace(/%3A/g, ':').replace(/%2F/g, '/'));
}
function escapeRE(str) {
return str.replace(/([\\\^\$\.\?\*\+\(\)\[\]])/g, '\\$1');
}


  if (HotCat.isCommonsVersion && wgServer.indexOf ('/commons') < 0) {
function substituteFactory (options) {
    // We're running in some other wiki, which hotlinks to the Commons version. The other wiki can put local settings
options = options || {};
    // in this file to override the Commons settings for all user languages. For instance, if on your wiki people do
var lead = options.indicator || '$';
    // not like automatic saving, you'd add in that file the line HotCat.no_autocommit = true; If you hotlink, you
var indicator = escapeRE (lead);
    // *must* adapt HotCat.categories in this file to the local translation in wgContentLanguage of your wiki of the
var lbrace = escapeRE (options.lbrace || '{');
    // English plural "Categories", and you should provide translations in wgContentLanguage of your wiki of all messages,
var rbrace = escapeRE (options.rbrace || '}');
    // tooltips, and of the engine names.
var re;
    loadJS ('MediaWiki:Gadget-HotCat.js/local_defaults');
  } else {
    loadTrigger.loaded();
  }


  if (wgUserLanguage != 'en') {
re = new RegExp(
    window.hotcat_translations_from_commons = window.hotcat_translations_from_commons === undefined ? wgServer.indexOf('//commons') === -1 : window.hotcat_translations_from_commons;
'(?:' + indicator + '(' + indicator + '))|'                                          // $$
    // Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
+'(?:' + indicator + '(\\d+))|'                                                        // $0, $1
    if (window.hotcat_translations_from_commons && wgServer.indexOf('//commons') === -1) {
+'(?:' + indicator + '(?:' + lbrace + '([^' + lbrace + rbrace + ']+)' + rbrace + '))|' // ${key}
      loadURI (
+'(?:' + indicator + '(?!(?:[' + indicator + lbrace + ']|\\d))(\\S+?)\\b)'            // $key (only if first char after $ is not $, digit, or { )
        '//commons.wikimedia.org/w/index.php?title=' +
,'g');
          'MediaWiki:Gadget-HotCat.js/' + wgUserLanguage +
// Replace $1, $2, or ${key1}, ${key2}, or $key1, $key2 by values from map. $$ is replaced by a single $.
          '&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400'
return function (str, map) {
      );
if (!map) return str;
    } else {
return str.replace(re
      // Load translations locally
,function (match, prefix, idx, key, alpha) {
      loadJS('MediaWiki:Gadget-HotCat.js/' + wgUserLanguage);
if (prefix == lead) return lead;
    }
var k = alpha || key || idx;
  } else {
var replacement = typeof map[k] === 'function' ? map[k](match, k) : map[k];
    loadTrigger.loaded();
return typeof replacement === 'string' ? replacement : (replacement || match);
  }
}
);
};
}


  // No further changes should be necessary here.
var substitute = substituteFactory();
var replaceShortcuts = (function () {
var replaceHash = substituteFactory({indicator:'#',lbrace:'[',rbrace:']'});
return function (str, map) {
var s = replaceHash (str, map);
return HotCat.capitalizePageNames ? capitalize(s) : s;
};
})();


  // The following regular expression strings are used when searching for categories in wikitext.
// Text modification
  var wikiTextBlank  = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]+';
  var wikiTextBlankRE = new RegExp (wikiTextBlank, 'g');
  // Regexp for handling blanks inside a category title or namespace name.
  // See http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/Title.php?revision=104051&view=markup#l2722
  // See also http://www.fileformat.info/info/unicode/category/Zs/list.htm
  //  MediaWiki collapses several contiguous blanks inside a page title to one single blank. It also replace a
  // number of special whitespace characters by simple blanks. And finally, blanks are treated as underscores.
  // Therefore, when looking for page titles in wikitext, we must handle all these cases.
  //  Note: we _do_ include the horizontal tab in the above list, even though the MediaWiki software for some reason
  // appears to not handle it. The zero-width space \u200B is _not_ handled as a space inside titles by MW.
  var wikiTextBlankOrBidi = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200B\\u200E\\u200F\\u2028-\\u202F\\u205F\\u3000]*';
  // Whitespace regexp for handling whitespace between link components. Including the horizontal tab, but not \n\r\f\v:
  // a link must be on one single line.
  //  MediaWiki also removes Unicode bidi override characters in page titles (and namespace names) completely.
  // This is *not* handled, as it would require us to allow any of [\u200E\u200F\u202A-\u202E] between any two
  // characters inside a category link. It _could_ be done though... We _do_ handle strange spaces, including the
  // zero-width space \u200B, and bidi overrides between the components of a category link (adjacent to the colon,
  // or adjacent to and inside of "[[" and "]]").


  // First auto-localize the regexps for the category and the template namespaces.
var findCatsRE =
  if (typeof (wgFormattedNamespaces) != 'undefined') {
new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(?:' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':[^\\]]+\\]\\]', 'g');
    function autoLocalize (namespaceNumber, fallback) {
      function create_regexp_str (name)
      {
        if (!name || name.length === 0) return "";
        var regex_name = "";
        for (var i = 0; i < name.length; i++){
          var initial = name.substr (i, 1);
          var ll = initial.toLowerCase ();
          var ul = initial.toUpperCase ();
          if (ll == ul){
            regex_name += initial;
          } else {
            regex_name += '[' + ll + ul + ']';
          }
        }
        return regex_name.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1')
                        .replace (wikiTextBlankRE, wikiTextBlank);
      }


      fallback = fallback.toLowerCase();
function replaceByBlanks (match) {
      var canonical  = wgFormattedNamespaces["" + namespaceNumber].toLowerCase();
return match.replace(/(\s|\S)/g, ' '); // /./ doesn't match linebreaks. /(\s|\S)/ does.
      var regexp    = create_regexp_str (canonical);
}
      if (fallback && canonical != fallback) regexp += '|' + create_regexp_str (fallback);
      for (var cat_name in wgNamespaceIds) {
        if (  typeof (cat_name) == 'string'
            && cat_name.toLowerCase () != canonical
            && cat_name.toLowerCase () != fallback
            && wgNamespaceIds[cat_name] == namespaceNumber)
        {
          regexp += '|' + create_regexp_str (cat_name);
        }
      }
      return regexp;
    }


    if (wgFormattedNamespaces['14']) {
function find_category (wikitext, category, once) {
      HotCat.category_canonical = wgFormattedNamespaces['14'];
var cat_regex = null;
      HotCat.category_regexp = autoLocalize (14, 'category');
if(HotCat.template_categories[category]){
    }
cat_regex = new RegExp ('\\{\\{' + wikiTextBlankOrBidi + '(' + HotCat.template_regexp + '(?=' + wikiTextBlankOrBidi + ':))?' + wikiTextBlankOrBidi
    if (wgFormattedNamespaces['10']) {
+ '(?:' + HotCat.template_categories[category] + ')'
      HotCat.template_regexp = autoLocalize (10, 'template');
+ wikiTextBlankOrBidi + '(\\|.*?)?\\}\\}', 'g'
    }
);
  }
} else {
var cat_name  = escapeRE (category);
var initial  = cat_name.substr (0, 1);
cat_regex = new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':' + wikiTextBlankOrBidi
+ (initial == '\\' || !HotCat.capitalizePageNames
? initial
: '[' + initial.toUpperCase() + initial.toLowerCase() + ']')
+ cat_name.substring (1).replace (wikiTextBlankRE, wikiTextBlank)
+ wikiTextBlankOrBidi + '(\\|.*?)?\\]\\]', 'g'
);
}
if (once) return cat_regex.exec (wikitext);
var copiedtext = wikitext
.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
var result = [];
var curr_match = null;
while ((curr_match = cat_regex.exec (copiedtext)) !== null) {
result.push ({match : curr_match});
}
result.re = cat_regex;
return result; // An array containing all matches, with positions, in result[i].match
}


  // Utility functions. Yes, this duplicates some functionality that also exists in other places, but
var interlanguageRE = null;
  // to keep this whole stuff in a single file not depending on any other on-wiki Javascripts, we re-do
  // these few operations here.
  function bind (func, target) {
    var f = func, tgt = target;
    return function () { return f.apply (tgt, arguments); };
  }
  function make (arg, literal) {
    if (!arg) return null;
    return literal ? document.createTextNode (arg) : document.createElement (arg);
  }
  function param (name, uri) {
    if (typeof (uri) == 'undefined' || uri === null) uri = document.location.href;
    var re = new RegExp ('[&?]' + name + '=([^&#]*)');
    var m = re.exec (uri);
    if (m && m.length > 1) return decodeURIComponent(m[1]);
    return null;
  }
  function title (href) {
    if (!href) return null;
    var script = wgScript + '?';
    if (href.indexOf (script) === 0 || href.indexOf (wgServer + script) === 0 || wgServer.substring(0, 2) == '//' && href.indexOf (document.location.protocol + wgServer + script) === 0) {
      // href="/w/index.php?title=..."
      return param ('title', href);
    } else {
      // href="/wiki/..."
      var prefix = wgArticlePath.replace ('$1', "");
      if (href.indexOf (prefix) != 0) prefix = wgServer + prefix; // Fully expanded URL?
      if (href.indexOf (prefix) != 0 && prefix.substring(0, 2) == '//') prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
      if (href.indexOf (prefix) === 0)
        return decodeURIComponent (href.substring (prefix.length));
    }
    return null;
  }
  function hasClass (elem, name) {
    return (' ' + elem.className + ' ').indexOf (' ' + name + ' ') >= 0;
  }
  function capitalize (str) {
    if (!str || str.length === 0) return str;
    return str.substr(0, 1).toUpperCase() + str.substr (1);
  }
  function wikiPagePath (pageName) {
    // Note: do not simply use encodeURI, it doesn't encode '&', which might break if wgArticlePath actually has the $1 in
    // a query parameter.
    return wgArticlePath.replace('$1', encodeURIComponent (pageName).replace(/%3A/g, ':').replace(/%2F/g, '/'));
  }
  function substitute (str, map) {
    // Replace $1, $2, or ${key1}, ${key2} by values from map. $$ is replaced by a single $.
    return str.replace(
      /\$(\$|(\d+)|\{([^{}]+)\})/g
    ,function (match, dollar, idx, key) {
        if (dollar == '$') return '$';
        var k = key || idx;
        var replacement = typeof (map[k]) === 'function' ? map[k](match, k) : map[k];
        return typeof (replacement) === 'string' ? replacement : (replacement || match);
      }
    );
  }


  // Text modification
function change_category (wikitext, toRemove, toAdd, key, is_hidden) {


  var findCatsRE =
function find_insertionpoint (wikitext) {
    new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(?:' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':[^\\]]+\\]\\]', 'g');
var copiedtext = wikitext
.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
// Search in copiedtext to avoid that we insert inside an HTML comment or a nowiki "element".
var index = -1;
findCatsRE.lastIndex = 0;
while (findCatsRE.exec(copiedtext) !== null) index = findCatsRE.lastIndex;
if (index < 0) {
// Find the index of the first interlanguage link...
var match = null;
if (!interlanguageRE) {
// Approximation without API: interlanguage links start with 2 to 3 lower case letters, optionally followed by
// a sequence of groups consisting of a dash followed by one or more lower case letters. Exceptions are "simple"
// and "tokipona".
match = /((^|\n\r?)(\[\[\s*(([a-z]{2,3}(-[a-z]+)*)|simple|tokipona)\s*:[^\]]+\]\]\s*))+$/.exec (copiedtext);
} else {
match = interlanguageRE.exec(copiedtext);
}
if (match) index = match.index;
return {idx : index, onCat : false};
}
return {idx : index, onCat : index >= 0};
}


   function replaceByBlanks (match) {
var summary   = [];
    return match.replace(/(\s|\S)/g, ' '); // /./ doesn't match linebreaks. /(\s|\S)/ does.
var nameSpace = HotCat.category_canonical;
  }
var cat_point = -1; // Position of removed category;


  function find_category (wikitext, category, once) {
if (key) key = '|' + key;
    var cat_regex = null;
var keyChange = (toRemove && toAdd && toRemove == toAdd && toAdd.length > 0);
    if(HotCat.template_categories[category]){
var matches;
      cat_regex = new RegExp ('\\{\\{' + wikiTextBlankOrBidi + '(' + HotCat.template_regexp + '(?=' + wikiTextBlankOrBidi + ':))?' + wikiTextBlankOrBidi
if (toRemove && toRemove.length > 0) {
                              + '(?:' + HotCat.template_categories[category] + ')'
matches = find_category (wikitext, toRemove);
                              + wikiTextBlankOrBidi + '(\\|.*?)?\\}\\}', 'g'
if (!matches || matches.length === 0) {
                            );
return {text: wikitext, 'summary': summary, error: HotCat.messages.cat_notFound.replace (/\$1/g, toRemove)};
    } else {
} else {
      var cat_name  = category.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1');
var before = wikitext.substring (0, matches[0].match.index);
      var initial  = cat_name.substr (0, 1);
var after  = wikitext.substring (matches[0].match.index + matches[0].match[0].length);
      cat_regex = new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':' + wikiTextBlankOrBidi
if (matches.length > 1) {
                              + (initial == '\\' || !HotCat.capitalizePageNames
// Remove all occurrences in after
                                ? initial
matches.re.lastIndex = 0;
                                : '[' + initial.toUpperCase() + initial.toLowerCase() + ']')
after = after.replace (matches.re, "");
                              + cat_name.substring (1).replace (wikiTextBlankRE, wikiTextBlank)
}
                              + wikiTextBlankOrBidi + '(\\|.*?)?\\]\\]', 'g'
if (toAdd) {
                            );
nameSpace = matches[0].match[1] || nameSpace;
    }
if (key === null) key = matches[0].match[2]; // Remember the category key, if any.
    if (once) return cat_regex.exec (wikitext);
}
    var copiedtext = wikitext.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
// Remove whitespace (properly): strip whitespace, but only up to the next line feed.
                            .replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
// If we then have two linefeeds in a row, remove one. Otherwise, if we have two non-
    var result = [];
// whitespace characters, insert a blank.
    var curr_match = null;
var i = before.length - 1;
    while ((curr_match = cat_regex.exec (copiedtext)) != null) {
while (i >= 0 && before.charAt (i) != '\n' && before.substr (i, 1).search (/\s/) >= 0) i--;
      result.push ({match : curr_match});
var j = 0;
    }
while (j < after.length && after.charAt (j) != '\n' && after.substr (j, 1).search (/\s/) >= 0)
    result.re = cat_regex;
j++;
    return result; // An array containing all matches, with positions, in result[i].match
if (i >= 0 && before.charAt (i) == '\n' && (after.length === 0 || j < after.length && after.charAt (j) == '\n'))
  }
i--;
if (i >= 0) before = before.substring (0, i+1); else before = "";
if (j < after.length) after = after.substring (j); else after = "";
if (before.length > 0 && before.substring (before.length - 1).search (/\S/) >= 0
&& after.length > 0 && after.substr (0, 1).search (/\S/) >= 0)
before += ' ';
cat_point = before.length;
if (cat_point === 0 && after.length > 0 && after.substr(0,1) == '\n') {
after = after.substr(1);
}
wikitext = before + after;
if (!keyChange) {
if(HotCat.template_categories[toRemove]) {
summary.push (HotCat.messages.template_removed.replace (/\$1/g, toRemove));
} else {
summary.push (HotCat.messages.cat_removed.replace (/\$1/g, toRemove));
}
}
}
}
if (toAdd && toAdd.length > 0) {
matches = find_category (wikitext, toAdd);
if (matches && matches.length > 0) {
return {text: wikitext, 'summary': summary, error : HotCat.messages.cat_exists.replace (/\$1/g, toAdd)};
} else {
var onCat = false;
if (cat_point < 0) {
var point = find_insertionpoint (wikitext);
cat_point = point.idx;
onCat = point.onCat;
} else {
onCat = true;
}
var newcatstring = '[[' + nameSpace + ':' + toAdd + (key || "") + ']]';
if (cat_point >= 0) {
var suffix = wikitext.substring (cat_point);
wikitext = wikitext.substring (0, cat_point) + (cat_point > 0 ? '\n' : "") + newcatstring + (!onCat ? '\n' : "");
if (suffix.length > 0 && suffix.substr(0, 1) != '\n') {
wikitext += '\n' + suffix;
} else {
wikitext += suffix;
}
} else {
if (wikitext.length > 0 && wikitext.substr (wikitext.length - 1, 1) != '\n')
wikitext += '\n';
wikitext += (wikitext.length > 0 ? '\n' : "") + newcatstring;
}
if (keyChange) {
var k = key || "";
if (k.length > 0) k = k.substr (1);
summary.push (substitute (HotCat.messages.cat_keychange, [null, toAdd, k]));
} else {
summary.push (HotCat.messages.cat_added.replace (/\$1/g, toAdd));
}
if (HotCat.uncat_regexp && !is_hidden) {
var txt = wikitext.replace (HotCat.uncat_regexp, ""); // Remove "uncat" templates
if (txt.length != wikitext.length) {
wikitext = txt;
summary.push (HotCat.messages.uncat_removed);
}
}
}
}
return {text: wikitext, 'summary': summary, error: null};
}


  var interlanguageRE = null;
// The real HotCat UI


  function change_category (wikitext, toRemove, toAdd, key, is_hidden) {
function evtKeys (e) {
e = e || window.event || window.Event; // W3C, IE, Netscape
var code = 0;
if (typeof e.ctrlKey != 'undefined') { // All modern browsers
// Ctrl-click seems to be overloaded in FF/Mac (it opens a pop-up menu), so treat cmd-click
// as a ctrl-click, too.
if (e.ctrlKey || e.metaKey) code |= 1;
if (e.shiftKey) code |= 2;
} else if (typeof e.modifiers != 'undefined') { // Netscape...
if (e.modifiers & (Event.CONTROL_MASK | Event.META_MASK)) code |= 1;
if (e.modifiers & Event.SHIFT_MASK) code |= 2;
}
return code;
}
function evtKill (e) {
e = e || window.event || window.Event; // W3C, IE, Netscape
if (typeof e.preventDefault != 'undefined') {
e.preventDefault ();
e.stopPropagation ();
} else
e.cancelBubble = true;
return false;
}
function addEvent (node, evt, f, capture) {
if (window.jQuery && (!capture || !node.addEventListener)) window.jQuery (node).bind (evt, f);
else if (node.addEventListener) node.addEventListener (evt, f, capture); // FF etc; IE >= 9
else if (node.attachEvent) node.attachEvent ('on' + evt, f); // Older IE; Opera
else node['on' + evt] = f; // Very old!
}


    function find_insertionpoint (wikitext) {
var catLine      = null;
      var copiedtext = wikitext.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
var onUpload    = false;
                              .replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
var editors      = [];
      // Search in copiedtext to avoid that we insert inside an HTML comment or a nowiki "element".
      var index = -1;
      findCatsRE.lastIndex = 0;
      while (findCatsRE.exec(copiedtext) != null) index = findCatsRE.lastIndex;
      if (index < 0) {
        // Find the index of the first interlanguage link...
        var match = null;
        if (!interlanguageRE) {
          // Approximation without API: interlanguage links start with 2 to 3 lower case letters, optionally followed by
          // a sequence of groups consisting of a dash followed by one or more lower case letters. Exceptions are "simple"
          // and "tokipona".
          match = /((^|\n\r?)(\[\[\s*(([a-z]{2,3}(-[a-z]+)*)|simple|tokipona)\s*:[^\]]+\]\]\s*))+$/.exec (copiedtext);
        } else {
          match = interlanguageRE.exec(copiedtext);
        }
        if (match) index = match.index;
        return {idx : index, onCat : false};
      }
      return {idx : index, onCat : index >= 0};
    }


    var summary  = [];
var commitButton = null;
    var nameSpace = HotCat.category_canonical;
var commitForm  = null;
    var cat_point = -1; // Position of removed category;
var multiSpan    = null;


    if (key) key = '|' + key;
var pageText     = null;
    var keyChange = (toRemove && toAdd && toRemove == toAdd && toAdd.length > 0);
var pageTime     = null;
     if (toRemove && toRemove.length > 0) {
var pageWatched  = false;
      var matches = find_category (wikitext, toRemove);
var watchCreate  = false;
      if (!matches || matches.length === 0) {
var watchEdit    = false;
        return {text: wikitext, 'summary': summary, error: HotCat.messages.cat_notFound.replace (/\$1/g, toRemove)};
var minorEdits  = false;
      } else {
var editToken    = null;
        var before = wikitext.substring (0, matches[0].match.index);
        var after  = wikitext.substring (matches[0].match.index + matches[0].match[0].length);
        if (matches.length > 1) {
          // Remove all occurrences in after
          matches.re.lastIndex = 0;
          after = after.replace (matches.re, "");
        }
        if (toAdd) {
          nameSpace = matches[0].match[1] || nameSpace;
          if (key == null) key = matches[0].match[2]; // Remember the category key, if any.
        }
        // Remove whitespace (properly): strip whitespace, but only up to the next line feed.
        // If we then have two linefeeds in a row, remove one. Otherwise, if we have two non-
        // whitespace characters, insert a blank.
        var i = before.length - 1;
        while (i >= 0 && before.charAt (i) != '\n' && before.substr (i, 1).search (/\s/) >= 0) i--;
        var j = 0;
        while (j < after.length && after.charAt (j) != '\n' && after.substr (j, 1).search (/\s/) >= 0)
          j++;
        if (i >= 0 && before.charAt (i) == '\n' && (after.length === 0 || j < after.length && after.charAt (j) == '\n'))
          i--;
        if (i >= 0) before = before.substring (0, i+1); else before = "";
        if (j < after.length) after = after.substring (j); else after = "";
        if (before.length > 0 && before.substring (before.length - 1).search (/\S/) >= 0
            && after.length > 0 && after.substr (0, 1).search (/\S/) >= 0)
          before += ' ';
        cat_point = before.length;
        wikitext = before + after;
        if (!keyChange) {
          if(HotCat.template_categories[toRemove]) {
            summary.push (HotCat.messages.template_removed.replace (/\$1/g, toRemove));
          } else {
            summary.push (HotCat.messages.cat_removed.replace (/\$1/g, toRemove));
          }
        }
      }
     }
    if (toAdd && toAdd.length > 0) {
      var matches = find_category (wikitext, toAdd);
      if (matches && matches.length > 0) {
        return {text: wikitext, 'summary': summary, error : HotCat.messages.cat_exists.replace (/\$1/g, toAdd)};
      } else {
        var onCat = false;
        if (cat_point < 0) {
          var point = find_insertionpoint (wikitext);
          cat_point = point.idx;
          onCat = point.onCat;
        } else {
          onCat = true;
        }
        var newcatstring = '[[' + nameSpace + ':' + toAdd + (key || "") + ']]';
        if (cat_point >= 0) {
          var suffix = wikitext.substring (cat_point);
          wikitext = wikitext.substring (0, cat_point) + (cat_point > 0 ? '\n' : "") + newcatstring + (!onCat ? '\n' : "");
          if (suffix.length > 0 && suffix.substr(0, 1) != '\n') {
            wikitext += '\n' + suffix;
          } else {
            wikitext += suffix;
          }
        } else {
          if (wikitext.length > 0 && wikitext.substr (wikitext.length - 1, 1) != '\n')
            wikitext += '\n';
          wikitext += '\n' + newcatstring;
        }
        if (keyChange) {
          var k = key || "";
          if (k.length > 0) k = k.substr (1);
          summary.push (substitute (HotCat.messages.cat_keychange, [null, toAdd, k]));
        } else {
          summary.push (HotCat.messages.cat_added.replace (/\$1/g, toAdd));
        }
        if (HotCat.uncat_regexp && !is_hidden) {
          var txt = wikitext.replace (HotCat.uncat_regexp, ""); // Remove "uncat" templates
          if (txt.length != wikitext.length) {
            wikitext = txt;
            summary.push (HotCat.messages.uncat_removed);
          }
        }
      }
    }
    return {text: wikitext, 'summary': summary, error: null};
  }


   // The real HotCat UI
var is_rtl      = false;
var serverTime   = null;
var lastRevId    = null;
var pageTextRevId = null;
var conflictingUser = null;


  function evtKeys (e) {
var newDOM       = false; // true if MediaWiki serves the new UL-LI DOM for categories
    e = e || window.event || window.Event; // W3C, IE, Netscape
    var code = 0;
    if (typeof (e.ctrlKey) != 'undefined') { // All modern browsers
       // Ctrl-click seems to be overloaded in FF/Mac (it opens a pop-up menu), so treat cmd-click
      // as a ctrl-click, too.
      if (e.ctrlKey || e.metaKey) code |= 1;
      if (e.shiftKey) code |= 2;
    } else if (typeof (e.modifiers) != 'undefined') { // Netscape...
      if (e.modifiers & (Event.CONTROL_MASK | Event.META_MASK)) code |= 1;
      if (e.modifiers & Event.SHIFT_MASK) code |= 2;
    }
    return code;
  }
  function evtKill (e) {
    e = e || window.event || window.Event; // W3C, IE, Netscape
    if (typeof (e.preventDefault) != 'undefined') {
      e.preventDefault ();
      e.stopPropagation ();
    } else
      e.cancelBubble = true;
    return false;
  }
  function addEvent (node, evt, f, capture)
  {
    if (window.jQuery && (!capture || !node.addEventListener)) window.jQuery (node).bind (evt, f);
    else if (node.attachEvent) node.attachEvent ('on' + evt, f);
    else if (node.addEventListener) node.addEventListener (evt, f, capture);
    else node['on' + evt] = f;
  }


  var catLine      = null;
function setMultiInput () {
  var onUpload    = false;
if (commitButton || onUpload) return;
  var editors      = [];
commitButton = make ('input');
commitButton.type  = 'button';
commitButton.value = HotCat.messages.commit;
commitButton.onclick = multiSubmit;
if (multiSpan) {
multiSpan.parentNode.replaceChild (commitButton, multiSpan);
} else {
catLine.appendChild (commitButton);
}
}


  var commitButton = null;
function checkMultiInput () {
  var commitForm  = null;
if (!commitButton) return;
  var multiSpan    = null;
var has_changes = false;
for (var i = 0; i < editors.length; i++) {
if (editors[i].state != CategoryEditor.UNCHANGED) {
has_changes = true;
break;
}
}
commitButton.disabled = !has_changes;
}


  var pageText    = null;
function currentTimestamp () {
  var pageTime    = null;
var now = new Date();
  var pageWatched = false;
var ts = "" + now.getUTCFullYear();
  var watchCreate  = false;
function two (s) { return s.substr (s.length - 2); }
  var watchEdit    = false;
ts = ts
  var minorEdits  = false;
+ two ('0' + (now.getUTCMonth() + 1))
  var editToken    = null;
+ two ('0' + now.getUTCDate())
+ two ('00' + now.getUTCHours())
+ two ('00' + now.getUTCMinutes())
+ two ('00' + now.getUTCSeconds());
return ts;
}


  var is_rtl      = false;
var saveInProgress = false;
  var serverTime  = null;
function initiateEdit (doEdit, failure) {
  var lastRevId    = null;
if (saveInProgress) return;
  var pageTextRevId = null;
saveInProgress = true;
  var conflictingUser = null;
var oldButtonState;
if (commitButton) {
oldButtonState = commitButton.disabled;
commitButton.disabled = true;
}


  var newDOM      = false; // true if MediaWiki serves the new UL-LI DOM for categories
function fail() {
saveInProgress = false;
if (commitButton) commitButton.disabled = oldButtonState;
failure.apply(this, arguments);
}


  function setMultiInput () {
// Must use Ajax here to get the user options and the edit token.
    if (commitButton || onUpload) return;
    commitButton = make ('input');
    commitButton.type  = 'button';
    commitButton.value = HotCat.messages.commit;
    commitButton.onclick = multiSubmit;
    if (multiSpan) {
      multiSpan.parentNode.replaceChild (commitButton, multiSpan);
    } else {
      catLine.appendChild (commitButton);
    }
  }


  function checkMultiInput () {
getJSON ({
    if (!commitButton) return;
uri : conf.wgServer + conf.wgScriptPath + '/api.php'
    var has_changes = false;
,data : 'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent (conf.wgPageName)
    for (var i = 0; i < editors.length; i++) {
+ '&prop=info%7Crevisions%7Clanglinks&inprop=watched&intoken=edit&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500'
      if (editors[i].state != CategoryEditor.UNCHANGED) {
+ '&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId
        has_changes = true;
+ '&meta=siteinfo%7Cuserinfo&uiprop=options'
        break;
,success : function (json) { setPage(json); doEdit(fail); }
      }
,error : function (req) { fail(req.status + ' ' + req.statusText); }
    }
});
    commitButton.disabled = !has_changes;
}
  }


  function currentTimestamp () {
function multiChangeMsg (count) {
    var now = new Date();
var msg = HotCat.messages.multi_change;
    var ts  = "" + now.getUTCFullYear();
if (typeof msg != 'string' && msg.length) {
    function two (s) { return s.substr (s.length - 2); }
if (window.mw && mw.language && mw.language.convertPlural) {
    ts = ts
msg = mw.language.convertPlural (count, msg);
      + two ('0' + (now.getUTCMonth() + 1))
} else {
      + two ('0' + now.getUTCDate())
msg = msg[msg.length-1];
      + two ('00' + now.getUTCHours())
}
      + two ('00' + now.getUTCMinutes())
}
      + two ('00' + now.getUTCSeconds());
return substitute (msg, [null, "" + count]);
    return ts;
}
  }


  var saveInProgress = false;
function performChanges (failure, singleEditor) {
   function initiateEdit (doEdit, failure) {
if (pageText === null) {
    if (saveInProgress) return;
failure (HotCat.messages.multi_error);
    saveInProgress = true;
return;
    var oldButtonState;
}
    if (commitButton) {
// Backwards compatibility after message change (added $2 to cat_keychange)
      oldButtonState = commitButton.disabled;
if (HotCat.messages.cat_keychange.indexOf ('$2') < 0) HotCat.messages.cat_keychange += '"$2"';
      commitButton.disabled = true;
// More backwards-compatibility with earlier HotCat versions:
    }
if (!HotCat.messages.short_catchange) HotCat.messages.short_catchange = '[[' + HotCat.category_canonical + ':$1]]';
// Create a form and submit it. We don't use the edit API (api.php?action=edit) because
// (a) sensibly reporting back errors like edit conflicts is always a hassle, and
// (b) we want to show a diff for multi-edits anyway, and
// (c) we want to trigger onsubmit events, allowing user code to intercept the edit.
// Using the form, we can do (b) and (c), and we get (a) for free. And, of course, using the form
// automatically reloads the page with the updated categories on a successful submit, which
// we would have to do explicitly if we used the edit API.
var action;
// Normally, we don't have to care about edit conflicts. If some other user edited the page in the meantime, the
// server will take care of it and merge the edit automatically or present an edit conflict screen. However, the
// server suppresses edit conflicts with oneself. Hence, if we have a conflict, and the conflicting user is the
// current user, then we set the "oldid" value and switch to diff, which gives the "you are editing an old version;
// if you save, any more recent changes will be lost" screen.
var editingOldVersion = lastRevId !== null && lastRevId != conf.wgCurRevisionId || pageTextRevId !== null && pageTextRevId != conf.wgCurRevisionId;
var selfEditConflict = editingOldVersion && conflictingUser && conflictingUser == conf.wgUserName;
if (singleEditor && !singleEditor.noCommit && !HotCat.no_autocommit && editToken && !selfEditConflict) {
// If we do have an edit conflict, but not with ourself, that's no reason not to attempt to save: the server side may actually be able to
// merge the changes. We just need to make sure that we do present a diff view if it's a self edit conflict.
commitForm.wpEditToken.value = editToken;
action = commitForm.wpDiff;
if (action) action.name = action.value = 'wpSave';
} else {
action = commitForm.wpSave;
if (action) action.name = action.value = 'wpDiff';
}
var result = { text : pageText };
var changed = [], added = [], deleted = [], changes = 0;
var toEdit = !!singleEditor ? [singleEditor] : editors;
var error = null;
var i;
for (i=0; i < toEdit.length; i++) {
if (toEdit[i].state == CategoryEditor.CHANGED) {
result = change_category (
result.text
, toEdit[i].originalCategory
, toEdit[i].currentCategory
, toEdit[i].currentKey
, toEdit[i].currentHidden
);
if (!result.error) {
changes++;
if (!toEdit[i].originalCategory || toEdit[i].originalCategory.length === 0) {
added.push (toEdit[i].currentCategory);
} else {
changed.push ({from : toEdit[i].originalCategory, to : toEdit[i].currentCategory});
}
} else if (error === null) {
error = result.error;
}
} else if (   toEdit[i].state == CategoryEditor.DELETED
  && toEdit[i].originalCategory
  && toEdit[i].originalCategory.length > 0)
{
result = change_category (result.text, toEdit[i].originalCategory, null, null, false);
if (!result.error) {
changes++;
deleted.push (toEdit[i].originalCategory);
} else if (error === null) {
error = result.error;
}
}
}
if (error !== null) { // Do not commit if there were errors
action = commitForm.wpSave;
if (action) action.name = action.value = 'wpDiff';
}
// Fill in the form and submit it
commitForm.wpAutoSummary.value = 'd41d8cd98f00b204e9800998ecf8427e'; // MD5 hash of the empty string
commitForm.wpMinoredit.checked = minorEdits;
commitForm.wpWatchthis.checked = conf.wgArticleId === 0 && watchCreate || watchEdit || pageWatched;
if (conf.wgArticleId > 0 || !!singleEditor) {
if (changes == 1) {
if (result.summary && result.summary.length > 0)
commitForm.wpSummary.value = HotCat.messages.prefix + result.summary.join (HotCat.messages.separator) + HotCat.messages.using;
commitForm.wpMinoredit.checked = HotCat.single_minor || minorEdits;
} else if (changes > 1) {
var summary = [];
var shortSummary = [];
// Deleted
for (i = 0; i < deleted.length; i++) {
summary.push ('-' + substitute (HotCat.messages.short_catchange, [null, deleted[i]]));
}
if (deleted.length == 1)
shortSummary.push ('-' + substitute (HotCat.messages.short_catchange, [null, deleted[0]]));
else if (deleted.length > 1)
shortSummary.push ('- ' + multiChangeMsg (deleted.length));
// Added
for (i = 0; i < added.length; i++) {
summary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[i]]));
}
if (added.length == 1)
shortSummary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[0]]));
else if (added.length > 1)
shortSummary.push ('+ ' + multiChangeMsg (added.length));
// Changed
var arrow = is_rtl ? '\u2190' : '\u2192'; // left and right arrows. Don't use ← and → in the code.
for (i = 0; i < changed.length; i++) {
if (changed[i].from != changed[i].to) {
summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]) + arrow
+ substitute (HotCat.messages.short_catchange, [null, changed[i].to]));
} else {
summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]));
}
}
if (changed.length == 1) {
if (changed[0].from != changed[0].to) {
shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]) + arrow
+ substitute (HotCat.messages.short_catchange, [null, changed[0].to]));
} else {
shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]));
}
} else if (changed.length > 1) {
shortSummary.push ('± ' + multiChangeMsg (changed.length));
}
if (summary.length > 0) {
summary = summary.join (HotCat.messages.separator);
if (summary.length > 200 - HotCat.messages.prefix.length - HotCat.messages.using.length) {
summary = shortSummary.join (HotCat.messages.separator);
}
commitForm.wpSummary.value = HotCat.messages.prefix + summary + HotCat.messages.using;
}
}
}
commitForm.wpTextbox1.value = result.text;
commitForm.wpStarttime.value = serverTime || currentTimestamp ();
commitForm.wpEdittime.value = pageTime || commitForm.wpStarttime.value;
if (selfEditConflict) commitForm.oldid.value = "" + (pageTextRevId || conf.wgCurRevisionId);
// Submit the form in a way that triggers onsubmit events: commitForm.submit() doesn't.
commitForm.hcCommit.click();
}


    function fail() {
function resolveMulti (toResolve, callback) {
      saveInProgress = false;
var i;
      if (commitButton) commitButton.disabled = oldButtonState;
for (i = 0; i < toResolve.length; i++) {
      failure.apply(this, arguments);
toResolve[i].dab = null;
    };
toResolve[i].dabInput = toResolve[i].lastInput;
}
if (noSuggestions) {
callback (toResolve);
return;
}
// Use %7C instead of |, otherwise Konqueror insists on re-encoding the arguments, resulting in doubly encoded
// category names. (That is a bug in Konqueror. Other browsers don't have this problem.)
var args = 'action=query&prop=info%7Clinks%7Ccategories%7Ccategoryinfo&plnamespace=14'
+ '&pllimit=' + (toResolve.length * 10)
+ '&cllimit=' + (toResolve.length * 10)
+ '&format=json&titles=';
for (i = 0; i < toResolve.length; i++) {
var v = toResolve[i].dabInput;
v = replaceShortcuts (v, HotCat.shortcuts);
toResolve[i].dabInputCleaned = v;
args += encodeURIComponent ('Category:' + v);
if (i+1 < toResolve.length) args += '%7C';
}
getJSON({
uri : conf.wgServer + conf.wgScriptPath + '/api.php'
,data : args
,success: function (json) { resolveRedirects (toResolve, json); callback (toResolve); }
,error: function (req) { if (!req) noSuggestions = true; callback (toResolve); }
});
}


     // Must use Ajax here to get the user options and the edit token.
function resolveOne (page, toResolve) {
   
var cats     = page.categories;
    getJSON ({
var lks      = page.links;
      uri : wgServer + wgScriptPath + '/api.php'
var is_dab  = false;
      ,data : 'format=json&action=query&titles=' + encodeURIComponent (wgPageName)
var is_redir = typeof page.redirect == 'string'; // Hard redirect?
        + '&prop=info%7Crevisions%7Clanglinks&inprop=watched&intoken=edit&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500'
var is_hidden = page.categoryinfo && typeof page.categoryinfo.hidden == 'string';
        + '&rvlimit=2&rvdir=newer&rvstartid=' + wgCurRevisionId
var is_missing = typeof page.missing == 'string';
        + '&meta=siteinfo%7Cuserinfo&uiprop=options'
var i;
      ,success : function (json) { setPage(json); doEdit(fail); }
for (i = 0; i < toResolve.length; i++) {
      ,error : function (req) { fail(req.status + ' ' + req.statusText); }
if (toResolve.length > 1 && toResolve[i].dabInputCleaned != page.title.substring (page.title.indexOf (':') + 1)) continue;
    });
// Note: the server returns in page an NFC normalized Unicode title. If our input was not NFC normalized, we may not find
  }
// any entry here. If we have only one editor to resolve (the most common case, I presume), we may simply skip the check.
toResolve[i].currentHidden = is_hidden;
toResolve[i].inputExists = !is_missing;
toResolve[i].icon.src = armorUri(is_missing ? HotCat.existsNo : HotCat.existsYes);
}
if (is_missing) return;
if (!is_redir && cats && (HotCat.disambig_category || HotCat.redir_category)) {
for (var c = 0; c < cats.length; c++) {
var cat = cats[c]['title'];
// Strip namespace prefix
if (cat) {
cat = cat.substring (cat.indexOf (':') + 1).replace(/_/g, ' ');
if (cat == HotCat.disambig_category) {
is_dab = true; break;
} else if (cat == HotCat.redir_category) {
is_redir = true; break;
}
}
}
}
if (!is_redir && !is_dab) return;
if (!lks || lks.length === 0) return;
var titles = [];
for (i = 0; i < lks.length; i++) {
if (  lks[i]['ns'] == 14                            // Category namespace -- always true since we ask only for the category links
&& lks[i]['title'] && lks[i]['title'].length > 0) // Name not empty
{
// Internal link to existing thingy. Extract the page name and remove the namespace.
var match = lks[i]['title'];
match = match.substring (match.indexOf (':') + 1);
// Exclude blacklisted categories.
if (!HotCat.blacklist || !HotCat.blacklist.test (match)) {
titles.push (match);
}
}
}
if (titles.length === 0) {
return;
}
for (i = 0; i < toResolve.length; i++) {
if (toResolve.length > 1 && toResolve[i].dabInputCleaned != page.title.substring (page.title.indexOf (':') + 1)) continue;
toResolve[i].inputExists = true; // Might actually be wrong if it's a redirect pointing to a non-existing category
toResolve[i].icon.src = armorUri(HotCat.existsYes);
if (titles.length > 1) {
toResolve[i].dab = titles;
} else {
toResolve[i].text.value =
titles[0] + (toResolve[i].currentKey !== null ? '|' + toResolve[i].currentKey : "");
}
}
}


  function multiChangeMsg (count) {
function resolveRedirects (toResolve, params) {
    var msg = HotCat.messages.multi_change;
if (!params || !params.query || !params.query.pages) return;
    if (typeof (msg) != 'string' && msg.length) {
for (var p in params.query.pages) resolveOne (params.query.pages[p], toResolve);
      if (window.mediaWiki && window.mediaWiki.language && window.mediaWiki.language.convertPlural) {
}
        msg = window.mediaWiki.language.convertPlural (count, msg);
      } else {
        msg = msg[msg.length-1];
      }
    }
    return substitute (msg, [null, "" + count]);    
  }


  function performChanges (failure, singleEditor) {
function multiSubmit () {
    if (pageText === null) {
var toResolve = [];
      failure (HotCat.messages.multi_error);
for (var i = 0; i < editors.length; i++) {
      return;
if (editors[i].state == CategoryEditor.CHANGE_PENDING || editors[i].state == CategoryEditor.OPEN)
    }
toResolve.push (editors[i]);
    // Backwards compatibility after message change (added $2 to cat_keychange)
}
    if (HotCat.messages.cat_keychange.indexOf ('$2') < 0) {
if (toResolve.length === 0) {
      HotCat.messages.cat_keychange += '"$2"';
initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
    }
return;
    // More backwards-compatibility with earlier HotCat versions:
}
    if (!HotCat.messages.short_catchange) {
resolveMulti (
      HotCat.messages.short_catchange = '[[' + HotCat.category_canonical + ':$1]]';
  toResolve
    }
, function (resolved) {
    // Create a form and submit it. We don't use the edit API (api.php?action=edit) because
var firstDab = null;
    // (a) sensibly reporting back errors like edit conflicts is always a hassle, and
var dontChange = false;
    // (b) we want to show a diff for multi-edits anyway, and
for (var i = 0; i < resolved.length; i++) {
    // (c) we want to trigger onsubmit events, allowing user code to intercept the edit.
if (resolved[i].lastInput != resolved[i].dabInput) {
    // Using the form, we can do (b) and (c), and we get (a) for free. And, of course, using the form
// We didn't disable all the open editors, but we did asynchronous calls. It is
    // automatically reloads the page with the updated categories on a successful submit, which
// theoretically possible that the user changed something...
    // we would have to do explicitly if we used the edit API.
dontChange = true;
    var action;
} else {
    // Normally, we don't have to care about edit conflicts. If some other user edited the page in the meantime, the
if (resolved[i].dab) {
    // server will take care of it and merge the edit automatically or present an edit conflict screen. However, the
if (!firstDab) firstDab = resolved[i];
    // server suppresses edit conflicts with oneself. Hence, if we have a conflict, and the conflicting user is the
} else {
    // current user, then we set the "oldid" value and switch to diff, which gives the "you are editing an old version;
if (resolved[i].acceptCheck(true)) resolved[i].commit();
    // if you save, any more recent changes will be lost" screen.
}
    var editingOldVersion = lastRevId !== null && lastRevId != wgCurRevisionId || pageTextRevId !== null && pageTextRevId != wgCurRevisionId;
}
    var selfEditConflict = editingOldVersion && conflictingUser && conflictingUser == wgUserName;
}
    if (singleEditor && !singleEditor.noCommit && !HotCat.no_autocommit && editToken && !selfEditConflict) {
if (firstDab) {
      // If we do have an edit conflict, but not with ourself, that's no reason not to attempt to save: the server side may actually be able to
showDab (firstDab);
      // merge the changes. We just need to make sure that we do present a diff view if it's a self edit conflict.
} else if (!dontChange) {
      commitForm.wpEditToken.value = editToken;
initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
      action = commitForm.wpDiff;
}
      if (action) action.name = action.value = 'wpSave';
}
    } else {
);
      action = commitForm.wpSave;
}
      if (action) action.name = action.value = 'wpDiff';
    }
    var result = { text : pageText };
    var changed = [], added = [], deleted = [], changes = 0;
    var toEdit = !!singleEditor ? [singleEditor] : editors;
    var error = null;
    for (var i=0; i < toEdit.length; i++) {
      if (toEdit[i].state == CategoryEditor.CHANGED) {
        result = change_category (
            result.text
          , toEdit[i].originalCategory
          , toEdit[i].currentCategory
          , toEdit[i].currentKey
          , toEdit[i].currentHidden
        );
        if (!result.error) {
          changes++;
          if (!toEdit[i].originalCategory || toEdit[i].originalCategory.length === 0) {
            added.push (toEdit[i].currentCategory);
          } else {
            changed.push ({from : toEdit[i].originalCategory, to : toEdit[i].currentCategory});
          }
        } else if (error === null) {
          error = result.error;
        }
      } else if (  toEdit[i].state == CategoryEditor.DELETED
                && toEdit[i].originalCategory
                && toEdit[i].originalCategory.length > 0)
      {
        result = change_category (result.text, toEdit[i].originalCategory, null, null, false);
        if (!result.error) {
          changes++;
          deleted.push (toEdit[i].originalCategory);
        } else if (error === null) {
          error = result.error;
        }
      }
    }
    if (error !== null) { // Do not commit if there were errors
      action = commitForm.wpSave;
      if (action) action.name = action.value = 'wpDiff';
    }
    // Fill in the form and submit it
    commitForm.wpAutoSummary.value = 'd41d8cd98f00b204e9800998ecf8427e'; // MD5 hash of the empty string
    commitForm.wpMinoredit.checked = minorEdits;
    commitForm.wpWatchthis.checked = wgArticleId == 0 && watchCreate || watchEdit || pageWatched;
    if (wgArticleId > 0 || !!singleEditor) {
      if (changes == 1) {
        if (result.summary && result.summary.length > 0)
          commitForm.wpSummary.value = HotCat.messages.prefix + result.summary.join (HotCat.messages.separator) + HotCat.messages.using;
        commitForm.wpMinoredit.checked = HotCat.single_minor || minorEdits;
      } else if (changes > 1) {
        var summary = [];
        var shortSummary = [];
        // Deleted
        for (var i=0; i < deleted.length; i++) {
          summary.push ('−' + substitute (HotCat.messages.short_catchange, [null, deleted[i]]));
        }
        if (deleted.length == 1)
          shortSummary.push ('−' + substitute (HotCat.messages.short_catchange, [null, deleted[0]]));
        else if (deleted.length > 1)
          shortSummary.push ('− ' + multiChangeMsg (deleted.length));
        // Added
        for (var i=0; i < added.length; i++) {
          summary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[i]]));
        }
        if (added.length == 1)
          shortSummary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[0]]));
        else if (added.length > 1)
          shortSummary.push ('+ ' + multiChangeMsg (added.length));
        // Changed
        var arrow = is_rtl ? "←" : "→";
        for (var i=0; i < changed.length; i++) {
          if (changed[i].from != changed[i].to) {
            summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]) + arrow
                        + substitute (HotCat.messages.short_catchange, [null, changed[i].to]));
          } else {
            summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]));
          }
        }
        if (changed.length == 1) {
          if (changed[0].from != changed[0].to) {
            shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]) + arrow
                        + substitute (HotCat.messages.short_catchange, [null, changed[0].to]));
          } else {
            shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]));
          }
        } else if (changed.length > 1) {
          shortSummary.push ('± ' + multiChangeMsg (changed.length));
        }
        if (summary.length > 0) {
          summary = summary.join (HotCat.messages.separator);
          if (summary.length > 200 - HotCat.messages.prefix.length - HotCat.messages.using.length) {
            summary = shortSummary.join (HotCat.messages.separator);
          }
          commitForm.wpSummary.value = HotCat.messages.prefix + summary + HotCat.messages.using;
        }
      }
    }
    commitForm.wpTextbox1.value = result.text;
    commitForm.wpStarttime.value = serverTime || currentTimestamp ();
    commitForm.wpEdittime.value = pageTime || commitForm.wpStarttime.value;
    if (selfEditConflict) commitForm.oldid.value = "" + (pageTextRevId || wgCurRevisionId);
    // Submit the form in a way that triggers onsubmit events: commitForm.submit() doesn't.
    commitForm.hcCommit.click();
  }


  function resolveMulti (toResolve, callback) {
var cat_prefix = null;
    for (var i = 0; i < toResolve.length; i++) {
var noSuggestions = false;
      toResolve[i].dab = null;
var suggestionEngines = {
      toResolve[i].dabInput = toResolve[i].lastInput;
opensearch :
    }
{ uri    : '/api.php?format=json&action=opensearch&namespace=14&limit=30&search=Category:$1' // $1 = search term
    if (noSuggestions) {
,handler : // Function to convert result of uri into an array of category names
      callback (toResolve);
function (queryResult, queryKey) {
      return;
if (queryResult && queryResult.length >= 2) {
    }
var key = queryResult[0].substring(queryResult[0].indexOf(':') + 1);
    // Use %7C instead of |, otherwise Konqueror insists on re-encoding the arguments, resulting in doubly encoded
var titles = queryResult[1];
     // category names. (That is a bug in Konqueror. Other browsers don't have this problem.)
var exists = false;
    var args = 'action=query&prop=info%7Clinks%7Ccategories%7Ccategoryinfo&plnamespace=14'
if (!cat_prefix) cat_prefix = new RegExp ('^(' + HotCat.category_regexp + ':)');
            + '&pllimit=' + (toResolve.length * 10)
for (var i = 0; i < titles.length; i++) {
            + '&cllimit=' + (toResolve.length * 10)
cat_prefix.lastIndex = 0;
            + '&format=json&titles=';
var m = cat_prefix.exec (titles[i]);
    for (var i = 0; i < toResolve.length; i++) {
if (m && m.length > 1) {
      args += encodeURIComponent ('Category:' + toResolve[i].dabInput);
titles[i] = titles[i].substring (titles[i].indexOf (':') + 1); // rm namespace
      if (i+1 < toResolve.length) args += '%7C';
if (key == titles[i]) exists = true;
    }
} else {
    getJSON({
titles.splice (i, 1); // Nope, it's not a category after all.
      uri : wgServer + wgScriptPath + '/api.php'
i--;
    ,data : args
}
    ,success: function (json) { resolveRedirects (toResolve, json); callback (toResolve); }
}
    ,error: function (req) { if (!req) noSuggestions = true; callback (toResolve); }
titles.exists = exists;
    });
if (queryKey != key) titles.normalized = key; // Remember the NFC normalized key we got back from the server
  }
return titles;
}
return null;
}
}
,internalsearch :
{ uri     : '/api.php?format=json&action=query&list=allpages&apnamespace=14&aplimit=30&apfrom=$1&apprefix=$1'
,handler :
function (queryResult, queryKey) {
if (queryResult && queryResult.query && queryResult.query.allpages) {
var titles = queryResult.query.allpages;
for (var i = 0; i < titles.length; i++) {
titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
}
return titles;
}
return null;
}
}
,exists :
{ uri    : '/api.php?format=json&action=query&prop=info&titles=Category:$1'
,handler :
function (queryResult, queryKey) {
if (queryResult && queryResult.query && queryResult.query.pages && !queryResult.query.pages[-1]) {
// Should have exactly 1
for (var p in queryResult.query.pages) {
var title = queryResult.query.pages[p].title;
title = title.substring (title.indexOf (':') + 1);
var titles = [title];
titles.exists = true;
if (queryKey != title) titles.normalized = title; // NFC
return titles;
}
}
return null;
}
}
,subcategories :
// I don't understand why they didn't map cmnamespace=14 automatically to cmtype=subcat,
// which gives better results and is faster.
{ uri    : '/api.php?format=json&action=query&list=categorymembers'
+(function (version) {
var m = version.match(/^(\d+)\.(\d+)/);
var major = 0, minor = 0;
if (m && m.length > 1) {
major = parseInt (m[1], 10);
minor = (m.length > 2 ? parseInt (m[2], 10) : 0);
}
if (major > 1 || major === 1 && minor > 17) return '&cmtype=subcat'; // Since MW1.18
return '&cmnamespace=14';
  }
)(conf.wgVersion)
+'&cmlimit=max&cmtitle=Category:$1'
,handler :
function (queryResult, queryKey) {
if (queryResult && queryResult.query && queryResult.query.categorymembers) {
var titles = queryResult.query.categorymembers;
for (var i = 0; i < titles.length; i++) {
titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
}
return titles;
}
return null;
}
}
,parentcategories :
{ uri     : '/api.php?format=json&action=query&prop=categories&titles=Category:$1&cllimit=max'
,handler :
function (queryResult, queryKey) {
if (queryResult && queryResult.query && queryResult.query.pages) {
for (var p in queryResult.query.pages) {
if (queryResult.query.pages[p].categories) {
var titles = queryResult.query.pages[p].categories;
for (var i = 0; i < titles.length; i++) {
titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
}
return titles;
}
}
}
return null;
}
}
};


  function resolveOne (page, toResolve) {
var suggestionConfigs = {
    var cats    = page.categories;
searchindex : {name: 'Search index', engines: ['opensearch'], cache: {}, show: true, temp: false, noCompletion : false}
    var lks      = page.links;
,pagelist    : {name: 'Page list', engines: ['internalsearch', 'exists'], cache: {}, show: true, temp: false, noCompletion : false}
    var is_dab  = false;
,combined    : {name: 'Combined search', engines: ['opensearch', 'internalsearch'], cache: {}, show: true, temp: false, noCompletion : false}
    var is_redir = typeof (page.redirect) == 'string'; // Hard redirect?
,subcat      : {name: 'Subcategories', engines: ['subcategories'], cache: {}, show: true, temp: true, noCompletion : true}
    var is_hidden = page.categoryinfo && typeof (page.categoryinfo.hidden) == 'string';
,parentcat  : {name: 'Parent categories', engines: ['parentcategories'], cache: {}, show: true, temp: true, noCompletion : true}
    for (var j = 0; j < toResolve.length; j++) {
};
      if (toResolve[j].dabInput != page.title.substring (page.title.indexOf (':') + 1)) continue;
      toResolve[j].currentHidden = is_hidden;
    }
    if (!is_redir && cats && (HotCat.disambig_category || HotCat.redir_category)) {
      for (var c = 0; c < cats.length; c++) {
        var cat = cats[c]['title'];
        // Strip namespace prefix
        if (cat) {
          cat = cat.substring (cat.indexOf (':') + 1).replace(/_/g, ' ');
          if (cat == HotCat.disambig_category) {
            is_dab = true; break;
          } else if (cat == HotCat.redir_category) {
            is_redir = true; break;
          }
        }
      }
    }
    if (!is_redir && !is_dab) return;
    if (!lks || lks.length === 0) return;
    var titles = [];
    for (var i = 0; i < lks.length; i++) {
      if (  lks[i]['ns'] == 14                            // Category namespace
          && lks[i]['title'] && lks[i]['title'].length > 0) // Name not empty
      {
        // Internal link to existing thingy. Extract the page name and remove the namespace.
        var match = lks[i]['title'];
        titles.push (match.substring (match.indexOf (':') + 1));
        if (is_redir) break;
      }
    }
    for (var j = 0; j < toResolve.length; j++) {
      if (toResolve[j].dabInput != page.title.substring (page.title.indexOf (':') + 1)) continue;
      if (titles.length > 1) {
        toResolve[j].dab = titles;
      } else {
        toResolve[j].inputExists = true; // Might actually be wrong...
        toResolve[j].icon.src = armorUri(HotCat.existsYes);
        toResolve[j].text.value =
          titles[0] + (toResolve[j].currentKey != null ? '|' + toResolve[j].currentKey : "");
      }
    }
  }


  function resolveRedirects (toResolve, params) {
function CategoryEditor () { this.initialize.apply (this, arguments); }
    if (!params || !params.query || !params.query.pages) return;
CategoryEditor.UNCHANGED      = 0;
    for (var p in params.query.pages) resolveOne (params.query.pages[p], toResolve);
CategoryEditor.OPEN          = 1; // Open, but no input yet
  }
CategoryEditor.CHANGE_PENDING = 2; // Open, some input made
CategoryEditor.CHANGED        = 3;
CategoryEditor.DELETED        = 4;


  function multiSubmit () {
// IE6 sometimes forgets to redraw the list when editors are opened or closed.
    var toResolve = [];
// Adding/removing a dummy element helps, at least when opening editors.
    for (var i = 0; i < editors.length; i++) {
var dummyElement = make ('\xa0', true);
      if (editors[i].state == CategoryEditor.CHANGE_PENDING || editors[i].state == CategoryEditor.OPEN)
        toResolve.push (editors[i]);
    }
    if (toResolve.length === 0) {
      initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
      return;
    }
    resolveMulti (
        toResolve
      , function (resolved) {
          var firstDab = null;
          var dontChange = false;
          for (var i = 0; i < resolved.length; i++) {
            if (resolved[i].lastInput != resolved[i].dabInput) {
              // We didn't disable all the open editors, but we did asynchronous calls. It is
              // theoretically possible that the user changed something...
              dontChange = true;
            } else {
              if (resolved[i].dab) {
                if (!firstDab) firstDab = resolved[i];
              } else {
                if (resolved[i].acceptCheck(true)) resolved[i].commit();
              }
            }
          }
          if (firstDab) {
            showDab (firstDab);
          } else if (!dontChange) {
            initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
          }
        }
    );
  }


  var cat_prefix = null;
function forceRedraw () {
  var noSuggestions = false;
if (!is_ie6) return;
  var suggestionEngines = {
if (dummyElement.parentNode) {
    opensearch :
document.body.removeChild (dummyElement);
      { uri    : '/api.php?format=json&action=opensearch&namespace=14&limit=30&search=Category:$1' // $1 = search term
} else {
      ,handler : // Function to convert result of uri into an array of category names
document.body.appendChild (dummyElement);
          function (queryResult, queryKey) {
}
            if (   queryResult != null && queryResult.length == 2
}
                && queryResult[0].toLowerCase() == 'category:' + queryKey.toLowerCase()
              )
            {
              var titles = queryResult[1];
              if (!cat_prefix) cat_prefix = new RegExp ('^(' + HotCat.category_regexp + ':)');
              for (var i = 0; i < titles.length; i++) {
                cat_prefix.lastIndex = 0;
                var m = cat_prefix.exec (titles[i]);
                if (m && m.length > 1) {
                  titles[i] = titles[i].substring (titles[i].indexOf (':') + 1); // rm namespace
                } else {
                  titles.splice (i, 1); // Nope, it's not a category after all.
                  i--;
                }
              }
              return titles;
            }
            return null;
          }
      }
    ,internalsearch :
      { uri    : '/api.php?format=json&action=query&list=allpages&apnamespace=14&aplimit=30&apfrom=$1'
      ,handler :
          function (queryResult, queryKey) {
            if (queryResult && queryResult.query && queryResult.query.allpages) {
              var titles = queryResult.query.allpages;
              var key    = queryKey.toLowerCase();
              for (var i = 0; i < titles.length; i++) {
                titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
                if (titles[i].toLowerCase().indexOf (key) != 0) {
                  titles.splice (i, 1); // Doesn't start with the query key
                  i--;
                }
              }
              return titles;
            }
            return null;
          }
      }
    ,subcategories :
      // I don't understand why they didn't map cmnamespace=14 automatically to cmtype=subcat,
      // which gives better results and is faster.
      { uri    : '/api.php?format=json&action=query&list=categorymembers'
                +(function (version) {
                    var m = version.match(/^(\d+)\.(\d+)/);
                    var major = 0, minor = 0;
                    if (m && m.length > 1) {
                      major = parseInt (m[1], 10);
                      minor = (m.length > 2 ? parseInt (m[2], 10) : 0);
                    }
                    if (major > 1 || major === 1 && minor > 17) return '&cmtype=subcat'; // Since MW1.18
                    return '&cmnamespace=14';
                  }
                  )(wgVersion)
                +'&cmlimit=max&cmtitle=Category:$1'
      ,handler :
          function (queryResult, queryKey) {
            if (queryResult && queryResult.query && queryResult.query.categorymembers) {
              var titles = queryResult.query.categorymembers;
              for (var i = 0; i < titles.length; i++) {
                titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
              }
              return titles;
            }
            return null;
          }
      }
  ,parentcategories :
      { uri    : '/api.php?format=json&action=query&prop=categories&titles=Category:$1&cllimit=max'
      ,handler :
          function (queryResult, queryKey) {
            if (queryResult && queryResult.query && queryResult.query.pages) {
              for (var p in queryResult.query.pages) {
                if (queryResult.query.pages[p].categories) {
                  var titles = queryResult.query.pages[p].categories;
                  for (var i = 0; i < titles.length; i++) {
                    titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
                  }
                  return titles;
                }
              }
            }
            return null;
          }
      }
  };


  var suggestionConfigs = {
// Event keyCodes that we handle in the text input field/suggestion list.
    searchindex : {name: 'Search index', engines: ['opensearch'], cache: {}, show: true, temp: false, noCompletion : false}
var BS = 8, TAB = 9, RET = 13, ESC = 27, SPACE = 32, PGUP = 33, PGDOWN = 34, UP = 38, DOWN = 40, DEL = 46, IME = 229;
  ,pagelist    : {name: 'Page list', engines: ['internalsearch'], cache: {}, show: true, temp: false, noCompletion : false}
  ,combined    : {name: 'Combined search', engines: ['opensearch', 'internalsearch'], cache: {}, show: true, temp: false, noCompletion : false}
function makeActive (which) {
  ,subcat      : {name: 'Subcategories', engines: ['subcategories'], cache: {}, show: true, temp: true, noCompletion : true}
if (which.is_active) return;
  ,parentcat  : {name: 'Parent categories', engines: ['parentcategories'], cache: {}, show: true, temp: true, noCompletion : true}
for (var i = 0; i < editors.length; i++) {
  };
if (editors[i] !== which) editors[i].inactivate ();
}
which.is_active = true;
if (which.dab) {
showDab (which);
} else {
// Check for programmatic value changes.
var expectedInput = which.lastRealInput || which.lastInput || "";
var actualValue = which.text.value || "";
if (expectedInput.length === 0 && actualValue.length > 0 || expectedInput.length > 0 && actualValue.indexOf (expectedInput) !== 0) {
// Somehow the field's value appears to have changed, and which.lastSelection therefore is no longer valid. Try to set the
// cursor at the end of the category, and do not display the old suggestion list.
which.showsList = false;
var v = actualValue.split('|');
which.lastRealInput = which.lastInput = v[0];
if (v.length > 1) which.currentKey = v[1];
if (which.lastSelection) which.lastSelection = {start: v[0].length, end: v[0].length};
}
if (which.showsList) which.displayList();
if (which.lastSelection) {
if (is_webkit) {
// WebKit (Safari, Chrome) has problems selecting inside focus()
// See http://code.google.com/p/chromium/issues/detail?id=32865#c6
window.setTimeout (
function () { which.setSelection (which.lastSelection.start, which.lastSelection.end); }
,1
);
} else {
which.setSelection (which.lastSelection.start, which.lastSelection.end);
}
}
}
}


  function CategoryEditor () { this.initialize.apply (this, arguments); };
function showDab (which) {
  CategoryEditor.UNCHANGED      = 0;
if (!which.is_active) {
  CategoryEditor.OPEN          = 1; // Open, but no input yet
makeActive(which);
  CategoryEditor.CHANGE_PENDING = 2; // Open, some input made
} else {
  CategoryEditor.CHANGED        = 3;
which.showSuggestions (which.dab, false, null, null); // do autocompletion, no key, no engine selector
  CategoryEditor.DELETED        = 4;
which.dab = null;
}
}


  // IE6 sometimes forgets to redraw the list when editors are opened or closed.
CategoryEditor.prototype = {
  // Adding/removing a dummy element helps, at least when opening editors.
  var dummyElement = make ('\xa0', true);


  function forceRedraw () {
initialize : function (line, span, after, key, is_hidden) {
    if (!is_ie6) return;
// If a span is given, 'after' is the category title, otherwise it may be an element after which to
    if (dummyElement.parentNode) {
// insert the new span. 'key' is likewise overloaded; if a span is given, it is the category key (if
      document.body.removeChild (dummyElement);
// known), otherwise it is a boolean indicating whether a bar shall be prepended.
    } else {
if (!span) {
      document.body.appendChild (dummyElement);
this.isAddCategory = true;
     }
// Create add span and append to catLinks
  }
this.originalCategory = "";
this.originalKey = null;
this.originalExists  = false;
if (!newDOM) {
span = make ('span');
span.className = 'noprint';
if (key) {
span.appendChild (make (' | ', true));
if (after) {
after.parentNode.insertBefore (span, after.nextSibling);
after = after.nextSibling;
} else {
line.appendChild (span);
}
} else if (line.firstChild) {
span.appendChild (make (' ', true));
line.appendChild (span);
}
}
this.linkSpan = make ('span');
this.linkSpan.className = 'noprint nopopups hotcatlink';
var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
lk.appendChild (make (HotCat.links.add, true)); lk.title = HotCat.tooltips.add;
this.linkSpan.appendChild (lk);
span = make (newDOM ? 'li' : 'span');
span.className = 'noprint';
if (is_rtl) span.dir = 'rtl';
span.appendChild (this.linkSpan);
if (after)
after.parentNode.insertBefore (span, after.nextSibling);
else
line.appendChild (span);
this.normalLinks = null;
this.undelLink = null;
this.catLink = null;
} else {
if (is_rtl) span.dir = 'rtl';
this.isAddCategory = false;
this.catLink = span.firstChild;
this.originalCategory = after;
this.originalKey = (key && key.length > 1) ? key.substr(1) : null; // > 1 because it includes the leading bar
this.originalExists  = !hasClass (this.catLink, 'new');
// Create change and del links
this.makeLinkSpan ();
if (!this.originalExists && this.upDownLinks) this.upDownLinks.style.display = 'none';
span.appendChild (this.linkSpan);
}
this.originalHidden     = is_hidden;
this.line              = line;
this.engine            = HotCat.suggestions;
this.span              = span;
this.currentCategory    = this.originalCategory;
this.currentExists      = this.originalExists;
this.currentHidden      = this.originalHidden;
this.currentKey        = this.originalKey;
this.state              = CategoryEditor.UNCHANGED;
this.lastSavedState    = CategoryEditor.UNCHANGED;
this.lastSavedCategory  = this.originalCategory;
this.lastSavedKey      = this.originalKey;
this.lastSavedExists    = this.originalExists;
this.lastSavedHidden    = this.originalHidden;
if (this.catLink && this.currentKey) {
this.catLink.title = this.currentKey;
}
editors[editors.length] = this;
},


  function makeActive (which) {
makeLinkSpan : function () {
    if (which.is_active) return;
this.normalLinks = make ('span');
    for (var i = 0; i < editors.length; i++) {
var lk = null;
      if (editors[i] !== which) editors[i].inactivate ();
if (this.originalCategory && this.originalCategory.length > 0) {
    }
lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.remove, this);
    which.is_active = true;
lk.appendChild (make (HotCat.links.remove, true)); lk.title = HotCat.tooltips.remove;
    if (which.dab) {
this.normalLinks.appendChild (make (' ', true));
      showDab (which);
this.normalLinks.appendChild (lk);
    } else {
}
      // Check for programmatic value changes.
if (!HotCat.template_categories[this.originalCategory]) {
      var expectedInput = which.lastRealInput || which.lastInput || "";
lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
      var actualValue = which.text.value || "";
lk.appendChild (make (HotCat.links.change, true)); lk.title = HotCat.tooltips.change;
      if (expectedInput.length == 0 && actualValue.length > 0 || expectedInput.length > 0 && actualValue.indexOf (expectedInput) !== 0) {
this.normalLinks.appendChild (make (' ', true));
        // Somehow the field's value appears to have changed, and which.lastSelection therefore is no longer valid. Try to set the
this.normalLinks.appendChild (lk);
        // cursor at the end of the category, and do not display the old suggestion list.
if (!noSuggestions && HotCat.use_up_down) {
        which.showsList = false;
this.upDownLinks = make ('span');
        var v = actualValue.split('|');
lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.down, this);
        which.lastRealInput = which.lastInput = v[0];
lk.appendChild (make (HotCat.links.down, true)); lk.title = HotCat.tooltips.down;
        if (v.length > 1) which.currentKey = v[1];
this.upDownLinks.appendChild (make (' ', true));
        if (which.lastSelection) which.lastSelection = {start: v[0].length, end: v[0].length};  
this.upDownLinks.appendChild (lk);
      }
lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.up, this);
      if (which.showsList) which.displayList();
lk.appendChild (make (HotCat.links.up, true)); lk.title = HotCat.tooltips.up;
      if (which.lastSelection) {
this.upDownLinks.appendChild (make (' ', true));
        if (is_webkit) {
this.upDownLinks.appendChild (lk);
          // WebKit (Safari, Chrome) has problems selecting inside focus()
this.normalLinks.appendChild (this.upDownLinks);
          // See http://code.google.com/p/chromium/issues/detail?id=32865#c6
}
          window.setTimeout (
}
            function () { which.setSelection (which.lastSelection.start, which.lastSelection.end); }
this.linkSpan = make ('span');
            ,1
this.linkSpan.className = 'noprint nopopups hotcatlink';
          );
this.linkSpan.appendChild (this.normalLinks);
        } else {
this.undelLink = make ('span');
          which.setSelection (which.lastSelection.start, which.lastSelection.end);
this.undelLink.className = 'nopopups hotcatlink';
        }
this.undelLink.style.display = 'none';
      }
lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.restore, this);
    }
lk.appendChild (make (HotCat.links.restore, true)); lk.title = HotCat.tooltips.restore;
  }
this.undelLink.appendChild (make (' ', true));
this.undelLink.appendChild (lk);
this.linkSpan.appendChild (this.undelLink);
},


  function showDab (which) {
invokeSuggestions : function (dont_autocomplete) {
    if (!which.is_active) {
if (this.engine && suggestionConfigs[this.engine] && suggestionConfigs[this.engine].temp && !dont_autocomplete) {
      makeActive(which);
this.engine = HotCat.suggestions; // Reset to a search upon input
    } else {
}
      which.showSuggestions (which.dab, false, null, null); // do autocompletion, no key, no engine selector
this.state = CategoryEditor.CHANGE_PENDING;
      which.dab = null;
var self = this;
    }
window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
  }
},


   CategoryEditor.prototype = {
makeForm : function () {
var form = make ('form');
form.method = 'POST'; form.onsubmit = bind (this.accept, this);
this.form = form;
var self = this;
var text = make ('input'); text.type = 'text'; text.size = HotCat.editbox_width;
if (!noSuggestions) {
// Be careful here to handle IME input. This is browser/OS/IME dependent, but basically there are two mechanisms:
// - Modern (DOM Level 3) browsers use compositionstart/compositionend events to signal composition; if the
//   composition is not canceled, there'll be a textInput event following. During a composition key events are
//  either all suppressed (FF/Gecko), or otherwise have keyDown === IME for all keys (Webkit).
//  - Webkit sends a textInput followed by keyDown === IME and a keyUp with the key that ended composition.
//  - Gecko doesn't send textInput but just a keyUp with the key that ended composition, without sending keyDown
//    first. Gecko doesn't send any keydown while IME is active.
// - Older browsers signal composition by keyDown === IME for the first and subsequent keys for a composition. The
//  first keyDown !== IME is certainly after the end of the composition. Typically, composition end can also be
//  detected by a keyDown IME with a keyUp of space, tab, escape, or return. (Example: IE8)
text.onkeyup =
function (evt) {
evt = evt || window.event || window.Event; // W3C, IE, Netscape
var key = evt.keyCode || 0;
if (self.ime && self.lastKey === IME && !self.usesComposition && (key === TAB || key === RET || key == ESC || key === SPACE)) self.ime = false;
if (self.ime) return true;
if (key === UP || key === DOWN || key === PGUP || key === PGDOWN) {
// In case a browser doesn't generate keypress events for arrow keys...
if (self.keyCount === 0) return self.processKey (evt);
} else {
if (key === ESC && self.lastKey !== IME) {
if (!self.resetKeySelection ()) {
// No undo of key selection: treat ESC as "cancel".
self.cancel ();
return;
}
}
// Also do this for ESC as a workaround for Firefox bug 524360
// https://bugzilla.mozilla.org/show_bug.cgi?id=524360
self.invokeSuggestions (key === BS || key === DEL || key === ESC);
}
return true;
};
text.onkeydown =
function (evt) {
evt = evt || window.event || window.Event; // W3C, IE, Netscape
var key = evt.keyCode || 0;
self.lastKey = key;
self.keyCount = 0;
// DOM Level < 3 IME input
if (!self.ime && key === IME && !self.usesComposition) {
// self.usesComposition catches browsers that may emit spurious keydown IME after a composition has ended
self.ime = true;
} else if (self.ime && key !== IME && !(key >= 16 && key <= 20 || key >= 91 && key <= 93 || key === 144)) {
// Ignore control keys: ctrl, shift, alt, alt gr, caps lock, windows/apple cmd keys, num lock. Only the windows keys
// terminate IME (apple cmd doesn't), but they also cause a blur, so it's OK to ignore them here.
// Note: Safari 4 (530.17) propagates ESC out of an IME composition (observed at least on Win XP).
self.ime = false;
}
if (self.ime) return true;
// Handle return explicitly, to override the default form submission to be able to check for ctrl
if (key === RET) return self.accept (evt);
// Inhibit default behavior of ESC (revert to last real input in FF: we do that ourselves)
return (key === ESC) ? evtKill(evt) : true;
};
// And handle continued pressing of arrow keys
text.onkeypress = function (evt) {self.keyCount++; return self.processKey (evt);};
addEvent (text, 'focus', function () { makeActive(self); });
// On IE, blur events are asynchronous, and may thus arrive after the element has lost the focus. Since IE
// can get the selection only while the element is active (has the focus), we may not always get the selection.
// Therefore, use an IE-specific synchronous event on IE...
// Don't test for text.selectionStart being defined; FF3.6.4 raises an exception when trying to access that
// property while the element is not being displayed.
addEvent (text
, (typeof text.onbeforedeactivate != 'undefined' && text.createTextRange) ? 'beforedeactivate' : 'blur'
, bind (this.saveView, this)
);
// DOM Level 3 IME handling
try {
// Setting lastKey = IME provides a fake keyDown for Gecko's single keyUp after a cmposition. If we didn't do this,
// cancelling a composition via ESC would also cancel and close the whole category input editor.
addEvent(text, 'compositionstart', function (evt) { self.lastKey = IME; self.usesComposition = true; self.ime = true; });
addEvent(text, 'compositionend', function (evt) { self.lastKey = IME; self.usesComposition = true; self.ime = false; });
addEvent(text, 'textInput', function (evt) { self.ime = false; self.invokeSuggestions(false); });
} catch (any) {
// Just in case some browsers might produce exceptions with these DOM Level 3 events
}
addEvent(text, 'blur', function (evt) { self.usesComposition = false; self.ime = false; });
}
this.text = text;


    initialize : function (line, span, after, key, is_hidden) {
this.icon = make ('img');
      // If a span is given, 'after' is the category title, otherwise it may be an element after which to
      // insert the new span. 'key' is likewise overloaded; if a span is given, it is the category key (if
      // known), otherwise it is a boolean indicating whether a bar shall be prepended.
      if (!span) {
        this.isAddCategory = true;
        // Create add span and append to catLinks
        this.originalCategory = "";
        this.originalKey = null;
        this.originalExists  = false;
        if (!newDOM) {
          span = make ('span');
          span.className = 'noprint';
          if (key) {
            span.appendChild (make (' | ', true));
            if (after) {
              after.parentNode.insertBefore (span, after.nextSibling);
              after = after.nextSibling;
            } else {
              line.appendChild (span);
            }
          } else if (line.firstChild) {
            span.appendChild (make (' ', true));
            line.appendChild (span);
          }
        }
        this.linkSpan = make ('span');
        this.linkSpan.className = 'noprint nopopups hotcatlink';
        var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
        lk.appendChild (make (HotCat.links.add, true)); lk.title = HotCat.tooltips.add;
        this.linkSpan.appendChild (lk);
        span = make (newDOM ? 'li' : 'span');
        span.className = 'noprint';
        if (is_rtl) span.dir = 'rtl';
        span.appendChild (this.linkSpan);
        if (after)
          after.parentNode.insertBefore (span, after.nextSibling);
        else
          line.appendChild (span);
        this.normalLinks = null;
        this.undelLink = null;
        this.catLink = null;
      } else {
        if (is_rtl) span.dir = 'rtl';
        this.isAddCategory = false;
        this.catLink = span.firstChild;
        this.originalCategory = after;
        this.originalKey = (key && key.length > 1) ? key.substr(1) : null; // > 1 because it includes the leading bar
        this.originalExists  = !hasClass (this.catLink, 'new');
        // Create change and del links
        this.makeLinkSpan ();
        if (!this.originalExists && this.upDownLinks) this.upDownLinks.style.display = 'none';
        span.appendChild (this.linkSpan);
      }
      this.originalHidden    = is_hidden;
      this.line              = line;
      this.engine            = HotCat.suggestions;
      this.span              = span;
      this.currentCategory    = this.originalCategory;
      this.currentExists      = this.originalExists;
      this.currentHidden      = this.originalHidden;
      this.currentKey        = this.originalKey;
      this.state              = CategoryEditor.UNCHANGED;
      this.lastSavedState    = CategoryEditor.UNCHANGED;
      this.lastSavedCategory  = this.originalCategory;
      this.lastSavedKey      = this.originalKey;
      this.lastSavedExists    = this.originalExists;
      this.lastSavedHidden    = this.originalHidden;
      if (this.catLink && this.currentKey) {
        this.catLink.title = this.currentKey;
      }
      editors[editors.length] = this;
    },


    makeLinkSpan : function () {
var list = null;
      this.normalLinks = make ('span');
if (!noSuggestions) {
      var lk = null;
list = make ('select');
      if (this.originalCategory && this.originalCategory.length > 0) {
list.onclick    = function (e) { if (self.highlightSuggestion(0)) self.textchange (false, true); };
        lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.remove, this);
list.ondblclick = function (e) { if (self.highlightSuggestion(0)) self.accept (e); };
        lk.appendChild (make (HotCat.links.remove, true)); lk.title = HotCat.tooltips.remove;
list.onchange = function (e) { self.highlightSuggestion(0); self.text.focus(); };
        this.normalLinks.appendChild (make (' ', true));
list.onkeyup =
        this.normalLinks.appendChild (lk);
function (evt) {
      }
evt = evt || window.event || window.Event; // W3C, IE, Netscape
      if (!HotCat.template_categories[this.originalCategory]) {
if (evt.keyCode === ESC) {
        lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
self.resetKeySelection ();
        lk.appendChild (make (HotCat.links.change, true)); lk.title = HotCat.tooltips.change;
self.text.focus();
        this.normalLinks.appendChild (make (' ', true));
window.setTimeout (function () {self.textchange (true);}, HotCat.suggest_delay);
        this.normalLinks.appendChild (lk);
} else if (evt.keyCode === RET) {
        if (!noSuggestions && HotCat.use_up_down) {
self.accept (evt);
          this.upDownLinks = make ('span');
}
          lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.down, this);
};
          lk.appendChild (make (HotCat.links.down, true)); lk.title = HotCat.tooltips.down;
if (!HotCat.fixed_search) {
          this.upDownLinks.appendChild (make (' ', true));
var engineSelector = make ('select');
          this.upDownLinks.appendChild (lk);
for (var key in suggestionConfigs) {
          lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.up, this);
if (suggestionConfigs[key].show) {
          lk.appendChild (make (HotCat.links.up, true)); lk.title = HotCat.tooltips.up;
var opt = make ('option');
          this.upDownLinks.appendChild (make (' ', true));
opt.value = key;
          this.upDownLinks.appendChild (lk);
if (key == this.engine) opt.selected = true;
          this.normalLinks.appendChild (this.upDownLinks);
opt.appendChild (make (suggestionConfigs[key].name, true));
        }
engineSelector.appendChild (opt);
      }
}
      this.linkSpan = make ('span');
}
      this.linkSpan.className = 'noprint nopopups hotcatlink';
engineSelector.onchange =
      this.linkSpan.appendChild (this.normalLinks);
function () {
      this.undelLink = make ('span');
self.engine = self.engineSelector.options[self.engineSelector.selectedIndex].value;
      this.undelLink.className = 'nopopups hotcatlink';
self.text.focus();
      this.undelLink.style.display = 'none';
self.textchange (true, true); // Don't autocomplete, force re-display of list
      lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.restore, this);
};
      lk.appendChild (make (HotCat.links.restore, true)); lk.title = HotCat.tooltips.restore;
this.engineSelector = engineSelector;
      this.undelLink.appendChild (make (' ', true));
}
      this.undelLink.appendChild (lk);
}
      this.linkSpan.appendChild (this.undelLink);
this.list = list;
    },


    makeForm : function () {
function button_label (id, defaultText) {
      var form = make ('form');
var label = null;
      form.method = 'POST'; form.onsubmit = bind (this.accept, this);
if (   onUpload
      this.form = form;
&& typeof UFUI != 'undefined'
      var self = this;
&& typeof UIElements != 'undefined'
      var text = make ('input'); text.type = 'text'; text.size = HotCat.editbox_width;
&& typeof UFUI.getLabel == 'function')
      if (!noSuggestions) {
{
        text.onkeyup =
try {
          function (evt) {
label = UFUI.getLabel (id, true);
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
// Extract the plain text. IE doesn't know that Node.TEXT_NODE === 3
            var key = evt.keyCode || 0;
while (label && label.nodeType != 3) label = label.firstChild;
            if (key === 38 || key === 40 || key === 33 || key === 34) { // Up and down arrows, page up/down
} catch (ex) {
              // In case a browser doesn't generate keypress events for arrow keys...
label = null;
              if (self.keyCount === 0) return self.processKey (evt);
}
            } else {
}
              if (key == 27) { // ESC
if (!label || !label.data) return defaultText;
                if (!self.resetKeySelection ()) {
return label.data;
                  // No undo of key selection: treat ESC as "cancel".
}
                  self.cancel ();
                  return;
                }
              }
              // Also do this for ESC as a workaround for Firefox bug 524360
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27); // BS, DEL, ESC
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
              self.state = CategoryEditor.CHANGE_PENDING;
              window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
            }
            return true;
          };
        text.onkeydown =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            self.lastKey = evt.keyCode || 0;
            self.keyCount = 0;
            // Handle return explicitly, to override the default form submission to be able to check for ctrl
            if (self.lastKey == 13) return self.accept (evt);
            // Inhibit default behavior of ESC (revert to last real input in FF: we do that ourselves)
            return (self.lastKey == 27) ? evtKill(evt) : true;
          };
        // And handle continued pressing of arrow keys
        text.onkeypress = function (evt) {self.keyCount++; return self.processKey (evt);};
      }
      this.text = text;


      this.icon = make ('img');
// Do not use type 'submit'; we cannot detect modifier keys if we do
var OK = make ('input'); OK.type = 'button';
OK.value = button_label ('wpOkUploadLbl', HotCat.messages.ok);
OK.onclick = bind (this.accept, this);
this.ok = OK;


      var list = null;
var cancel = make ('input'); cancel.type = 'button';
      if (!noSuggestions) {
cancel.value = button_label ('wpCancelUploadLbl', HotCat.messages.cancel);
        list = make ('select');
cancel.onclick = bind (this.cancel, this);
        list.onclick    = function (e) { if (self.highlightSuggestion(0)) self.textchange (false, true); };
this.cancelButton = cancel;
        list.ondblclick = function (e) { if (self.highlightSuggestion(0)) self.accept (e); };
        list.onchange = function (e) { self.highlightSuggestion(0); self.text.focus(); };
        list.onkeyup =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            if (evt.keyCode == 27) {
              self.resetKeySelection ();
              self.text.focus();
              window.setTimeout (function () {self.textchange (true);}, HotCat.suggest_delay);
            } else if (evt.keyCode == 13) {
              self.accept (evt);
            }
          };
        if (!HotCat.fixed_search) {
          var engineSelector = make ('select');
          for (var key in suggestionConfigs) {
            if (suggestionConfigs[key].show) {
              var opt = make ('option');
              opt.value = key;
              if (key == this.engine) opt.selected = true;
              opt.appendChild (make (suggestionConfigs[key].name, true));
              engineSelector.appendChild (opt);
            }
          }
          engineSelector.onchange =
            function () {
              self.engine = self.engineSelector.options[self.engineSelector.selectedIndex].value;
              self.text.focus();
              self.textchange (true, true); // Don't autocomplete, force re-display of list
            };
          this.engineSelector = engineSelector;
        }
      }
      this.list = list;


      function button_label (id, defaultText) {
var span = make ('span');
        var label = null;
span.className = 'hotcatinput';
        if (   onUpload
span.style.position = 'relative';
            && typeof (UFUI) != 'undefined'
// FF3.6: add the input field first, then the two absolutely positioned elements. Otherwise, FF3.6 may leave the
            && typeof (UIElements) != 'undefined'
// suggestions and the selector at the right edge of the screen if display of the input field causes a re-layout
            && typeof (UFUI.getLabel) == 'function') {
// moving the form to the front of the next line.
          try {
span.appendChild (text);
            label = UFUI.getLabel (id, true);
            // Extract the plain text. IE doesn't know that Node.TEXT_NODE == 3
            while (label && label.nodeType != 3) label = label.firstChild;
          } catch (ex) {
            label = null;
          }
        }
        if (!label || !label.data) return defaultText;
        return label.data;
      }


      // Do not use type 'submit'; we cannot detect modifier keys if we do
// IE8/IE9: put some text into this span (a0 is nbsp) and make sure it always stays on the
      var OK = make ('input'); OK.type = 'button';
// same line as the input field, otherwise, IE8/9 miscalculates the height of the span and
      OK.value = button_label ('wpOkUploadLbl', HotCat.messages.ok);
// then the engine selector may overlap the input field.
      OK.onclick = bind (this.accept, this);
span.appendChild (make ('\xa0', true));
      this.ok = OK;
span.style.whiteSpace = 'nowrap';


      var cancel = make ('input'); cancel.type = 'button';
if (list) span.appendChild (list);
      cancel.value = button_label ('wpCancelUploadLbl', HotCat.messages.cancel);
if (this.engineSelector) span.appendChild (this.engineSelector);
      cancel.onclick = bind (this.cancel, this);
if (!noSuggestions) span.appendChild (this.icon);
      this.cancelButton = cancel;
span.appendChild (OK);
span.appendChild (cancel);
form.appendChild(span);
form.style.display = 'none';
this.span.appendChild (form);
},


      var span = make ('span');
display : function (evt) {
      span.className = 'hotcatinput';
if (this.isAddCategory && !onUpload) {
      span.style.position = 'relative';
var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
      // FF3.6: add the input field first, then the two absolutely positioned elements. Otherwise, FF3.6 may leave the
}
      // suggestions and the selector at the right edge of the screen if display of the input field causes a re-layout
if (!commitButton && !onUpload) {
      // moving the form to the front of the next line.
for (var i = 0; i < editors.length; i++) {
      span.appendChild (text);
if (editors[i].state != CategoryEditor.UNCHANGED) {
setMultiInput();
break;
}
}
}
if (!this.form) {
this.makeForm ();
}
if (this.list) this.list.style.display = 'none';
if (this.engineSelector) this.engineSelector.style.display = 'none';
this.currentCategory = this.lastSavedCategory;
this.currentExists  = this.lastSavedExists;
this.currentHidden  = this.lastSavedHidden;
this.currentKey      = this.lastSavedKey;
this.icon.src = armorUri(this.currentExists ? HotCat.existsYes : HotCat.existsNo);
this.text.value = this.currentCategory + (this.currentKey !== null ? '|' + this.currentKey : "");
this.originalState = this.state;
this.lastInput    = this.currentCategory;
this.inputExists  = this.currentExists;
this.state        = this.state == CategoryEditor.UNCHANGED ? CategoryEditor.OPEN : CategoryEditor.CHANGE_PENDING;
this.lastSelection = {start: this.currentCategory.length, end: this.currentCategory.length};
this.showsList = false;
// Display the form
if (this.catLink) this.catLink.style.display = 'none';
this.linkSpan.style.display = 'none';
this.form.style.display = 'inline';
this.ok.disabled = false;
// Kill the event before focussing, otherwise IE will kill the onfocus event!
var result = evtKill (evt);
this.text.focus();
this.text.readOnly = false;
checkMultiInput ();
return result;
},


      // IE8/IE9: put some text into this span (a0 is nbsp) and make sure it always stays on the
show : function (evt, engine, readOnly) {
      // same line as the input field, otherwise, IE8/9 miscalculates the height of the span and
var result = this.display (evt);
      // then the engine selector may overlap the input field.
var v = this.lastSavedCategory;
      span.appendChild (make ('\xa0', true));
if (v.length === 0) return result;
      span.style.whiteSpace = 'nowrap';
this.text.readOnly = !!readOnly;
this.engine = engine;
this.textchange (false, true); // do autocompletion, force display of suggestions
forceRedraw ();
return result;
},


      if (list) span.appendChild (list);
open : function (evt) {
      if (this.engineSelector) span.appendChild (this.engineSelector);
return this.show (evt, (this.engine && suggestionConfigs[this.engine].temp) ? HotCat.suggestions : this.engine);
      if (!noSuggestions) span.appendChild (this.icon);
},
      span.appendChild (OK);
      span.appendChild (cancel);
      form.appendChild(span);
      form.style.display = 'none';
      this.span.appendChild (form);
      addEvent (text, 'focus', function () { makeActive(self); });
      // On IE, blur events are asynchronous, and may thus arrive after the element has lost the focus. Since IE
      // can get the selection only while the element is active (has the focus), we may not always get the selection.
      // Therefore, use an IE-specific synchronous event on IE...
      // Don't test for text.selectionStart being defined; FF3.6.4 raises an exception when trying to access that
      // property while the element is not being displayed.
      addEvent (text, (typeof text.onbeforedeactivate != 'undefined' && text.createTextRange) ? 'beforedeactivate' : 'blur', bind (this.saveView, this));  
    },


    display : function (evt) {
down : function (evt) {
      if (this.isAddCategory && !onUpload) {
return this.show (evt, 'subcat', true);
        var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
},
      }
      if (!commitButton && !onUpload) {
        for (var i = 0; i < editors.length; i++) {
          if (editors[i].state != CategoryEditor.UNCHANGED) {
            setMultiInput();
            break;
          }
        }
      }
      if (!this.form) {
        this.makeForm ();
      }
      if (this.list) this.list.style.display = 'none';
      if (this.engineSelector) this.engineSelector.style.display = 'none';
      this.currentCategory = this.lastSavedCategory;
      this.currentExists  = this.lastSavedExists;
      this.currentHidden  = this.lastSavedHidden;
      this.currentKey      = this.lastSavedKey;
      this.icon.src = armorUri(this.currentExists ? HotCat.existsYes : HotCat.existsNo);
      this.text.value = this.currentCategory + (this.currentKey != null ? '|' + this.currentKey : "");
      this.originalState = this.state;
      this.lastInput    = this.currentCategory;
      this.inputExists  = this.currentExists;
      this.state        = this.state == CategoryEditor.UNCHANGED ? CategoryEditor.OPEN : CategoryEditor.CHANGE_PENDING;
      this.lastSelection = {start: this.currentCategory.length, end: this.currentCategory.length};
      this.showsList = false;
      // Display the form
      if (this.catLink) this.catLink.style.display = 'none';
      this.linkSpan.style.display = 'none';
      this.form.style.display = 'inline';
      this.ok.disabled = false;
      // Kill the event before focussing, otherwise IE will kill the onfocus event!
      var result = evtKill (evt);
      this.text.focus();
      this.text.readOnly = false;
      checkMultiInput ();
      return result;
    },


    show : function (evt, engine, readOnly) {
up : function (evt) {
      var result = this.display (evt);
return this.show (evt, 'parentcat');
      var v = this.lastSavedCategory;
},
      if (v.length === 0) return result;
      this.text.readOnly = !!readOnly;
      this.engine = engine;
      this.textchange (false, true); // do autocompletion, force display of suggestions
      forceRedraw ();
      return result;
    },
    open : function (evt) {
      return this.show (evt, (this.engine && suggestionConfigs[this.engine].temp) ? HotCat.suggestions : this.engine);
    },


    down : function (evt) {
cancel : function () {
      return this.show (evt, 'subcat', true);
if (this.isAddCategory && !onUpload) {
    },
this.removeEditor(); // We added a new adder when opening
return;
}
// Close, re-display link
this.inactivate();
this.form.style.display = 'none';
if (this.catLink) this.catLink.style.display = "";
this.linkSpan.style.display = "";
this.state = this.originalState;
this.currentCategory = this.lastSavedCategory;
this.currentKey      = this.lastSavedKey;
this.currentExists  = this.lastSavedExists;
this.currentHidden  = this.lastSavedHidden;
if (this.catLink) {
if (this.currentKey && this.currentKey.length > 0) {
this.catLink.title = this.currentKey;
} else {
this.catLink.title = "";
}
}
if (this.state == CategoryEditor.UNCHANGED) {
if (this.catLink) this.catLink.style.backgroundColor = 'transparent';
} else {
if (!onUpload) {
try {
this.catLink.style.backgroundColor = HotCat.bg_changed;
} catch (ex) {}
}
}
checkMultiInput ();
forceRedraw ();
},


    up : function (evt) {
removeEditor : function () {
      return this.show (evt, 'parentcat');
if (!newDOM) {
    },
var next = this.span.nextSibling;
if (next) next.parentNode.removeChild (next);
}
this.span.parentNode.removeChild (this.span);
for (var i = 0; i < editors.length; i++) {
if (editors[i] == this) {
editors.splice (i, 1);
break;
}
}
checkMultiInput ();
var self = this;
window.setTimeout (function () {delete self;}, 10);
},


    cancel : function () {
rollback : function (evt) {
      if (this.isAddCategory && !onUpload) {
this.undoLink.parentNode.removeChild (this.undoLink);
        this.removeEditor(); // We added a new adder when opening
this.undoLink = null;
        return;
this.currentCategory = this.originalCategory;
      }
this.currentKey = this.originalKey;
      // Close, re-display link
this.currentExists = this.originalExists;
      this.inactivate();
this.currentHidden = this.originalHidden;
      this.form.style.display = 'none';
this.lastSavedCategory = this.originalCategory;
      if (this.catLink) this.catLink.style.display = "";
this.lastSavedKey = this.originalKey;
      this.linkSpan.style.display = "";
this.lastSavedExists = this.originalExists;
      this.state = this.originalState;
this.lastSavedHidden = this.originalHidden;
      this.currentCategory = this.lastSavedCategory;
this.state = CategoryEditor.UNCHANGED;
      this.currentKey      = this.lastSavedKey;
if (!this.currentCategory || this.currentCategory.length === 0) {
      this.currentExists  = this.lastSavedExists;
// It was a newly added category. Remove the whole editor.
      this.currentHidden  = this.lastSavedHidden;
this.removeEditor();
      if (this.catLink) {
} else {
        if (this.currentkey && this.currentKey.length > 0) {
// Redisplay the link...
          this.catLink.title = this.currentKey;
this.catLink.removeChild (this.catLink.firstChild);
        } else {
this.catLink.appendChild (make (this.currentCategory, true));
          this.catLink.title = null;
this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
        }
this.catLink.title = this.currentKey || "";
      }
this.catLink.className = this.currentExists ? "" : 'new';
      if (this.state == CategoryEditor.UNCHANGED) {
this.catLink.style.backgroundColor = 'transparent';
        if (this.catLink) this.catLink.style.backgroundColor = 'transparent';
if (this.upDownLinks) this.upDownLinks.style.display = this.currentExists ? "" : 'none';
      } else {
checkMultiInput ();
        if (!onUpload) {
}
          try {
return evtKill (evt);
            this.catLink.style.backgroundColor = HotCat.bg_changed;
},
          } catch (ex) {}
        }
      }
      checkMultiInput ();
      forceRedraw ();
    },


    removeEditor : function () {
inactivate : function () {
      if (!newDOM) {
if (this.list) this.list.style.display = 'none';
        var next = this.span.nextSibling;
if (this.engineSelector) this.engineSelector.style.display = 'none';
        if (next) next.parentNode.removeChild (next);
this.is_active = false;
      }
},
      this.span.parentNode.removeChild (this.span);
      for (var i = 0; i < editors.length; i++) {
        if (editors[i] == this) {
          editors.splice (i, 1);
          break;
        }
      }
      checkMultiInput ();
      var self = this;
      window.setTimeout (function () {delete self;}, 10);
    },


    rollback : function (evt) {
acceptCheck : function (dontCheck) {
      this.undoLink.parentNode.removeChild (this.undoLink);
this.sanitizeInput ();
      this.undoLink = null;
var value = this.text.value.split('|');
      this.currentCategory = this.originalCategory;
var key  = null;
      this.currentKey = this.originalKey;
if (value.length > 1) key = value[1];
      this.currentExists = this.originalExists;
var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
      this.currentHidden = this.originalHidden;
if (HotCat.capitalizePageNames) v = capitalize (v);
      this.lastSavedCategory = this.originalCategory;
this.lastInput = v;
      this.lastSavedKey = this.originalKey;
v = replaceShortcuts(v, HotCat.shortcuts);
      this.lastSavedExists = this.originalExists;
if (v.length === 0) {
      this.lastSavedHidden = this.originalHidden;
this.cancel ();
      this.state = CategoryEditor.UNCHANGED;
return false;
      if (!this.currentCategory || this.currentCategory.length === 0) {
}
        // It was a newly added category. Remove the whole editor.
if (!dontCheck
        this.removeEditor();
&& (   conf.wgNamespaceNumber === 14 && v == conf.wgTitle
      } else {
|| HotCat.blacklist && HotCat.blacklist.test(v))
        // Redisplay the link...
  )
        this.catLink.removeChild (this.catLink.firstChild);
{
        this.catLink.appendChild (make (this.currentCategory, true));
this.cancel ();
        this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
return false;
        this.catLink.title = this.currentKey;
}
        this.catLink.className = this.currentExists ? "" : 'new';
this.currentCategory = v;
        this.catLink.style.backgroundColor = 'transparent';
this.currentKey = key;
        if (this.upDownLinks) this.upDownLinks.style.display = this.currentExists ? "" : 'none';
this.currentExists = this.inputExists;
        checkMultiInput ();
return true;
      }
},
      return evtKill (evt);
    },


    inactivate : function () {
accept : function (evt) {
      if (this.list) this.list.style.display = 'none';
this.noCommit = (evtKeys (evt) & 1) !== 0;
      if (this.engineSelector) this.engineSelector.style.display = 'none';
var result = evtKill (evt);
      this.is_active = false;
if (this.acceptCheck ()) {
    },
var toResolve = [this];
var original  = this.currentCategory;
resolveMulti (
toResolve
,function (resolved) {
if (resolved[0].dab) {
showDab (resolved[0]);
} else {
if (resolved[0].acceptCheck(true)) {
resolved[0].commit (
(resolved[0].currentCategory != original)
? HotCat.messages.cat_resolved.replace (/\$1/g, original)
: null
);
}
}
}
);
}
return result;
},


    acceptCheck : function (dontCheck) {
close : function () {
      this.sanitizeInput ();
if (!this.catLink) {
      var value = this.text.value.split('|');
// Create a catLink
      var key  = null;
this.catLink = make ('a');
      if (value.length > 1) key = value[1];
this.catLink.appendChild (make ('foo', true));
      var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
this.catLink.style.display = 'none';
      if (HotCat.capitalizePageNames) v = capitalize (v);
this.span.insertBefore (this.catLink, this.span.firstChild.nextSibling);
      this.lastInput = v;
}
      if (v.length === 0) {
this.catLink.removeChild (this.catLink.firstChild);
        this.cancel ();
this.catLink.appendChild (make (this.currentCategory, true));
        return false;
this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
      }
this.catLink.className = this.currentExists ? "" : 'new';
      if (!dontCheck
this.lastSavedCategory = this.currentCategory;
          && (   wgNamespaceNumber == 14 && v == wgTitle
this.lastSavedKey      = this.currentKey;
              || HotCat.blacklist != null && HotCat.blacklist.test(v))
this.lastSavedExists  = this.currentExists;
        ) {
this.lastSavedHidden  = this.currentHidden;
        this.cancel ();
// Close form and redisplay category
        return false;
this.inactivate();
      }
this.form.style.display = 'none';
      this.currentCategory = v;
this.catLink.title = this.currentKey || "";
      this.currentKey = key;
this.catLink.style.display = "";
      this.currentExists = this.inputExists;
if (this.isAddCategory) {
      return true;
if (onUpload) {
    },
var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
}
this.isAddCategory = false;
this.linkSpan.parentNode.removeChild (this.linkSpan);
this.makeLinkSpan ();
this.span.appendChild (this.linkSpan);
}
if (!this.undoLink) {
// Append an undo link.
var span = make ('span');
var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.rollback, this);
lk.appendChild (make (HotCat.links.undo, true)); lk.title = HotCat.tooltips.undo;
span.appendChild (make (' ', true));
span.appendChild (lk);
this.normalLinks.appendChild (span);
this.undoLink = span;
if (!onUpload) {
try {
this.catLink.style.backgroundColor = HotCat.bg_changed;
} catch (ex) {}
}
}
if (this.upDownLinks) this.upDownLinks.style.display = this.lastSavedExists ? "" : 'none';
this.linkSpan.style.display = "";
this.state = CategoryEditor.CHANGED;
checkMultiInput ();
forceRedraw ();
},


    accept : function (evt) {
commit : function (comment) {
      this.noCommit = (evtKeys (evt) & 1) != 0;
// Check again to catch problem cases after redirect resolution
      var result = evtKill (evt);
if (  (  this.currentCategory == this.originalCategory
      if (this.acceptCheck ()) {
&& (this.currentKey == this.originalKey
        var toResolve = [this];
|| this.currentKey === null && this.originalKey.length === 0
        var original  = this.currentCategory;
  )
        resolveMulti (
  )
            toResolve
|| conf.wgNamespaceNumber == 14 && this.currentCategory == conf.wgTitle
          , function (resolved) {
|| HotCat.blacklist && HotCat.blacklist.test (this.currentCategory)
              if (resolved[0].dab) {
  )
                showDab (resolved[0]);
{
              } else {
this.cancel ();
                if (resolved[0].acceptCheck(true)) {
return;
                  resolved[0].commit (
}
                    (resolved[0].currentCategory != original)
if (commitButton || onUpload) {
                      ? HotCat.messages.cat_resolved.replace (/\$1/g, original)
this.close ();
                      : null
} else {
                  );
this.close ();
                }
var self = this;
              }
initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {alert (msg);});
            }
}
        );
},
      }
      return result;
    },


    close : function () {
remove : function (evt) {
      if (!this.catLink) {
this.doRemove (evtKeys (evt) & 1);
        // Create a catLink
return evtKill (evt);
        this.catLink = make ('a');
},
        this.catLink.appendChild (make ('foo', true));
        this.catLink.style.display = 'none';
        this.span.insertBefore (this.catLink, this.span.firstChild.nextSibling);
      }
      this.catLink.removeChild (this.catLink.firstChild);
      this.catLink.appendChild (make (this.currentCategory, true));
      this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
      this.catLink.title = "";
      this.catLink.className = this.currentExists ? "" : 'new';
      this.lastSavedCategory = this.currentCategory;
      this.lastSavedKey      = this.currentKey;
      this.lastSavedExists  = this.currentExists;
      this.lastSavedHidden  = this.currentHidden;
      // Close form and redisplay category
      this.inactivate();
      this.form.style.display = 'none';
      this.catLink.title = this.currentKey;
      this.catLink.style.display = "";
      if (this.isAddCategory) {
        if (onUpload) {
          var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
        }
        this.isAddCategory = false;
        this.linkSpan.parentNode.removeChild (this.linkSpan);
        this.makeLinkSpan ();
        this.span.appendChild (this.linkSpan);
      }
      if (!this.undoLink) {
        // Append an undo link.
        var span = make ('span');
        var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.rollback, this);
        lk.appendChild (make (HotCat.links.undo, true)); lk.title = HotCat.tooltips.undo;
        span.appendChild (make (' ', true));
        span.appendChild (lk);
        this.normalLinks.appendChild (span);
        this.undoLink = span;
        if (!onUpload) {
          try {
            this.catLink.style.backgroundColor = HotCat.bg_changed;
          } catch (ex) {}
        }
      }
      if (this.upDownLinks) this.upDownLinks.style.display = this.lastSavedExists ? "" : 'none';
      this.linkSpan.style.display = "";
      this.state = CategoryEditor.CHANGED;
      checkMultiInput ();
      forceRedraw ();
    },


    commit : function (comment) {
doRemove : function (noCommit) {
      // Check again to catch problem cases after redirect resolution
if (this.isAddCategory) { // Empty input on adding a new category
      if (   (  this.currentCategory == this.originalCategory
this.cancel ();
              && (this.currentKey == this.originalKey
return;
                  || this.currentKey === null && this.originalKey.length === 0
}
                )
if (!commitButton && !onUpload) {
            )
for (var i = 0; i < editors.length; i++) {
          || wgNamespaceNumber == 14 && this.currentCategory == wgTitle
if (editors[i].state != CategoryEditor.UNCHANGED) {
          || HotCat.blacklist != null && HotCat.blacklist.test (this.currentCategory)
setMultiInput();
        )
break;
      {
}
        this.cancel ();
}
        return;
}
      }
if (commitButton) {
      if (commitButton || onUpload) {
this.catLink.title = "";
        this.close ();
this.catLink.style.cssText += '; text-decoration : line-through !important;';
      } else {
try {
        this.close ();
this.catLink.style.backgroundColor = HotCat.bg_changed;
        var self = this;
} catch (ex) {}
        initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {alert (msg);});
this.originalState = this.state;
      }
this.state = CategoryEditor.DELETED;
    },
this.normalLinks.style.display = 'none';
this.undelLink.style.display = "";
checkMultiInput ();
} else {
if (onUpload) {
// Remove this editor completely
this.removeEditor ();
} else {
this.originalState = this.state;
this.state = CategoryEditor.DELETED;
this.noCommit = noCommit || HotCat.del_needs_diff;
var self = this;
initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {self.state = self.originalState; alert (msg);});
}
}
},


    remove : function (evt) {
restore : function (evt) {
      this.doRemove (evtKeys (evt) & 1);
// Can occur only if we do have a commit button and are not on the upload form
      return evtKill (evt);
this.catLink.title = this.currentKey || "";
    },
this.catLink.style.textDecoration = "";
this.state = this.originalState;
if (this.state == CategoryEditor.UNCHANGED) {
this.catLink.style.backgroundColor = 'transparent';
} else {
try {
this.catLink.style.backgroundColor = HotCat.bg_changed;
} catch (ex) {}
}
this.normalLinks.style.display = "";
this.undelLink.style.display = 'none';
checkMultiInput ();
return evtKill (evt);
},


    doRemove : function (noCommit) {
// Internal operations
      if (this.isAddCategory) { // Empty input on adding a new category
        this.cancel ();
        return;
      }
      if (!commitButton && !onUpload) {
        for (var i = 0; i < editors.length; i++) {
          if (editors[i].state != CategoryEditor.UNCHANGED) {
            setMultiInput();
            break;
          }
        }
      }
      if (commitButton) {
        this.catLink.title = "";
        this.catLink.style.cssText += '; text-decoration : line-through !important;';
        try {
          this.catLink.style.backgroundColor = HotCat.bg_changed;
        } catch (ex) {}
        this.originalState = this.state;
        this.state = CategoryEditor.DELETED;
        this.normalLinks.style.display = 'none';
        this.undelLink.style.display = "";
        checkMultiInput ();
      } else {
        if (onUpload) {
          // Remove this editor completely
          this.removeEditor ();
        } else {
          this.originalState = this.state;
          this.state = CategoryEditor.DELETED;
          this.noCommit = noCommit || HotCat.del_needs_diff;
          var self = this;
          initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {self.state = self.originalState; alert (msg);});
        }
      }
    },


    restore : function (evt) {
selectEngine : function (engineName) {
      // Can occur only if we do have a commit button and are not on the upload form
if (!this.engineSelector) return;
      this.catLink.title = this.currentKey;
for (var i = 0; i < this.engineSelector.options.length; i++) {
      this.catLink.style.textDecoration = "";
this.engineSelector.options[i].selected = this.engineSelector.options[i].value == engineName;
      this.state = this.originalState;
}
      if (this.state == CategoryEditor.UNCHANGED) {
},
        this.catLink.style.backgroundColor = 'transparent';
      } else {
        try {
          this.catLink.style.backgroundColor = HotCat.bg_changed;
        } catch (ex) {}
      }
      this.normalLinks.style.display = "";
      this.undelLink.style.display = 'none';
      checkMultiInput ();
      return evtKill (evt);
    },


    // Internal operations
sanitizeInput : function () {
var v = this.text.value || "";
v = v.replace(/^(\s|_)+/, ""); // Trim leading blanks and underscores
var re = new RegExp ('^(' + HotCat.category_regexp + '):');
if (re.test (v)) {
v = v.substring (v.indexOf (':') + 1).replace(/^(\s|_)+/, "");
}
if (HotCat.capitalizePageNames) v = capitalize (v);
// Only update the input field if there is a difference. IE8 appears to reset the selection
// and place the cursor at the front upon reset, which makes our autocompletetion become a
// nuisance. FF and IE6 don't seem to have this problem.
if (this.text.value !== null && this.text.value != v)
this.text.value = v;
},


    selectEngine : function (engineName) {
makeCall : function (url, callbackObj, engine, queryKey, cleanKey) {
      if (!this.engineSelector) return;
var cb = callbackObj;
      for (var i = 0; i < this.engineSelector.options.length; i++) {
var e  = engine;
        this.engineSelector.options[i].selected = this.engineSelector.options[i].value == engineName;
var = queryKey;
      }
var z  = cleanKey;
    },
var thisObj = this;


    sanitizeInput : function () {
function done () {
      var v = this.text.value || "";
cb.callsMade++;
      v = v.replace(/^(\s|_)+/, ""); // Trim leading blanks and underscores
if (cb.callsMade === cb.nofCalls) {
      var re = new RegExp ('^(' + HotCat.category_regexp + '):');
if (cb.exists) cb.allTitles.exists = true;
      if (re.test (v)) v = v.substring (v.indexOf (':') + 1);
if (cb.normalized) cb.allTitles.normalized = cb.normalized;
      if (HotCat.capitalizePageNames) v = capitalize (v);
if (!cb.dontCache && !suggestionConfigs[cb.engineName].cache[z]) {
      // Only update the input field if there is a difference. IE8 appears to reset the selection
suggestionConfigs[cb.engineName].cache[z] = cb.allTitles;
      // and place the cursor at the front upon reset, which makes our autocompletetion become a
}
      // nuisance. FF and IE6 don't seem to have this problem.
thisObj.text.readOnly = false;
      if (this.text.value != null && this.text.value != v)
if (!cb.cancelled) thisObj.showSuggestions (cb.allTitles, cb.noCompletion, v, cb.engineName);
        this.text.value = v;
if (cb === thisObj.callbackObj) thisObj.callbackObj = null;
    },
delete cb;
}
}


    makeCall : function (url, callbackObj, engine, queryKey, cleanKey) {
getJSON ({
      var cb = callbackObj;
  uri : url
      var e  = engine;
,success : function (json) {
      var v  = queryKey;
var titles = e.handler (json, z);
      var z  = cleanKey;
if (titles && titles.length > 0) {
      var thisObj = this;
if (cb.allTitles === null) {
cb.allTitles = titles;
} else {
cb.allTitles = cb.allTitles.concat (titles);
}
if (titles.exists) cb.exists = true;
if (titles.normalized) cb.normalized = titles.normalized;
}
done();
  }
,error : function (req) {if (!req) noSuggestions = true; cb.dontCache = true; done(); }
});
},


      function done () {
callbackObj : null,
        cb.callsMade++;
        if (cb.callsMade === cb.nofCalls) {
          if (!cb.dontCache && !suggestionConfigs[cb.engineName].cache[z]) {
            suggestionConfigs[cb.engineName].cache[z] = cb.allTitles;
          }
          thisObj.text.readOnly = false;
          if (!cb.cancelled) thisObj.showSuggestions (cb.allTitles, cb.noCompletion, v, cb.engineName);
          if (cb === thisObj.callbackObj) thisObj.callbackObj = null;
          delete cb;
        }
      }


      getJSON ({
textchange : function (dont_autocomplete, force) {
        uri : url
// Hide all other lists
      ,success : function (json) {
makeActive (this);
          var titles = e.handler (json, z);
// Get input value, omit sort key, if any
          if (titles && titles.length > 0) {
this.sanitizeInput ();
            if (cb.allTitles === null) {
var v = this.text.value;
              cb.allTitles = titles;
// Disregard anything after a pipe.
            } else {
var pipe = v.indexOf ('|');
              cb.allTitles = cb.allTitles.concat (titles);
if (pipe >= 0) {
            }
this.currentKey = v.substring (pipe+1);
          }
v = v.substring (0, pipe);
          done();
} else {
        }
this.currentKey = null;
      ,error : function (req) {if (!req) noSuggestions = true; cb.dontCache = true; done(); }
}
      });          
if (this.lastInput == v && !force) return; // No change
    },
if (this.lastInput != v) checkMultiInput ();
this.lastInput = v;
this.lastRealInput = v;


    callbackObj : null,
// Mark blacklisted inputs.
this.ok.disabled = v.length > 0 && HotCat.blacklist && HotCat.blacklist.test (v);


    textchange : function (dont_autocomplete, force) {
if (noSuggestions) {
      // Hide all other lists
// No Ajax: just make sure the list is hidden
      makeActive (this);
if (this.list) this.list.style.display = 'none';
      // Get input value, omit sort key, if any
if (this.engineSelector) this.engineSelector.style.display = 'none';
      this.sanitizeInput ();
if (this.icon) this.icon.style.display = 'none';
      var v = this.text.value;
return;
      // Disregard anything after a pipe.
}
      var pipe = v.indexOf ('|');
      if (pipe >= 0) {
        this.currentKey = v.substring (pipe+1);
        v = v.substring (0, pipe);
      } else {
        this.currentKey = null;
      }
      if (this.lastInput == v && !force) return; // No change
      if (this.lastInput != v) checkMultiInput ();
      this.lastInput = v;
      this.lastRealInput = v;


      // Mark blacklisted inputs.
if (v.length === 0) { this.showSuggestions([]); return; }
      this.ok.disabled = v.length > 0 && HotCat.blacklist != null && HotCat.blacklist.test (v);
var cleanKey = v.replace(/[\u200E\u200F\u202A-\u202E]/g, "").replace(wikiTextBlankRE, ' ');
cleanKey = replaceShortcuts(cleanKey, HotCat.shortcuts);
cleanKey = cleanKey.replace(/^\s+|\s+$/g, '');
if (cleanKey.length === 0) { this.showSuggestions([]); return; }


      if (noSuggestions) {
if (this.callbackObj) this.callbackObj.cancelled = true;
        // No Ajax: just make sure the list is hidden
var engineName  = suggestionConfigs[this.engine] ? this.engine : 'combined';
        if (this.list) this.list.style.display = 'none';
        if (this.engineSelector) this.engineSelector.style.display = 'none';
        if (this.icon) this.icon.style.display = 'none';
        return;
      }


      if (v.length === 0) { this.showSuggestions([]); return; }
dont_autocomplete = dont_autocomplete || suggestionConfigs[engineName].noCompletion;
      if (this.callbackObj) this.callbackObj.cancelled = true;
if (suggestionConfigs[engineName].cache[cleanKey]) {
      var engineName  = suggestionConfigs[this.engine] ? this.engine : 'combined';
this.showSuggestions (suggestionConfigs[engineName].cache[cleanKey], dont_autocomplete, v, engineName);
return;
}


      var cleanKey = v.replace(/[\u200E\u200F\u202A-\u202E]/g, "")
var engines = suggestionConfigs[engineName].engines;
                      .replace(wikiTextBlankRE, ' ');
this.callbackObj =
      dont_autocomplete = dont_autocomplete || suggestionConfigs[engineName].noCompletion;
{allTitles: null, callsMade: 0, nofCalls: engines.length, noCompletion: dont_autocomplete, engineName: engineName};
      if (suggestionConfigs[engineName].cache[cleanKey]) {
this.makeCalls (engines, this.callbackObj, v, cleanKey);
        this.showSuggestions (suggestionConfigs[engineName].cache[cleanKey], dont_autocomplete, v, engineName);
},
        return;
      }


      var engines = suggestionConfigs[engineName].engines;
makeCalls : function (engines, cb, v, cleanKey) {
      this.callbackObj =
for (var j = 0; j < engines.length; j++) {
        {allTitles: null, callsMade: 0, nofCalls: engines.length, noCompletion: dont_autocomplete, engineName: engineName};
var engine = suggestionEngines[engines[j]];
      this.makeCalls (engines, this.callbackObj, v, cleanKey);
var url = conf.wgServer + conf.wgScriptPath + engine.uri.replace (/\$1/g, encodeURIComponent (cleanKey));
    },
this.makeCall (url, cb, engine, v, cleanKey);
}
},


    makeCalls : function (engines, cb, v, cleanKey) {
showSuggestions : function (titles, dontAutocomplete, queryKey, engineName) {
      for (var j = 0; j < engines.length; j++) {
this.text.readOnly = false;
        var engine = suggestionEngines[engines[j]];
this.dab = null;
        var url = wgServer + wgScriptPath + engine.uri.replace (/\$1/g, encodeURIComponent (cleanKey));
this.showsList = false;
        this.makeCall (url, cb, engine, v, cleanKey);
if (!this.list) return;
      }
if (noSuggestions) {
    },
if (this.list) this.list.style.display = 'none';
if (this.engineSelector) this.engineSelector.style.display = 'none';
if (this.icon) this.icon.style.display = 'none';
this.inputExists = true; // Default...
return;
}
this.engineName = engineName;
if (engineName) {
if (!this.engineSelector) this.engineName = null;
} else {
if (this.engineSelector) this.engineSelector.style.display = 'none';
}
if (queryKey) {
if (this.lastInput.indexOf (queryKey) !== 0) return;
if (this.lastQuery && this.lastInput.indexOf (this.lastQuery) === 0 && this.lastQuery.length > queryKey.length)
return;
}
this.lastQuery = queryKey;


    showSuggestions : function (titles, dontAutocomplete, queryKey, engineName) {
// Get current input text
      this.text.readOnly = false;
var v = this.text.value.split('|');
      this.dab = null;
var key = v.length > 1 ? '|' + v[1] : "";
      this.showsList = false;
v = (HotCat.capitalizePageNames ? capitalize (v[0]) : v[0]);
      if (!this.list) return;
var vNormalized = v;
      if (noSuggestions) {
var knownToExist = titles && titles.exists;
        if (this.list) this.list.style.display = 'none';
var i;
        if (this.engineSelector) this.engineSelector.style.display = 'none';
if (titles) {
        if (this.icon) this.icon.style.display = 'none';
if (titles.normalized && v.indexOf(queryKey) === 0) {
        this.inputExists = true; // Default...
// We got back a different normalization than what is in the input field
        return;
vNormalized = titles.normalized + v.substring(queryKey.length);
      }    
}
      this.engineName = engineName;
var vLow = vNormalized.toLowerCase ();
      if (engineName) {
// Strip blacklisted categories
        if (!this.engineSelector) this.engineName = null;
if (HotCat.blacklist) {
      } else {
for (i = 0; i < titles.length; i++) {
        if (this.engineSelector) this.engineSelector.style.display = 'none';
if (HotCat.blacklist.test (titles[i])) {
      }
titles.splice(i, 1);
      if (queryKey) {
i--;
        if (this.lastInput.indexOf (queryKey) != 0) return;
}
        if (this.lastQuery && this.lastInput.indexOf (this.lastQuery) === 0 && this.lastQuery.length > queryKey.length)
}
          return;
}
      }
titles.sort (
      this.lastQuery = queryKey;
function (a, b) {
if (a == b) return 0;
if (a.indexOf (b) === 0) return 1; // a begins with b: a > b
if (b.indexOf (a) === 0) return -1; // b begins with a: a < b
// Opensearch may return stuff not beginning with the search prefix!
var prefixMatchA = (a.indexOf (vNormalized) === 0 ? 1 : 0);
var prefixMatchB = (b.indexOf (vNormalized) === 0 ? 1 : 0);
if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
// Case-insensitive prefix match!
var aLow = a.toLowerCase(), bLow = b.toLowerCase();
prefixMatchA = (aLow.indexOf (vLow) === 0 ? 1 : 0);
prefixMatchB = (bLow.indexOf (vLow) === 0 ? 1 : 0);
if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
if (a < b) return -1;
if (b < a) return 1;
return 0;
}
);
// Remove duplicates and self-references
for (i = 0; i < titles.length; i++) {
if (   i+1 < titles.length && titles[i] == titles[i+1]
|| conf.wgNamespaceNumber == 14 && titles[i] == conf.wgTitle
  )
{
titles.splice (i, 1);
i--;
}
}
}
if (!titles || titles.length === 0) {
if (this.list) this.list.style.display = 'none';
if (this.engineSelector) this.engineSelector.style.display = 'none';
if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
if (this.icon) this.icon.src = armorUri(HotCat.existsNo);
this.inputExists = false;
}
return;
}


      // Get current input text
var firstTitle = titles[0];
      var v = this.text.value.split('|');
var completed = this.autoComplete (firstTitle, v, vNormalized, key, dontAutocomplete);
      var key = v.length > 1 ? '|' + v[1] : "";
var existing = completed || knownToExist || firstTitle == replaceShortcuts(v, HotCat.shortcuts);
      v = (HotCat.capitalizePageNames ? capitalize (v[0]) : v[0]);
if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
this.icon.src = armorUri(existing ? HotCat.existsYes : HotCat.existsNo);
this.inputExists = existing;
}
if (completed) {
this.lastInput = firstTitle;
if (titles.length === 1) {
this.list.style.display = 'none';
if (this.engineSelector) this.engineSelector.style.display = 'none';
return;
}
}
// (Re-)fill the list
while (this.list.firstChild) this.list.removeChild (this.list.firstChild);
for (i = 0 ; i < titles.length ; i++) {
var opt = make ('option') ;
opt.appendChild (make (titles[i], true));
opt.selected = completed && (i === 0);
this.list.appendChild (opt);
}
this.displayList();
},


      if (titles) {
displayList : function () {
        var vLow = v.toLowerCase ();
this.showsList = true;
        // Strip blacklisted categories
if (!this.is_active) {
        if (HotCat.blacklist != null) {
this.list.style.display = 'none';
          for (var i = 0; i < titles.length; i++) {
if (this.engineSelector) this.engineSelector.style.display = 'none';
            if (HotCat.blacklist.test (titles[i])) {
return;
              titles.splice(i, 1);
}
              i--;
var nofItems = (this.list.options.length > HotCat.list_size ? HotCat.list_size : this.list.options.length);
            }
if (nofItems <= 1) nofItems = 2;
          }
this.list.size = nofItems;
        }
this.list.style.align    = is_rtl ? 'right' : 'left';
        titles.sort (
this.list.style.zIndex  = 5;
          function (a, b) {
this.list.style.position = 'absolute';
            if (a.indexOf (b) === 0) return 1; // a begins with b: a > b
// Compute initial list position. First the height.
            if (b.indexOf (a) === 0) return -1; // b begins with a: a < b
var anchor = is_rtl ? 'right' : 'left';
            // Opensearch may return stuff not beginning with the search prefix!
var listh = 0;
            var prefixMatchA = (a.indexOf (v) === 0 ? 1 : 0);
if (this.list.style.display == 'none') {
            var prefixMatchB = (b.indexOf (v) === 0 ? 1 : 0);
// Off-screen display to get the height
            if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
this.list.style.top = this.text.offsetTop + 'px';
            // Case-insensitive prefix match!
this.list.style[anchor] = '-10000px';
            var aLow = a.toLowerCase(), bLow = b.toLowerCase();
this.list.style.display = "";
            prefixMatchA = (aLow.indexOf (vLow) === 0 ? 1 : 0);
listh = this.list.offsetHeight;
            prefixMatchB = (bLow.indexOf (vLow) === 0 ? 1 : 0);
this.list.style.display = 'none';
            if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
} else {
            if (a < b) return -1;
listh = this.list.offsetHeight;
            if (b < a) return 1;
}
            return 0;
// Approximate calculation of maximum list size
          }
var maxListHeight = listh;
        );
if (nofItems < HotCat.list_size) maxListHeight = (listh / nofItems) * HotCat.list_size;
        // Remove duplicates and self-references
        for (var i = 0; i < titles.length; i++) {
          if (  i+1 < titles.length && titles[i] == titles[i+1]
              || wgNamespaceNumber == 14 && titles[i] == wgTitle
            )
          {
            titles.splice (i, 1);
            i--;
          }
        }
      }
      if (!titles || titles.length === 0) {
        if (this.list) this.list.style.display = 'none';
        if (this.engineSelector) this.engineSelector.style.display = 'none';
        if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
          if (this.icon) this.icon.src = armorUri(HotCat.existsNo);
          this.inputExists = false;
        }
        return;
      }


      var firstTitle = titles[0];
function viewport (what) {
      var completed = this.autoComplete (firstTitle, v, key, dontAutocomplete);
if (is_webkit && !document.evaluate)
      if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
return window['inner' + what]; // Safari < 3.0
        this.icon.src = armorUri(completed ? HotCat.existsYes : HotCat.existsNo);
var s = 'client' + what;
        this.inputExists = completed;
if (window.opera) return document.body[s];
      }
return (document.documentElement ? document.documentElement[s] : 0)
      if (completed) {
|| document.body[s] || 0;
        this.lastInput = firstTitle;
}
        if (titles.length === 1) {
function scroll_offset (what) {
          this.list.style.display = 'none';
var s = 'scroll' + what;
          if (this.engineSelector) this.engineSelector.style.display = 'none';
var result = (document.documentElement ? document.documentElement[s] : 0)
          return;
|| document.body[s] || 0;
        }
if (is_rtl && what == 'Left') {
      }
// RTL inconsistencies.
      // (Re-)fill the list
// FF: 0 at the far right, then increasingly negative values.
      while (this.list.firstChild) this.list.removeChild (this.list.firstChild);
// IE >= 8: 0 at the far right, then increasingly positive values.
      for (var i = 0 ; i < titles.length ; i++) {
// Webkit: scrollWidth - clientWidth at the far right, then down to zero.
        var opt = make ('option') ;
// IE 7: like webkit; IE6: disabled in RTL anyway since too many problems.
        opt.appendChild (make (titles[i], true));
// Opera: don't know...
        opt.selected = completed && (i === 0);
if (result < 0) result = - result;
        this.list.appendChild (opt);
if (!is_webkit && !is_ie_lt8) {
      }
result = scroll_offset('Width') - viewport('Width') - result;
      this.displayList();
}
    },
// Now all have webkit behavior, i.e. zero if at the leftmost edge.
}
return result;
}
function position (node) {
// Stripped-down simplified position function. It's good enough for our purposes.
if (node.getBoundingClientRect) {
var box    = node.getBoundingClientRect ();
return { x : Math.round (box.left + scroll_offset ('Left'))
,y : Math.round (box.top + scroll_offset ('Top'))
  };
}
var t = 0, l = 0;
do {
t = t + (node.offsetTop  || 0);
l = l + (node.offsetLeft || 0);
node = node.offsetParent;
} while (node);
return {x : l, y : t};
}


    displayList : function () {
var textPos = position (this.text);
      this.showsList = true;
var nl = 0;
      if (!this.is_active) {
var nt = 0;
        this.list.style.display = 'none';
var offset = 0;
        if (this.engineSelector) this.engineSelector.style.display = 'none';
// Opera 9.5 somehow has offsetWidth = 0 here?? Use the next best value...
        return;
var textBoxWidth = this.text.offsetWidth || this.text.clientWidth;
      }
if (this.engineName) {
      var nofItems = (this.list.options.length > HotCat.list_size ? HotCat.list_size : this.list.options.length);
this.engineSelector.style.zIndex = 5;
      if (nofItems <= 1) nofItems = 2;
this.engineSelector.style.position = 'absolute';
      this.list.size = nofItems;
this.engineSelector.style.width = textBoxWidth + 'px';
      this.list.style.align    = is_rtl ? 'right' : 'left';
// Figure out the height of this selector: display it off-screen, then hide it again.
      this.list.style.zIndex  = 5;
if (this.engineSelector.style.display == 'none') {
      this.list.style.position = 'absolute';
this.engineSelector.style[anchor] = '-10000px';
      // Compute initial list position. First the height.
this.engineSelector.style.top = '0px';
      var listh = 0;
this.engineSelector.style.display = "";
      if (this.list.style.display == 'none') {
offset = this.engineSelector.offsetHeight;
        // Off-screen display to get the height
this.engineSelector.style.display = 'none';
        this.list.style.top = this.text.offsetTop + 'px';
} else {
        this.list.style.left = '-10000px';
offset = this.engineSelector.offsetHeight;
        this.list.style.display = "";
}
        listh = this.list.offsetHeight;
this.engineSelector.style[anchor]  = nl + 'px';
        this.list.style.display = 'none';
}
      } else {
if (textPos.y < maxListHeight + offset + 1) {
        listh = this.list.offsetHeight;
// The list might extend beyond the upper border of the page. Let's avoid that by placing it
      }
// below the input text field.
      // Approximate calculation of maximum list size
nt = this.text.offsetHeight + offset + 1;
      var maxListHeight = listh;
if (this.engineName) this.engineSelector.style.top = this.text.offsetHeight + 'px';
      if (nofItems < HotCat.list_size) maxListHeight = (listh / nofItems) * HotCat.list_size;
} else {
nt = - listh - offset - 1;
if (this.engineName) this.engineSelector.style.top = - (offset + 1) + 'px';
}
this.list.style.top = nt + 'px';
this.list.style.width = ""; // No fixed width (yet)
this.list.style[anchor] = nl + 'px';
if (this.engineName) {
this.selectEngine (this.engineName);
this.engineSelector.style.display = "";
}
this.list.style.display = 'block';
// Set the width of the list
if (this.list.offsetWidth < textBoxWidth ) {
this.list.style.width = textBoxWidth + 'px';
return;
}
// If the list is wider than the textbox: make sure it fits horizontally into the browser window
var scroll = scroll_offset ('Left');
var view_w = viewport ('Width');
var w      = this.list.offsetWidth;
var l_pos  = position (this.list);
var left  = l_pos.x;
var right  = left + w;
if (left < scroll || right > scroll + view_w) {
if (w > view_w) {
w = view_w;
this.list.style.width = w + 'px';
if (is_rtl) {
left = right - w;
} else {
right = left + w;
}
}
var relative_offset = 0;
if (left < scroll) {
relative_offset = scroll - left;
} else if (right > scroll + view_w) {
relative_offset = - (right - scroll - view_w);
}
if (is_rtl) relative_offset = - relative_offset;
if (relative_offset !== 0) {
this.list.style[anchor] = (nl + relative_offset) + 'px';
}
}
},


      function scroll_offset (what) {
autoComplete : function (newVal, actVal, normalizedActVal, key, dontModify) {
        var s = 'scroll' + what;
if (newVal == actVal) return true;
        return (document.documentElement ? document.documentElement[s] : 0)
if (dontModify || this.ime || !this.canSelect()) return false;
              || document.body[s] || 0;
// If we can't select properly or an IME composition is ongoing, autocompletion would be a major annoyance to the user.
      }
if (newVal.indexOf (actVal) !== 0) {
      function viewport (what) {
// Maybe it'll work with the normalized value (NFC)?
        if (is_webkit && !document.evaluate)
if (normalizedActVal && newVal.indexOf(normalizedActVal) === 0) {
          return window['inner' + what]; // Safari < 3.0
if (this.lastRealInput == actVal) this.lastRealInput = normalizedActVal;
        var s = 'client' + what;
actVal = normalizedActVal;
        if (window.opera) return document.body[s];
} else {
        return (document.documentElement ? document.documentElement[s] : 0)
return false;
              || document.body[s] || 0;
}
      }
}
      function position (node) {
// Actual input is a prefix of the new text. Fill in new text, selecting the newly added suffix
        // Stripped-down simplified position function. It's good enough for our purposes.
// such that it can be easily removed by typing backspace if the suggestion is unwanted.
        if (node.getBoundingClientRect) {
this.text.focus();
          var box    = node.getBoundingClientRect ();
this.text.value = newVal + key;
          return { x : Math.round (box.left + scroll_offset ('Left'))
this.setSelection (actVal.length, newVal.length);
                  ,y : Math.round (box.top + scroll_offset ('Top'))
return true;
                };
},
        }
        var t = 0, l = 0;
        do {
          t = t + (node.offsetTop  || 0);
          l = l + (node.offsetLeft || 0);
          node = node.offsetParent;
        } while (node);
        return {x : l, y : t};
      }


      var textPos = position (this.text);
canSelect : function () {
      var nl = 0;
return this.text.setSelectionRange
      var nt = 0;
|| this.text.createTextRange
      var offset = 0;
||    typeof this.text.selectionStart != 'undefined'
      // Opera 9.5 somehow has offsetWidth = 0 here?? Use the next best value...
  && typeof this.text.selectionEnd != 'undefined';
      var textBoxWidth = this.text.offsetWidth || this.text.clientWidth;
},
      if (this.engineName) {
        this.engineSelector.style.zIndex = 5;
        this.engineSelector.style.position = 'absolute';
        this.engineSelector.style.width = textBoxWidth + 'px';
        // Figure out the height of this selector: display it off-screen, then hide it again.
        if (this.engineSelector.style.display == 'none') {
          this.engineSelector.style.left  = '-10000px';
          this.engineSelector.style.top  = '0px';
          this.engineSelector.style.display = "";
          offset = this.engineSelector.offsetHeight;
          this.engineSelector.style.display = 'none';
        } else {
          offset = this.engineSelector.offsetHeight;
        }
        this.engineSelector.style.left  = nl + 'px';
      }
      if (textPos.y < maxListHeight + offset + 1) {
        // The list might extend beyond the upper border of the page. Let's avoid that by placing it
        // below the input text field.
        nt = this.text.offsetHeight + offset + 1;
        if (this.engineName) this.engineSelector.style.top = this.text.offsetHeight + 'px';
      } else {
        nt = - listh - offset - 1;
        if (this.engineName) this.engineSelector.style.top = - (offset + 1) + 'px';
      }
      this.list.style.top = nt + 'px';
      this.list.style.width = ""; // No fixed width (yet)
      this.list.style.left = nl + 'px';
      if (this.engineName) {
        this.selectEngine (this.engineName);
        this.engineSelector.style.display = "";
      }
      this.list.style.display = 'block';
      // Set the width of the list
      var scroll = scroll_offset ('Left');
      var view_w = viewport ('Width');
      var l_pos  = position (this.list);
      if (this.list.offsetWidth < textBoxWidth ) {
        this.list.style.width = textBoxWidth + 'px';
        return;
      }
      // Make sure that the list fits horizontally into the browser window
      var w      = this.list.offsetWidth;
      if (l_pos.x + w > scroll + view_w) {
        if (w > view_w) w = view_w;
        this.list.style.width = w + 'px';
        this.list.style.left = nl - (l_pos.x + w - scroll - view_w) + 'px';
      }
    },


    autoComplete : function (newVal, actVal, key, dontModify) {
setSelection : function (from, to) {
      if (newVal == actVal) return true;
// this.text must be focused (at least on IE)
      if (dontModify || newVal.indexOf (actVal) != 0) return false;
if (!this.text.value) return;
      // Actual input is a prefix of the new text. Fill in new text, selecting the newly added suffix
if (this.text.setSelectionRange) {    // e.g. khtml
      // such that it can be easily removed by typing backspace if the suggestion is unwanted.
this.text.setSelectionRange (from, to);
      if (!this.canSelect()) return false;
} else if (typeof this.text.selectionStart != 'undefined') {
      // If we can't select properly, autocompletion would be a major annoyance to the user.
if (from > this.text.selectionStart) {
      this.text.focus();
this.text.selectionEnd  = to;
      this.text.value = newVal + key;
this.text.selectionStart = from;
      this.setSelection (actVal.length, newVal.length);
} else {
      return true;
this.text.selectionStart = from;
    },
this.text.selectionEnd  = to;
}
} else if (this.text.createTextRange) { // IE
var new_selection = this.text.createTextRange();
new_selection.move ('character', from);
new_selection.moveEnd ('character', to - from);
new_selection.select();
}
},


    canSelect : function () {
getSelection : function () {
      return    this.text.setSelectionRange
var from = 0, to = 0;
              || this.text.createTextRange
// this.text must be focused (at least on IE)
              ||    typeof (this.text.selectionStart) != 'undefined'
if (!this.text.value) {
                  && typeof (this.text.selectionEnd) != 'undefined';
// No text.
    },
} else if (typeof this.text.selectionStart != 'undefined') {
from = this.text.selectionStart;
to  = this.text.selectionEnd;
} else if (document.selection && document.selection.createRange) { // IE
var rng = document.selection.createRange().duplicate();
if (rng.parentElement() === this.text) {
try {
var textRng = this.text.createTextRange();
textRng.move('character', 0);
textRng.setEndPoint('EndToEnd', rng);
// We're in a single-line input box: no need to care about IE's strange
// handling of line ends
to = textRng.text.length;
textRng.setEndPoint('EndToStart', rng);
from = textRng.text.length;
} catch (notFocused) {
from = this.text.value.length; to = from; // At end of text
}
}
}
return {start: from, end: to};
},


    setSelection : function (from, to) {
saveView : function (evt) {
      // this.text must be focused (at least on IE)
this.lastSelection = this.getSelection ();
      if (!this.text.value) return;
},
      if (this.text.setSelectionRange) {    // e.g. khtml
        this.text.setSelectionRange (from, to);
      } else if (typeof (this.text.selectionStart) != 'undefined') {
        if (from > this.text.selectionStart) {
          this.text.selectionEnd  = to;
          this.text.selectionStart = from;
        } else {
          this.text.selectionStart = from;
          this.text.selectionEnd  = to;
        }
      } else if (this.text.createTextRange) { // IE
        var new_selection = this.text.createTextRange();
        new_selection.move ('character', from);
        new_selection.moveEnd ('character', to - from);
        new_selection.select();
      }
    },


    getSelection : function () {
processKey : function (evt) {
      var from = 0, to = 0;
var dir = 0;
      // this.text must be focused (at least on IE)
switch (this.lastKey) {
      if (!this.text.value) {
case UP: dir = -1;
        // No text.
case DOWN: if (dir === 0) dir = 1;
      } else if (typeof (this.text.selectionStart) != 'undefined') {
case PGUP: if (dir === 0) dir = -HotCat.list_size;
        from = this.text.selectionStart;
case PGDOWN: if (dir === 0) dir = HotCat.list_size;
        to  = this.text.selectionEnd;
if (this.list.style.display != 'none') {
      } else if (document.selection && document.selection.createRange) { // IE
// List is visible, so there are suggestions
        var rng = document.selection.createRange().duplicate();
this.highlightSuggestion (dir);
        if (rng.parentElement() === this.text) {
// Kill the event, otherwise some browsers (e.g., Firefox) may additionally treat an up-arrow
          try {
// as "place the text cursor at the front", which we don't want here.
            var textRng = this.text.createTextRange();
return evtKill (evt);
            textRng.move('character', 0);
} else if (   this.keyCount <= 1
            textRng.setEndPoint('EndToEnd', rng);
  && (!this.callbackObj || this.callbackObj.callsMade == this.callbackObj.nofCalls)
            // We're in a single-line input box: no need to care about IE's strange
  )
            // handling of line ends
{
            to = textRng.text.length;
// If no suggestions displayed, get them, unless we're already getting them.
            textRng.setEndPoint('EndToStart', rng);
this.textchange ();
            from = textRng.text.length;
}
          } catch (notFocused) {
break;
            from = this.text.value.length; to = from; // At end of text
case ESC: // Inhibit default behavior (revert to last real input in FF: we do that ourselves)
          }
return evtKill (evt);
        }
}
      }
return true;
      return {start: from, end: to};
},
    },


    saveView : function (evt) {
highlightSuggestion : function (dir) {
      this.lastSelection = this.getSelection ();
if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
    },
var curr = this.list.selectedIndex;
var tgt  = -1;
if (dir === 0) {
if (curr < 0 || curr >= this.list.options.length) return false;
tgt = curr;
} else {
tgt = curr < 0 ? 0 : curr + dir;
tgt = tgt < 0 ? 0 : tgt;
if (tgt >= this.list.options.length) tgt = this.list.options.length - 1;
}
if (tgt != curr || dir === 0) {
if (curr >= 0 && curr < this.list.options.length && dir !== 0) this.list.options[curr].selected = false;
this.list.options[tgt].selected = true;
// Get current input text
var v = this.text.value.split('|');
var key = v.length > 1 ? '|' + v[1] : "";
var completed = this.autoComplete (this.list.options[tgt].text, this.lastRealInput, null, key, false);
if (!completed || this.list.options[tgt].text == this.lastRealInput) {
this.text.value = this.list.options[tgt].text + key;
if (this.canSelect()) this.setSelection (this.list.options[tgt].text.length, this.list.options[tgt].text.length);
}
this.lastInput = this.list.options[tgt].text;
this.inputExists = true; // Might be wrong if from a dab list...
if (this.icon) this.icon.src = armorUri(HotCat.existsYes);
this.state = CategoryEditor.CHANGE_PENDING;
}
return true;
},


    processKey : function (evt) {
resetKeySelection : function () {
      var dir = 0;
if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
      switch (this.lastKey) {
var curr = this.list.selectedIndex;
        case 38: dir = -1; // Up arrow
if (curr >= 0 && curr < this.list.options.length) {
        case 40: if (dir === 0) dir = 1; // Down arrow
this.list.options[curr].selected = false;
        case 33: if (dir === 0) dir = -HotCat.list_size; // Page up
// Get current input text
        case 34: if (dir === 0) dir = HotCat.list_size; // Page down
var v = this.text.value.split('|');
          if (this.list.style.display != 'none') {
var key = v.length > 1 ? '|' + v[1] : "";
            // List is visible, so there are suggestions
// ESC is handled strangely by some browsers (e.g., FF); somehow it resets the input value before
            this.highlightSuggestion (dir);
// our event handlers ever get a chance to run.
            // Kill the event, otherwise some browsers (e.g., Firefox) may additionally treat an up-arrow
var result = v[0] != this.lastInput;
            // as "place the text cursor at the front", which we don't want here.
if (v[0] != this.lastRealInput) {
            return evtKill (evt);
this.text.value = this.lastRealInput + key;
          } else if (   this.keyCount <= 1
result = true;
                    && (!this.callbackObj || this.callbackObj.callsMade == this.callbackObj.nofCalls)
}
                    )
this.lastInput = this.lastRealInput;
          {
return result;
            // If no suggestions displayed, get them, unless we're already getting them.
}
            this.textchange ();
return false;
          }
}
          break;
        case 27: // ESC: inhibit default behavior (revert to last real input in FF: we do that ourselves)
          return evtKill (evt);
      }
      return true;
    },


    highlightSuggestion : function (dir) {
}; // end CategoryEditor.prototype
      if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
      var curr = this.list.selectedIndex;
      var tgt  = -1;
      if (dir === 0) {
        if (curr < 0 || curr >= this.list.options.length) return false;
        tgt = curr;
      } else {
        tgt = curr < 0 ? 0 : curr + dir;
        tgt = tgt < 0 ? 0 : tgt;
        if (tgt >= this.list.options.length) tgt = this.list.options.length - 1;
      }
      if (tgt != curr || dir === 0) {
        if (curr >= 0 && curr < this.list.options.length && dir != 0) this.list.options[curr].selected = false;
        this.list.options[tgt].selected = true;
        // Get current input text
        var v = this.text.value.split('|');
        var key = v.length > 1 ? '|' + v[1] : "";
        var completed = this.autoComplete (this.list.options[tgt].text, this.lastRealInput, key, false);
        if (!completed) {
          this.text.value = this.list.options[tgt].text + key;
        }
        this.lastInput = this.list.options[tgt].text;
        this.inputExists = true; // Might be wrong if from a dab list...
        if (this.icon) this.icon.src = armorUri(HotCat.existsYes);
        this.state = CategoryEditor.CHANGE_PENDING;
      }
      return true;
    },


    resetKeySelection : function () {
function initialize () {
      if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
// User configurations. Do this here, called from the onload handler, so that users can
      var curr = this.list.selectedIndex;
// override it easily in their own user script files by just declaring variables. JSconfig
      if (curr >= 0 && curr < this.list.options.length) {
// is some feature used at Wikimedia Commons.
        this.list.options[curr].selected = false;
var config = (typeof JSconfig != 'undefined' && JSconfig.keys) ? JSconfig.keys : {};
        // Get current input text
HotCat.dont_add_to_watchlist =
        var v = this.text.value.split('|');
(typeof window.hotcat_dont_add_to_watchlist != 'undefined'
        var key = v.length > 1 ? '|' + v[1] : "";
? !!window.hotcat_dont_add_to_watchlist
        // ESC is handled strangely by some browsers (e.g., FF); somehow it resets the input value before
: (typeof config.HotCatDontAddToWatchlist != 'undefined'
        // our event handlers ever get a chance to run.
? config.HotCatDontAddToWatchlist
        var result = v[0] != this.lastInput;
: HotCat.dont_add_to_watchlist
        if (v[0] != this.lastRealInput) {
  )
          this.text.value = this.lastRealInput + key;
);
          result = true;
HotCat.no_autocommit =
        }
(typeof window.hotcat_no_autocommit != 'undefined'
        this.lastInput = this.lastRealInput;
? !!window.hotcat_no_autocommit
        return result;
: (typeof config.HotCatNoAutoCommit != 'undefined'
      }
? config.HotCatNoAutoCommit
      return false;
: HotCat.no_autocommit
    }
  )
);
HotCat.del_needs_diff =
(typeof window.hotcat_del_needs_diff != 'undefined'
? !!window.hotcat_del_needs_diff
: (typeof config.HotCatDelNeedsDiff != 'undefined'
? config.HotCatDelNeedsDiff
: HotCat.del_needs_diff
  )
);
HotCat.suggest_delay = window.hotcat_suggestion_delay
|| config['HotCatSuggestionDelay']
|| HotCat.suggest_delay;
HotCat.editbox_width = window.hotcat_editbox_width
|| config['HotCatEditBoxWidth']
|| HotCat.editbox_width;
HotCat.suggestions  = window.hotcat_suggestions
|| config['HotCatSuggestions']
|| HotCat.suggestions;
if (typeof HotCat.suggestions != 'string' || !suggestionConfigs[HotCat.suggestions])
HotCat.suggestions = 'combined';
HotCat.fixed_search  =
(typeof window.hotcat_suggestions_fixed != 'undefined'
? !!window.hotcat_suggestions_fixed
: (typeof config.HotCatFixedSuggestions != 'undefined'
? config.HotCatFixedSuggestions
: HotCat.fixed_search
  )
);
HotCat.single_minor =
(typeof window.hotcat_single_changes_are_minor != 'undefined'
? !!window.hotcat_single_changes_are_minor
: (typeof config.HotCatMinorSingleChanges != 'undefined'
? config.HotCatMinorSingleChanges
: HotCat.single_minor
  )
);
HotCat.bg_changed = window.hotcat_changed_background
|| config.HotCatChangedBackground
|| HotCat.bg_changed;
HotCat.use_up_down =
(typeof window.hotcat_use_category_links != 'undefined'
? !!window.hotcat_use_category_links
: (typeof config.HotCatUseCategoryLinks != 'undefined'
? config.HotCatUseCategoryLinks
: HotCat.use_up_down
  )
);
HotCat.list_size = window.hotcat_list_size
|| config.HotCatListSize
|| HotCat.list_size;
// Numeric input, make sure we have a numeric value
HotCat.list_size = parseInt (HotCat.list_size, 10);
if (isNaN (HotCat.list_size) || HotCat.list_size < 5) HotCat.list_size = 5;
if (HotCat.list_size > 15) HotCat.list_size = 15;
// Localize search engine names
if (HotCat.engine_names) {
for (var key in HotCat.engine_names) {
if (suggestionConfigs[key] && HotCat.engine_names[key]) {
suggestionConfigs[key].name = HotCat.engine_names[key];
}
}
}
// Catch both native RTL and "faked" RTL through [[MediaWiki:Rtl.js]]
is_rtl = hasClass (document.body, 'rtl');
if (!is_rtl) {
if (document.defaultView && document.defaultView.getComputedStyle) { // Gecko etc.
is_rtl = document.defaultView.getComputedStyle (document.body, null).getPropertyValue ('direction');
} else if (document.body.currentStyle) { // IE, has subtle differences to getComputedStyle
is_rtl = document.body.currentStyle['direction'];
} else { // Not exactly right, but best effort
is_rtl = document.body.style['direction'];
}
is_rtl = (is_rtl == 'rtl');
}
}


   }; // end CategoryEditor.prototype
function can_edit () {
var container = null;
switch (mw.config.get('skin')) {
case 'cologneblue':
container = document.getElementById ('quickbar');
// Fall through
case 'standard':
case 'nostalgia':
if (!container) container = document.getElementById ('topbar');
var lks = container.getElementsByTagName ('a');
for (var i = 0; i < lks.length; i++) {
if (   param ('title', lks[i].href) == conf.wgPageName
&& param ('action', lks[i].href) == 'edit')
return true;
}
return false;
default:
// all modern skins:
return document.getElementById ('ca-edit') !== null;
}
return false;
}


  function initialize () {
function setup_upload () {
    // User configurations. Do this here, called from the onload handler, so that users can
onUpload = true;
    // override it easily in their own user script files by just declaring variables. JSconfig
// Add an empty category bar at the end of the table containing the description, and change the onsubmit handler.
    // is some feature used at Wikimedia Commons.
var ip = document.getElementById ('mw-htmlform-description') || document.getElementById ('wpDestFile');
    var config = (typeof (JSconfig) != 'undefined' && JSconfig.keys) ? JSconfig.keys : {};
if (!ip) {
    HotCat.dont_add_to_watchlist =
ip = document.getElementById ('wpDestFile');
      (typeof (window.hotcat_dont_add_to_watchlist) != 'undefined'
while (ip && ip.nodeName.toLowerCase() != 'table') ip = ip.parentNode;
        ? !!window.hotcat_dont_add_to_watchlist
}
        : (typeof (config['HotCatDontAddToWatchlist']) != 'undefined'
if (!ip) return;
            ? config['HotCatDontAddToWatchlist']
var reupload = document.getElementById ('wpForReUpload');
            : HotCat.dont_add_to_watchlist
var destFile = document.getElementById ('wpDestFile');
          )
if (  (reupload && !!reupload.value)
      );
|| (destFile && (destFile.disabled || destFile.readOnly)))
    HotCat.no_autocommit =
return; // re-upload form...
      (typeof (window.hotcat_no_autocommit) != 'undefined'
// Insert a table row with two fields (label and empty category bar)
        ? !!window.hotcat_no_autocommit
var labelCell = make ('td');
        : (typeof (config['HotCatNoAutoCommit']) != 'undefined'
var lineCell  = make ('td');
            ? config['HotCatNoAutoCommit']
// Create the category line
            : HotCat.no_autocommit
catLine = make ('div');
          )
catLine.className = 'catlinks';
      );
catLine.id = 'catlinks';
    HotCat.del_needs_diff =
catLine.style.textAlign = is_rtl ? 'right' : 'left';
      (typeof (window.hotcat_del_needs_diff) != 'undefined'
// We'll be inside a table row. Make sure that we don't have margins or strange borders.
        ? !!window.hotcat_del_needs_diff
catLine.style.margin = '0';
        : (typeof (config['HotCatDelNeedsDiff']) != 'undefined'
catLine.style.border = 'none';
            ? config['HotCatDelNeedsDiff']
lineCell.appendChild (catLine);
            : HotCat.del_needs_diff
// Create the label
          )
var label = null;
      );
if (   typeof UFUI != 'undefined'
    HotCat.suggest_delay =   window.hotcat_suggestion_delay
&& typeof UIElements != 'undefined'
                          || config['HotCatSuggestionDelay']
&& typeof UFUI.getLabel == 'function'
                          || HotCat.suggest_delay;
  )
    HotCat.editbox_width =   window.hotcat_editbox_width
{
                          || config['HotCatEditBoxWidth']
try {
                          || HotCat.editbox_width;
label = UFUI.getLabel('wpCategoriesUploadLbl');
    HotCat.suggestions  =   window.hotcat_suggestions
} catch (ex) {
                          || config['HotCatSuggestions']
label = null;
                          || HotCat.suggestions;
}
    if (typeof (HotCat.suggestions) != 'string' || !suggestionConfigs[HotCat.suggestions])
}
      HotCat.suggestions = 'combined';
if (!label) {
    HotCat.fixed_search  =
labelCell.id = 'hotcatLabel';
      (typeof (window.hotcat_suggestions_fixed) != 'undefined'
labelCell.appendChild (make (HotCat.categories, true));
        ? !!window.hotcat_suggestions_fixed
} else {
        : (typeof (config['HotCatFixedSuggestions']) != 'undefined'
labelCell.id = 'hotcatLabelTranslated';
            ? config['HotCatFixedSuggestions']
labelCell.appendChild (label);
            : HotCat.fixed_search
}
          )
labelCell.className          = 'mw-label';
      );
labelCell.style.textAlign    = 'right';
    HotCat.single_minor  =
labelCell.style.verticalAlign = 'middle';
      (typeof (window.hotcat_single_changes_are_minor) != 'undefined'
// Change the onsubmit handler
        ? !!window.hotcat_single_changes_are_minor
var form = document.getElementById('upload') || document.getElementById('mw-upload-form');
        : (typeof (config['HotCatMinorSingleChanges']) != 'undefined'
if (form) {
            ? config['HotCatMinorSingleChanges']
var newRow = ip.insertRow (-1);
            : HotCat.single_minor
newRow.appendChild (labelCell);
          )
newRow.appendChild (lineCell);
      );
form.onsubmit = (function (oldSubmit) {
    HotCat.bg_changed    =   window.hotcat_changed_background
return function () {
                          || config['HotCatChangedBackground']
var do_submit = true;
                          || HotCat.bg_changed;
if (oldSubmit) {
    HotCat.use_up_down  =
if (typeof oldSubmit == 'string')
      (typeof (window.hotcat_use_category_links) != 'undefined'
do_submit = eval (oldSubmit);
        ? !!window.hotcat_use_category_links
else if (typeof oldSubmit == 'function')
        : (typeof (config['HotCatUseCategoryLinks']) != 'undefined'
do_submit = oldSubmit.apply (form, arguments);
            ? config['HotCatUseCategoryLinks']
}
            : HotCat.use_up_down
if (!do_submit) return false;
          )
closeForm ();
      );
// Copy the categories
    HotCat.list_size =   window.hotcat_list_size
var eb = document.getElementById ('wpUploadDescription')
                      || config['HotCatListSize']
|| document.getElementById ('wpDesc');
                      || HotCat.list_size;
var addedOne = false;
    // Numeric input, make sure we have a numeric value
for (var i = 0; i < editors.length; i++) {
    HotCat.list_size = parseInt (HotCat.list_size, 10);
var t = editors[i].currentCategory;
    if (isNaN (HotCat.list_size) || HotCat.list_size < 5) HotCat.list_size = 5;
if (!t) continue ;
    if (HotCat.list_size > 15) HotCat.list_size = 15;
var key = editors[i].currentKey;
    // Localize search engine names
var new_cat = '[[' + HotCat.category_canonical + ':' + t + (key ? '|' + key : "") + ']]';
    if (HotCat.engine_names) {
// Only add if not already present
      for (var key in HotCat.engine_names) {
var cleanedText = eb.value
        if (suggestionConfigs[key] && HotCat.engine_names[key]) {
.replace(/<\!--(\s|\S)*?--\>/g, "")
          suggestionConfigs[key].name = HotCat.engine_names[key];
.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
        }
if (!find_category (cleanedText, t, true)) {
      }
eb.value += '\n' + new_cat;
    }
addedOne = true;
    // Catch both native RTL and "faked" RTL through [[MediaWiki:Rtl.js]]
}
    is_rtl = hasClass (document.body, 'rtl');
}
    if (!is_rtl) {
if (addedOne) {
      if (document.defaultView && document.defaultView.getComputedStyle) { // Gecko etc.
// Remove "subst:unc" added by Flinfo if it didn't find categories
        is_rtl = document.defaultView.getComputedStyle (document.body, null).getPropertyValue ('direction');
eb.value = eb.value.replace(/\{\{subst:unc\}\}/g, "");
      } else if (document.body.currentStyle) { // IE, has subtle differences to getComputedStyle
}
        is_rtl = document.body.currentStyle['direction'];
return true;
      } else { // Not exactly right, but best effort
};
        is_rtl = document.body.style['direction'];
}) (form.onsubmit);
      }
}
      is_rtl = (is_rtl == 'rtl');
}
    }
  }


  function can_edit () {
var cleanedText = null;
    var container = null;
    switch (skin) {
      case 'cologneblue':
        container = document.getElementById ('quickbar');
        // Fall through
      case 'standard':
      case 'nostalgia':
        if (!container) container = document.getElementById ('topbar');
        var lks = container.getElementsByTagName ('a');
        for (var i = 0; i < lks.length; i++) {
          if (  param ('title', lks[i].href) == wgPageName
              && param ('action', lks[i].href) == 'edit')
            return true;
        }
        return false;
      default:
        // all modern skins:
        return document.getElementById ('ca-edit') != null;
    }
    return false;
  }


  function setup_upload () {
function isOnPage (span) {
    onUpload = true;
var catTitle = title (span.firstChild.getAttribute ('href', 2));
    // Add an empty category bar at the end of the table containing the description, and change the onsubmit handler.
if (!catTitle) return null;
    var ip = document.getElementById ('mw-htmlform-description') || document.getElementById ('wpDestFile');
catTitle = catTitle.substr (catTitle.indexOf (':') + 1).replace (/_/g, ' ');
    if (!ip) {
if (HotCat.blacklist && HotCat.blacklist.test (catTitle)) return null;
      ip = document.getElementById ('wpDestFile');
var result = { title : catTitle, match : ["", "", ""] };
      while (ip && ip.nodeName.toLowerCase() != 'table') ip = ip.parentNode;
if (pageText === null) return result;
    }
if (cleanedText === null) {
    if (!ip) return;
cleanedText = pageText
    var reupload = document.getElementById ('wpForReUpload');
.replace(/<\!--(\s|\S)*?--\>/g, "")
    var destFile = document.getElementById ('wpDestFile');
.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
    if (  (reupload && !!reupload.value)
}
        || (destFile && (destFile.disabled || destFile.readOnly)))
result.match = find_category (cleanedText, catTitle, true);
      return; // re-upload form...
return result;
    // Insert a table row with two fields (label and empty category bar)
}
    var labelCell = make ('td');
    var lineCell  = make ('td');
    // Create the category line
    catLine = make ('div');
    catLine.className = 'catlinks';
    catLine.id = 'catlinks';
    catLine.style.textAlign = is_rtl ? 'right' : 'left';
    // We'll be inside a table row. Make sure that we don't have margins or strange borders.
    catLine.style.margin = '0';
    catLine.style.border = 'none';
    lineCell.appendChild (catLine);
    // Create the label
    var label = null;
    if (   typeof (UFUI) != 'undefined'
        && typeof (UIElements) != 'undefined'
        && typeof (UFUI.getLabel) == 'function') {
      try {
        label = UFUI.getLabel ('wpCategoriesUploadLbl');
      } catch (ex) {
        label = null;
      }
    }
    if (!label) {
      labelCell.id = 'hotcatLabel';
      labelCell.appendChild (make (HotCat.categories, true));
    } else {
      labelCell.id = 'hotcatLabelTranslated';
      labelCell.appendChild (label);
    }
    labelCell.className          = 'mw-label';
    labelCell.style.textAlign    = 'right';
    labelCell.style.verticalAlign = 'middle';
    // Change the onsubmit handler
    var form = document.getElementById ('upload') || document.getElementById ('mw-upload-form');
    if (form) {
      var newRow = ip.insertRow (-1);
      newRow.appendChild (labelCell);
      newRow.appendChild (lineCell);
      form.onsubmit = (function (oldSubmit) {
        return function () {
          var do_submit = true;
          if (oldSubmit) {
            if (typeof (oldSubmit) == 'string')
              do_submit = eval (oldSubmit);
            else if (typeof (oldSubmit) == 'function')
              do_submit = oldSubmit.apply (form, arguments);
          }
          if (!do_submit) return false;
          closeForm ();
          // Copy the categories
          var eb =    document.getElementById ('wpUploadDescription')
                  || document.getElementById ('wpDesc');
          var addedOne = false;
          for (var i = 0; i < editors.length; i++) {
            var t = editors[i].currentCategory;
            if (!t) continue ;
            var key = editors[i].currentKey;
            var new_cat = '[[' + HotCat.category_canonical + ':' + t + (key ? '|' + key : "") + ']]';
            // Only add if not already present
            var cleanedText = eb.value.replace(/<\!--(\s|\S)*?--\>/g, "")
                                      .replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
            if (!find_category (cleanedText, t, true)) {
              eb.value += '\n' + new_cat;
              addedOne = true;
            }
          }
          if (addedOne) {
            // Remove "subst:unc" added by Flinfo if it didn't find categories
            eb.value = eb.value.replace(/\{\{subst:unc\}\}/g, "");
          }
          return true;
        };
      }) (form.onsubmit);
    }
  }


  var cleanedText = null;
var initialized = false;
var setupTimeout = null;


  function isOnPage (span) {
function findByClass (scope, tag, className) {
    var catTitle = title (span.firstChild.getAttribute ('href', 2));
// Compatibility routine. Uses jQuery if available, otherwise works with older getElementsByClassName
    if (!catTitle) return null;
var result;
    catTitle = catTitle.substr (catTitle.indexOf (':') + 1).replace (/_/g, ' ');
if (window.jQuery) {
    if (HotCat.blacklist != null && HotCat.blacklist.test (catTitle)) return null;
result = window.jQuery(scope).find(tag + '.' + className);
    var result = { title : catTitle, match : ["", "", ""] };
} else {
    if (pageText === null) return result;
result = getElementsByClassName(scope, tag, className);
    if (cleanedText === null) {
}
      cleanedText = pageText.replace(/<\!--(\s|\S)*?--\>/g, "")
return (result && result.length) ? result[0] : null;
                            .replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
}
    }
    result.match = find_category (cleanedText, catTitle, true);
    return result;
  }


  var initialized = false;
function setup (additionalWork) {
  var setupTimeout = null;
if (initialized) return;
initialized = true;
if (setupTimeout) {
window.clearTimeout (setupTimeout);
setupTimeout = null;
}
// Find the category bar, or create an empty one if there isn't one. Then add -/+- links after
// each category, and add the + link.
catLine =  catLine                                        // Special:Upload
|| document.getElementById ('mw-normal-catlinks') // MW >= 1.13alpha
|| findByClass (document , 'p' , 'catlinks');    // MW < 1.13
var hiddenCats = document.getElementById ('mw-hidden-catlinks');
if (!catLine) {
var footer = null;
if (!hiddenCats) {
footer = findByClass (document , 'div' , 'printfooter');
if (!footer) return; // Don't know where to insert the category line
}
catLine = make ('div');
catLine.id = 'mw-normal-catlinks';
catLine.style.textAlign = is_rtl ? 'right' : 'left';
// Add a label
var label = make ('a');
label.href  = conf.wgArticlePath.replace ('$1', 'Special:Categories');
label.title = HotCat.categories;
label.appendChild (make (HotCat.categories, true));
catLine.appendChild (label);
catLine.appendChild (make (':', true));
// Insert the new category line
var container = (hiddenCats ? hiddenCats.parentNode : document.getElementById ('catlinks'));
if (!container) {
container = make ('div');
container.id = 'catlinks';
footer.parentNode.insertBefore (container, footer.nextSibling);
}
container.className = 'catlinks noprint';
container.style.display = "";
if (!hiddenCats) {
container.appendChild (catLine);
} else {
container.insertBefore (catLine, hiddenCats);
}
} // end if catLine exists
if (is_rtl) catLine.dir = 'rtl';


  function setup (additionalWork) {
// Create editors for all existing categories
    if (initialized) return;
    initialized = true;
    if (setupTimeout) {
      window.clearTimeout (setupTimeout);
      setupTimeout = null;
    }
    // Find the category bar, or create an empty one if there isn't one. Then add -/+- links after
    // each category, and add the + link.
    catLine =  catLine                                                  // Special:Upload
            || document.getElementById ('mw-normal-catlinks')          // MW >= 1.13alpha
            || getElementsByClassName (document , 'p' , 'catlinks')[0]; // MW < 1.13
    var hiddenCats = document.getElementById ('mw-hidden-catlinks');
    if (!catLine) {
      var footer = null;
      if (!hiddenCats) {
        footer = getElementsByClassName (document , 'div' , 'printfooter')[0];
        if (!footer) return; // Don't know where to insert the category line
      }
      catLine = make ('div');
      catLine.id = 'mw-normal-catlinks';
      catLine.style.textAlign = is_rtl ? 'right' : 'left';
      // Add a label
      var label = make ('a');
      label.href  = wgArticlePath.replace ('$1', 'Special:Categories');
      label.title = HotCat.categories;
      label.appendChild (make (HotCat.categories, true));
      catLine.appendChild (label);
      catLine.appendChild (make (':', true));
      // Insert the new category line
      var container = (hiddenCats ? hiddenCats.parentNode : document.getElementById ('catlinks'));
      if (!container) {
        container = make ('div');
        container.id = 'catlinks';
        footer.parentNode.insertBefore (container, footer.nextSibling);
      }
      container.className = 'catlinks noprint';
      container.style.display = "";
      if (!hiddenCats) {
        container.appendChild (catLine);
      } else {
        container.insertBefore (catLine, hiddenCats);
      }
    } // end if catLine exists
    if (is_rtl) catLine.dir = 'rtl';


    // Create editors for all existing categories
function createEditors (line, is_hidden) {
var i;
var cats = line.getElementsByTagName ('li');
if (cats.length > 0) {
newDOM = true; line = cats[0].parentNode;
} else {
cats = line.getElementsByTagName ('span');
}
// Copy cats, otherwise it'll also magically contain our added spans as it is a live collection!
var copyCats = new Array (cats.length);
for (i = 0; i < cats.length; i++) copyCats[i] = cats[i];
var editor = null;
for (i = 0; i < copyCats.length; i++) {
var test = isOnPage (copyCats[i]);
if (test !== null && test.match !== null) {
editor = new CategoryEditor (line, copyCats[i], test.title, test.match[2], is_hidden);
}
}
return copyCats.length > 0 ? copyCats[copyCats.length-1] : null;
}


    function createEditors (line, is_hidden) {
var lastSpan = createEditors (catLine, false);
      var cats = line.getElementsByTagName ('li');
// Create one to add a new category
      if (cats.length > 0) {
var editor = new CategoryEditor(newDOM ? catLine.getElementsByTagName('ul')[0] : catLine, null, null, lastSpan !== null, false);
        newDOM = true; line = cats[0].parentNode;
if (!onUpload) {
      } else {
if (pageText !== null && hiddenCats) {
        cats = line.getElementsByTagName ('span');
if (is_rtl) hiddenCats.dir = 'rtl';
      }
createEditors (hiddenCats, true);
      // Copy cats, otherwise it'll also magically contain our added spans as it is a live collection!
}
      var copyCats = new Array (cats.length);
// And finally add the "multi-mode" span. (Do this at the end, otherwise it ends up in the list above.)
      for (var i = 0; i < cats.length; i++) copyCats[i] = cats[i];
var enableMulti = make ('span');
      var editor = null;
enableMulti.className = 'noprint';
      for (var i = 0; i < copyCats.length; i++) {
if (is_rtl) enableMulti.dir = 'rtl';
        var test = isOnPage (copyCats[i]);
catLine.insertBefore (enableMulti, catLine.firstChild.nextSibling);
        if (test !== null && test.match !== null) {
enableMulti.appendChild (make ('\xa0', true)); // nbsp
          editor = new CategoryEditor (line, copyCats[i], test.title, test.match[2], is_hidden);
multiSpan = make ('span');
        }
enableMulti.appendChild (multiSpan);
      }
multiSpan.innerHTML = '(<a>' + HotCat.addmulti + '</a>)';
      return copyCats.length > 0 ? copyCats[copyCats.length-1] : null;
var lk = multiSpan.getElementsByTagName ('a')[0];
    }
lk.onclick = function (evt) {setMultiInput (); checkMultiInput (); return evtKill (evt);};
lk.title = HotCat.multi_tooltip;
lk.style.cursor = 'pointer';
}
cleanedText = null;
if (typeof additionalWork == 'function') additionalWork();
setupCompleted.loaded(); // Trigger signal; execute registered functions
if (window.jQuery) jQuery('body').trigger('hotcatSetupCompleted');
}


    var lastSpan = createEditors (catLine, false);
function setPage (json) {
    // Create one to add a new category
var startTime = null;
    var editor = new CategoryEditor(newDOM ? catLine.getElementsByTagName('ul')[0] : catLine, null, null, lastSpan != null, false);
if (json && json.query) {
    if (!onUpload) {
if (json.query.pages) {
      if (pageText !== null && hiddenCats) {
var page = json.query.pages[conf.wgArticleId === 0 ? "-1" : "" + conf.wgArticleId];
        if (is_rtl) hiddenCats.dir = 'rtl';
if (page) {
        createEditors (hiddenCats, true);
if (page.revisions && page.revisions.length > 0) {
      }
// Revisions are sorted by revision ID, hence [0] is the one we asked for, and possibly there's a [1] if we're
      // And finally add the "multi-mode" span. (Do this at the end, otherwise it ends up in the list above.)
// not on the latest revision (edit conflicts and such).
      var enableMulti = make ('span');
pageText = page.revisions[0]['*'];
      enableMulti.className = 'noprint';
if (page.revisions[0].timestamp) pageTime = page.revisions[0].timestamp.replace (/\D/g, "");
      if (is_rtl) enableMulti.dir = 'rtl';
if (page.revisions[0].revid) pageTextRevId = page.revisions[0].revid;
      catLine.insertBefore (enableMulti, catLine.firstChild.nextSibling);
if (page.revisions.length > 1) conflictingUser = page.revisions[1].user;
      enableMulti.appendChild (make ('\xa0', true)); // nbsp
}
      multiSpan = make ('span');
if (page.lastrevid) lastRevId = page.lastrevid;
      enableMulti.appendChild (multiSpan);
if (page.starttimestamp) startTime = page.starttimestamp.replace (/\D/g, "");
      multiSpan.innerHTML = '(<a>' + HotCat.addmulti + '</a>)';
pageWatched = typeof page.watched == 'string';
      var lk = multiSpan.getElementsByTagName ('a')[0];
editToken = page.edittoken;
      lk.onclick = function (evt) {setMultiInput (); checkMultiInput (); return evtKill (evt);};
if (page.langlinks && (!json['query-continue'] || !json['query-continue'].langlinks)) {
      lk.title = HotCat.multi_tooltip;
// We have interlanguage links, and we got them all.
      lk.style.cursor = 'pointer';
var re = "";
    }
for (var i = 0; i < page.langlinks.length; i++) {
    cleanedText = null;
re += (i > 0 ? '|' : "") + page.langlinks[i].lang.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1');
    if (typeof (additionalWork) == 'function') additionalWork();
}
    setupCompleted.loaded(); // Trigger signal; execute registered functions
if (re.length > 0) {
    if (window.jQuery) window.jQuery('body').trigger ('hotcatSetupCompleted');
interlanguageRE = new RegExp ('((^|\\n\\r?)(\\[\\[\\s*(' + re + ')\\s*:[^\\]]+\\]\\]\\s*))+$');
  }
}
}


  function setPage (json) {
}
    var startTime = null;
}
    if (json && json.query) {
// Siteinfo
      if (json.query.pages) {
if (json.query.general) {
        var page = json.query.pages[wgArticleId == 0 ? "-1" : "" + wgArticleId];
HotCat.capitalizePageNames = (json.query.general['case'] == 'first-letter');
        if (page) {
if (json.query.general.time && !startTime) startTime = json.query.general.time.replace (/\D/g, "");
          if (page.revisions && page.revisions.length > 0) {
}
            // Revisions are sorted by revision ID, hence [0] is the one we asked for, and possibly there's a [1] if we're
serverTime = startTime;
            // not on the latest revision (edit conflicts and such).
// Userinfo
            pageText = page.revisions[0]['*'];
if (json.query.userinfo && json.query.userinfo.options) {
            if (page.revisions[0].timestamp) pageTime = page.revisions[0].timestamp.replace (/\D/g, "");
watchCreate = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchcreations == '1';
            if (page.revisions[0].revid) pageTextRevId = page.revisions[0].revid;
watchEdit  = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchdefault == '1';
            if (page.revisions.length > 1) conflictingUser = page.revisions[1].user;
minorEdits  = json.query.userinfo.options.minordefault == 1;
          }
// If the user has the "All edits are minor" preference enabled, we should honor that
          if (page.lastrevid) lastRevId = page.lastrevid;
// for single category changes, no matter what the site configuration is.
          if (page.starttimestamp) startTime = page.starttimestamp.replace (/\D/g, "");
if (minorEdits) HotCat.single_minor = true;
          pageWatched = typeof (page.watched) == 'string';
}
          editToken = page.edittoken;
}
          if (page.langlinks && (!json['query-continue'] || !json['query-continue'].langlinks)) {
}
            // We have interlanguage links, and we got them all.
            var re = "";
            for (var i = 0; i < page.langlinks.length; i++) {
              re += (i > 0 ? '|' : "") + page.langlinks[i].lang.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1');
            }
            if (re.length > 0) {
              interlanguageRE = new RegExp ('((^|\\n\\r?)(\\[\\[\\s*(' + re + ')\\s*:[^\\]]+\\]\\]\\s*))+$');
            }
          }


        }
function createCommitForm () {
      }
if (commitForm) return;
      // Siteinfo
var formContainer = make ('div');
      if (json.query.general) {
formContainer.style.display = 'none';
        HotCat.capitalizePageNames = (json.query.general['case'] == 'first-letter');
document.body.appendChild (formContainer);
        if (json.query.general.time && !startTime) startTime = json.query.general.time.replace (/\D/g, "");
formContainer.innerHTML =
      }
'<form id="hotcatCommitForm" method="post" enctype="multipart/form-data" action="'
      serverTime = startTime;
+ conf.wgScript + '?title=' + encodeURIComponent (conf.wgPageName)
      // Userinfo
+ '&action=edit">'
      if (json.query.userinfo && json.query.userinfo.options) {
+ '<input type="hidden" name="wpTextbox1" />'
        watchCreate = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchcreations == '1';
+ '<input type="hidden" name="model" value="wikitext" />'
        watchEdit  = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchdefault == '1';
+ '<input type="hidden" name="format" value="text/x-wiki" />'
        minorEdits  = json.query.userinfo.options.minordefault == 1;
+ '<input type="hidden" name="wpSummary" value="" />'
        // If the user has the "All edits are minor" preference enabled, we should honor that
+ '<input type="checkbox" name="wpMinoredit" value="1" />'
        // for single category changes, no matter what the site configuration is.
+ '<input type="checkbox" name="wpWatchthis" value="1" />'
        if (minorEdits) HotCat.single_minor = true;
+ '<input type="hidden" name="wpAutoSummary" value="" />'
      }
+ '<input type="hidden" name="wpEdittime" />'
    }
+ '<input type="hidden" name="wpStarttime" />'
  }
+ '<input type="hidden" name="wpDiff" value="wpDiff" />'
+ '<input type="hidden" name="oldid" value="0" />'
+ '<input type="submit" name="hcCommit" value="hcCommit" />'
+ '<input type="hidden" name="wpEditToken" />'
+ '<input type="hidden" name="wpUltimateParam" value="1" />'
+ '</form>';
commitForm = document.getElementById ('hotcatCommitForm');
}


  function createCommitForm () {
function getPage () {
    if (commitForm) return;
// We know we have an article here.
    var formContainer = make ('div');
if (conf.wgArticleId === 0) {
    formContainer.style.display = 'none';
// Doesn't exist yet.
    document.body.appendChild (formContainer);
if (conf.wgNamespaceNumber === 2) {
    formContainer.innerHTML =
// Disable on non-existing User pages -- might be a global user page.
        '<form id="hotcatCommitForm" method="post" enctype="multipart/form-data" action="'
return;
      + wgScript + '?title=' + encodeURIComponent (wgPageName)
}
      + '&action=edit">'
pageText = "";
      + '<input type="hidden" name="wpTextbox1" />'
pageTime = null;
      + '<input type="hidden" name="wpSummary" value="" />'
setup (createCommitForm);
      + '<input type="checkbox" name="wpMinoredit" value="1" />'
} else {
      + '<input type="checkbox" name="wpWatchthis" value="1" />'
var url = conf.wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles='
      + '<input type="hidden" name="wpAutoSummary" value="" />'
+ encodeURIComponent (conf.wgPageName)
      + '<input type="hidden" name="wpEdittime" />'
+ '&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid='
      + '<input type="hidden" name="wpStarttime" />'
+ conf.wgCurRevisionId;
      + '<input type="hidden" name="wpEditToken" />'
var s = make ('script');
      + '<input type="hidden" name="wpDiff" value="wpDiff" />'
s.src = armorUri(url);
      + '<input type="hidden" name="oldid" value="0" />'
s.type = 'text/javascript';
      + '<input type="submit" name="hcCommit" value="hcCommit" />'
HotCat.start = function (json) { setPage (json); setup (createCommitForm); };
      + '</form>';
document.getElementsByTagName ('head')[0].appendChild (s);
    commitForm = document.getElementById ('hotcatCommitForm');
setupTimeout = window.setTimeout (function () {setup (createCommitForm);}, 4000); // 4 sec, just in case getting the wikitext takes longer.
  }
}
}


  function getPage () {
function run () {
    // We know we have an article here.
if (HotCat.started) return;
    if (wgArticleId === 0) {
HotCat.started = true;
      // Doesn't exist yet.
loadTrigger.register(really_run);
      pageText = "";
}
      pageTime = null;
      setup (createCommitForm);
    } else {
      var url = wgServer + wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&titles='
              + encodeURIComponent (wgPageName)
              + '&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid='
              + wgCurRevisionId;
      var s = make ('script');
      s.src = armorUri(url);
      s.type = 'text/javascript';
      HotCat.start = function (json) { setPage (json); setup (createCommitForm); };
      document.getElementsByTagName ('head')[0].appendChild (s);
      setupTimeout = window.setTimeout (function () {setup (createCommitForm);}, 4000); // 4 sec, just in case getting the wikitext takes longer.
    }
  }


  function run () {
function really_run () {
    if (HotCat.started) return;
initialize ();
    HotCat.started = true;
    loadTrigger.register(really_run);
  }


   function really_run () {
if (is_rtl && is_ie6) return; // Disabled! IE6 with RTL is just too broken...
    initialize ();
if (!HotCat.upload_disabled && conf.wgNamespaceNumber === -1 && conf.wgCanonicalSpecialPageName == 'Upload' && conf.wgUserName) {
setup_upload ();
setup (function () {
// Check for state restoration once the setup is done otherwise, but before signalling setup completion
if (   typeof UploadForm != 'undefined'
&& typeof UploadForm.previous_hotcat_state != 'undefined'
&& UploadForm.previous_hotcat_state !== null)
{
UploadForm.previous_hotcat_state = setState (UploadForm.previous_hotcat_state);
}
});
} else {
if (!conf.wgIsArticle || conf.wgAction != 'view' || param('diff') !== null || param('oldid') !== null || !can_edit() || HotCat.disable()) return;
getPage ();
}
}


    if (is_rtl && is_ie6) return; // Disabled! IE6 with RTL is just too broken...
// Legacy stuff
    if (!HotCat.upload_disabled && wgNamespaceNumber === -1 && wgCanonicalSpecialPageName == 'Upload' && wgUserName) {
      setup_upload ();
      setup (function () {
        // Check for state restoration once the setup is done otherwise, but before signalling setup completion
        if (  typeof (UploadForm) != 'undefined'
            && typeof (UploadForm.previous_hotcat_state) != 'undefined'
            && UploadForm.previous_hotcat_state != null) {
          UploadForm.previous_hotcat_state = setState (UploadForm.previous_hotcat_state);
        }
      });
    } else {
      if (!wgIsArticle || wgAction != 'view' || param('diff') != null || param('oldid') != null || !can_edit() || HotCat.disable()) return;
      getPage ();
    }
  }


   // Legacy stuff
function closeForm () {
// Close all open editors without redirect resolution and other asynchronous stuff.
for (var i = 0; i < editors.length; i++) {
if (editors[i].state == CategoryEditor.OPEN) {
editors[i].cancel();
} else if (editors[i].state == CategoryEditor.CHANGE_PENDING) {
editors[i].sanitizeInput ();
var value = editors[i].text.value.split('|');
var key   = null;
if (value.length > 1) key = value[1];
var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
if (v.length === 0) {
editors[i].cancel ();
} else {
editors[i].currentCategory = v;
editors[i].currentKey = key;
editors[i].currentExists = this.inputExists;
editors[i].close ();
}
}
}
}


  function closeForm () {
function getState () {
    // Close all open editors without redirect resolution and other asynchronous stuff.
var result = null;
    for (var i = 0; i < editors.length; i++) {
for (var i = 0; i < editors.length; i++) {
      if (editors[i].state == CategoryEditor.OPEN) {
var text = editors[i].currentCategory;
        editors[i].cancel();
var key  = editors[i].currentKey;
      } else if (editors[i].state == CategoryEditor.CHANGE_PENDING) {
if (text && text.length > 0) {
        editors[i].sanitizeInput ();
if (key !== null) text += '|' + key;
        var value = editors[i].text.value.split('|');
if (result === null)
        var key  = null;
result = text;
        if (value.length > 1) key = value[1];
else
        var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
result = result + '\n' + text;
        if (v.length === 0) {
}
          editors[i].cancel ();
}
        } else {
return result;
          editors[i].currentCategory = v;
}
          editors[i].currentKey = key;
          editors[i].currentExists = this.inputExists;
          editors[i].close ();
        }
      }
    }
  }


  function getState () {
function setState (state) {
    var result = null;
var cats = state.split ('\n');
    for (var i = 0; i < editors.length; i++) {
if (cats.length === 0) return null;
      var text = editors[i].currentCategory;
if (initialized && editors.length == 1 && editors[0].isAddCategory) {
      var key = editors[i].currentKey;
// Insert new spans and create new editors for them.
      if (text && text.length > 0) {
var newSpans = [];
        if (key != null) text += '|' + key;
var before = editors.length == 1 ? editors[0].span : null;
        if (result == null)
var i;
          result = text;
for (i = 0; i < cats.length; i++) {
        else
if (cats[i].length === 0) continue;
          result = result + '\n' + text;
var cat = cats[i].split ('|');
      }
var key = cat.length > 1 ? cat[1] : null;
    }
cat = cat[0];
    return result;
var lk = make ('a'); lk.href = wikiPagePath (HotCat.category_canonical + ':' + cat);
  }
lk.appendChild (make (cat, true));
lk.title = cat;
var span = make ('span');
span.appendChild (lk);
if (i === 0) catLine.insertBefore (make (' ', true), before);
catLine.insertBefore (span, before);
if (before && i+1 < cats.length) parent.insertBefore (make (' | ', true), before);
newSpans.push ({element: span, title: cat, 'key': key});
}
// And change the last one...
if (before) {
before.parentNode.insertBefore (make (' | ', true), before);
}
var editor = null;
for (i = 0; i < newSpans.length; i++) {
editor = new CategoryEditor (catLine, newSpans[i].element, newSpans[i].title, newSpans[i].key);
}
}
return null;
}


  function setState (state) {
// Export legacy functions
    var cats = state.split ('\n');
window.hotcat_get_state  = function () { return getState(); };
    if (cats.length === 0) return null;
window.hotcat_set_state  = function (state) { return setState (state); };
    if (initialized && editors.length == 1 && editors[0].isAddCategory) {
window.hotcat_close_form = function () { closeForm (); };
      // Insert new spans and create new editors for them.
      var newSpans = [];
      var before = editors.length == 1 ? editors[0].span : null;
      for (var i = 0; i < cats.length; i++) {
        if (cats[i].length === 0) continue;
        var cat = cats[i].split ('|');
        var key = cat.length > 1 ? cat[1] : null;
        cat = cat[0];
        var lk = make ('a'); lk.href = wikiPagePath (HotCat.category_canonical + ':' + cat);
        lk.appendChild (make (cat, true));
        lk.title = cat;
        var span = make ('span');
        span.appendChild (lk);
        if (i === 0) catLine.insertBefore (make (' ', true), before);
        catLine.insertBefore (span, before);
        if (before && i+1 < cats.length) parent.insertBefore (make (' | ', true), before);
        newSpans.push ({element: span, title: cat, 'key': key});
      }
      // And change the last one...
      if (before) {
        before.parentNode.insertBefore (make (' | ', true), before);
      }
      var editor = null;
      for (var i = 0; i < newSpans.length; i++) {
        editor = new CategoryEditor (catLine, newSpans[i].element, newSpans[i].title, newSpans[i].key);
      }
    }
    return null;
  }


  // Now export these legacy functions
if (window.mw) {
  window.hotcat_get_state  = function () { return getState(); };
// Make sure we don't get conflicts with AjaxCategories (core development that should one day
  window.hotcat_set_state  = function (state) { return setState (state); };
// replace HotCat).
  window.hotcat_close_form = function () { closeForm (); };
mw.config.set('disableAJAXCategories', true);
}
// Run as soon as possible. This varies depending on MediaWiki version;
// window's 'load' event is always safe, but usually we can do better than that.


  if (window.mediaWiki && window.mediaWiki.config) {
// Check for version to avoid MediaWiki bug 32537.
    // Make sure we don't get conflicts with AjaxCategories (core development that should one day
var mwVersion = conf.wgVersion.split('.');
    // replace HotCat).
if (mwVersion[0] >= 1 && parseFloat(mwVersion[1]) > 20) {
    window.mediaWiki.config.set('disableAJAXCategories', true);
// We can safely trigger just after user configuration is loaded. Also start HotCat if the user module fails to load.
  }
// Avoid using Promise methods of mw.loader.using as those aren't supported in older
  if (window.jQuery) {
// MediaWiki versions.
    window.jQuery(document).ready(run);
var startHotCat = function(){
  } else {
jQuery(document).ready(run);
    addOnloadHook (run);
};
  }
mw.loader.using('user', startHotCat, startHotCat);
if (parseFloat(mwVersion[1]) > 21) {
// Reload HotCat after page is saved (bug T103285)
mw.hook('postEdit').add( function () {
catLine = null;
editors = [];
initialized = false;
HotCat.started = false;
run ();
} );
}
} else {
// mw.loader.using('user', ...) could have unintended side-effects on MW <= 1.20. Fall back to dom-ready.
jQuery(document).ready(run);
}
})();
})();


} // end if (guard)
//</source>
//</source>

Latest revision as of 19:21, 7 November 2015

//<source lang="javascript">

/*
 HotCat V2.32

 Ajax-based simple Category manager. Allows adding/removing/changing categories on a page view.
 Supports multiple category changes, as well as redirect and disambiguation resolution. Also
 plugs into the upload form. Search engines to use for the suggestion list are configurable, and
 can be selected interactively.

 Documentation: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat
 List of main authors: https://commons.wikimedia.org/wiki/Help:Gadget-HotCat/Version_history

 License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)

 Choose whichever license of these you like best :-)
*/

/*
 This code is MW version safe. It should run on any MediaWiki installation >= MW 1.15. Note
 that HotCat is supposed to run with or without jQuery, and also on older installations that
 do not yet have window.mw. If you use any of these newer features, make sure you qualify them
 by checking whether they exist at all, and by providing some meaningful fallback implementation
 if not. To start itself, HotCat uses jQuery(document).ready(). If it doesn't exist, HotCat won't
 start.
*/
/* jshint ignore:start */ // This old code uses too many coding conventions incompatible with jshint.
(function () {
	// Support: MW 1.16
	var conf = window.mw ? mw.config.values : window;

	if (
		// Guard against double inclusions (in old IE/Opera element ids become window properties)
		(window.HotCat && !window.HotCat.nodeName)
		// Not on edit pages
		|| conf.wgAction == 'edit'

	) {
		return;
	}

// Configuration stuff.
window.HotCat = {
	// Localize these messages to the main language of your wiki.
	messages :
	{cat_removed  : 'removed [[Category:$1]]'
	,template_removed  : 'removed {{[[Category:$1]]}}'
	,cat_added    : 'added [[Category:$1]]'
	,cat_keychange: 'new key for [[Category:$1]]: "$2"' // $2 is the new key
	,cat_notFound : 'Category "$1" not found'
	,cat_exists   : 'Category "$1" already exists; not added.'
	,cat_resolved : ' (redirect [[Category:$1]] resolved)'
	,uncat_removed: 'removed {{uncategorized}}'
	,separator    : '; '
	,prefix       : ""
		// Some text to prefix to the edit summary.
	,using        : ' using [[Help:Gadget-HotCat|HotCat]]'
		// Some text to append to the edit summary. Named 'using' for historical reasons. If you prefer
		// to have a marker at the front, use prefix and set this to the empty string.
	,multi_change : '$1 categories'
		// $1 is replaced by a number. If your language has several plural forms (c.f. [[:en:Dual (grammatical form)]]),
		// you can set this to an array of strings suitable for passing to mw.language.configPlural().
		// If that function doesn't exist, HotCat will simply fall back to using the last
		// entry in the array.
	,commit       : 'Save'
		// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
		// see localization hook below.
	,ok           : 'OK'
		// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
		// see localization hook below.
	,cancel       : 'Cancel'
		// Button text. Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
		// see localization hook below.
	,multi_error  : 'Could not retrieve the page text from the server. Therefore, your category changes '
					+'cannot be saved. We apologize for the inconvenience.'
		// Localize to wgContentLanguage here; localize to wgUserLanguage in a subpage,
		// see localization hook below.
	,short_catchange : null
		// Defaults to '[[' + category_canonical + ':$1]]'. Can be overridden if in the short edit summaries
		// not the standard category name should be used but, say, a shorter namespace alias. $1 is replaced
		// by a category name.
	}
 ,category_regexp    : '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]'
	// Regular sub-expression matching all possible names for the category namespace. Is automatically localized
	// correctly if you're running MediaWiki 1.16 or later. Otherwise, set it appropriately, e.g. at the German
	// Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|[Kk][Aa][Tt][Ee][Gg][Oo][Rr][Ii][Ee]', or at the
	// Chinese Wikipedia, use '[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]|分类|分類'. Note that namespaces are case-
	// insensitive!
 ,category_canonical : 'Category'
	// The standard category name on your wiki. Is automatically localized correctly if you're running
	// MediaWiki 1.16 or later; otherwise, set it to the preferred category name (e.g., "Kategorie").
 ,categories         : 'Categories'
	// Plural of category_canonical.
 ,disambig_category  : 'Disambiguation'
	// Any category in this category is deemed a disambiguation category; i.e., a category that should not contain
	// any items, but that contains links to other categories where stuff should be categorized. If you don't have
	// that concept on your wiki, set it to null. Use blanks, not underscores.
 ,redir_category     : 'Category redirects'
	// Any category in this category is deemed a (soft) redirect to some other category defined by a link
	// to another non-blacklisted category. If your wiki doesn't have soft category redirects, set this to null.
    // If a soft-redirected category contains more than one link to another non-blacklisted category, it's considered
    // a disambiguation category instead.
 ,links : {change: '(±)', remove: '(\u2212)', add: '(+)', restore: '(×)', undo: '(×)', down: '(\u2193)', up: '(\u2191)'}
	// The little modification links displayed after category names. U+2212 is a minus sign; U+2193 and U+2191 are
	// downward and upward pointing arrows. Do not use ↓ and ↑ in the code!
 ,tooltips : {
	 change:  'Modify'
	,remove:  'Remove'
	,add:     'Add a new category'
	,restore: 'Undo changes'
	,undo:    'Undo changes'
	,down:    'Open for modifying and display subcategories'
	,up:      'Open for modifying and display parent categories'
  }
	// The tooltips for the above links
 ,addmulti           : '<span>+<sup>+</sup></span>'
	// The HTML content of the "enter multi-mode" link at the front.
 ,multi_tooltip      : 'Modify several categories'
	// Tooltip for the "enter multi-mode" link
 ,disable            :
		function () { // Return true to disable HotCat.
			var ns = conf.wgNamespaceNumber;
			var nsIds = conf.wgNamespaceIds;
			return (   ns < 0     // Special pages; Special:Upload is handled differently
					|| ns === 10  // Templates
					|| ns === 828 // Module (Lua)
					|| ns === 8   // MediaWiki
					|| ns === 6 && conf.wgArticleId === 0 // Non-existing file pages
					|| ns === 2 && /\.(js|css)$/.test(conf.wgTitle) // User scripts
					|| nsIds
						&& (   ns === nsIds['creator']
							|| ns === nsIds['timedtext']
							|| ns === nsIds['institution']
						   )
				   );
		}
 ,uncat_regexp : /\{\{\s*([Uu]ncat(egori[sz]ed( image)?)?|[Nn]ocat|[Nn]eedscategory)[^}]*\}\}\s*(<\!--.*?--\>)?/g
	// A regexp matching a templates used to mark uncategorized pages, if your wiki does have that.
	// If not, set it to null.
 ,existsYes    : '//upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png'
 ,existsNo     : '//upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png'
	// The images used for the little indication icon. Should not need changing.
 ,template_regexp    : '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]'
	// Regexp to recognize templates. Like "category" above; autolocalized for MW 1.16+, otherwise set manually here.
	// On the German Wikipedia, you might use '[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]|[Vv][Oo][Rr][Ll][Aa][Gg][Ee]'.
 ,template_categories : {}
	// a list of categories which can be removed by removing a template
	// key: the category without namespace
	// value: A regexp matching the template name, again without namespace
	// If you don't have this at your wiki, or don't want this, set it to an empty object {}.
 ,engine_names : {
	 searchindex : 'Search index'
	,pagelist    : 'Page list'
	,combined    : 'Combined search'
	,subcat      : 'Subcategories'
	,parentcat   : 'Parent categories'
  }
	// Names for the search engines
 ,capitalizePageNames : true
	// Set to false if your wiki has case-sensitive page names. MediaWiki has two modes: either the first letter
	// of a page is automatically capitalized ("first-letter"; Category:aa == Category:Aa), or it isn't
	// ("case-sensitive"; Category:aa != Category:Aa). It doesn't currently have a fully case-insensitive mode
	// (which would mean Category:aa == Category:Aa == Category:AA == Category:aA)
	// HotCat tries to set this correctly automatically using an API query. It's still a good idea to manually
	// configure it correctly; either directly here if you copied HotCat, or in the local configuration file
	// MediaWiki:Gadget-HotCat.js/local_defaults if you hotlink to the Commons-version, to ensure it is set even
	// if that API query should fail for some strange reason.
 ,upload_disabled : false
	// If upload_disabled is true, HotCat will not be used on the Upload form.
 ,blacklist : null
	// Single regular expression matching blacklisted categories that cannot be changed or
	// added using HotCat. For instance /\bstubs?$/ (any category ending with the word "stub"
	// or "stubs"), or /(\bstubs?$)|\bmaintenance\b/ (stub categories and any category with the
	// word "maintenance" in its title.

// Stuff changeable by users:
 ,bg_changed : '#F8CCB0'
	// Background for changed categories in multi-edit mode. Default is a very light salmon pink.
 ,no_autocommit : false
	// If true, HotCat will never automatically submit changes. HotCat will only open an edit page with
	// the changes; users must always save explicitly.
 ,del_needs_diff : false
	// If true, the "category deletion" link "(-)" will never save automatically but always show an
	// edit page where the user has to save the edit manually. Is false by default because that's the
	// traditional behavior. This setting overrides no_autocommit for "(-)" links.
 ,suggest_delay : 100
	// Time, in milliseconds, that HotCat waits after a keystroke before making a request to the
	// server to get suggestions.
 ,editbox_width : 40
	// Default width, in characters, of the text input field.
 ,suggestions : 'combined'
	// One of the engine_names above, to be used as the default suggestion engine.
 ,fixed_search : false
	// If true, always use the default engine, and never display a selector.
 ,use_up_down : true
	// If false, do not display the "up" and "down" links
 ,list_size : 5
	// Default list size
 ,single_minor : true
	// If true, single category changes are marked as minor edits. If false, they're not.
 ,dont_add_to_watchlist : false
	// If true, never add a page to the user's watchlist. If false, pages get added to the watchlist if
	// the user has the "Add pages I edit to my watchlist" or the "Add pages I create to my watchlist"
	// options in his or her preferences set.
 ,shortcuts : null
 ,addShortcuts :
		function (map) {
			if (!map) return;
			window.HotCat.shortcuts = window.HotCat.shortcuts || {};
			for (var k in map) {
				if (!map.hasOwnProperty (k) || typeof k != 'string') continue;
				var v = map[k];
				if (typeof v != 'string') continue;
				k = k.replace (/^\s+|\s+$/g, "");
				v = v.replace (/^\s+|\s+$/g, "");
				if (k.length === 0 || v.length === 0) continue;
				window.HotCat.shortcuts[k] = v;
			}
		}
};

	// More backwards compatibility. We have a few places where we test for the browser: once for
	// Safari < 3.0, twice for WebKit (Chrome or Safari, any versions), twice for IE <= 6, and
	// once for IE < 8.
	var ua = navigator.userAgent.toLowerCase();
	var is_ie6 = /msie ([0-9]{1,}[\.0-9]{0,})/.exec(ua) !== null && parseFloat(RegExp.$1) <= 6.0;
	var is_ie_lt8 = /msie ([0-9]{1,}[\.0-9]{0,})/.exec(ua) !== null && parseFloat(RegExp.$1) < 8.0;
	var is_webkit = /applewebkit\/\d+/.test(ua) && ua.indexOf ('spoofer') < 0;
	// And even more compatbility. HotCat was developed without jQuery, and anyway current jQuery
	// (1.7.1) doesn't seem to support in jquery.getJSON() or jQuery.ajax() the automatic
	// switching from GET to POST requests if the query arguments would make the uri too long.
	// (IE has a hard limit of 2083 bytes, and the servers may have limits around 4 or 8kB.)
	//    Anyway, HotCat is supposed to run on wikis without jQuery, so we'd have to supply some
	// ajax routines ourselves in any case. We can't rely on the old sajax_init_object(), newer
	// MW versions (>= 1.19) might not have it.
	var getJSON = (function () {
		function getRequest () {
			var request = null;
			try {
				request = new window.XMLHttpRequest();
			} catch (anything) {
				if (window.ActiveXObject) {
					try {
						request = new window.ActiveXObject('Microsoft.XMLHTTP');
					} catch (any) {
					}
				} // end if IE
			} // end try-catch
			return request;
		}

		return function (settings) {
			var req = getRequest();
			if (!req && settings && settings.error) settings.error (req);
			if (!req || !settings || !settings.uri) return req;
			var uri = armorUri (settings.uri);
			var args = settings.data || null;
			var method;
			if (args && uri.length + args.length + 1 > 2000) {
				// We lose caching, but at least we can make the request
				method = 'POST';
				req.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
			} else {
				method = 'GET';
				if (args) uri += '?' + args;
				args = null;
			}
			req.open (method, uri, true);
			req.onreadystatechange = function () {
				if (req.readyState != 4) return;
				if (req.status != 200 || !req.responseText || !(/^\s*[\{\[]/.test(req.responseText))) {
					if (settings.error) settings.error (req);
				} else {
					if (settings.success) settings.success (eval ('(' + req.responseText + ')'));
				}
			};
			req.setRequestHeader ('Pragma', 'cache=yes');
			req.setRequestHeader ('Cache-Control', 'no-transform');
			req.send (args);
			return req;
		};
	})();

	function armorUri (uri) {
		// Avoid protocol-relative URIs, IE7 has a bug with them in Ajax calls
		if (uri.length >= 2 && uri.substring(0, 2) == '//') return document.location.protocol + uri;
		return uri;
	}

	function LoadTrigger (needed) {
		this.queue = [];
		this.toLoad = needed;
	}
	LoadTrigger.prototype = {
		register : function (callback) {
			if (this.toLoad <= 0) {
				callback (); // Execute directly
			} else {
				this.queue[this.queue.length] = callback;
			}
		},

		loaded : function () {
			if (this.toLoad > 0) {
				this.toLoad--;
				if (this.toLoad === 0) {
					// Run queued callbacks once
					for (var i = 0; i < this.queue.length; i++) this.queue[i]();
					this.queue = [];
				}
			}
		}

	};

	var setupCompleted = new LoadTrigger(1);
	// Used to run user-registered code once HotCat is fully set up and ready.
	HotCat.runWhenReady = function (callback) {setupCompleted.register(callback);};

	var loadTrigger = new LoadTrigger(2);
	// Used to delay running the HotCat setup until /local_defaults and localizations have been loaded.

	function load (uri) {
		var head = document.getElementsByTagName ('head')[0];
		var s = document.createElement ('script');
		s.setAttribute ('src', armorUri(uri));
		s.setAttribute ('type', 'text/javascript');
		var done = false;

		function afterLoad () {
			if (done) return;
			done = true;
			s.onload = s.onreadystatechange = s.onerror = null; // Properly clean up to avoid memory leaks in IE
			if (head && s.parentNode) head.removeChild (s);
			loadTrigger.loaded();
		}

		s.onload = s.onreadystatechange = function () { // onreadystatechange for IE, onload for all others
			if (done) return;
			if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
				afterLoad ();
			}
		};
		s.onerror = afterLoad; // Clean up, but otherwise ignore errors
		head.insertBefore (s, head.firstChild); // appendChild may trigger bugs in IE6 here
	}

	function loadJS (page) {
		load (conf.wgServer + conf.wgScript + '?title=' + encodeURIComponent (page) + '&action=raw&ctype=text/javascript');
	}

	function loadURI (href) {
		var url = href;
		if (url.substring (0, 2) == '//') {
			url = window.location.protocol + url;
		} else if (url.substring (0, 1) == '/') {
			url = conf.wgServer + url;
		}
		load (url);
	}

	// Load local configurations, overriding the pre-set default values in the HotCat object above. This is always loaded
	// from the wiki where this script is executing, even if this script itself is hotlinked from the Commons. This can
	// be used to change the default settings, or to provide localized interface texts for edit summaries and so on.
	loadJS ('MediaWiki:Gadget-HotCat.js/local_defaults');

	// Load localized UI texts. These are the texts that HotCat displays on the page itself. Texts shown in edit summaries
	// should be localized in /local_defaults above.
	if (conf.wgUserLanguage != 'en') {
		// Lupo: somebody thought it would be a good idea to add this. So the default is true, and you have to set it to false
		// explicitly if you're not on the Commons and don't want that.
		if (typeof window.hotcat_translations_from_commons == 'undefined') {
			window.hotcat_translations_from_commons = true;
		}
		// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
		if (window.hotcat_translations_from_commons && conf.wgServer.indexOf('//commons') < 0) {
			loadURI ('//commons.wikimedia.org/w/index.php?title='
				+ 'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage
				+ '&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400'
			);
		} else {
			// Load translations locally
			loadJS ('MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage);
		}
	} else {
		loadTrigger.loaded();
	}

	// No further changes should be necessary here.

	// The following regular expression strings are used when searching for categories in wikitext.
	var wikiTextBlank   = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]+';
	var wikiTextBlankRE = new RegExp (wikiTextBlank, 'g');
	// Regexp for handling blanks inside a category title or namespace name.
	// See http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/Title.php?revision=104051&view=markup#l2722
	// See also http://www.fileformat.info/info/unicode/category/Zs/list.htm
	//   MediaWiki collapses several contiguous blanks inside a page title to one single blank. It also replace a
	// number of special whitespace characters by simple blanks. And finally, blanks are treated as underscores.
	// Therefore, when looking for page titles in wikitext, we must handle all these cases.
	//   Note: we _do_ include the horizontal tab in the above list, even though the MediaWiki software for some reason
	// appears to not handle it. The zero-width space \u200B is _not_ handled as a space inside titles by MW.
	var wikiTextBlankOrBidi = '[\\t _\\xA0\\u1680\\u180E\\u2000-\\u200B\\u200E\\u200F\\u2028-\\u202F\\u205F\\u3000]*';
	// Whitespace regexp for handling whitespace between link components. Including the horizontal tab, but not \n\r\f\v:
	// a link must be on one single line.
	//   MediaWiki also removes Unicode bidi override characters in page titles (and namespace names) completely.
	// This is *not* handled, as it would require us to allow any of [\u200E\u200F\u202A-\u202E] between any two
	// characters inside a category link. It _could_ be done though... We _do_ handle strange spaces, including the
	// zero-width space \u200B, and bidi overrides between the components of a category link (adjacent to the colon,
	// or adjacent to and inside of "[[" and "]]").

	// First auto-localize the regexps for the category and the template namespaces.
	var formattedNamespaces = conf.wgFormattedNamespaces;
	var namespaceIds = conf.wgNamespaceIds;
	if (formattedNamespaces) {
		function autoLocalize (namespaceNumber, fallback) {
			function create_regexp_str (name)
			{
				if (!name || name.length === 0) return "";
				var regex_name = "";
				for (var i = 0; i < name.length; i++){
					var initial = name.substr (i, 1);
					var ll = initial.toLowerCase ();
					var ul = initial.toUpperCase ();
					if (ll == ul){
						regex_name += initial;
					} else {
						regex_name += '[' + ll + ul + ']';
					}
				}
				return regex_name
							.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1')
							.replace (wikiTextBlankRE, wikiTextBlank);
			}

			fallback = fallback.toLowerCase();
			var canonical  = formattedNamespaces["" + namespaceNumber].toLowerCase();
			var regexp     = create_regexp_str (canonical);
			if (fallback && canonical != fallback) regexp += '|' + create_regexp_str(fallback);
			if (namespaceIds) {
				for (var cat_name in namespaceIds) {
					if (   typeof cat_name == 'string'
						&& cat_name.toLowerCase() != canonical
						&& cat_name.toLowerCase() != fallback
						&& namespaceIds[cat_name] == namespaceNumber)
					{
						regexp += '|' + create_regexp_str(cat_name);
					}
				}
			}
			return regexp;
		}

		if (formattedNamespaces['14']) {
			HotCat.category_canonical = formattedNamespaces['14'];
			HotCat.category_regexp = autoLocalize (14, 'category');
		}
		if (formattedNamespaces['10']) {
			HotCat.template_regexp = autoLocalize (10, 'template');
		}
	}

	// Utility functions. Yes, this duplicates some functionality that also exists in other places, but
	// to keep this whole stuff in a single file not depending on any other on-wiki Javascripts, we re-do
	// these few operations here.
	function bind (func, target) {
		var f = func, tgt = target;
		return function () { return f.apply (tgt, arguments); };
	}
	function make (arg, literal) {
		if (!arg) return null;
		return literal ? document.createTextNode (arg) : document.createElement (arg);
	}
	function param (name, uri) {
		if (typeof uri == 'undefined' || uri === null) uri = document.location.href;
		var re = new RegExp ('[&?]' + name + '=([^&#]*)');
		var m = re.exec (uri);
		if (m && m.length > 1) return decodeURIComponent(m[1]);
		return null;
	}
	function title (href) {
		if (!href) return null;
		var script = conf.wgScript + '?';
		if (href.indexOf (script) === 0 || href.indexOf (conf.wgServer + script) === 0 || conf.wgServer.substring(0, 2) == '//' && href.indexOf (document.location.protocol + conf.wgServer + script) === 0) {
			// href="/w/index.php?title=..."
			return param ('title', href);
		} else {
			// href="/wiki/..."
			var prefix = conf.wgArticlePath.replace ('$1', "");
			if (href.indexOf (prefix) !== 0) prefix = conf.wgServer + prefix; // Fully expanded URL?
			if (href.indexOf (prefix) !== 0 && prefix.substring(0, 2) == '//') prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
			if (href.indexOf (prefix) === 0)
				return decodeURIComponent (href.substring (prefix.length));
		}
		return null;
	}
	function hasClass (elem, name) {
		return (' ' + elem.className + ' ').indexOf (' ' + name + ' ') >= 0;
	}
	function capitalize (str) {
		if (!str || str.length === 0) return str;
		return str.substr(0, 1).toUpperCase() + str.substr (1);
	}
	function wikiPagePath (pageName) {
		// Note: do not simply use encodeURI, it doesn't encode '&', which might break if wgArticlePath actually has the $1 in
		// a query parameter.
		return conf.wgArticlePath.replace('$1', encodeURIComponent (pageName).replace(/%3A/g, ':').replace(/%2F/g, '/'));
	}
	function escapeRE(str) {
		return str.replace(/([\\\^\$\.\?\*\+\(\)\[\]])/g, '\\$1');
	}

	function substituteFactory (options) {
		options = options || {};
		var lead = options.indicator || '$';
		var indicator = escapeRE (lead);
		var lbrace = escapeRE (options.lbrace || '{');
		var rbrace = escapeRE (options.rbrace || '}');
		var re;

		re = new RegExp(
			 '(?:' + indicator + '(' + indicator + '))|'                                           // $$
			+'(?:' + indicator + '(\\d+))|'                                                        // $0, $1
			+'(?:' + indicator + '(?:' + lbrace + '([^' + lbrace + rbrace + ']+)' + rbrace + '))|' // ${key}
			+'(?:' + indicator + '(?!(?:[' + indicator + lbrace + ']|\\d))(\\S+?)\\b)'             // $key (only if first char after $ is not $, digit, or { )
			,'g');
		// Replace $1, $2, or ${key1}, ${key2}, or $key1, $key2 by values from map. $$ is replaced by a single $.
		return function (str, map) {
			if (!map) return str;
			return str.replace(re
				,function (match, prefix, idx, key, alpha) {
					if (prefix == lead) return lead;
					var k = alpha || key || idx;
					var replacement = typeof map[k] === 'function' ? map[k](match, k) : map[k];
					return typeof replacement === 'string' ? replacement : (replacement || match);
				}
			);
		};
	}

	var substitute = substituteFactory();
	var replaceShortcuts = (function () {
		var replaceHash = substituteFactory({indicator:'#',lbrace:'[',rbrace:']'});
		return function (str, map) {
			var s = replaceHash (str, map);
			return HotCat.capitalizePageNames ? capitalize(s) : s;
		};
	})();

	// Text modification

	var findCatsRE =
		new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(?:' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':[^\\]]+\\]\\]', 'g');

	function replaceByBlanks (match) {
		return match.replace(/(\s|\S)/g, ' '); // /./ doesn't match linebreaks. /(\s|\S)/ does.
	}

	function find_category (wikitext, category, once) {
		var cat_regex = null;
		if(HotCat.template_categories[category]){
			cat_regex = new RegExp ('\\{\\{' + wikiTextBlankOrBidi + '(' + HotCat.template_regexp + '(?=' + wikiTextBlankOrBidi + ':))?' + wikiTextBlankOrBidi
									+ '(?:' + HotCat.template_categories[category] + ')'
									+ wikiTextBlankOrBidi + '(\\|.*?)?\\}\\}', 'g'
			);
		} else {
			var cat_name  = escapeRE (category);
			var initial   = cat_name.substr (0, 1);
			cat_regex = new RegExp ('\\[\\[' + wikiTextBlankOrBidi + '(' + HotCat.category_regexp + ')' + wikiTextBlankOrBidi + ':' + wikiTextBlankOrBidi
									+ (initial == '\\' || !HotCat.capitalizePageNames
										? initial
										: '[' + initial.toUpperCase() + initial.toLowerCase() + ']')
									+ cat_name.substring (1).replace (wikiTextBlankRE, wikiTextBlank)
									+ wikiTextBlankOrBidi + '(\\|.*?)?\\]\\]', 'g'
			);
		}
		if (once) return cat_regex.exec (wikitext);
		var copiedtext = wikitext
							.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
							.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
		var result = [];
		var curr_match = null;
		while ((curr_match = cat_regex.exec (copiedtext)) !== null) {
			result.push ({match : curr_match});
		}
		result.re = cat_regex;
		return result; // An array containing all matches, with positions, in result[i].match
	}

	var interlanguageRE = null;

	function change_category (wikitext, toRemove, toAdd, key, is_hidden) {

		function find_insertionpoint (wikitext) {
			var copiedtext = wikitext
								.replace(/<\!--(\s|\S)*?--\>/g, replaceByBlanks)
								.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, replaceByBlanks);
			// Search in copiedtext to avoid that we insert inside an HTML comment or a nowiki "element".
			var index = -1;
			findCatsRE.lastIndex = 0;
			while (findCatsRE.exec(copiedtext) !== null) index = findCatsRE.lastIndex;
			if (index < 0) {
				// Find the index of the first interlanguage link...
				var match = null;
				if (!interlanguageRE) {
					// Approximation without API: interlanguage links start with 2 to 3 lower case letters, optionally followed by
					// a sequence of groups consisting of a dash followed by one or more lower case letters. Exceptions are "simple"
					// and "tokipona".
					match = /((^|\n\r?)(\[\[\s*(([a-z]{2,3}(-[a-z]+)*)|simple|tokipona)\s*:[^\]]+\]\]\s*))+$/.exec (copiedtext);
				} else {
					match = interlanguageRE.exec(copiedtext);
				}
				if (match) index = match.index;
				return {idx : index, onCat : false};
			}
			return {idx : index, onCat : index >= 0};
		}

		var summary   = [];
		var nameSpace = HotCat.category_canonical;
		var cat_point = -1; // Position of removed category;

		if (key) key = '|' + key;
		var keyChange = (toRemove && toAdd && toRemove == toAdd && toAdd.length > 0);
		var matches;
		if (toRemove && toRemove.length > 0) {
			matches = find_category (wikitext, toRemove);
			if (!matches || matches.length === 0) {
				return {text: wikitext, 'summary': summary, error: HotCat.messages.cat_notFound.replace (/\$1/g, toRemove)};
			} else {
				var before = wikitext.substring (0, matches[0].match.index);
				var after  = wikitext.substring (matches[0].match.index + matches[0].match[0].length);
				if (matches.length > 1) {
					// Remove all occurrences in after
					matches.re.lastIndex = 0;
					after = after.replace (matches.re, "");
				}
				if (toAdd) {
					nameSpace = matches[0].match[1] || nameSpace;
					if (key === null) key = matches[0].match[2]; // Remember the category key, if any.
				}
				// Remove whitespace (properly): strip whitespace, but only up to the next line feed.
				// If we then have two linefeeds in a row, remove one. Otherwise, if we have two non-
				// whitespace characters, insert a blank.
				var i = before.length - 1;
				while (i >= 0 && before.charAt (i) != '\n' && before.substr (i, 1).search (/\s/) >= 0) i--;
				var j = 0;
				while (j < after.length && after.charAt (j) != '\n' && after.substr (j, 1).search (/\s/) >= 0)
					j++;
				if (i >= 0 && before.charAt (i) == '\n' && (after.length === 0 || j < after.length && after.charAt (j) == '\n'))
					i--;
				if (i >= 0) before = before.substring (0, i+1); else before = "";
				if (j < after.length) after = after.substring (j); else after = "";
				if (before.length > 0 && before.substring (before.length - 1).search (/\S/) >= 0
						&& after.length > 0 && after.substr (0, 1).search (/\S/) >= 0)
					before += ' ';
				cat_point = before.length;
				if (cat_point === 0 && after.length > 0 && after.substr(0,1) == '\n') {
					after = after.substr(1);
				}
				wikitext = before + after;
				if (!keyChange) {
					if(HotCat.template_categories[toRemove]) {
						summary.push (HotCat.messages.template_removed.replace (/\$1/g, toRemove));
					} else {
						summary.push (HotCat.messages.cat_removed.replace (/\$1/g, toRemove));
					}
				}
			}
		}
		if (toAdd && toAdd.length > 0) {
			matches = find_category (wikitext, toAdd);
			if (matches && matches.length > 0) {
				return {text: wikitext, 'summary': summary, error : HotCat.messages.cat_exists.replace (/\$1/g, toAdd)};
			} else {
				var onCat = false;
				if (cat_point < 0) {
					var point = find_insertionpoint (wikitext);
					cat_point = point.idx;
					onCat = point.onCat;
				} else {
					onCat = true;
				}
				var newcatstring = '[[' + nameSpace + ':' + toAdd + (key || "") + ']]';
				if (cat_point >= 0) {
					var suffix = wikitext.substring (cat_point);
					wikitext = wikitext.substring (0, cat_point) + (cat_point > 0 ? '\n' : "") + newcatstring + (!onCat ? '\n' : "");
					if (suffix.length > 0 && suffix.substr(0, 1) != '\n') {
						wikitext += '\n' + suffix;
					} else {
						wikitext += suffix;
					}
				} else {
					if (wikitext.length > 0 && wikitext.substr (wikitext.length - 1, 1) != '\n')
						wikitext += '\n';
					wikitext += (wikitext.length > 0 ? '\n' : "") + newcatstring;
				}
				if (keyChange) {
					var k = key || "";
					if (k.length > 0) k = k.substr (1);
					summary.push (substitute (HotCat.messages.cat_keychange, [null, toAdd, k]));
				} else {
					summary.push (HotCat.messages.cat_added.replace (/\$1/g, toAdd));
				}
				if (HotCat.uncat_regexp && !is_hidden) {
					var txt = wikitext.replace (HotCat.uncat_regexp, ""); // Remove "uncat" templates
					if (txt.length != wikitext.length) {
						wikitext = txt;
						summary.push (HotCat.messages.uncat_removed);
					}
				}
			}
		}
		return {text: wikitext, 'summary': summary, error: null};
	}

	// The real HotCat UI

	function evtKeys (e) {
		e = e || window.event || window.Event; // W3C, IE, Netscape
		var code = 0;
		if (typeof e.ctrlKey != 'undefined') { // All modern browsers
			// Ctrl-click seems to be overloaded in FF/Mac (it opens a pop-up menu), so treat cmd-click
			// as a ctrl-click, too.
			if (e.ctrlKey || e.metaKey) code |= 1;
			if (e.shiftKey) code |= 2;
		} else if (typeof e.modifiers != 'undefined') { // Netscape...
			if (e.modifiers & (Event.CONTROL_MASK | Event.META_MASK)) code |= 1;
			if (e.modifiers & Event.SHIFT_MASK) code |= 2;
		}
		return code;
	}
	function evtKill (e) {
		e = e || window.event || window.Event; // W3C, IE, Netscape
		if (typeof e.preventDefault != 'undefined') {
			e.preventDefault ();
			e.stopPropagation ();
		} else
			e.cancelBubble = true;
		return false;
	}
	function addEvent (node, evt, f, capture) {
		if (window.jQuery && (!capture || !node.addEventListener)) window.jQuery (node).bind (evt, f);
		else if (node.addEventListener) node.addEventListener (evt, f, capture); // FF etc; IE >= 9
		else if (node.attachEvent) node.attachEvent ('on' + evt, f); // Older IE; Opera
		else node['on' + evt] = f; // Very old!
	}

	var catLine      = null;
	var onUpload     = false;
	var editors      = [];

	var commitButton = null;
	var commitForm   = null;
	var multiSpan    = null;

	var pageText     = null;
	var pageTime     = null;
	var pageWatched  = false;
	var watchCreate  = false;
	var watchEdit    = false;
	var minorEdits   = false;
	var editToken    = null;

	var is_rtl       = false;
	var serverTime   = null;
	var lastRevId    = null;
	var pageTextRevId = null;
	var conflictingUser = null;

	var newDOM       = false; // true if MediaWiki serves the new UL-LI DOM for categories

	function setMultiInput () {
		if (commitButton || onUpload) return;
		commitButton = make ('input');
		commitButton.type  = 'button';
		commitButton.value = HotCat.messages.commit;
		commitButton.onclick = multiSubmit;
		if (multiSpan) {
			multiSpan.parentNode.replaceChild (commitButton, multiSpan);
		} else {
			catLine.appendChild (commitButton);
		}
	}

	function checkMultiInput () {
		if (!commitButton) return;
		var has_changes = false;
		for (var i = 0; i < editors.length; i++) {
			if (editors[i].state != CategoryEditor.UNCHANGED) {
				has_changes = true;
				break;
			}
		}
		commitButton.disabled = !has_changes;
	}

	function currentTimestamp () {
		var now = new Date();
		var ts  = "" + now.getUTCFullYear();
		function two (s) { return s.substr (s.length - 2); }
		ts = ts
			+ two ('0' + (now.getUTCMonth() + 1))
			+ two ('0' + now.getUTCDate())
			+ two ('00' + now.getUTCHours())
			+ two ('00' + now.getUTCMinutes())
			+ two ('00' + now.getUTCSeconds());
		return ts;
	}

	var saveInProgress = false;
	function initiateEdit (doEdit, failure) {
		if (saveInProgress) return;
		saveInProgress = true;
		var oldButtonState;
		if (commitButton) {
			oldButtonState = commitButton.disabled;
			commitButton.disabled = true;
		}

		function fail() {
			saveInProgress = false;
			if (commitButton) commitButton.disabled = oldButtonState;
			failure.apply(this, arguments);
		}

		// Must use Ajax here to get the user options and the edit token.

		getJSON ({
			 uri : conf.wgServer + conf.wgScriptPath + '/api.php'
			,data : 'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent (conf.wgPageName)
				+ '&prop=info%7Crevisions%7Clanglinks&inprop=watched&intoken=edit&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500'
				+ '&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId
				+ '&meta=siteinfo%7Cuserinfo&uiprop=options'
			,success : function (json) { setPage(json); doEdit(fail); }
			,error : function (req) { fail(req.status + ' ' + req.statusText); }
		});
	}

	function multiChangeMsg (count) {
		var msg = HotCat.messages.multi_change;
		if (typeof msg != 'string' && msg.length) {
			if (window.mw && mw.language && mw.language.convertPlural) {
				msg = mw.language.convertPlural (count, msg);
			} else {
				msg = msg[msg.length-1];
			}
		}
		return substitute (msg, [null, "" + count]);
	}

	function performChanges (failure, singleEditor) {
		if (pageText === null) {
			failure (HotCat.messages.multi_error);
			return;
		}
		// Backwards compatibility after message change (added $2 to cat_keychange)
		if (HotCat.messages.cat_keychange.indexOf ('$2') < 0) HotCat.messages.cat_keychange += '"$2"';
		// More backwards-compatibility with earlier HotCat versions:
		if (!HotCat.messages.short_catchange) HotCat.messages.short_catchange = '[[' + HotCat.category_canonical + ':$1]]';
		// Create a form and submit it. We don't use the edit API (api.php?action=edit) because
		// (a) sensibly reporting back errors like edit conflicts is always a hassle, and
		// (b) we want to show a diff for multi-edits anyway, and
		// (c) we want to trigger onsubmit events, allowing user code to intercept the edit.
		// Using the form, we can do (b) and (c), and we get (a) for free. And, of course, using the form
		// automatically reloads the page with the updated categories on a successful submit, which
		// we would have to do explicitly if we used the edit API.
		var action;
		// Normally, we don't have to care about edit conflicts. If some other user edited the page in the meantime, the
		// server will take care of it and merge the edit automatically or present an edit conflict screen. However, the
		// server suppresses edit conflicts with oneself. Hence, if we have a conflict, and the conflicting user is the
		// current user, then we set the "oldid" value and switch to diff, which gives the "you are editing an old version;
		// if you save, any more recent changes will be lost" screen.
		var editingOldVersion = lastRevId !== null && lastRevId != conf.wgCurRevisionId || pageTextRevId !== null && pageTextRevId != conf.wgCurRevisionId;
		var selfEditConflict = editingOldVersion && conflictingUser && conflictingUser == conf.wgUserName;
		if (singleEditor && !singleEditor.noCommit && !HotCat.no_autocommit && editToken && !selfEditConflict) {
			// If we do have an edit conflict, but not with ourself, that's no reason not to attempt to save: the server side may actually be able to
			// merge the changes. We just need to make sure that we do present a diff view if it's a self edit conflict.
			commitForm.wpEditToken.value = editToken;
			action = commitForm.wpDiff;
			if (action) action.name = action.value = 'wpSave';
		} else {
			action = commitForm.wpSave;
			if (action) action.name = action.value = 'wpDiff';
		}
		var result = { text : pageText };
		var changed = [], added = [], deleted = [], changes = 0;
		var toEdit = !!singleEditor ? [singleEditor] : editors;
		var error = null;
		var i;
		for (i=0; i < toEdit.length; i++) {
			if (toEdit[i].state == CategoryEditor.CHANGED) {
				result = change_category (
						result.text
					, toEdit[i].originalCategory
					, toEdit[i].currentCategory
					, toEdit[i].currentKey
					, toEdit[i].currentHidden
				);
				if (!result.error) {
					changes++;
					if (!toEdit[i].originalCategory || toEdit[i].originalCategory.length === 0) {
						added.push (toEdit[i].currentCategory);
					} else {
						changed.push ({from : toEdit[i].originalCategory, to : toEdit[i].currentCategory});
					}
				} else if (error === null) {
					error = result.error;
				}
			} else if (   toEdit[i].state == CategoryEditor.DELETED
					   && toEdit[i].originalCategory
					   && toEdit[i].originalCategory.length > 0)
			{
				result = change_category (result.text, toEdit[i].originalCategory, null, null, false);
				if (!result.error) {
					changes++;
					deleted.push (toEdit[i].originalCategory);
				} else if (error === null) {
					error = result.error;
				}
			}
		}
		if (error !== null) { // Do not commit if there were errors
			action = commitForm.wpSave;
			if (action) action.name = action.value = 'wpDiff';
		}
		// Fill in the form and submit it
		commitForm.wpAutoSummary.value = 'd41d8cd98f00b204e9800998ecf8427e'; // MD5 hash of the empty string
		commitForm.wpMinoredit.checked = minorEdits;
		commitForm.wpWatchthis.checked = conf.wgArticleId === 0 && watchCreate || watchEdit || pageWatched;
		if (conf.wgArticleId > 0 || !!singleEditor) {
			if (changes == 1) {
				if (result.summary && result.summary.length > 0)
					commitForm.wpSummary.value = HotCat.messages.prefix + result.summary.join (HotCat.messages.separator) + HotCat.messages.using;
				commitForm.wpMinoredit.checked = HotCat.single_minor || minorEdits;
			} else if (changes > 1) {
				var summary = [];
				var shortSummary = [];
				// Deleted
				for (i = 0; i < deleted.length; i++) {
					summary.push ('-' + substitute (HotCat.messages.short_catchange, [null, deleted[i]]));
				}
				if (deleted.length == 1)
					shortSummary.push ('-' + substitute (HotCat.messages.short_catchange, [null, deleted[0]]));
				else if (deleted.length > 1)
					shortSummary.push ('- ' + multiChangeMsg (deleted.length));
				// Added
				for (i = 0; i < added.length; i++) {
					summary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[i]]));
				}
				if (added.length == 1)
					shortSummary.push ('+' + substitute (HotCat.messages.short_catchange, [null, added[0]]));
				else if (added.length > 1)
					shortSummary.push ('+ ' + multiChangeMsg (added.length));
				// Changed
				var arrow = is_rtl ? '\u2190' : '\u2192'; // left and right arrows. Don't use ← and → in the code.
				for (i = 0; i < changed.length; i++) {
					if (changed[i].from != changed[i].to) {
						summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]) + arrow
										+ substitute (HotCat.messages.short_catchange, [null, changed[i].to]));
					} else {
						summary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[i].from]));
					}
				}
				if (changed.length == 1) {
					if (changed[0].from != changed[0].to) {
						shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]) + arrow
											+ substitute (HotCat.messages.short_catchange, [null, changed[0].to]));
					} else {
						shortSummary.push ('±' + substitute (HotCat.messages.short_catchange, [null, changed[0].from]));
					}
				} else if (changed.length > 1) {
					shortSummary.push ('± ' + multiChangeMsg (changed.length));
				}
				if (summary.length > 0) {
					summary = summary.join (HotCat.messages.separator);
					if (summary.length > 200 - HotCat.messages.prefix.length - HotCat.messages.using.length) {
						summary = shortSummary.join (HotCat.messages.separator);
					}
					commitForm.wpSummary.value = HotCat.messages.prefix + summary + HotCat.messages.using;
				}
			}
		}
		commitForm.wpTextbox1.value = result.text;
		commitForm.wpStarttime.value = serverTime || currentTimestamp ();
		commitForm.wpEdittime.value = pageTime || commitForm.wpStarttime.value;
		if (selfEditConflict) commitForm.oldid.value = "" + (pageTextRevId || conf.wgCurRevisionId);
		// Submit the form in a way that triggers onsubmit events: commitForm.submit() doesn't.
		commitForm.hcCommit.click();
	}

	function resolveMulti (toResolve, callback) {
		var i;
		for (i = 0; i < toResolve.length; i++) {
			toResolve[i].dab = null;
			toResolve[i].dabInput = toResolve[i].lastInput;
		}
		if (noSuggestions) {
			callback (toResolve);
			return;
		}
		// Use %7C instead of |, otherwise Konqueror insists on re-encoding the arguments, resulting in doubly encoded
		// category names. (That is a bug in Konqueror. Other browsers don't have this problem.)
		var args = 'action=query&prop=info%7Clinks%7Ccategories%7Ccategoryinfo&plnamespace=14'
				+ '&pllimit=' + (toResolve.length * 10)
				+ '&cllimit=' + (toResolve.length * 10)
				+ '&format=json&titles=';
		for (i = 0; i < toResolve.length; i++) {
			var v = toResolve[i].dabInput;
			v = replaceShortcuts (v, HotCat.shortcuts);
			toResolve[i].dabInputCleaned = v;
			args += encodeURIComponent ('Category:' + v);
			if (i+1 < toResolve.length) args += '%7C';
		}
		getJSON({
			 uri : conf.wgServer + conf.wgScriptPath + '/api.php'
			,data : args
			,success: function (json) { resolveRedirects (toResolve, json); callback (toResolve); }
			,error: function (req) { if (!req) noSuggestions = true; callback (toResolve); }
		});
	}

	function resolveOne (page, toResolve) {
		var cats     = page.categories;
		var lks      = page.links;
		var is_dab   = false;
		var is_redir = typeof page.redirect == 'string'; // Hard redirect?
		var is_hidden = page.categoryinfo && typeof page.categoryinfo.hidden == 'string';
		var is_missing = typeof page.missing == 'string';
		var i;
		for (i = 0; i < toResolve.length; i++) {
			if (toResolve.length > 1 && toResolve[i].dabInputCleaned != page.title.substring (page.title.indexOf (':') + 1)) continue;
			// Note: the server returns in page an NFC normalized Unicode title. If our input was not NFC normalized, we may not find
			// any entry here. If we have only one editor to resolve (the most common case, I presume), we may simply skip the check.
			toResolve[i].currentHidden = is_hidden;
			toResolve[i].inputExists = !is_missing;
			toResolve[i].icon.src = armorUri(is_missing ? HotCat.existsNo : HotCat.existsYes);
		}
		if (is_missing) return;
		if (!is_redir && cats && (HotCat.disambig_category || HotCat.redir_category)) {
			for (var c = 0; c < cats.length; c++) {
				var cat = cats[c]['title'];
				// Strip namespace prefix
				if (cat) {
					cat = cat.substring (cat.indexOf (':') + 1).replace(/_/g, ' ');
					if (cat == HotCat.disambig_category) {
						is_dab = true; break;
					} else if (cat == HotCat.redir_category) {
						is_redir = true; break;
					}
				}
			}
		}
		if (!is_redir && !is_dab) return;
		if (!lks || lks.length === 0) return;
		var titles = [];
		for (i = 0; i < lks.length; i++) {
			if (   lks[i]['ns'] == 14                             // Category namespace -- always true since we ask only for the category links
				&& lks[i]['title'] && lks[i]['title'].length > 0) // Name not empty
			{
				// Internal link to existing thingy. Extract the page name and remove the namespace.
				var match = lks[i]['title'];
				match = match.substring (match.indexOf (':') + 1);
				// Exclude blacklisted categories.
				if (!HotCat.blacklist || !HotCat.blacklist.test (match)) {
					titles.push (match);
				}
			}
		}
		if (titles.length === 0) {
			return;
		}
		for (i = 0; i < toResolve.length; i++) {
			if (toResolve.length > 1 && toResolve[i].dabInputCleaned != page.title.substring (page.title.indexOf (':') + 1)) continue;
			toResolve[i].inputExists = true; // Might actually be wrong if it's a redirect pointing to a non-existing category
			toResolve[i].icon.src = armorUri(HotCat.existsYes);
			if (titles.length > 1) {
				toResolve[i].dab = titles;
			} else {
				toResolve[i].text.value =
					titles[0] + (toResolve[i].currentKey !== null ? '|' + toResolve[i].currentKey : "");
			}
		}
	}

	function resolveRedirects (toResolve, params) {
		if (!params || !params.query || !params.query.pages) return;
		for (var p in params.query.pages) resolveOne (params.query.pages[p], toResolve);
	}

	function multiSubmit () {
		var toResolve = [];
		for (var i = 0; i < editors.length; i++) {
			if (editors[i].state == CategoryEditor.CHANGE_PENDING || editors[i].state == CategoryEditor.OPEN)
				toResolve.push (editors[i]);
		}
		if (toResolve.length === 0) {
			initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
			return;
		}
		resolveMulti (
			  toResolve
			, function (resolved) {
					var firstDab = null;
					var dontChange = false;
					for (var i = 0; i < resolved.length; i++) {
						if (resolved[i].lastInput != resolved[i].dabInput) {
							// We didn't disable all the open editors, but we did asynchronous calls. It is
							// theoretically possible that the user changed something...
							dontChange = true;
						} else {
							if (resolved[i].dab) {
								if (!firstDab) firstDab = resolved[i];
							} else {
								if (resolved[i].acceptCheck(true)) resolved[i].commit();
							}
						}
					}
					if (firstDab) {
						showDab (firstDab);
					} else if (!dontChange) {
						initiateEdit (function (failure) {performChanges (failure);}, function (msg) {alert (msg);});
					}
				}
		);
	}

	var cat_prefix = null;
	var noSuggestions = false;
	var suggestionEngines = {
		opensearch :
			{ uri     : '/api.php?format=json&action=opensearch&namespace=14&limit=30&search=Category:$1' // $1 = search term
			 ,handler : // Function to convert result of uri into an array of category names
				function (queryResult, queryKey) {
					if (queryResult && queryResult.length >= 2) {
						var key = queryResult[0].substring(queryResult[0].indexOf(':') + 1);
						var titles = queryResult[1];
						var exists = false;
						if (!cat_prefix) cat_prefix = new RegExp ('^(' + HotCat.category_regexp + ':)');
						for (var i = 0; i < titles.length; i++) {
							cat_prefix.lastIndex = 0;
							var m = cat_prefix.exec (titles[i]);
							if (m && m.length > 1) {
								titles[i] = titles[i].substring (titles[i].indexOf (':') + 1); // rm namespace
								if (key == titles[i]) exists = true;
							} else {
								titles.splice (i, 1); // Nope, it's not a category after all.
								i--;
							}
						}
						titles.exists = exists;
						if (queryKey != key) titles.normalized = key; // Remember the NFC normalized key we got back from the server
						return titles;
					}
					return null;
				}
			}
		,internalsearch :
			{ uri     : '/api.php?format=json&action=query&list=allpages&apnamespace=14&aplimit=30&apfrom=$1&apprefix=$1'
			 ,handler :
				function (queryResult, queryKey) {
					if (queryResult && queryResult.query && queryResult.query.allpages) {
						var titles = queryResult.query.allpages;
						for (var i = 0; i < titles.length; i++) {
							titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
						}
						return titles;
					}
					return null;
				}
			}
		,exists :
			{ uri     : '/api.php?format=json&action=query&prop=info&titles=Category:$1'
			 ,handler :
				function (queryResult, queryKey) {
					if (queryResult && queryResult.query && queryResult.query.pages && !queryResult.query.pages[-1]) {
						// Should have exactly 1
						for (var p in queryResult.query.pages) {
							var title = queryResult.query.pages[p].title;
							title = title.substring (title.indexOf (':') + 1);
							var titles = [title];
							titles.exists = true;
							if (queryKey != title) titles.normalized = title; // NFC
							return titles;
						}
					}
					return null;
				}
			}
		,subcategories :
			// I don't understand why they didn't map cmnamespace=14 automatically to cmtype=subcat,
			// which gives better results and is faster.
			{ uri     : '/api.php?format=json&action=query&list=categorymembers'
				+(function (version) {
					var m = version.match(/^(\d+)\.(\d+)/);
					var major = 0, minor = 0;
					if (m && m.length > 1) {
						major = parseInt (m[1], 10);
						minor = (m.length > 2 ? parseInt (m[2], 10) : 0);
					}
					if (major > 1 || major === 1 && minor > 17) return '&cmtype=subcat'; // Since MW1.18
					return '&cmnamespace=14';
				  }
				)(conf.wgVersion)
				+'&cmlimit=max&cmtitle=Category:$1'
			 ,handler :
				function (queryResult, queryKey) {
					if (queryResult && queryResult.query && queryResult.query.categorymembers) {
						var titles = queryResult.query.categorymembers;
						for (var i = 0; i < titles.length; i++) {
							titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
						}
						return titles;
					}
					return null;
				}
			}
		,parentcategories :
			{ uri     : '/api.php?format=json&action=query&prop=categories&titles=Category:$1&cllimit=max'
			 ,handler :
				function (queryResult, queryKey) {
					if (queryResult && queryResult.query && queryResult.query.pages) {
						for (var p in queryResult.query.pages) {
							if (queryResult.query.pages[p].categories) {
								var titles = queryResult.query.pages[p].categories;
								for (var i = 0; i < titles.length; i++) {
									titles[i] = titles[i].title.substring (titles[i].title.indexOf (':') + 1); // rm namespace
								}
								return titles;
							}
						}
					}
					return null;
				}
			}
	};

	var suggestionConfigs = {
		 searchindex : {name: 'Search index', engines: ['opensearch'], cache: {}, show: true, temp: false, noCompletion : false}
		,pagelist    : {name: 'Page list', engines: ['internalsearch', 'exists'], cache: {}, show: true, temp: false, noCompletion : false}
		,combined    : {name: 'Combined search', engines: ['opensearch', 'internalsearch'], cache: {}, show: true, temp: false, noCompletion : false}
		,subcat      : {name: 'Subcategories', engines: ['subcategories'], cache: {}, show: true, temp: true, noCompletion : true}
		,parentcat   : {name: 'Parent categories', engines: ['parentcategories'], cache: {}, show: true, temp: true, noCompletion : true}
	};

	function CategoryEditor () { this.initialize.apply (this, arguments); }
	CategoryEditor.UNCHANGED      = 0;
	CategoryEditor.OPEN           = 1; // Open, but no input yet
	CategoryEditor.CHANGE_PENDING = 2; // Open, some input made
	CategoryEditor.CHANGED        = 3;
	CategoryEditor.DELETED        = 4;

	// IE6 sometimes forgets to redraw the list when editors are opened or closed.
	// Adding/removing a dummy element helps, at least when opening editors.
	var dummyElement = make ('\xa0', true);

	function forceRedraw () {
		if (!is_ie6) return;
		if (dummyElement.parentNode) {
			document.body.removeChild (dummyElement);
		} else {
			document.body.appendChild (dummyElement);
		}
	}

	// Event keyCodes that we handle in the text input field/suggestion list.
	var BS = 8, TAB = 9, RET = 13, ESC = 27, SPACE = 32, PGUP = 33, PGDOWN = 34, UP = 38, DOWN = 40, DEL = 46, IME = 229;
	
	function makeActive (which) {
		if (which.is_active) return;
		for (var i = 0; i < editors.length; i++) {
			if (editors[i] !== which) editors[i].inactivate ();
		}
		which.is_active = true;
		if (which.dab) {
			showDab (which);
		} else {
			// Check for programmatic value changes.
			var expectedInput = which.lastRealInput || which.lastInput || "";
			var actualValue = which.text.value || "";
			if (expectedInput.length === 0 && actualValue.length > 0 || expectedInput.length > 0 && actualValue.indexOf (expectedInput) !== 0) {
				// Somehow the field's value appears to have changed, and which.lastSelection therefore is no longer valid. Try to set the
				// cursor at the end of the category, and do not display the old suggestion list.
				which.showsList = false;
				var v = actualValue.split('|');
				which.lastRealInput = which.lastInput = v[0];
				if (v.length > 1) which.currentKey = v[1];
				if (which.lastSelection) which.lastSelection = {start: v[0].length, end: v[0].length};
			}
			if (which.showsList) which.displayList();
			if (which.lastSelection) {
				if (is_webkit) {
					// WebKit (Safari, Chrome) has problems selecting inside focus()
					// See http://code.google.com/p/chromium/issues/detail?id=32865#c6
					window.setTimeout (
						 function () { which.setSelection (which.lastSelection.start, which.lastSelection.end); }
						,1
					);
				} else {
					which.setSelection (which.lastSelection.start, which.lastSelection.end);
				}
			}
		}
	}

	function showDab (which) {
		if (!which.is_active) {
			makeActive(which);
		} else {
			which.showSuggestions (which.dab, false, null, null); // do autocompletion, no key, no engine selector
			which.dab = null;
		}
	}

	CategoryEditor.prototype = {

		initialize : function (line, span, after, key, is_hidden) {
			// If a span is given, 'after' is the category title, otherwise it may be an element after which to
			// insert the new span. 'key' is likewise overloaded; if a span is given, it is the category key (if
			// known), otherwise it is a boolean indicating whether a bar shall be prepended.
			if (!span) {
				this.isAddCategory = true;
				// Create add span and append to catLinks
				this.originalCategory = "";
				this.originalKey = null;
				this.originalExists   = false;
				if (!newDOM) {
					span = make ('span');
					span.className = 'noprint';
					if (key) {
						span.appendChild (make (' | ', true));
						if (after) {
							after.parentNode.insertBefore (span, after.nextSibling);
							after = after.nextSibling;
						} else {
							line.appendChild (span);
						}
					} else if (line.firstChild) {
						span.appendChild (make (' ', true));
						line.appendChild (span);
					}
				}
				this.linkSpan = make ('span');
				this.linkSpan.className = 'noprint nopopups hotcatlink';
				var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
				lk.appendChild (make (HotCat.links.add, true)); lk.title = HotCat.tooltips.add;
				this.linkSpan.appendChild (lk);
				span = make (newDOM ? 'li' : 'span');
				span.className = 'noprint';
				if (is_rtl) span.dir = 'rtl';
				span.appendChild (this.linkSpan);
				if (after)
					after.parentNode.insertBefore (span, after.nextSibling);
				else
					line.appendChild (span);
				this.normalLinks = null;
				this.undelLink = null;
				this.catLink = null;
			} else {
				if (is_rtl) span.dir = 'rtl';
				this.isAddCategory = false;
				this.catLink = span.firstChild;
				this.originalCategory = after;
				this.originalKey = (key && key.length > 1) ? key.substr(1) : null; // > 1 because it includes the leading bar
				this.originalExists   = !hasClass (this.catLink, 'new');
				// Create change and del links
				this.makeLinkSpan ();
				if (!this.originalExists && this.upDownLinks) this.upDownLinks.style.display = 'none';
				span.appendChild (this.linkSpan);
			}
			this.originalHidden     = is_hidden;
			this.line               = line;
			this.engine             = HotCat.suggestions;
			this.span               = span;
			this.currentCategory    = this.originalCategory;
			this.currentExists      = this.originalExists;
			this.currentHidden      = this.originalHidden;
			this.currentKey         = this.originalKey;
			this.state              = CategoryEditor.UNCHANGED;
			this.lastSavedState     = CategoryEditor.UNCHANGED;
			this.lastSavedCategory  = this.originalCategory;
			this.lastSavedKey       = this.originalKey;
			this.lastSavedExists    = this.originalExists;
			this.lastSavedHidden    = this.originalHidden;
			if (this.catLink && this.currentKey) {
				this.catLink.title = this.currentKey;
			}
			editors[editors.length] = this;
		},

		makeLinkSpan : function () {
			this.normalLinks = make ('span');
			var lk = null;
			if (this.originalCategory && this.originalCategory.length > 0) {
				lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.remove, this);
				lk.appendChild (make (HotCat.links.remove, true)); lk.title = HotCat.tooltips.remove;
				this.normalLinks.appendChild (make (' ', true));
				this.normalLinks.appendChild (lk);
			}
			if (!HotCat.template_categories[this.originalCategory]) {
				lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.open, this);
				lk.appendChild (make (HotCat.links.change, true)); lk.title = HotCat.tooltips.change;
				this.normalLinks.appendChild (make (' ', true));
				this.normalLinks.appendChild (lk);
				if (!noSuggestions && HotCat.use_up_down) {
					this.upDownLinks = make ('span');
					lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.down, this);
					lk.appendChild (make (HotCat.links.down, true)); lk.title = HotCat.tooltips.down;
					this.upDownLinks.appendChild (make (' ', true));
					this.upDownLinks.appendChild (lk);
					lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.up, this);
					lk.appendChild (make (HotCat.links.up, true)); lk.title = HotCat.tooltips.up;
					this.upDownLinks.appendChild (make (' ', true));
					this.upDownLinks.appendChild (lk);
					this.normalLinks.appendChild (this.upDownLinks);
				}
			}
			this.linkSpan = make ('span');
			this.linkSpan.className = 'noprint nopopups hotcatlink';
			this.linkSpan.appendChild (this.normalLinks);
			this.undelLink = make ('span');
			this.undelLink.className = 'nopopups hotcatlink';
			this.undelLink.style.display = 'none';
			lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.restore, this);
			lk.appendChild (make (HotCat.links.restore, true)); lk.title = HotCat.tooltips.restore;
			this.undelLink.appendChild (make (' ', true));
			this.undelLink.appendChild (lk);
			this.linkSpan.appendChild (this.undelLink);
		},

		invokeSuggestions : function (dont_autocomplete) {
			if (this.engine && suggestionConfigs[this.engine] && suggestionConfigs[this.engine].temp && !dont_autocomplete) {
				this.engine = HotCat.suggestions; // Reset to a search upon input
			}
			this.state = CategoryEditor.CHANGE_PENDING;
			var self = this;
			window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
		},

		makeForm : function () {
			var form = make ('form');
			form.method = 'POST'; form.onsubmit = bind (this.accept, this);
			this.form = form;
			var self = this;
			var text = make ('input'); text.type = 'text'; text.size = HotCat.editbox_width;
			if (!noSuggestions) {
				// Be careful here to handle IME input. This is browser/OS/IME dependent, but basically there are two mechanisms:
				// - Modern (DOM Level 3) browsers use compositionstart/compositionend events to signal composition; if the
				//   composition is not canceled, there'll be a textInput event following. During a composition key events are
				//   either all suppressed (FF/Gecko), or otherwise have keyDown === IME for all keys (Webkit).
				//   - Webkit sends a textInput followed by keyDown === IME and a keyUp with the key that ended composition.
				//   - Gecko doesn't send textInput but just a keyUp with the key that ended composition, without sending keyDown
				//     first. Gecko doesn't send any keydown while IME is active.
				// - Older browsers signal composition by keyDown === IME for the first and subsequent keys for a composition. The
				//   first keyDown !== IME is certainly after the end of the composition. Typically, composition end can also be
				//   detected by a keyDown IME with a keyUp of space, tab, escape, or return. (Example: IE8)
				text.onkeyup =
					function (evt) {
						evt = evt || window.event || window.Event; // W3C, IE, Netscape
						var key = evt.keyCode || 0;
						if (self.ime && self.lastKey === IME && !self.usesComposition && (key === TAB || key === RET || key == ESC || key === SPACE)) self.ime = false;
						if (self.ime) return true;
						if (key === UP || key === DOWN || key === PGUP || key === PGDOWN) {
							// In case a browser doesn't generate keypress events for arrow keys...
							if (self.keyCount === 0) return self.processKey (evt);
						} else {
							if (key === ESC && self.lastKey !== IME) {
								if (!self.resetKeySelection ()) {
									// No undo of key selection: treat ESC as "cancel".
									self.cancel ();
									return;
								}
							}
							// Also do this for ESC as a workaround for Firefox bug 524360
							// https://bugzilla.mozilla.org/show_bug.cgi?id=524360
							self.invokeSuggestions (key === BS || key === DEL || key === ESC);
						}
						return true;
					};
				text.onkeydown =
					function (evt) {
						evt = evt || window.event || window.Event; // W3C, IE, Netscape
						var key = evt.keyCode || 0;
						self.lastKey = key;
						self.keyCount = 0;
						// DOM Level < 3 IME input	
						if (!self.ime && key === IME && !self.usesComposition) {
							// self.usesComposition catches browsers that may emit spurious keydown IME after a composition has ended
							self.ime = true;
						} else if (self.ime && key !== IME && !(key >= 16 && key <= 20 || key >= 91 && key <= 93 || key === 144)) {
							// Ignore control keys: ctrl, shift, alt, alt gr, caps lock, windows/apple cmd keys, num lock. Only the windows keys
							// terminate IME (apple cmd doesn't), but they also cause a blur, so it's OK to ignore them here.
							// Note: Safari 4 (530.17) propagates ESC out of an IME composition (observed at least on Win XP).
							self.ime = false;
						}
						if (self.ime) return true;
						// Handle return explicitly, to override the default form submission to be able to check for ctrl
						if (key === RET) return self.accept (evt);
						// Inhibit default behavior of ESC (revert to last real input in FF: we do that ourselves)
						return (key === ESC) ? evtKill(evt) : true;
					};
				// And handle continued pressing of arrow keys
				text.onkeypress = function (evt) {self.keyCount++; return self.processKey (evt);};
				addEvent (text, 'focus', function () { makeActive(self); });
				// On IE, blur events are asynchronous, and may thus arrive after the element has lost the focus. Since IE
				// can get the selection only while the element is active (has the focus), we may not always get the selection.
				// Therefore, use an IE-specific synchronous event on IE...
				// Don't test for text.selectionStart being defined; FF3.6.4 raises an exception when trying to access that
				// property while the element is not being displayed.
				addEvent (text
					, (typeof text.onbeforedeactivate != 'undefined' && text.createTextRange) ? 'beforedeactivate' : 'blur'
					, bind (this.saveView, this)
				);
				// DOM Level 3 IME handling
				try {
					// Setting lastKey = IME provides a fake keyDown for Gecko's single keyUp after a cmposition. If we didn't do this,
					// cancelling a composition via ESC would also cancel and close the whole category input editor.
					addEvent(text, 'compositionstart', function (evt) { self.lastKey = IME; self.usesComposition = true; self.ime = true; });
					addEvent(text, 'compositionend', function (evt) { self.lastKey = IME; self.usesComposition = true; self.ime = false; });
					addEvent(text, 'textInput', function (evt) { self.ime = false; self.invokeSuggestions(false); });
				} catch (any) {
					// Just in case some browsers might produce exceptions with these DOM Level 3 events
				}
				addEvent(text, 'blur', function (evt) { self.usesComposition = false; self.ime = false; });
			}
			this.text = text;

			this.icon = make ('img');

			var list = null;
			if (!noSuggestions) {
				list = make ('select');
				list.onclick    = function (e) { if (self.highlightSuggestion(0)) self.textchange (false, true); };
				list.ondblclick = function (e) { if (self.highlightSuggestion(0)) self.accept (e); };
				list.onchange = function (e) { self.highlightSuggestion(0); self.text.focus(); };
				list.onkeyup =
					function (evt) {
						evt = evt || window.event || window.Event; // W3C, IE, Netscape
						if (evt.keyCode === ESC) {
							self.resetKeySelection ();
							self.text.focus();
							window.setTimeout (function () {self.textchange (true);}, HotCat.suggest_delay);
						} else if (evt.keyCode === RET) {
							self.accept (evt);
						}
					};
				if (!HotCat.fixed_search) {
					var engineSelector = make ('select');
					for (var key in suggestionConfigs) {
						if (suggestionConfigs[key].show) {
							var opt = make ('option');
							opt.value = key;
							if (key == this.engine) opt.selected = true;
							opt.appendChild (make (suggestionConfigs[key].name, true));
							engineSelector.appendChild (opt);
						}
					}
					engineSelector.onchange =
						function () {
							self.engine = self.engineSelector.options[self.engineSelector.selectedIndex].value;
							self.text.focus();
							self.textchange (true, true); // Don't autocomplete, force re-display of list
						};
					this.engineSelector = engineSelector;
				}
			}
			this.list = list;

			function button_label (id, defaultText) {
				var label = null;
				if (   onUpload
					&& typeof UFUI != 'undefined'
					&& typeof UIElements != 'undefined'
					&& typeof UFUI.getLabel == 'function')
				{
					try {
						label = UFUI.getLabel (id, true);
						// Extract the plain text. IE doesn't know that Node.TEXT_NODE === 3
						while (label && label.nodeType != 3) label = label.firstChild;
					} catch (ex) {
						label = null;
					}
				}
				if (!label || !label.data) return defaultText;
				return label.data;
			}

			// Do not use type 'submit'; we cannot detect modifier keys if we do
			var OK = make ('input'); OK.type = 'button';
			OK.value = button_label ('wpOkUploadLbl', HotCat.messages.ok);
			OK.onclick = bind (this.accept, this);
			this.ok = OK;

			var cancel = make ('input'); cancel.type = 'button';
			cancel.value = button_label ('wpCancelUploadLbl', HotCat.messages.cancel);
			cancel.onclick = bind (this.cancel, this);
			this.cancelButton = cancel;

			var span = make ('span');
			span.className = 'hotcatinput';
			span.style.position = 'relative';
			// FF3.6: add the input field first, then the two absolutely positioned elements. Otherwise, FF3.6 may leave the
			// suggestions and the selector at the right edge of the screen if display of the input field causes a re-layout
			// moving the form to the front of the next line.
			span.appendChild (text);

			// IE8/IE9: put some text into this span (a0 is nbsp) and make sure it always stays on the
			// same line as the input field, otherwise, IE8/9 miscalculates the height of the span and
			// then the engine selector may overlap the input field.
			span.appendChild (make ('\xa0', true));
			span.style.whiteSpace = 'nowrap';

			if (list) span.appendChild (list);
			if (this.engineSelector) span.appendChild (this.engineSelector);
			if (!noSuggestions) span.appendChild (this.icon);
			span.appendChild (OK);
			span.appendChild (cancel);
			form.appendChild(span);
			form.style.display = 'none';
			this.span.appendChild (form);
		},

		display : function (evt) {
			if (this.isAddCategory && !onUpload) {
				var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
			}
			if (!commitButton && !onUpload) {
				for (var i = 0; i < editors.length; i++) {
					if (editors[i].state != CategoryEditor.UNCHANGED) {
						setMultiInput();
						break;
					}
				}
			}
			if (!this.form) {
				this.makeForm ();
			}
			if (this.list) this.list.style.display = 'none';
			if (this.engineSelector) this.engineSelector.style.display = 'none';
			this.currentCategory = this.lastSavedCategory;
			this.currentExists   = this.lastSavedExists;
			this.currentHidden   = this.lastSavedHidden;
			this.currentKey      = this.lastSavedKey;
			this.icon.src = armorUri(this.currentExists ? HotCat.existsYes : HotCat.existsNo);
			this.text.value = this.currentCategory + (this.currentKey !== null ? '|' + this.currentKey : "");
			this.originalState = this.state;
			this.lastInput     = this.currentCategory;
			this.inputExists   = this.currentExists;
			this.state         = this.state == CategoryEditor.UNCHANGED ? CategoryEditor.OPEN : CategoryEditor.CHANGE_PENDING;
			this.lastSelection = {start: this.currentCategory.length, end: this.currentCategory.length};
			this.showsList = false;
			// Display the form
			if (this.catLink) this.catLink.style.display = 'none';
			this.linkSpan.style.display = 'none';
			this.form.style.display = 'inline';
			this.ok.disabled = false;
			// Kill the event before focussing, otherwise IE will kill the onfocus event!
			var result = evtKill (evt);
			this.text.focus();
			this.text.readOnly = false;
			checkMultiInput ();
			return result;
		},

		show : function (evt, engine, readOnly) {
			var result = this.display (evt);
			var v = this.lastSavedCategory;
			if (v.length === 0) return result;
			this.text.readOnly = !!readOnly;
			this.engine = engine;
			this.textchange (false, true); // do autocompletion, force display of suggestions
			forceRedraw ();
			return result;
		},

		open : function (evt) {
			return this.show (evt, (this.engine && suggestionConfigs[this.engine].temp) ? HotCat.suggestions : this.engine);
		},

		down : function (evt) {
			return this.show (evt, 'subcat', true);
		},

		up : function (evt) {
			return this.show (evt, 'parentcat');
		},

		cancel : function () {
			if (this.isAddCategory && !onUpload) {
				this.removeEditor(); // We added a new adder when opening
				return;
			}
			// Close, re-display link
			this.inactivate();
			this.form.style.display = 'none';
			if (this.catLink) this.catLink.style.display = "";
			this.linkSpan.style.display = "";
			this.state = this.originalState;
			this.currentCategory = this.lastSavedCategory;
			this.currentKey      = this.lastSavedKey;
			this.currentExists   = this.lastSavedExists;
			this.currentHidden   = this.lastSavedHidden;
			if (this.catLink) {
				if (this.currentKey && this.currentKey.length > 0) {
					this.catLink.title = this.currentKey;
				} else {
					this.catLink.title = "";
				}
			}
			if (this.state == CategoryEditor.UNCHANGED) {
				if (this.catLink) this.catLink.style.backgroundColor = 'transparent';
			} else {
				if (!onUpload) {
					try {
						this.catLink.style.backgroundColor = HotCat.bg_changed;
					} catch (ex) {}
				}
			}
			checkMultiInput ();
			forceRedraw ();
		},

		removeEditor : function () {
			if (!newDOM) {
				var next = this.span.nextSibling;
				if (next) next.parentNode.removeChild (next);
			}
			this.span.parentNode.removeChild (this.span);
			for (var i = 0; i < editors.length; i++) {
				if (editors[i] == this) {
					editors.splice (i, 1);
					break;
				}
			}
			checkMultiInput ();
			var self = this;
			window.setTimeout (function () {delete self;}, 10);
		},

		rollback : function (evt) {
			this.undoLink.parentNode.removeChild (this.undoLink);
			this.undoLink = null;
			this.currentCategory = this.originalCategory;
			this.currentKey = this.originalKey;
			this.currentExists = this.originalExists;
			this.currentHidden = this.originalHidden;
			this.lastSavedCategory = this.originalCategory;
			this.lastSavedKey = this.originalKey;
			this.lastSavedExists = this.originalExists;
			this.lastSavedHidden = this.originalHidden;
			this.state = CategoryEditor.UNCHANGED;
			if (!this.currentCategory || this.currentCategory.length === 0) {
				// It was a newly added category. Remove the whole editor.
				this.removeEditor();
			} else {
				// Redisplay the link...
				this.catLink.removeChild (this.catLink.firstChild);
				this.catLink.appendChild (make (this.currentCategory, true));
				this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
				this.catLink.title = this.currentKey || "";
				this.catLink.className = this.currentExists ? "" : 'new';
				this.catLink.style.backgroundColor = 'transparent';
				if (this.upDownLinks) this.upDownLinks.style.display = this.currentExists ? "" : 'none';
				checkMultiInput ();
			}
			return evtKill (evt);
		},

		inactivate : function () {
			if (this.list) this.list.style.display = 'none';
			if (this.engineSelector) this.engineSelector.style.display = 'none';
			this.is_active = false;
		},

		acceptCheck : function (dontCheck) {
			this.sanitizeInput ();
			var value = this.text.value.split('|');
			var key   = null;
			if (value.length > 1) key = value[1];
			var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
			if (HotCat.capitalizePageNames) v = capitalize (v);
			this.lastInput = v;
			v = replaceShortcuts(v, HotCat.shortcuts);
			if (v.length === 0) {
				this.cancel ();
				return false;
			}
			if (!dontCheck
				&& (   conf.wgNamespaceNumber === 14 && v == conf.wgTitle
					|| HotCat.blacklist && HotCat.blacklist.test(v))
			   )
			{
				this.cancel ();
				return false;
			}
			this.currentCategory = v;
			this.currentKey = key;
			this.currentExists = this.inputExists;
			return true;
		},

		accept : function (evt) {
			this.noCommit = (evtKeys (evt) & 1) !== 0;
			var result = evtKill (evt);
			if (this.acceptCheck ()) {
				var toResolve = [this];
				var original  = this.currentCategory;
				resolveMulti (
					 toResolve
					,function (resolved) {
						if (resolved[0].dab) {
							showDab (resolved[0]);
						} else {
							if (resolved[0].acceptCheck(true)) {
								resolved[0].commit (
									(resolved[0].currentCategory != original)
										? HotCat.messages.cat_resolved.replace (/\$1/g, original)
										: null
								);
							}
						}
					 }
				);
			}
			return result;
		},

		close : function () {
			if (!this.catLink) {
				// Create a catLink
				this.catLink = make ('a');
				this.catLink.appendChild (make ('foo', true));
				this.catLink.style.display = 'none';
				this.span.insertBefore (this.catLink, this.span.firstChild.nextSibling);
			}
			this.catLink.removeChild (this.catLink.firstChild);
			this.catLink.appendChild (make (this.currentCategory, true));
			this.catLink.href = wikiPagePath (HotCat.category_canonical + ':' + this.currentCategory);
			this.catLink.className = this.currentExists ? "" : 'new';
			this.lastSavedCategory = this.currentCategory;
			this.lastSavedKey      = this.currentKey;
			this.lastSavedExists   = this.currentExists;
			this.lastSavedHidden   = this.currentHidden;
			// Close form and redisplay category
			this.inactivate();
			this.form.style.display = 'none';
			this.catLink.title = this.currentKey || "";
			this.catLink.style.display = "";
			if (this.isAddCategory) {
				if (onUpload) {
					var newAdder = new CategoryEditor (this.line, null, this.span, true); // Create a new one
				}
				this.isAddCategory = false;
				this.linkSpan.parentNode.removeChild (this.linkSpan);
				this.makeLinkSpan ();
				this.span.appendChild (this.linkSpan);
			}
			if (!this.undoLink) {
				// Append an undo link.
				var span = make ('span');
				var lk = make ('a'); lk.href = '#catlinks'; lk.onclick = bind (this.rollback, this);
				lk.appendChild (make (HotCat.links.undo, true)); lk.title = HotCat.tooltips.undo;
				span.appendChild (make (' ', true));
				span.appendChild (lk);
				this.normalLinks.appendChild (span);
				this.undoLink = span;
				if (!onUpload) {
					try {
						this.catLink.style.backgroundColor = HotCat.bg_changed;
					} catch (ex) {}
				}
			}
			if (this.upDownLinks) this.upDownLinks.style.display = this.lastSavedExists ? "" : 'none';
			this.linkSpan.style.display = "";
			this.state = CategoryEditor.CHANGED;
			checkMultiInput ();
			forceRedraw ();
		},

		commit : function (comment) {
			// Check again to catch problem cases after redirect resolution
			if (   (   this.currentCategory == this.originalCategory
					&& (this.currentKey == this.originalKey
						|| this.currentKey === null && this.originalKey.length === 0
					   )
				   )
				|| conf.wgNamespaceNumber == 14 && this.currentCategory == conf.wgTitle
				|| HotCat.blacklist && HotCat.blacklist.test (this.currentCategory)
			   )
			{
				this.cancel ();
				return;
			}
			if (commitButton || onUpload) {
				this.close ();
			} else {
				this.close ();
				var self = this;
				initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {alert (msg);});
			}
		},

		remove : function (evt) {
			this.doRemove (evtKeys (evt) & 1);
			return evtKill (evt);
		},

		doRemove : function (noCommit) {
			if (this.isAddCategory) { // Empty input on adding a new category
				this.cancel ();
				return;
			}
			if (!commitButton && !onUpload) {
				for (var i = 0; i < editors.length; i++) {
					if (editors[i].state != CategoryEditor.UNCHANGED) {
						setMultiInput();
						break;
					}
				}
			}
			if (commitButton) {
				this.catLink.title = "";
				this.catLink.style.cssText += '; text-decoration : line-through !important;';
				try {
					this.catLink.style.backgroundColor = HotCat.bg_changed;
				} catch (ex) {}
				this.originalState = this.state;
				this.state = CategoryEditor.DELETED;
				this.normalLinks.style.display = 'none';
				this.undelLink.style.display = "";
				checkMultiInput ();
			} else {
				if (onUpload) {
					// Remove this editor completely
					this.removeEditor ();
				} else {
					this.originalState = this.state;
					this.state = CategoryEditor.DELETED;
					this.noCommit = noCommit || HotCat.del_needs_diff;
					var self = this;
					initiateEdit (function (failure) {performChanges (failure, self);}, function (msg) {self.state = self.originalState; alert (msg);});
				}
			}
		},

		restore : function (evt) {
			// Can occur only if we do have a commit button and are not on the upload form
			this.catLink.title = this.currentKey || "";
			this.catLink.style.textDecoration = "";
			this.state = this.originalState;
			if (this.state == CategoryEditor.UNCHANGED) {
				this.catLink.style.backgroundColor = 'transparent';
			} else {
				try {
					this.catLink.style.backgroundColor = HotCat.bg_changed;
				} catch (ex) {}
			}
			this.normalLinks.style.display = "";
			this.undelLink.style.display = 'none';
			checkMultiInput ();
			return evtKill (evt);
		},

		// Internal operations

		selectEngine : function (engineName) {
			if (!this.engineSelector) return;
			for (var i = 0; i < this.engineSelector.options.length; i++) {
				this.engineSelector.options[i].selected = this.engineSelector.options[i].value == engineName;
			}
		},

		sanitizeInput : function () {
			var v = this.text.value || "";
			v = v.replace(/^(\s|_)+/, ""); // Trim leading blanks and underscores
			var re = new RegExp ('^(' + HotCat.category_regexp + '):');
			if (re.test (v)) {
				v = v.substring (v.indexOf (':') + 1).replace(/^(\s|_)+/, "");
			}
			if (HotCat.capitalizePageNames) v = capitalize (v);
			// Only update the input field if there is a difference. IE8 appears to reset the selection
			// and place the cursor at the front upon reset, which makes our autocompletetion become a
			// nuisance. FF and IE6 don't seem to have this problem.
			if (this.text.value !== null && this.text.value != v)
				this.text.value = v;
		},

		makeCall : function (url, callbackObj, engine, queryKey, cleanKey) {
			var cb = callbackObj;
			var e  = engine;
			var v  = queryKey;
			var z  = cleanKey;
			var thisObj = this;

			function done () {
				cb.callsMade++;
				if (cb.callsMade === cb.nofCalls) {
					if (cb.exists) cb.allTitles.exists = true;
					if (cb.normalized) cb.allTitles.normalized = cb.normalized;
					if (!cb.dontCache && !suggestionConfigs[cb.engineName].cache[z]) {
						suggestionConfigs[cb.engineName].cache[z] = cb.allTitles;
					}
					thisObj.text.readOnly = false;
					if (!cb.cancelled) thisObj.showSuggestions (cb.allTitles, cb.noCompletion, v, cb.engineName);
					if (cb === thisObj.callbackObj) thisObj.callbackObj = null;
					delete cb;
				}
			}

			getJSON ({
			  uri : url
			 ,success : function (json) {
				var titles = e.handler (json, z);
				if (titles && titles.length > 0) {
					if (cb.allTitles === null) {
						cb.allTitles = titles;
					} else {
						cb.allTitles = cb.allTitles.concat (titles);
					}
					if (titles.exists) cb.exists = true;
					if (titles.normalized) cb.normalized = titles.normalized;
				}
				done();
			  }
			 ,error : function (req) {if (!req) noSuggestions = true; cb.dontCache = true; done(); }
			});
		},

		callbackObj : null,

		textchange : function (dont_autocomplete, force) {
			// Hide all other lists
			makeActive (this);
			// Get input value, omit sort key, if any
			this.sanitizeInput ();
			var v = this.text.value;
			// Disregard anything after a pipe.
			var pipe = v.indexOf ('|');
			if (pipe >= 0) {
				this.currentKey = v.substring (pipe+1);
				v = v.substring (0, pipe);
			} else {
				this.currentKey = null;
			}
			if (this.lastInput == v && !force) return; // No change
			if (this.lastInput != v) checkMultiInput ();
			this.lastInput = v;
			this.lastRealInput = v;

			// Mark blacklisted inputs.
			this.ok.disabled = v.length > 0 && HotCat.blacklist && HotCat.blacklist.test (v);

			if (noSuggestions) {
				// No Ajax: just make sure the list is hidden
				if (this.list) this.list.style.display = 'none';
				if (this.engineSelector) this.engineSelector.style.display = 'none';
				if (this.icon) this.icon.style.display = 'none';
				return;
			}

			if (v.length === 0) { this.showSuggestions([]); return; }
			var cleanKey = v.replace(/[\u200E\u200F\u202A-\u202E]/g, "").replace(wikiTextBlankRE, ' ');
			cleanKey = replaceShortcuts(cleanKey, HotCat.shortcuts);
			cleanKey = cleanKey.replace(/^\s+|\s+$/g, '');
			if (cleanKey.length === 0) { this.showSuggestions([]); return; }

			if (this.callbackObj) this.callbackObj.cancelled = true;
			var engineName  = suggestionConfigs[this.engine] ? this.engine : 'combined';

			dont_autocomplete = dont_autocomplete || suggestionConfigs[engineName].noCompletion;
			if (suggestionConfigs[engineName].cache[cleanKey]) {
				this.showSuggestions (suggestionConfigs[engineName].cache[cleanKey], dont_autocomplete, v, engineName);
				return;
			}

			var engines = suggestionConfigs[engineName].engines;
			this.callbackObj =
				{allTitles: null, callsMade: 0, nofCalls: engines.length, noCompletion: dont_autocomplete, engineName: engineName};
			this.makeCalls (engines, this.callbackObj, v, cleanKey);
		},

		makeCalls : function (engines, cb, v, cleanKey) {
			for (var j = 0; j < engines.length; j++) {
				var engine = suggestionEngines[engines[j]];
				var url = conf.wgServer + conf.wgScriptPath + engine.uri.replace (/\$1/g, encodeURIComponent (cleanKey));
				this.makeCall (url, cb, engine, v, cleanKey);
			}
		},

		showSuggestions : function (titles, dontAutocomplete, queryKey, engineName) {
			this.text.readOnly = false;
			this.dab = null;
			this.showsList = false;
			if (!this.list) return;
			if (noSuggestions) {
				if (this.list) this.list.style.display = 'none';
				if (this.engineSelector) this.engineSelector.style.display = 'none';
				if (this.icon) this.icon.style.display = 'none';
				this.inputExists = true; // Default...
				return;
			}
			this.engineName = engineName;
			if (engineName) {
				if (!this.engineSelector) this.engineName = null;
			} else {
				if (this.engineSelector) this.engineSelector.style.display = 'none';
			}
			if (queryKey) {
				if (this.lastInput.indexOf (queryKey) !== 0) return;
				if (this.lastQuery && this.lastInput.indexOf (this.lastQuery) === 0 && this.lastQuery.length > queryKey.length)
					return;
			}
			this.lastQuery = queryKey;

			// Get current input text
			var v = this.text.value.split('|');
			var key = v.length > 1 ? '|' + v[1] : "";
			v = (HotCat.capitalizePageNames ? capitalize (v[0]) : v[0]);
			var vNormalized = v;
			var knownToExist = titles && titles.exists;
			var i;
			if (titles) {
				if (titles.normalized && v.indexOf(queryKey) === 0) {
					// We got back a different normalization than what is in the input field
					vNormalized = titles.normalized + v.substring(queryKey.length);
				}
				var vLow = vNormalized.toLowerCase ();
				// Strip blacklisted categories
				if (HotCat.blacklist) {
					for (i = 0; i < titles.length; i++) {
						if (HotCat.blacklist.test (titles[i])) {
							titles.splice(i, 1);
							i--;
						}
					}
				}
				titles.sort (
					function (a, b) {
						if (a == b) return 0;
						if (a.indexOf (b) === 0) return 1; // a begins with b: a > b
						if (b.indexOf (a) === 0) return -1; // b begins with a: a < b
						// Opensearch may return stuff not beginning with the search prefix!
						var prefixMatchA = (a.indexOf (vNormalized) === 0 ? 1 : 0);
						var prefixMatchB = (b.indexOf (vNormalized) === 0 ? 1 : 0);
						if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
						// Case-insensitive prefix match!
						var aLow = a.toLowerCase(), bLow = b.toLowerCase();
						prefixMatchA = (aLow.indexOf (vLow) === 0 ? 1 : 0);
						prefixMatchB = (bLow.indexOf (vLow) === 0 ? 1 : 0);
						if (prefixMatchA != prefixMatchB) return prefixMatchB - prefixMatchA;
						if (a < b) return -1;
						if (b < a) return 1;
						return 0;
					}
				);
				// Remove duplicates and self-references
				for (i = 0; i < titles.length; i++) {
					if (   i+1 < titles.length && titles[i] == titles[i+1]
						|| conf.wgNamespaceNumber == 14 && titles[i] == conf.wgTitle
					   )
					{
						titles.splice (i, 1);
						i--;
					}
				}
			}
			if (!titles || titles.length === 0) {
				if (this.list) this.list.style.display = 'none';
				if (this.engineSelector) this.engineSelector.style.display = 'none';
				if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
					if (this.icon) this.icon.src = armorUri(HotCat.existsNo);
					this.inputExists = false;
				}
				return;
			}

			var firstTitle = titles[0];
			var completed = this.autoComplete (firstTitle, v, vNormalized, key, dontAutocomplete);
			var existing = completed || knownToExist || firstTitle == replaceShortcuts(v, HotCat.shortcuts);
			if (engineName && suggestionConfigs[engineName] && !suggestionConfigs[engineName].temp) {
				this.icon.src = armorUri(existing ? HotCat.existsYes : HotCat.existsNo);
				this.inputExists = existing;
			}
			if (completed) {
				this.lastInput = firstTitle;
				if (titles.length === 1) {
					this.list.style.display = 'none';
					if (this.engineSelector) this.engineSelector.style.display = 'none';
					return;
				}
			}
			// (Re-)fill the list
			while (this.list.firstChild) this.list.removeChild (this.list.firstChild);
			for (i = 0 ; i < titles.length ; i++) {
				var opt = make ('option') ;
				opt.appendChild (make (titles[i], true));
				opt.selected = completed && (i === 0);
				this.list.appendChild (opt);
			}
			this.displayList();
		},

		displayList : function () {
			this.showsList = true;
			if (!this.is_active) {
				this.list.style.display = 'none';
				if (this.engineSelector) this.engineSelector.style.display = 'none';
				return;
			}
			var nofItems = (this.list.options.length > HotCat.list_size ? HotCat.list_size : this.list.options.length);
			if (nofItems <= 1) nofItems = 2;
			this.list.size = nofItems;
			this.list.style.align    = is_rtl ? 'right' : 'left';
			this.list.style.zIndex   = 5;
			this.list.style.position = 'absolute';
			// Compute initial list position. First the height.
			var anchor = is_rtl ? 'right' : 'left';
			var listh = 0;
			if (this.list.style.display == 'none') {
				// Off-screen display to get the height
				this.list.style.top = this.text.offsetTop + 'px';
				this.list.style[anchor] = '-10000px';
				this.list.style.display = "";
				listh = this.list.offsetHeight;
				this.list.style.display = 'none';
			} else {
				listh = this.list.offsetHeight;
			}
			// Approximate calculation of maximum list size
			var maxListHeight = listh;
			if (nofItems < HotCat.list_size) maxListHeight = (listh / nofItems) * HotCat.list_size;

			function viewport (what) {
				if (is_webkit && !document.evaluate)
					return window['inner' + what]; // Safari < 3.0
				var s = 'client' + what;
				if (window.opera) return document.body[s];
				return (document.documentElement ? document.documentElement[s] : 0)
					|| document.body[s] || 0;
			}
			function scroll_offset (what) {
				var s = 'scroll' + what;
				var result = (document.documentElement ? document.documentElement[s] : 0)
							|| document.body[s] || 0;
				if (is_rtl && what == 'Left') {
					// RTL inconsistencies.
					// FF: 0 at the far right, then increasingly negative values.
					// IE >= 8: 0 at the far right, then increasingly positive values.
					// Webkit: scrollWidth - clientWidth at the far right, then down to zero.
					// IE 7: like webkit; IE6: disabled in RTL anyway since too many problems.
					// Opera: don't know...
					if (result < 0) result = - result;
					if (!is_webkit && !is_ie_lt8) {
						result = scroll_offset('Width') - viewport('Width') - result;
					}
					// Now all have webkit behavior, i.e. zero if at the leftmost edge.
				}
				return result;
			}
			function position (node) {
				// Stripped-down simplified position function. It's good enough for our purposes.
				if (node.getBoundingClientRect) {
					var box    = node.getBoundingClientRect ();
					return { x : Math.round (box.left + scroll_offset ('Left'))
							,y : Math.round (box.top + scroll_offset ('Top'))
						   };
				}
				var t = 0, l = 0;
				do {
					t = t + (node.offsetTop  || 0);
					l = l + (node.offsetLeft || 0);
					node = node.offsetParent;
				} while (node);
				return {x : l, y : t};
			}

			var textPos = position (this.text);
			var nl = 0;
			var nt = 0;
			var offset = 0;
			// Opera 9.5 somehow has offsetWidth = 0 here?? Use the next best value...
			var textBoxWidth = this.text.offsetWidth || this.text.clientWidth;
			if (this.engineName) {
				this.engineSelector.style.zIndex = 5;
				this.engineSelector.style.position = 'absolute';
				this.engineSelector.style.width = textBoxWidth + 'px';
				// Figure out the height of this selector: display it off-screen, then hide it again.
				if (this.engineSelector.style.display == 'none') {
					this.engineSelector.style[anchor] = '-10000px';
					this.engineSelector.style.top = '0px';
					this.engineSelector.style.display = "";
					offset = this.engineSelector.offsetHeight;
					this.engineSelector.style.display = 'none';
				} else {
					offset = this.engineSelector.offsetHeight;
				}
				this.engineSelector.style[anchor]  = nl + 'px';
			}
			if (textPos.y < maxListHeight + offset + 1) {
				// The list might extend beyond the upper border of the page. Let's avoid that by placing it
				// below the input text field.
				nt = this.text.offsetHeight + offset + 1;
				if (this.engineName) this.engineSelector.style.top = this.text.offsetHeight + 'px';
			} else {
				nt = - listh - offset - 1;
				if (this.engineName) this.engineSelector.style.top = - (offset + 1) + 'px';
			}
			this.list.style.top = nt + 'px';
			this.list.style.width = ""; // No fixed width (yet)
			this.list.style[anchor] = nl + 'px';
			if (this.engineName) {
				this.selectEngine (this.engineName);
				this.engineSelector.style.display = "";
			}
			this.list.style.display = 'block';
			// Set the width of the list
			if (this.list.offsetWidth < textBoxWidth ) {
				this.list.style.width = textBoxWidth + 'px';
				return;
			}
			// If the list is wider than the textbox: make sure it fits horizontally into the browser window
			var scroll = scroll_offset ('Left');
			var view_w = viewport ('Width');
			var w      = this.list.offsetWidth;
			var l_pos  = position (this.list);
			var left   = l_pos.x;
			var right  = left + w;
			if (left < scroll || right > scroll + view_w) {
				if (w > view_w) {
					w = view_w;
					this.list.style.width = w + 'px';
					if (is_rtl) {
						left = right - w;
					} else {
						right = left + w;
					}
				}
				var relative_offset = 0;
				if (left < scroll) {
					relative_offset = scroll - left;
				} else if (right > scroll + view_w) {
					relative_offset = - (right - scroll - view_w);
				}
				if (is_rtl) relative_offset = - relative_offset;
				if (relative_offset !== 0) {
					this.list.style[anchor] = (nl + relative_offset) + 'px';
				}
			}
		},

		autoComplete : function (newVal, actVal, normalizedActVal, key, dontModify) {
			if (newVal == actVal) return true;
			if (dontModify || this.ime || !this.canSelect()) return false;
			// If we can't select properly or an IME composition is ongoing, autocompletion would be a major annoyance to the user.
			if (newVal.indexOf (actVal) !== 0) {
				// Maybe it'll work with the normalized value (NFC)?
				if (normalizedActVal && newVal.indexOf(normalizedActVal) === 0) {
					if (this.lastRealInput == actVal) this.lastRealInput = normalizedActVal;
					actVal = normalizedActVal;
				} else {
					return false;
				}
			}
			// Actual input is a prefix of the new text. Fill in new text, selecting the newly added suffix
			// such that it can be easily removed by typing backspace if the suggestion is unwanted.
			this.text.focus();
			this.text.value = newVal + key;
			this.setSelection (actVal.length, newVal.length);
			return true;
		},

		canSelect : function () {
			return this.text.setSelectionRange
				|| this.text.createTextRange
				||    typeof this.text.selectionStart != 'undefined'
				   && typeof this.text.selectionEnd != 'undefined';
		},

		setSelection : function (from, to) {
			// this.text must be focused (at least on IE)
			if (!this.text.value) return;
			if (this.text.setSelectionRange) {    // e.g. khtml
				this.text.setSelectionRange (from, to);
			} else if (typeof this.text.selectionStart != 'undefined') {
				if (from > this.text.selectionStart) {
					this.text.selectionEnd   = to;
					this.text.selectionStart = from;
				} else {
					this.text.selectionStart = from;
					this.text.selectionEnd   = to;
				}
			} else if (this.text.createTextRange) { // IE
				var new_selection = this.text.createTextRange();
				new_selection.move ('character', from);
				new_selection.moveEnd ('character', to - from);
				new_selection.select();
			}
		},

		getSelection : function () {
			var from = 0, to = 0;
			// this.text must be focused (at least on IE)
			if (!this.text.value) {
				// No text.
			} else if (typeof this.text.selectionStart != 'undefined') {
				from = this.text.selectionStart;
				to   = this.text.selectionEnd;
			} else if (document.selection && document.selection.createRange) { // IE
				var rng = document.selection.createRange().duplicate();
				if (rng.parentElement() === this.text) {
					try {
						var textRng = this.text.createTextRange();
						textRng.move('character', 0);
						textRng.setEndPoint('EndToEnd', rng);
						// We're in a single-line input box: no need to care about IE's strange
						// handling of line ends
						to = textRng.text.length;
						textRng.setEndPoint('EndToStart', rng);
						from = textRng.text.length;
					} catch (notFocused) {
						from = this.text.value.length; to = from; // At end of text
					}
				}
			}
			return {start: from, end: to};
		},

		saveView : function (evt) {
			this.lastSelection = this.getSelection ();
		},

		processKey : function (evt) {
			var dir = 0;
			switch (this.lastKey) {
				case UP: dir = -1;
				case DOWN: if (dir === 0) dir = 1; 
				case PGUP: if (dir === 0) dir = -HotCat.list_size;
				case PGDOWN: if (dir === 0) dir = HotCat.list_size;
					if (this.list.style.display != 'none') {
						// List is visible, so there are suggestions
						this.highlightSuggestion (dir);
						// Kill the event, otherwise some browsers (e.g., Firefox) may additionally treat an up-arrow
						// as "place the text cursor at the front", which we don't want here.
						return evtKill (evt);
					} else if (   this.keyCount <= 1
							   && (!this.callbackObj || this.callbackObj.callsMade == this.callbackObj.nofCalls)
							  )
					{
						// If no suggestions displayed, get them, unless we're already getting them.
						this.textchange ();
					}
					break;
				case ESC: // Inhibit default behavior (revert to last real input in FF: we do that ourselves)
					return evtKill (evt);
			}
			return true;
		},

		highlightSuggestion : function (dir) {
			if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
			var curr = this.list.selectedIndex;
			var tgt  = -1;
			if (dir === 0) {
				if (curr < 0 || curr >= this.list.options.length) return false;
				tgt = curr;
			} else {
				tgt = curr < 0 ? 0 : curr + dir;
				tgt = tgt < 0 ? 0 : tgt;
				if (tgt >= this.list.options.length) tgt = this.list.options.length - 1;
			}
			if (tgt != curr || dir === 0) {
				if (curr >= 0 && curr < this.list.options.length && dir !== 0) this.list.options[curr].selected = false;
				this.list.options[tgt].selected = true;
				// Get current input text
				var v = this.text.value.split('|');
				var key = v.length > 1 ? '|' + v[1] : "";
				var completed = this.autoComplete (this.list.options[tgt].text, this.lastRealInput, null, key, false);
				if (!completed || this.list.options[tgt].text == this.lastRealInput) {
					this.text.value = this.list.options[tgt].text + key;
					if (this.canSelect()) this.setSelection (this.list.options[tgt].text.length, this.list.options[tgt].text.length);
				}
				this.lastInput = this.list.options[tgt].text;
				this.inputExists = true; // Might be wrong if from a dab list...
				if (this.icon) this.icon.src = armorUri(HotCat.existsYes);
				this.state = CategoryEditor.CHANGE_PENDING;
			}
			return true;
		},

		resetKeySelection : function () {
			if (noSuggestions || !this.list || this.list.style.display == 'none') return false;
			var curr = this.list.selectedIndex;
			if (curr >= 0 && curr < this.list.options.length) {
				this.list.options[curr].selected = false;
				// Get current input text
				var v = this.text.value.split('|');
				var key = v.length > 1 ? '|' + v[1] : "";
				// ESC is handled strangely by some browsers (e.g., FF); somehow it resets the input value before
				// our event handlers ever get a chance to run.
				var result = v[0] != this.lastInput;
				if (v[0] != this.lastRealInput) {
					this.text.value = this.lastRealInput + key;
					result = true;
				}
				this.lastInput = this.lastRealInput;
				return result;
			}
			return false;
		}

	}; // end CategoryEditor.prototype

	function initialize () {
		// User configurations. Do this here, called from the onload handler, so that users can
		// override it easily in their own user script files by just declaring variables. JSconfig
		// is some feature used at Wikimedia Commons.
		var config = (typeof JSconfig != 'undefined' && JSconfig.keys) ? JSconfig.keys : {};
		HotCat.dont_add_to_watchlist =
			(typeof window.hotcat_dont_add_to_watchlist != 'undefined'
				? !!window.hotcat_dont_add_to_watchlist
				: (typeof config.HotCatDontAddToWatchlist != 'undefined'
					? config.HotCatDontAddToWatchlist
					: HotCat.dont_add_to_watchlist
				  )
			);
		HotCat.no_autocommit =
			(typeof window.hotcat_no_autocommit != 'undefined'
				? !!window.hotcat_no_autocommit
				: (typeof config.HotCatNoAutoCommit != 'undefined'
					? config.HotCatNoAutoCommit
					: HotCat.no_autocommit
				  )
			);
		HotCat.del_needs_diff =
			(typeof window.hotcat_del_needs_diff != 'undefined'
				? !!window.hotcat_del_needs_diff
				: (typeof config.HotCatDelNeedsDiff != 'undefined'
					? config.HotCatDelNeedsDiff
					: HotCat.del_needs_diff
				  )
			);
		HotCat.suggest_delay = window.hotcat_suggestion_delay
							|| config['HotCatSuggestionDelay']
							|| HotCat.suggest_delay;
		HotCat.editbox_width = window.hotcat_editbox_width
							|| config['HotCatEditBoxWidth']
							|| HotCat.editbox_width;
		HotCat.suggestions   = window.hotcat_suggestions
							|| config['HotCatSuggestions']
							|| HotCat.suggestions;
		if (typeof HotCat.suggestions != 'string' || !suggestionConfigs[HotCat.suggestions])
			HotCat.suggestions = 'combined';
		HotCat.fixed_search  =
			(typeof window.hotcat_suggestions_fixed != 'undefined'
				? !!window.hotcat_suggestions_fixed
				: (typeof config.HotCatFixedSuggestions != 'undefined'
					? config.HotCatFixedSuggestions
					: HotCat.fixed_search
				  )
			);
		HotCat.single_minor =
			(typeof window.hotcat_single_changes_are_minor != 'undefined'
				? !!window.hotcat_single_changes_are_minor
				: (typeof config.HotCatMinorSingleChanges != 'undefined'
					? config.HotCatMinorSingleChanges
					: HotCat.single_minor
				  )
			);
		HotCat.bg_changed = window.hotcat_changed_background
							|| config.HotCatChangedBackground
							|| HotCat.bg_changed;
		HotCat.use_up_down =
			(typeof window.hotcat_use_category_links != 'undefined'
				? !!window.hotcat_use_category_links
				: (typeof config.HotCatUseCategoryLinks != 'undefined'
					? config.HotCatUseCategoryLinks
					: HotCat.use_up_down
				  )
			);
		HotCat.list_size = window.hotcat_list_size
							|| config.HotCatListSize
							|| HotCat.list_size;
		// Numeric input, make sure we have a numeric value
		HotCat.list_size = parseInt (HotCat.list_size, 10);
		if (isNaN (HotCat.list_size) || HotCat.list_size < 5) HotCat.list_size = 5;
		if (HotCat.list_size > 15) HotCat.list_size = 15;
		// Localize search engine names
		if (HotCat.engine_names) {
			for (var key in HotCat.engine_names) {
				if (suggestionConfigs[key] && HotCat.engine_names[key]) {
					suggestionConfigs[key].name = HotCat.engine_names[key];
				}
			}
		}
		// Catch both native RTL and "faked" RTL through [[MediaWiki:Rtl.js]]
		is_rtl = hasClass (document.body, 'rtl');
		if (!is_rtl) {
			if (document.defaultView && document.defaultView.getComputedStyle) { // Gecko etc.
				is_rtl = document.defaultView.getComputedStyle (document.body, null).getPropertyValue ('direction');
			} else if (document.body.currentStyle) { // IE, has subtle differences to getComputedStyle
				is_rtl = document.body.currentStyle['direction'];
			} else { // Not exactly right, but best effort
				is_rtl = document.body.style['direction'];
			}
			is_rtl = (is_rtl == 'rtl');
		}
	}

	function can_edit () {
		var container = null;
		switch (mw.config.get('skin')) {
			case 'cologneblue':
				container = document.getElementById ('quickbar');
				// Fall through
			case 'standard':
			case 'nostalgia':
				if (!container) container = document.getElementById ('topbar');
				var lks = container.getElementsByTagName ('a');
				for (var i = 0; i < lks.length; i++) {
					if (   param ('title', lks[i].href) == conf.wgPageName
						&& param ('action', lks[i].href) == 'edit')
						return true;
				}
				return false;
			default:
				// all modern skins:
				return document.getElementById ('ca-edit') !== null;
		}
		return false;
	}

	function setup_upload () {
		onUpload = true;
		// Add an empty category bar at the end of the table containing the description, and change the onsubmit handler.
		var ip = document.getElementById ('mw-htmlform-description') || document.getElementById ('wpDestFile');
		if (!ip) {
			ip = document.getElementById ('wpDestFile');
			while (ip && ip.nodeName.toLowerCase() != 'table') ip = ip.parentNode;
		}
		if (!ip) return;
		var reupload = document.getElementById ('wpForReUpload');
		var destFile = document.getElementById ('wpDestFile');
		if (   (reupload && !!reupload.value)
			|| (destFile && (destFile.disabled || destFile.readOnly)))
			return; // re-upload form...
		// Insert a table row with two fields (label and empty category bar)
		var labelCell = make ('td');
		var lineCell  = make ('td');
		// Create the category line
		catLine = make ('div');
		catLine.className = 'catlinks';
		catLine.id = 'catlinks';
		catLine.style.textAlign = is_rtl ? 'right' : 'left';
		// We'll be inside a table row. Make sure that we don't have margins or strange borders.
		catLine.style.margin = '0';
		catLine.style.border = 'none';
		lineCell.appendChild (catLine);
		// Create the label
		var label = null;
		if (   typeof UFUI != 'undefined'
			&& typeof UIElements != 'undefined'
			&& typeof UFUI.getLabel == 'function'
		   )
		{
			try {
				label = UFUI.getLabel('wpCategoriesUploadLbl');
			} catch (ex) {
				label = null;
			}
		}
		if (!label) {
			labelCell.id = 'hotcatLabel';
			labelCell.appendChild (make (HotCat.categories, true));
		} else {
			labelCell.id = 'hotcatLabelTranslated';
			labelCell.appendChild (label);
		}
		labelCell.className           = 'mw-label';
		labelCell.style.textAlign     = 'right';
		labelCell.style.verticalAlign = 'middle';
		// Change the onsubmit handler
		var form = document.getElementById('upload') || document.getElementById('mw-upload-form');
		if (form) {
			var newRow = ip.insertRow (-1);
			newRow.appendChild (labelCell);
			newRow.appendChild (lineCell);
			form.onsubmit = (function (oldSubmit) {
				return function () {
					var do_submit = true;
					if (oldSubmit) {
						if (typeof oldSubmit == 'string')
							do_submit = eval (oldSubmit);
						else if (typeof oldSubmit == 'function')
							do_submit = oldSubmit.apply (form, arguments);
					}
					if (!do_submit) return false;
					closeForm ();
					// Copy the categories
					var eb = document.getElementById ('wpUploadDescription')
						|| document.getElementById ('wpDesc');
					var addedOne = false;
					for (var i = 0; i < editors.length; i++) {
						var t = editors[i].currentCategory;
						if (!t) continue ;
						var key = editors[i].currentKey;
						var new_cat = '[[' + HotCat.category_canonical + ':' + t + (key ? '|' + key : "") + ']]';
						// Only add if not already present
						var cleanedText = eb.value
								.replace(/<\!--(\s|\S)*?--\>/g, "")
								.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
						if (!find_category (cleanedText, t, true)) {
							eb.value += '\n' + new_cat;
							addedOne = true;
						}
					}
					if (addedOne) {
						// Remove "subst:unc" added by Flinfo if it didn't find categories
						eb.value = eb.value.replace(/\{\{subst:unc\}\}/g, "");
					}
					return true;
				};
			}) (form.onsubmit);
		}
	}

	var cleanedText = null;

	function isOnPage (span) {
		var catTitle = title (span.firstChild.getAttribute ('href', 2));
		if (!catTitle) return null;
		catTitle = catTitle.substr (catTitle.indexOf (':') + 1).replace (/_/g, ' ');
		if (HotCat.blacklist && HotCat.blacklist.test (catTitle)) return null;
		var result = { title : catTitle, match : ["", "", ""] };
		if (pageText === null) return result;
		if (cleanedText === null) {
			cleanedText = pageText
				.replace(/<\!--(\s|\S)*?--\>/g, "")
				.replace(/<nowiki\>(\s|\S)*?<\/nowiki>/g, "");
		}
		result.match = find_category (cleanedText, catTitle, true);
		return result;
	}

	var initialized = false;
	var setupTimeout = null;

	function findByClass (scope, tag, className) {
		// Compatibility routine. Uses jQuery if available, otherwise works with older getElementsByClassName
		var result;
		if (window.jQuery) {
			result = window.jQuery(scope).find(tag + '.' + className);
		} else {
			result = getElementsByClassName(scope, tag, className);
		}
		return (result && result.length) ? result[0] : null;
	}

	function setup (additionalWork) {
		if (initialized) return;
		initialized = true;
		if (setupTimeout) {
			window.clearTimeout (setupTimeout);
			setupTimeout = null;
		}
		// Find the category bar, or create an empty one if there isn't one. Then add -/+- links after
		// each category, and add the + link.
		catLine =  catLine                                        // Special:Upload
				|| document.getElementById ('mw-normal-catlinks') // MW >= 1.13alpha
				|| findByClass (document , 'p' , 'catlinks');     // MW < 1.13
		var hiddenCats = document.getElementById ('mw-hidden-catlinks');
		if (!catLine) {
			var footer = null;
			if (!hiddenCats) {
				footer = findByClass (document , 'div' , 'printfooter');
				if (!footer) return; // Don't know where to insert the category line
			}
			catLine = make ('div');
			catLine.id = 'mw-normal-catlinks';
			catLine.style.textAlign = is_rtl ? 'right' : 'left';
			// Add a label
			var label = make ('a');
			label.href  = conf.wgArticlePath.replace ('$1', 'Special:Categories');
			label.title = HotCat.categories;
			label.appendChild (make (HotCat.categories, true));
			catLine.appendChild (label);
			catLine.appendChild (make (':', true));
			// Insert the new category line
			var container = (hiddenCats ? hiddenCats.parentNode : document.getElementById ('catlinks'));
			if (!container) {
				container = make ('div');
				container.id = 'catlinks';
				footer.parentNode.insertBefore (container, footer.nextSibling);
			}
			container.className = 'catlinks noprint';
			container.style.display = "";
			if (!hiddenCats) {
				container.appendChild (catLine);
			} else {
				container.insertBefore (catLine, hiddenCats);
			}
		} // end if catLine exists
		if (is_rtl) catLine.dir = 'rtl';

		// Create editors for all existing categories

		function createEditors (line, is_hidden) {
			var i;
			var cats = line.getElementsByTagName ('li');
			if (cats.length > 0) {
				newDOM = true; line = cats[0].parentNode;
			} else {
				cats = line.getElementsByTagName ('span');
			}
			// Copy cats, otherwise it'll also magically contain our added spans as it is a live collection!
			var copyCats = new Array (cats.length);
			for (i = 0; i < cats.length; i++) copyCats[i] = cats[i];
			var editor = null;
			for (i = 0; i < copyCats.length; i++) {
				var test = isOnPage (copyCats[i]);
				if (test !== null && test.match !== null) {
					editor = new CategoryEditor (line, copyCats[i], test.title, test.match[2], is_hidden);
				}
			}
			return copyCats.length > 0 ? copyCats[copyCats.length-1] : null;
		}

		var lastSpan = createEditors (catLine, false);
		// Create one to add a new category
		var editor = new CategoryEditor(newDOM ? catLine.getElementsByTagName('ul')[0] : catLine, null, null, lastSpan !== null, false);
		if (!onUpload) {
			if (pageText !== null && hiddenCats) {
				if (is_rtl) hiddenCats.dir = 'rtl';
				createEditors (hiddenCats, true);
			}
			// And finally add the "multi-mode" span. (Do this at the end, otherwise it ends up in the list above.)
			var enableMulti = make ('span');
			enableMulti.className = 'noprint';
			if (is_rtl) enableMulti.dir = 'rtl';
			catLine.insertBefore (enableMulti, catLine.firstChild.nextSibling);
			enableMulti.appendChild (make ('\xa0', true)); // nbsp
			multiSpan = make ('span');
			enableMulti.appendChild (multiSpan);
			multiSpan.innerHTML = '(<a>' + HotCat.addmulti + '</a>)';
			var lk = multiSpan.getElementsByTagName ('a')[0];
			lk.onclick = function (evt) {setMultiInput (); checkMultiInput (); return evtKill (evt);};
			lk.title = HotCat.multi_tooltip;
			lk.style.cursor = 'pointer';
		}
		cleanedText = null;
		if (typeof additionalWork == 'function') additionalWork();
		setupCompleted.loaded(); // Trigger signal; execute registered functions
		if (window.jQuery) jQuery('body').trigger('hotcatSetupCompleted');
	}

	function setPage (json) {
		var startTime = null;
		if (json && json.query) {
			if (json.query.pages) {
				var page = json.query.pages[conf.wgArticleId === 0 ? "-1" : "" + conf.wgArticleId];
				if (page) {
					if (page.revisions && page.revisions.length > 0) {
						// Revisions are sorted by revision ID, hence [0] is the one we asked for, and possibly there's a [1] if we're
						// not on the latest revision (edit conflicts and such).
						pageText = page.revisions[0]['*'];
						if (page.revisions[0].timestamp) pageTime = page.revisions[0].timestamp.replace (/\D/g, "");
						if (page.revisions[0].revid) pageTextRevId = page.revisions[0].revid;
						if (page.revisions.length > 1) conflictingUser = page.revisions[1].user;
					}
					if (page.lastrevid) lastRevId = page.lastrevid;
					if (page.starttimestamp) startTime = page.starttimestamp.replace (/\D/g, "");
					pageWatched = typeof page.watched == 'string';
					editToken = page.edittoken;
					if (page.langlinks && (!json['query-continue'] || !json['query-continue'].langlinks)) {
						// We have interlanguage links, and we got them all.
						var re = "";
						for (var i = 0; i < page.langlinks.length; i++) {
							re += (i > 0 ? '|' : "") + page.langlinks[i].lang.replace(/([\\\^\$\.\?\*\+\(\)])/g, '\\$1');
						}
						if (re.length > 0) {
							interlanguageRE = new RegExp ('((^|\\n\\r?)(\\[\\[\\s*(' + re + ')\\s*:[^\\]]+\\]\\]\\s*))+$');
						}
					}

				}
			}
			// Siteinfo
			if (json.query.general) {
				HotCat.capitalizePageNames = (json.query.general['case'] == 'first-letter');
				if (json.query.general.time && !startTime) startTime = json.query.general.time.replace (/\D/g, "");
			}
			serverTime = startTime;
			// Userinfo
			if (json.query.userinfo && json.query.userinfo.options) {
				watchCreate = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchcreations == '1';
				watchEdit   = !HotCat.dont_add_to_watchlist && json.query.userinfo.options.watchdefault == '1';
				minorEdits  = json.query.userinfo.options.minordefault == 1;
				// If the user has the "All edits are minor" preference enabled, we should honor that
				// for single category changes, no matter what the site configuration is.
				if (minorEdits) HotCat.single_minor = true;
			}
		}
	}

	function createCommitForm () {
		if (commitForm) return;
		var formContainer = make ('div');
		formContainer.style.display = 'none';
		document.body.appendChild (formContainer);
		formContainer.innerHTML =
			'<form id="hotcatCommitForm" method="post" enctype="multipart/form-data" action="'
			+ conf.wgScript + '?title=' + encodeURIComponent (conf.wgPageName)
			+ '&action=edit">'
			+ '<input type="hidden" name="wpTextbox1" />'
			+ '<input type="hidden" name="model" value="wikitext" />'
			+ '<input type="hidden" name="format" value="text/x-wiki" />'
			+ '<input type="hidden" name="wpSummary" value="" />'
			+ '<input type="checkbox" name="wpMinoredit" value="1" />'
			+ '<input type="checkbox" name="wpWatchthis" value="1" />'
			+ '<input type="hidden" name="wpAutoSummary" value="" />'
			+ '<input type="hidden" name="wpEdittime" />'
			+ '<input type="hidden" name="wpStarttime" />'
			+ '<input type="hidden" name="wpDiff" value="wpDiff" />'
			+ '<input type="hidden" name="oldid" value="0" />'
			+ '<input type="submit" name="hcCommit" value="hcCommit" />'
			+ '<input type="hidden" name="wpEditToken" />'
			+ '<input type="hidden" name="wpUltimateParam" value="1" />'
			+ '</form>';
		commitForm = document.getElementById ('hotcatCommitForm');
	}

	function getPage () {
		// We know we have an article here.
		if (conf.wgArticleId === 0) {
			// Doesn't exist yet.
			if (conf.wgNamespaceNumber === 2) {
				// Disable on non-existing User pages -- might be a global user page.
				return;
			}
			pageText = "";
			pageTime = null;
			setup (createCommitForm);
		} else {
			var url = conf.wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles='
					+ encodeURIComponent (conf.wgPageName)
					+ '&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid='
					+ conf.wgCurRevisionId;
			var s = make ('script');
			s.src = armorUri(url);
			s.type = 'text/javascript';
			HotCat.start = function (json) { setPage (json); setup (createCommitForm); };
			document.getElementsByTagName ('head')[0].appendChild (s);
			setupTimeout = window.setTimeout (function () {setup (createCommitForm);}, 4000); // 4 sec, just in case getting the wikitext takes longer.
		}
	}

	function run () {
		if (HotCat.started) return;
		HotCat.started = true;
		loadTrigger.register(really_run);
	}

	function really_run () {
		initialize ();

		if (is_rtl && is_ie6) return; // Disabled! IE6 with RTL is just too broken...
		if (!HotCat.upload_disabled && conf.wgNamespaceNumber === -1 && conf.wgCanonicalSpecialPageName == 'Upload' && conf.wgUserName) {
			setup_upload ();
			setup (function () {
				// Check for state restoration once the setup is done otherwise, but before signalling setup completion
				if (   typeof UploadForm != 'undefined'
					&& typeof UploadForm.previous_hotcat_state != 'undefined'
					&& UploadForm.previous_hotcat_state !== null)
				{
					UploadForm.previous_hotcat_state = setState (UploadForm.previous_hotcat_state);
				}
			});
		} else {
			if (!conf.wgIsArticle || conf.wgAction != 'view' || param('diff') !== null || param('oldid') !== null || !can_edit() || HotCat.disable()) return;
			getPage ();
		}
	}

	// Legacy stuff

	function closeForm () {
		// Close all open editors without redirect resolution and other asynchronous stuff.
		for (var i = 0; i < editors.length; i++) {
			if (editors[i].state == CategoryEditor.OPEN) {
				editors[i].cancel();
			} else if (editors[i].state == CategoryEditor.CHANGE_PENDING) {
				editors[i].sanitizeInput ();
				var value = editors[i].text.value.split('|');
				var key   = null;
				if (value.length > 1) key = value[1];
				var v = value[0].replace(/_/g, ' ').replace(/^\s+|\s+$/g, "");
				if (v.length === 0) {
					editors[i].cancel ();
				} else {
					editors[i].currentCategory = v;
					editors[i].currentKey = key;
					editors[i].currentExists = this.inputExists;
					editors[i].close ();
				}
			}
		}
	}

	function getState () {
		var result = null;
		for (var i = 0; i < editors.length; i++) {
			var text = editors[i].currentCategory;
			var key  = editors[i].currentKey;
			if (text && text.length > 0) {
				if (key !== null) text += '|' + key;
				if (result === null)
					result = text;
				else
					result = result + '\n' + text;
			}
		}
		return result;
	}

	function setState (state) {
		var cats = state.split ('\n');
		if (cats.length === 0) return null;
		if (initialized && editors.length == 1 && editors[0].isAddCategory) {
			// Insert new spans and create new editors for them.
			var newSpans = [];
			var before = editors.length == 1 ? editors[0].span : null;
			var i;
			for (i = 0; i < cats.length; i++) {
				if (cats[i].length === 0) continue;
				var cat = cats[i].split ('|');
				var key = cat.length > 1 ? cat[1] : null;
				cat = cat[0];
				var lk = make ('a'); lk.href = wikiPagePath (HotCat.category_canonical + ':' + cat);
				lk.appendChild (make (cat, true));
				lk.title = cat;
				var span = make ('span');
				span.appendChild (lk);
				if (i === 0) catLine.insertBefore (make (' ', true), before);
				catLine.insertBefore (span, before);
				if (before && i+1 < cats.length) parent.insertBefore (make (' | ', true), before);
				newSpans.push ({element: span, title: cat, 'key': key});
			}
			// And change the last one...
			if (before) {
				before.parentNode.insertBefore (make (' | ', true), before);
			}
			var editor = null;
			for (i = 0; i < newSpans.length; i++) {
				editor = new CategoryEditor (catLine, newSpans[i].element, newSpans[i].title, newSpans[i].key);
			}
		}
		return null;
	}

	// Export legacy functions
	window.hotcat_get_state  = function () { return getState(); };
	window.hotcat_set_state  = function (state) { return setState (state); };
	window.hotcat_close_form = function () { closeForm (); };

	if (window.mw) {
		// Make sure we don't get conflicts with AjaxCategories (core development that should one day
		// replace HotCat).
		mw.config.set('disableAJAXCategories', true);
	}
	// Run as soon as possible. This varies depending on MediaWiki version;
	// window's 'load' event is always safe, but usually we can do better than that.

	// Check for version to avoid MediaWiki bug 32537.
	var mwVersion = conf.wgVersion.split('.');
	if (mwVersion[0] >= 1 && parseFloat(mwVersion[1]) > 20) {
		// We can safely trigger just after user configuration is loaded. Also start HotCat if the user module fails to load.
		// Avoid using Promise methods of mw.loader.using as those aren't supported in older
		// MediaWiki versions.
		var startHotCat = function(){
			jQuery(document).ready(run);
		};
		mw.loader.using('user', startHotCat, startHotCat);
		if (parseFloat(mwVersion[1]) > 21) {
			// Reload HotCat after page is saved (bug T103285)
			mw.hook('postEdit').add( function () { 
				catLine = null;
				editors = [];
				initialized = false;
				HotCat.started = false;
				run ();
			} );
		}
	} else {
		// mw.loader.using('user', ...) could have unintended side-effects on MW <= 1.20. Fall back to dom-ready.
		jQuery(document).ready(run);
	}
})();

//</source>