User:Mschel/monobook.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <nowiki>

// Script to embed InstaView in MediaWiki's edit page
addOnloadHook(function(){
  if (document.getElementById('editpage-copywarn')) {
    var oldPreview = document.getElementById('wpPreview');
    var newPreview = document.createElement('input');
    newPreview.setAttribute('type', 'button');
    newPreview.setAttribute('style', 'font-style: italic');
    newPreview.setAttribute('value', 'InstaView');
    newPreview.setAttribute('onclick', "InstaView.dump('wpTextbox1', 'InstaViewDump')");
    oldPreview.parentNode.insertBefore(newPreview, oldPreview);
    oldPreview.parentNode.innerHTML += '<div style="margin: 5px 0 5px 0; padding: 5px; border: 2px solid orange;" id="InstaViewDump"></div>';
    oldPreview.value = 'Classic Preview';
    }
});

var InstaView = {}

// options
InstaView.conf =
{
	user: {},
	
	wiki: {
		lang: 'en',
		interwiki: 'ab|aa|af|ak|sq|als|am|ang|ar|an|arc|hy|roa-rup|as|ast|av|ay|az|bm|ba|eu|be|bn|bh|bi|bs|br|bg|my|ca|ch|ce|chr|chy|ny|zh|zh-tw|zh-cn|cho|cv|kw|co|cr|hr|cs|da|dv|nl|dz|en|eo|et|ee|fo|fj|fi|fr|fy|ff|gl|ka|de|got|el|kl|gn|gu|ht|ha|haw|he|hz|hi|ho|hu|is|io|ig|id|ia|ie|iu|ik|ga|it|ja|jv|kn|kr|csb|ks|kk|km|ki|rw|rn|tlh|kv|kg|ko|kj|ku|ky|lo|la|lv|li|ln|lt|jbo|nds|lg|lb|mk|mg|ms|ml|mt|gv|mi|minnan|mr|mh|zh-min-nan|mo|mn|mus|nah|na|nv|ne|se|no|nn|oc|or|om|pi|fa|pl|pt|pa|ps|qu|ro|rm|ru|sm|sg|sa|sc|gd|sr|sh|st|tn|sn|scn|simple|sd|si|sk|sl|so|st|es|su|sw|ss|sv|tl|ty|tg|ta|tt|te|th|bo|ti|tpi|to|tokipona|ts|tum|tr|tk|tw|uk|ur|ug|uz|ve|vi|vo|wa|cy|wo|xh|ii|yi|yo|za|zu',
		default_thumb_width: 180
	},
	
	paths: {
		articles: '/wiki/',
		math: '/math/',
		images: '',
		images_fallback: 'http://upload.wikimedia.org/wikipedia/commons/',
		magnify_icon: 'skins/common/images/magnify-clip.png'
	},
	
	locale: {
		user: 'User',
		image: 'Image',
		category: 'Category',
		months: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
	}
}

// options with default values or backreferences
with (InstaView.conf) {
	user.name = user.name || 'Wikipedian'
	user.signature = '[['+locale.user+':'+user.name+'|'+user.name+']]'
	paths.images = 'http://upload.wikimedia.org/wikipedia/' + wiki.lang + '/'
}

// define constants
InstaView.BLOCK_IMAGE = new RegExp('^\\[\\['+InstaView.conf.locale.image+':.*?\\|.*?(?:frame|thumbnail|thumb|none|right|left|center)', 'i');

InstaView.dump = function(from, to)
{
	if (typeof from == 'string') from = document.getElementById(from)
	if (typeof to == 'string') to = document.getElementById(to)
	to.innerHTML = this.convert(from.value)
}

InstaView.convert = function(wiki)
{
	var 	ll = (typeof wiki == 'string')? wiki.replace(/\r/g,'').split(/\n/): wiki, // lines of wikicode
		o='',	// output
		p=0,	// para flag
		$r	// result of passing a regexp to $()
	
	// some shorthands
	function remain() { return ll.length }
	function sh() { return ll.shift() } // shift
	function ps(s) { o+=s } // push
	
	function f() // similar to C's printf, uses ? as placeholders, ?? to escape question marks
	{
		var i=1,a=arguments,f=a[0],o='',c,p
		for (;i<a.length; i++) if ((p=f.indexOf('?'))+1) {
			// allow character escaping
			i -= c=f.charAt(p+1)=='?'?1:0
			o += f.substring(0,p)+(c?'?':a[i])
			f=f.substr(p+1+c)
		} else break;
		return o+f
	}
	
	function html_entities(s) { return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;") }
	
	function max(a,b) { return (a>b)?a:b }
	function min(a,b) { return (a<b)?a:b }
	
	// return the first non matching character position between two strings
	function str_imatch(a, b)
	{
		for (var i=0, l=min(a.length, b.length); i<l; i++) if (a.charAt(i)!=b.charAt(i)) break
		return i
	}
	
	// compare current line against a string or regexp
	// if passed a string it will compare only the first string.length characters
	// if passed a regexp the result is stored in $r
	function $(c) { return (typeof c == 'string') ? (ll[0].substr(0,c.length)==c) : ($r = ll[0].match(c)) }
	
	function $$(c) { return ll[0]==c } // compare current line against a string
	function _(p) { return ll[0].charAt(p) } // return char at pos p
	
	function endl(s) { ps(s); sh() }
	
	function parse_list()
	{
		var prev='';
		
		while (remain() && $(/^([*#:;]+)(.*)$/)) {
			
			var l_match = $r
			
			sh()
			
			var ipos = str_imatch(prev, l_match[1])
			
			// close uncontinued lists
			for (var i=prev.length-1; i >= ipos; i--) {
				
				var pi = prev.charAt(i)
				
				if (pi=='*') ps('</ul>')
				else if (pi=='#') ps('</ol>')
				// close a dl only if the new item is not a dl item (:, ; or empty)
				else switch (l_match[1].charAt(i)) { case'':case'*':case'#': ps('</dl>') }
			}
			
			// open new lists
			for (var i=ipos; i<l_match[1].length; i++) {
				
				var li = l_match[1].charAt(i)
				
				if (li=='*') ps('<ul>')
				else if (li=='#') ps('<ol>')
				// open a new dl only if the prev item is not a dl item (:, ; or empty)
				else switch(prev.charAt(i)) { case'':case'*':case'#': ps('<dl>') }
			}
			
			switch (l_match[1].charAt(l_match[1].length-1)) {
			
				case '*': case '#':
					ps('<li>' + parse_inline_nowiki(l_match[2])); break
					
				case ';':
					ps('<dt>')
					
					var dt_match
					
					// handle ;dt :dd format
					if (dt_match = l_match[2].match(/(.*?) (:.*?)$/)) {
						
						ps(parse_inline_nowiki(dt_match[1]))
						ll.unshift(dt_match[2])
						
					} else ps(parse_inline_nowiki(l_match[2]))
					
					break
					
				case ':':
					ps('<dd>' + parse_inline_nowiki(l_match[2]))
			}
			
			prev=l_match[1]
		}
		
		// close remaining lists
		for (var i=prev.length-1; i>=0; i--)
			ps(f('</?>', (prev.charAt(i)=='*')? 'ul': ((prev.charAt(i)=='#')? 'ol': 'dl')))
	}
	
	function parse_table()
	{
		endl(f('<table?>', $(/^\{\|( .*)$/)? $r[1]: ''))
		
		for (;remain();) if ($('|')) switch (_(1)) {
			case '}': endl('</table>'); return
			case '-': endl(f('<tr ?>', $(/\|-*(.*)/)[1])); break
			default: parse_table_data()
		}
		else if ($('!')) parse_table_data()
		else sh()
	}
	
	function parse_table_data()
	{
		var td_line, match_i
		
		// 1: "|+", '|' or '+'
		// 2: ??
		// 3: attributes ??
		// TODO: finish commenting this regexp
		var td_match = sh().match(/^(\|\+|\||!)((?:([^[|]*?)\|(?!\|))?(.*))$/)
		
		if (td_match[1] == '|+') ps('<caption');
		else ps('<t' + ((td_match[1]=='|')?'d':'h'))
		
		if (typeof td_match[3] != 'undefined') {
			
			ps(' ' + td_match[3])
			match_i = 4
			
		} else match_i = 2
		
		ps('>')
		
		if (td_match[1] != '|+') {
			
			// use || or !! as a cell separator depending on context
			// NOTE: when split() is passed a regexp make sure to use non-capturing brackets
			td_line = td_match[match_i].split((td_match[1] == '|')? '||': /(?:\|\||!!)/)
			
			ps(parse_inline_nowiki(td_line.shift()))
			
			while (td_line.length) ll.unshift(td_match[1] + td_line.pop())
			
		} else ps(td_match[match_i])
		
		var tc = 0, td = []
		
		for (;remain(); td.push(sh()))
		if ($('|')) {
			if (!tc) break // we're at the outer-most level (no nested tables), skip to td parse
			else if (_(1)=='}') tc--
		}
		else if (!tc && $('!')) break
		else if ($('{|')) tc++
		
		if (td.length) ps(InstaView.convert(td))
	}
	
	function parse_pre()
	{
		ps('<pre>')
		do endl(parse_inline_nowiki(ll[0].substring(1)) + "\n"); while (remain() && $(' '))
		ps('</pre>')
	}
	
	function parse_block_image()
	{
		ps(parse_image(sh()))
	}

	function parse_image(str)
	{
		// get what's in between "[[Image:" and "]]"
		var tag = str.substring(InstaView.conf.locale.image.length + 3, str.length - 2);
		
		var width;
		var attr = [], filename, caption = '';
		var thumb=0, frame=0, center=0;
		var align='';
		
		if (tag.match(/\|/)) {
			// manage nested links
			var nesting = 0;
			var last_attr;
			for (var i = tag.length-1; i > 0; i--) {
				if (tag.charAt(i) == '|' && !nesting) {
					last_attr = tag.substr(i+1);
					tag = tag.substring(0, i);
					break;
				} else switch (tag.substr(i-1, 2)) {
					case ']]':
						nesting++;
						i--;
						break;
					case '[[':
						nesting--;
						i--;
				}
			}
			
			attr = tag.split(/\s*\|\s*/);
			attr.push(last_attr);
			filename = attr.shift();
			
			var w_match;
			
			for (;attr.length; attr.shift())
			if (w_match = attr[0].match(/^(\d*)px$/)) width = w_match[1]
			else switch(attr[0]) {
				case 'thumb':
				case 'thumbnail':
					thumb=true;
				case 'frame':
					frame=true;
					break;
				case 'none':
				case 'right':
				case 'left':
					center=false;
					align=attr[0];
					break;
				case 'center':
					center=true;
					align='none';
					break;
				default:
					if (attr.length == 1) caption = attr[0];
			}
			
		} else filename = tag;
		
		
		var o='';
		
		if (frame) {
		
			if (align=='') align = 'right';
			
			o += f("<div class='thumb t?'>", align);
			
			if (thumb) {
				if (!width) width = InstaView.conf.wiki.default_thumb_width;
				
				o += f("<div style='width:?px;'>?", 2+width*1, make_image(filename, caption, width)) +
					f("<div class='thumbcaption'><div class='magnify' style='float:right'><a href='?' class='internal' title='Enlarge'><img src='?'></a></div>?</div>",
						InstaView.conf.paths.articles + InstaView.conf.locale.image + ':' + filename,
						InstaView.conf.paths.magnify_icon,
						parse_inline_nowiki(caption)
					)
			} else {
				o += '<div>' + make_image(filename, caption) + f("<div class='thumbcaption'>?</div>", parse_inline_nowiki(caption))
			}
			
			o += '</div></div>';
			
		} else if (align != '') {
			o += f("<div class='float?'><span>?</span></div>", align, make_image(filename, caption, width));
		} else {
			return make_image(filename, caption, width);
		}
		
		return center? f("<div class='center'>?</div>", o): o;
	}
	
	function parse_inline_nowiki(str)
	{
		var start, lastend=0
		var substart=0, nestlev=0, open, close, subloop;
		var html='';
		
		while (-1 != (start = str.indexOf('<nowiki>', substart))) {
			html += parse_inline_wiki(str.substring(lastend, start));
			start += 8;
			substart = start;
			subloop = true;
			do {
				open = str.indexOf('<nowiki>', substart);
				close = str.indexOf('</nowiki>', substart);
				if (close<=open || open==-1) {
					if (close==-1) {
						return html + html_entities(str.substr(start));
					}
					substart = close+9;
					if (nestlev) {
						nestlev--;
					} else {
						lastend = substart;
						html += html_entities(str.substring(start, lastend-9));
						subloop = false;
					}
				} else {
					substart = open+8;
					nestlev++;
				}
			} while (subloop)
		}
		
		return html + parse_inline_wiki(str.substr(lastend));
	}
	
	function make_image(filename, caption, width)
	{
		// uppercase first letter in file name
		filename = filename.charAt(0).toUpperCase() + filename.substr(1);
		// replace spaces with underscores
		filename = filename.replace(/ /g, '_');
		
		caption = strip_inline_wiki(caption);
		
		var md5 = hex_md5(filename);
		
		var source = md5.charAt(0) + '/' + md5.substr(0,2) + '/' + filename;
		
		if (width) width = "width='" + width + "px'";
		
		var img = f("<img onerror=\"this.onerror=null;this.src='?'\" src='?' ? ?>", InstaView.conf.paths.images_fallback + source, InstaView.conf.paths.images + source, (caption!='')? "alt='" + caption + "'" : '', width);
		
		return f("<a class='image' ? href='?'>?</a>", (caption!='')? "title='" + caption + "'" : '', InstaView.conf.paths.articles + InstaView.conf.locale.image + ':' + filename, img);
	}
	
	function parse_inline_images(str)
	{
		var start, substart=0, nestlev=0;
		var loop, close, open, wiki, html;
		
		while (-1 != (start=str.indexOf('[[', substart))) {
			if(str.substr(start+2).match(RegExp('^' + InstaView.conf.locale.image + ':','i'))) {
				loop=true;
				substart=start;
				do {
					substart+=2;
					close=str.indexOf(']]',substart);
					open=str.indexOf('[[',substart);
					if (close<=open||open==-1) {
						if (close==-1) return str;
						substart=close;
						if (nestlev) {
							nestlev--;
						} else {
							wiki=str.substring(start,close+2);
							html=parse_image(wiki);
							str=str.replace(wiki,html);
							substart=start+html.length;
							loop=false;
						}
					} else {
						substart=open;
						nestlev++;
					}
				} while (loop)
				
			} else break;
		}
		
		return str;
	}
	
	// the output of this function doesn't respect the FILO structure of HTML
	// but since most browsers can handle it I'll save myself the hassle
	function parse_inline_formatting(str)
	{
		var em,st,i,li,o='';
		while ((i=str.indexOf("''",li))+1) {
			o += str.substring(li,i);
			li=i+2;
			if (str.charAt(i+2)=="'") {
				li++;
				st=!st;
				o+=st?'<strong>':'</strong>';
			} else {
				em=!em;
				o+=em?'<em>':'</em>';
			}
		}
		return o+str.substr(li);
	}
	
	function parse_inline_wiki(str)
	{
		var aux_match;
		
		str = parse_inline_images(str);
		str = parse_inline_formatting(str);
		
		// math
		while (aux_match = str.match(/<(?:)math>(.*?)<\/math>/i)) {
			var math_md5 = hex_md5(aux_match[1]);
			str = str.replace(aux_match[0], f("<img src='?.png'>", InstaView.conf.paths.math+math_md5));
		}
		
		// Build a Mediawiki-formatted date string
		var date = new Date;
		var minutes = date.getUTCMinutes();
		if (minutes < 10) minutes = '0' + minutes;
		var date = f("?:?, ? ? ? (UTC)", date.getUTCHours(), minutes, date.getUTCDate(), InstaView.conf.locale.months[date.getUTCMonth()], date.getUTCFullYear());
		
		// text formatting
		return str.
			// signatures
			replace(/~{5}(?!~)/g, date).
			replace(/~{4}(?!~)/g, InstaView.conf.user.name+' '+date).
			replace(/~{3}(?!~)/g, InstaView.conf.user.name).
			
			// [[:Category:...]], [[:Image:...]], etc...
			replace(RegExp('\\[\\[:((?:'+InstaView.conf.locale.category+'|'+InstaView.conf.locale.image+'|'+InstaView.conf.wiki.interwiki+'):.*?)\\]\\]','gi'), "<a href='"+InstaView.conf.paths.articles+"$1'>$1</a>").
			replace(RegExp('\\[\\[(?:'+InstaView.conf.locale.category+'|'+InstaView.conf.wiki.interwiki+'):.*?\\]\\]','gi'),'').
			
			// [[/Relative links]]
			replace(/\[\[(\/[^|]*?)\]\]/g, f("<a href='?$1'>$1</a>", location)).
			
			// [[/Replaced|Relative links]]
			replace(/\[\[(\/.*?)\|(.+?)\]\]/g, f("<a href='?$1'>$2</a>", location)).
			
			// [[Common links]]
			replace(/\[\[([^|]*?)\]\](\w*)/g, f("<a href='?$1'>$1$2</a>", InstaView.conf.paths.articles)).
			
			// [[Replaced|Links]]
			replace(/\[\[(.*?)\|([^\]]+?)\]\](\w*)/g, f("<a href='?$1'>$2$3</a>", InstaView.conf.paths.articles)).
			
			// [[Stripped:Namespace|Namespace]]
			replace(/\[\[([^\]]*?:)?(.*?)( *\(.*?\))?\|\]\]/g, f("<a href='?$1$2$3'>$2</a>", InstaView.conf.paths.articles)).
			
			// External links
			replace(/\[(https?|news|ftp|mailto|gopher|irc):(\/*)([^\]]*?) (.*?)\]/g, "<a href='$1:$2$3'>$4</a>").
			replace(/\[http:\/\/(.*?)\]/g, "<a href='http://$1'>[#]</a>").
			replace(/\[(news|ftp|mailto|gopher|irc):(\/*)(.*?)\]/g, "<a href='$1:$2$3'>$1:$2$3</a>").
			replace(/(^| )(https?|news|ftp|mailto|gopher|irc):(\/*)([^ $]*)/g, "$1<a href='$2:$3$4'>$2:$3$4</a>").
			
			replace('__NOTOC__','').
			replace('__NOEDITSECTION__','');
	}
	
	function strip_inline_wiki(str)
	{
		return str
			.replace(/\[\[[^\]]*\|(.*?)\]\]/g,'$1')
			.replace(/\[\[(.*?)\]\]/g,'$1')
			.replace(/''(.*?)''/g,'$1');
	}
	
	// begin parsing
	for (;remain();) if ($(/^(={1,6})(.*)\1(.*)$/)) {
		p=0
		endl(f('<h?>?</h?>?', $r[1].length, parse_inline_nowiki($r[2]), $r[1].length, $r[3]))
		
	} else if ($(/^[*#:;]/)) {
		p=0
		parse_list()
		
	} else if ($(' ')) {
		p=0
		parse_pre()
		
	} else if ($('{|')) {
		p=0
		parse_table()
		
	} else if ($(/^----+$/)) {
		p=0
		endl('<hr>')
		
	} else if ($(InstaView.BLOCK_IMAGE)) {
		p=0
		parse_block_image()
		
	} else {
		
		// handle paragraphs
		if ($$('')) {
			if (p = (remain()>1 && ll[1]==(''))) endl('<p><br>')
		} else {
			if(!p) {
				ps('<p>')
				p=1
			}
			ps(parse_inline_nowiki(ll[0]) + ' ')
		}
		
		sh();
	}
	
	return o
}


/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.2-alpha Copyright (C) Paul Johnston 1999 - 2005
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s)    { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
function b64_md5(s)    { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
function hex_hmac_md5(k, d)
  { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
function b64_hmac_md5(k, d)
  { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
function any_hmac_md5(k, d, e)
  { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }

/*
 * Calculate the MD5 of a raw string
 */
function rstr_md5(s)
{
  return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
}

/*
 * Calculate the HMAC-MD5, of a key and some data (raw strings)
 */
function rstr_hmac_md5(key, data)
{
  var bkey = rstr2binl(key);
  if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
}

/*
 * Convert a raw string to a hex string
 */
function rstr2hex(input)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var output = "";
  var x;
  for(var i = 0; i < input.length; i++)
  {
    x = input.charCodeAt(i);
    output += hex_tab.charAt((x >>> 4) & 0x0F)
           +  hex_tab.charAt( x        & 0x0F);
  }
  return output;
}

/*
 * Convert a raw string to a base-64 string
 */
function rstr2b64(input)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var output = "";
  var len = input.length;
  for(var i = 0; i < len; i += 3)
  {
    var triplet = (input.charCodeAt(i) << 16)
                | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
                | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > input.length * 8) output += b64pad;
      else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
    }
  }
  return output;
}

/*
 * Convert a raw string to an arbitrary string encoding
 */
function rstr2any(input, encoding)
{
  var divisor = encoding.length;
  var remainders = Array();
  var i, q, x, quotient;

  /* Convert to an array of 16-bit big-endian values, forming the dividend */
  var dividend = Array(input.length / 2);
  for(i = 0; i < dividend.length; i++)
  {
    dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  }

  /*
   * Repeatedly perform a long division. The binary array forms the dividend,
   * the length of the encoding is the divisor. Once computed, the quotient
   * forms the dividend for the next step. We stop when the dividend is zero.
   * All remainders are stored for later use.
   */
  while(dividend.length > 0)
  {
    quotient = Array();
    x = 0;
    for(i = 0; i < dividend.length; i++)
    {
      x = (x << 16) + dividend[i];
      q = Math.floor(x / divisor);
      x -= q * divisor;
      if(quotient.length > 0 || q > 0)
        quotient[quotient.length] = q;
    }
    remainders[remainders.length] = x;
    dividend = quotient;
  }

  /* Convert the remainders to the output string */
  var output = "";
  for(i = remainders.length - 1; i >= 0; i--)
    output += encoding.charAt(remainders[i]);

  return output;
}

/*
 * Encode a string as utf-8.
 * For efficiency, this assumes the input is valid utf-16.
 */
function str2rstr_utf8(input)
{
  var output = "";
  var i = -1;
  var x, y;

  while(++i < input.length)
  {
    /* Decode utf-16 surrogate pairs */
    x = input.charCodeAt(i);
    y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
    if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
    {
      x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
      i++;
    }

    /* Encode output as utf-8 */
    if(x <= 0x7F)
      output += String.fromCharCode(x);
    else if(x <= 0x7FF)
      output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
                                    0x80 | ( x         & 0x3F));
    else if(x <= 0xFFFF)
      output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
                                    0x80 | ((x >>> 6 ) & 0x3F),
                                    0x80 | ( x         & 0x3F));
    else if(x <= 0x1FFFFF)
      output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
                                    0x80 | ((x >>> 12) & 0x3F),
                                    0x80 | ((x >>> 6 ) & 0x3F),
                                    0x80 | ( x         & 0x3F));
  }
  return output;
}

/*
 * Encode a string as utf-16
 */
function str2rstr_utf16le(input)
{
  var output = "";
  for(var i = 0; i < input.length; i++)
    output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,
                                  (input.charCodeAt(i) >>> 8) & 0xFF);
  return output;
}

function str2rstr_utf16be(input)
{
  var output = "";
  for(var i = 0; i < input.length; i++)
    output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
                                   input.charCodeAt(i)        & 0xFF);
  return output;
}

/*
 * Convert a raw string to an array of little-endian words
 * Characters >255 have their high-byte silently ignored.
 */
function rstr2binl(input)
{
  var output = Array(input.length >> 2);
  for(var i = 0; i < output.length; i++)
    output[i] = 0;
  for(var i = 0; i < input.length * 8; i += 8)
    output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  return output;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2rstr(input)
{
  var output = "";
  for(var i = 0; i < input.length * 32; i += 8)
    output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  return output;
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length.
 */
function binl_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

// </nowiki>




// Have debug on now.
//Status.debugLevel = 1;

/**
Twinklefluff revert and antivandalism utillity
*/
var VERSION = '1.0';

// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}

/**
TwinkleConfig.revertMaxRevisions (int)
defines how many revision to query maximum, maximum possible is 50, default is 50
*/
if( typeof( TwinkleConfig.revertMaxRevisions ) == 'undefined' ) {
	TwinkleConfig.revertMaxRevisions = 50;
}


/**
TwinkleConfig.userTalkPageMode may take arguments:
'window': open a new window, remmenber the opened window
'tab': opens in a new tab, if possible.
'blank': force open in a new window, even if a such window exist
*/
if( typeof( TwinkleConfig.userTalkPageMode ) == 'undefined' ) {
	TwinkleConfig.userTalkPageMode = 'window';
}

/**
TwinkleConfig.openTalkPage (array)
What types of actions that should result in opening of talk page
*/
if( typeof( TwinkleConfig.openTalkPage ) == 'undefined' ) {
	TwinkleConfig.openTalkPage = [ 'agf', 'norm', 'vand' ];
}

/**
TwinkleConfig.openTalkPageOnAutoRevert (bool)
Defines if talk page should be opened when canling revert from contrib page, this because from there, actions may be multiple, and opening talk page not suitable. If set to true, openTalkPage defines then if talk page will be opened.
*/
if( typeof( TwinkleConfig.openTalkPageOnAutoRevert ) == 'undefined' ) {
	TwinkleConfig.openTalkPageOnAutoRevert = false;
}

/**
TwinkleConfig.openAOLAnonTalkPage may take arguments:
true: to open Anon AOL talk pages on revert
false: to not open them
*/
if( typeof( TwinkleConfig.openAOLAnonTalkPage ) == 'undefined' ) {
	TwinkleConfig.openAOLAnonTalkPage = false;
}

/**
TwinkleConfig.summaryAd (string)
If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
*/
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = "";
}

/**
TwinkleConfig.markRevertedPagesAsMinor (array)
What types of actions that should result in marking edit as minor
*/
if( typeof( TwinkleConfig.markRevertedPagesAsMinor ) == 'undefined' ) {
	TwinkleConfig.markRevertedPagesAsMinor = [ ];
}

/**
TwinkleConfig.watchRevertedPages (array)
What types of actions that should result in forced addition to watchlist
*/
if( typeof( TwinkleConfig.watchRevertedPages ) == 'undefined' ) {
	TwinkleConfig.watchRevertedPages = [ 'agf', 'norm', 'vand', 'torev' ];
}


// a list of usernames, usually only bots, that vandalism revert is jumped over, that is
// if vandalism revert is choosen on such username, then it's target in on the revision before.
// This is for handeling quick bots that makes edits seconds after the original edit is made.
// This only affect vandalism rollback, for good faith rollback, it will stop, indicating a bot 
// has no faith, and for normal rollback, it will rollback that edit.
var WHITELIST = [
	'HagermanBot',
	'HBC AIV helperbot',
	'HBC AIV helperbot2',
	'HBC AIV helperbot3',
]

var revertXML;
var contentXML;
var contentDoc;
var editXML;
var vandal;
var type;
var goodRev;
var nbrOfRevisions;
var curStatus;
var curVersion = true;

addOnloadHook( function() {
	if( QueryString.exists( 'twinklerevert' ) ) {
		twinkleAutoRevert();
	} else {
		addRevertButtons();
	}
} );

function twinkleAutoRevert() {
	if( QueryString.get( 'oldid' ) != wgCurRevisionId ) {
		// not latest revision
		return;
	}

	var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];
	if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Current revision' ) {
		// not latest revision
		return;
	}

	vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue.replace("'", "\\'");

	if( !TwinkleConfig.openTalkPageOnAutoRevert ) {
		TwinkleConfig.openTalkPage = [];
	}

	return revertPage( QueryString.get( 'twinklerevert' ), vandal );
}

function addRevertButtons() {

	var spanTag = function( color, content ) {
		var span = document.createElement( 'span' );
		span.style.color = color;
		span.appendChild( document.createTextNode( content ) );
		return span;
	}

	if( wgNamespaceNumber == -1 && wgCanonicalSpecialPageName == "Contributions" ) {
		var list = document.getElementById('bodyContent').getElementsByTagName( 'ul' )[0].getElementsByTagName( 'li' );
		var vandal = document.getElementById('contentSub').getElementsByTagName( 'a' )[0].getAttribute( 'title' ).replace(/^User( talk)?:/ , '').replace("'", "\\'");

		var revNode = document.createElement('strong');
		var revLink = document.createElement('a');
		revLink.appendChild( spanTag( 'Black', ' [' ) );
		revLink.appendChild( spanTag( 'SteelBlue', 'rollback' ) );
		revLink.appendChild( spanTag( 'Black', ']' ) );
		revNode.appendChild(revLink);

		var revVandNode = document.createElement('strong');
		var revVandLink = document.createElement('a');
		revVandLink.appendChild( spanTag( 'Black', ' [' ) );
		revVandLink.appendChild( spanTag( 'Red', 'vandalism' ) );
		revVandLink.appendChild( spanTag( 'Black', ']' ) );
		revVandNode.appendChild(revVandLink);

		for(var i in list ) {
			var item = list[i].lastChild;
			if ( !item ) {
				continue;
			}
			if( userIsInGroup( 'sysop' ) ) {
				item = item.previousSibling;
			}
			if( item.nodeName != 'STRONG' ) {
				continue
			}

			var href = list[i].getElementsByTagName( 'a' )[1].getAttribute( 'href' );
			var tmpNode = revNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'norm' } ) );
			list[i].appendChild( tmpNode );
			var tmpNode = revVandNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'vand' } ) );
			list[i].appendChild( tmpNode );
		}


	} else {

		var otitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-otitle' )[0];
		var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];

		if( !ntitle ) {
			// Nothing to see here, move along...
			return;
		}

		if( !otitle.getElementsByTagName('a')[0] ) {
			// no previous revision available
			return;
		}

		// Lets first add a [edit this revision] link
		var query = new QueryString( decodeURI( otitle.getElementsByTagName( 'a' )[0].getAttribute( 'href' ).split( '?', 2 )[1] ) );

		var oldrev = query.get( 'oldid' );

		var oldEditNode = document.createElement('strong');

		var oldEditLink = document.createElement('a');
		oldEditLink.href = "javascript:revertToRevision('" + oldrev + "')";
		oldEditLink.appendChild( spanTag( 'Black', '[' ) );
		oldEditLink.appendChild( spanTag( 'SaddleBrown', 'restore this version' ) );
		oldEditLink.appendChild( spanTag( 'Black', ']' ) );
		oldEditNode.appendChild(oldEditLink);

		var cur = otitle.insertBefore(oldEditNode, otitle.firstChild);
		otitle.insertBefore(document.createElement('br'), cur.nextSibling);

		if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Current revision' ) {
			// not latest revision
			curVersion = false;
			return;
		}

		vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue.replace("'", "\\'");

		var agfNode = document.createElement('strong');
		var vandNode = document.createElement('strong');
		var normNode = document.createElement('strong');

		var agfLink = document.createElement('a');
		var vandLink = document.createElement('a');
		var normLink = document.createElement('a');

		agfLink.href = "javascript:revertPage('agf' , '" + vandal + "')"; 
		vandLink.href = "javascript:revertPage('vand' , '" + vandal + "')"; 
		normLink.href = "javascript:revertPage('norm' , '" + vandal + "')"; 

		agfLink.appendChild( spanTag( 'Black', '[' ) );
		agfLink.appendChild( spanTag( 'DarkOliveGreen', 'rollback (AGF)' ) );
		agfLink.appendChild( spanTag( 'Black', ']' ) );

		vandLink.appendChild( spanTag( 'Black', '[' ) );
		vandLink.appendChild( spanTag( 'Red', 'rollback (VANDAL)' ) );
		vandLink.appendChild( spanTag( 'Black', ']' ) );

		normLink.appendChild( spanTag( 'Black', '[' ) );
		normLink.appendChild( spanTag( 'SteelBlue', 'rollback' ) );
		normLink.appendChild( spanTag( 'Black', ']' ) );

		agfNode.appendChild(agfLink);
		vandNode.appendChild(vandLink);
		normNode.appendChild(normLink);

		var cur = ntitle.insertBefore(agfNode, ntitle.firstChild);
		cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
		cur = ntitle.insertBefore(normNode, cur.nextSibling);
		cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
		cur = ntitle.insertBefore(vandNode, cur.nextSibling);
		cur = ntitle.insertBefore(document.createElement('br'), cur.nextSibling);
	}

}

function revertPage( pType, pVandal, rev, page ) {

	wgPageName = page || wgPageName;
	wgCurRevisionId = rev || wgCurRevisionId;


	try {
		vandal = pVandal;
		type = pType;
		Status.init( document.getElementById('bodyContent') );

		revertXML = sajax_init_object();
		Status.debug( 'revertXML' + revertXML );
		revertXML.overrideMimeType('text/xml');

		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': TwinkleConfig.revertMaxRevisions,
			'rvprop': [ 'timestamp', 'user', 'comment' ],
			'format': 'xml'
		}

		Status.status( 'Querying revisions' );
		revertXML.onreadystatechange = revertPageCallback;
		revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ), true );
		revertXML.send( null );
	} catch(e) {
		if( e instanceof Exception ) {
			Status.error( 'Error: ' + e.what() );
		} else {
			Status.error( 'Error: ' + e );
		}
	}

}
function revertPageCallback() {

	if ( revertXML.readyState != 4 ){
		Status.progress('.');
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error('Bad status , bailing out');
		return;
	}

	var doc = revertXML.responseXML.documentElement;

	if( !doc ) {
		Status.error( 'Possible failure in recieving document, will abort.' );
		return;
	}
	var revisions = doc.getElementsByTagName('rev');
	var top = revisions[0];
	Status.debug( 'revisions[0]: ' + top );

	if( top.getAttribute( 'revid' ) < wgCurRevisionId ) {
		Status.error( [ 'The recieved top revision id ', htmlNode( 'strong', top.getAttribute('revid') ), ' is less than our current revision id, this could indicate that the current revision has been deleted, the server is lagging, or that bad data has been recieved. Will stop proceeding at this point.' ] );
		return;
	}
	if( !top ) {
		Status.error( 'No top revision found,  this could indicate that the page has been deleted, or that a problem in the transmittion has occoured, will abort reversion ');
		return;
	}


	Status.status( [ 'Evaluating revisions to see if ', htmlNode( 'strong', vandal), ' is the last contributor...' ] );
	Status.debug( 'wgCurRevisionId: ' + wgCurRevisionId + ', top.getAttribute(revid): ' + top.getAttribute('revid') );

	if( wgCurRevisionId != top.getAttribute('revid') ) {
		Status.warn( [ 'Latest revision ', htmlNode( 'strong', top.getAttribute('revid') ), ' doesn\'t equals our revision ', htmlNode( 'strong', wgCurRevisionId) ] );
		Status.debug( 'top.getAttribute(user): ' + top.getAttribute( 'user' ) );

		if( top.getAttribute( 'user' ) == vandal ) {
			switch( type ) {
				case 'vand':
				Status.info( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', as we assume vandalism, we continue to revert' ]);
				break;
				case 'afg':
				Status.warn( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', as we assume good faith, we stop reverting, as the problem might have been fixed.' ]);
				return;
				default:
				Status.warn( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', but we will stop reverting anyway.' ] );
				return;
			}
		} else if( 
			type == 'vand' && 
			WHITELIST.indexOf( top.getAttribute( 'user' ) ) != -1 && 
			top.nextSibling.getAttribute( 'pageId' ) == wgCurRevisionId 
		) {
			Status.info( [ 'Latest revision is made by ', htmlNode( 'strong', top.getAttribute( 'user' ) ), ', a trusted bot, and the revision before was made by our vandal, so we proceed with the revert.' ] );
			top = top.nextSibling;
		} else {
			Status.error( [ 'Latest revision is made by ', htmlNode( 'strong', top.getAttribute( 'user' ) ), ', so it might already been reverted, stopping  reverting.'] );
			return;
		}
	} 

	if( WHITELIST.indexOf( vandal ) != -1  ) {
		switch( type ) {
			case 'vand':
			Status.info( [ 'Vandalism revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, we assume you wanted to revert vandalism made by the previous user instead.' ] );
			top = top.nextSibling;
			vandal = top.getAttribute( 'user' );

			break;
			case 'agf':
			Status.warn( [ 'Good faith revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, it makes no sense at all to revert it as a good faith edit, will stop reverting.' ] );
			return;

			break;
			case 'norm':
			default:
			var cont = confirm( 'Normal revert is choosen, but the top user (' + vandal + ') is a whitelisted bot, do you want to revert the revision before instead?' );
			if( cont ) {
				Status.info( [ 'Normal revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, and per confirm, we\'ll revert the previous revision instead.' ] );
				top = top.nextSibling;
				vandal = top.getAttribute( 'user' );
			} else {
				Status.warn( [ 'Normal revert is choosen on ', htmlNode( 'strong', vandal ), ', this is a whitelisted bot, bet per confirm, revert will proceed.' ] );
			}
			break;
		}
	}

	Status.status( 'Finding last good revision...' );

	goodRev = top;
	nbrOfRevisions = 0;

	while( goodRev.getAttribute('user') == vandal ) {

		goodRev = goodRev.nextSibling;

		nbrOfRevisions++;

		if( goodRev == null ) {
			Status.error( [ 'No previous revision found, perhaps ', htmlNode( 'strong', vandal ), ' is the only contributor, or that the user has made more than ' + TwinkleConfig.revertMaxRevisions + ' edits in a row.' ] );
			return;
		}
	}

	if( nbrOfRevisions == 0 ) {
		Status.error( "We where to revert zero revisions. As that makes no sense, we'll stop reverting this time. It could be that the edit already have been reverted, but the revision id was still the same." );
		return;
	}

	if( 
		type != 'vand' && 
		nbrOfRevisions > 1  && 
		!confirm( vandal + ' has done ' + nbrOfRevisions + ' edits in a row. Are you sure you want to revert them all?' ) 
	) {
		Status.info( 'Stopping reverting per user input' );
		return;
	}

	Status.progress( [ ' revision ', htmlNode( 'strong', goodRev.getAttribute( 'revid' ) ), ' that was made ', htmlNode( 'strong', nbrOfRevisions ), ' revisions ago by ', htmlNode( 'strong', goodRev.getAttribute( 'user' ) ) ] );

	Status.status( [ 'Getting content for revision ', htmlNode( 'strong', goodRev.getAttribute( 'revid' ) ) ] );
	var query = {
		'action': 'query',
		'prop': 'revisions',
		'titles': wgPageName,
		'rvlimit': 1,
		'rvprop': 'content',
		'rvstartid': goodRev.getAttribute( 'revid' ),
		'format': 'xml'
	}

	Status.debug( 'query:' + query.toSource() );

	// getting the content for the last good revision
	revertXML = sajax_init_object();
	revertXML.overrideMimeType('text/xml');
	revertXML.onreadystatechange = revertCallback2;
	revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ), true );
	revertXML.send( null );

}

function revertCallback2() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	contentDoc = revertXML.responseXML.documentElement;
	if( !contentDoc ) {
		Status.error( 'Failed to recieve revision to revert to, will abort.');
		return;
	}

	Status.status( 'Grabbing edit form' );

	revertXML = sajax_init_object();
	revertXML.overrideMimeType('text/xml');
	revertXML.onreadystatechange = revertCallback3;

	var query = {
		'title': wgPageName,
		'action': 'submit'
	};

	Status.debug( 'query:' + query.toSource() );

	revertXML.open( 'GET' , wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), true );
	revertXML.send( null );
}

function revertCallback3() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	Status.status( 'Updating the textbox...' );

	var doc = revertXML.responseXML;

	var form = doc.getElementById( 'editform' );
	Status.debug( 'editform: ' + form );
	if( !form ) {
		Status.error( 'couldn\'t grab element "editform", aborting, this could indicate failed respons from the server' );
		return;
	}
	form.style.display = 'none';


	var content = contentDoc.getElementsByTagName('rev')[0];
	if( !content ) {
		Status.error( 'we recieved no revision, something is wrong, bailing out!' );
		return;
	}

	var textbox = doc.getElementById( 'wpTextbox1' );

	textbox.value = "";

	var cn =  content.childNodes;

	for( var i in cn ) {
		textbox.value += cn[i].nodeValue ? cn[i].nodeValue : '';
	}

	Status.status( 'Updating the summary...' );
	var summary;



	switch( type ) {
		case 'agf':
		summary = "Reverted good faith edits by [[Special:Contributions/" + vandal + "|" + vandal + "]] per policy concerns. Please read up on policies and guidelines. Thanks!" + TwinkleConfig.summaryAd;
		break;
		case 'vand':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by [[Special:Contributions/" + vandal + "|" + vandal + "]] identified as vandalism to last revision by [[User:" + goodRev.getAttribute( 'user' ) + "|" + goodRev.getAttribute( 'user' ) + "]]." + TwinkleConfig.summaryAd;
		break;
		case 'norm':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by [[Special:Contributions/" + vandal + "|" + vandal + "]]  to last revision by  [[User:" + goodRev.getAttribute( 'user' ) + "|" + goodRev.getAttribute( 'user' ) + "]]." + TwinkleConfig.summaryAd;
	}
	doc.getElementById( 'wpSummary' ).value = summary;

	if( TwinkleConfig.markRevertedPagesAsMinor.indexOf( type ) != -1 ) {
		doc.getElementById( 'wpMinoredit' ).checked = true;
	}

	if( TwinkleConfig. watchRevertedPages.indexOf( type ) != -1 ) {
		doc.getElementById( 'wpWatchthis' ).checked = true;
	}

	Status.status( [ 'Open user talk page edit form for user ', htmlNode( 'strong', vandal ) ]);

	var opentalk = true;

	if( TwinkleConfig.openTalkPage.indexOf( type ) != -1 ) {

		if( isIPAddress( vandal ) ) {
			Status.info( [ htmlNode( 'strong', vandal ), ' is an ip-address, checking if it\'s inside the AOL range' ] );

			if( AOLNetworks.some( function( net ) { return isInNetwork( vandal, net ) } )) {
				if( TwinkleConfig.openAOLAnonTalkPage ) {
					Status.info( [ htmlNode( 'strong', vandal ), ' is an AOL address. Per configuration, we will open talk page anyway' ] );
				} else {
					Status.warn( [ htmlNode( 'strong', vandal ), ' is an AOL address. will not open a edit form for the user talk page because AOL addresses are randomly assigned' ] );
					opentalk = false;
				}
			} else {
				Status.info( [ htmlNode( 'strong', vandal ), ' is an normal ip-address, opening user talk page' ] );
			}

		}

		if( opentalk ) {
			var query = {
				'title': 'User talk:' + vandal,
				'action': 'edit',
				'vanarticle': wgPageName.replace(/_/g, ' '),
				'vanarticlerevid': wgCurRevisionId,
				'vanarticlegoodrevid': goodRev.getAttribute( 'revid' ),
				'type': type,
				'count': nbrOfRevisions
			}

			Status.debug( 'query:' + query.toSource() );

			switch( TwinkleConfig.userTalkPageMode ) {
				case 'tab':
				window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), '_tab' );
				break;
				case 'blank':
				window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), '_blank', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
				break;
				case 'window':
				default :
				window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), 'twinklewarnwindow', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
				break;
			}
		}
	}

	document.getElementById('globalWrapper').appendChild( form );

	Status.status( 'Submitting the form...' );
	form.submit();
}

function revertToRevision( oldrev ) {

	try {
		Status.init( document.getElementById('bodyContent') );

		revertXML = sajax_init_object();
		revertXML.overrideMimeType('text/xml');

		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': 1,
			'rvstartid': oldrev,
			'rvprop': [ 'timestamp', 'user', 'comment', 'content' ],
			'format': 'xml'
		}

		Status.status( 'Querying revision' );
		revertXML.onreadystatechange = revertToRevisionCallback;
		revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ), true );
		revertXML.send( null );
	} catch(e) {
		if( e instanceof Exception ) {
			Status.error( 'Error: ' + e.what() );
		} else {
			Status.error( 'Error: ' + e );
		}
	}

}

function revertToRevisionCallback() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	contentDoc = revertXML.responseXML.documentElement;

	Status.status( 'Grabbing edit form' );

	revertXML = sajax_init_object();
	revertXML.overrideMimeType('text/xml');
	revertXML.onreadystatechange = revertToRevisionCallback2;
	revertXML.open( 'GET' , wgServer + wgScriptPath + '/index.php?' + QueryString.create( { 'title': wgPageName, 'action': 'submit' } ), true );
	revertXML.send( null );
}

function revertToRevisionCallback2() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	Status.status( 'Updating the textbox...' );

	var doc = revertXML.responseXML;

	var form = doc.getElementById( 'editform' );
	Status.debug( 'editform: ' + form );
	if( !form ) {
		Status.error( 'couldn\'t grab element "editform", aborting, this could indicate failed respons from the server' );
		return;
	}
	form.style.display = 'none';


	var content = contentDoc.getElementsByTagName('rev')[0];

	var textbox = doc.getElementById( 'wpTextbox1' );

	textbox.value = "";

	var cn =  content.childNodes;

	for( var i in cn ) {
		textbox.value += cn[i].nodeValue ? cn[i].nodeValue : '';
	}

	Status.status( 'Updating the summary...' );
	var summary = 'Reverted to revision ' + content.getAttribute( 'revid' ) + ' by [[User:' + content.getAttribute( 'user' ) + '|' + content.getAttribute( 'user' ) + ']].' +TwinkleConfig.summaryAd;

	doc.getElementById( 'wpSummary' ).value = summary;

	if( TwinkleConfig.markRevertedPagesAsMinor.indexOf( 'torev' ) != -1 ) {
		doc.getElementById( 'wpMinoredit' ).checked = true;
	}

	if( TwinkleConfig. watchRevertedPages.indexOf( 'torev' ) != -1 ) {
		doc.getElementById( 'wpWatchthis' ).checked = true;
	}

	document.getElementById('globalWrapper').appendChild( form );

	Status.status( 'Submitting the form...' );
	form.submit();
}

// (C) Andrea Giammarchi - JSL 1.4b
var undefined;
function $JSL(){
	this.inArray=function(){
		var tmp=false,i=arguments[1].length;
		while(i&&!tmp)tmp=arguments[1][--i]===arguments[0];
		return tmp;
	};
	this.has=function(str){return $JSL.inArray(str,$has)};
	this.random=function(elm){
		var tmp=$JSL.$random();
		while(typeof(elm[tmp])!=="undefined")tmp=$JSL.$random();
		return tmp;
	};
	this.$random=function(){return (Math.random()*1234567890).toString()};
	this.reverse=function(str){return str.split("").reverse().join("")};
	this.replace=function(str){
		var tmp=str.split(""),i=tmp.length;
		while(i>0)tmp[--i]=$JSL.$replace(tmp[i]);
		return tmp.join("");
	};
	this.$replace=function(tmp){
		var i=tmp.length===1?tmp.charCodeAt(0):0;
		switch(i) {
			case 8	:tmp="\\b";break;
			case 10	:tmp="\\n";break;
			case 11	:tmp="\\v";break;
			case 12	:tmp="\\f";break;
			case 13	:tmp="\\r";break;
			case 34	:tmp="\\\"";break;
			case 92	:tmp="\\\\";break;
			default:
				tmp=tmp.replace(/([\x00-\x07]|[\x0E-\x1F]|[\x7F-\xFF])/g,function(a,b){return "\\x"+$JSL.charCodeAt(b)}).
					replace(/([\u0100-\uFFFF])/g,function(a,b){b=$JSL.charCodeAt(b);return b.length<4?"\\u0"+b:"\\u"+b});
				break;
		};
		return tmp;
	};
	this.charCodeAt=function(str){return $JSL.$charCodeAt(str.charCodeAt(0))};
	this.$charCodeAt=function(i){
		var str=i.toString(16).toUpperCase();
		return str.length<2?"0"+str:str;
	};
	this.$toSource=function(elm){return elm.toSource().replace(/^(\(new \w+\()([^\000]+)(\)\))$/,"$2")};
	this.$toInternalSource=function(elm){
		var tmp=null;
		switch(elm.constructor) {
			case Boolean:
			case Number:
				tmp=elm;
				break;
			case String:
				tmp=$JSL.$toSource(elm);
				break;
			default:
				tmp=elm.toSource();
				break;
		};
		return tmp;
	};
	this.getElementsByTagName=function(scope,i,elm,str){
		var tmp=$JSL.$getElementsByTagName(scope),j=tmp.length,$tmp=[];
		while(i<j){if(tmp[i][str]===elm||elm==="*")$tmp.push($JSL.$getElementsByName(tmp[i]));++i};
		if(!$tmp.item){if(!$JSL.has("item"))$has.push("item");$tmp.item=function(tmp){return this[tmp]}};
		return $tmp;
	};
	this.$getElementsByTagName=function(scope){return scope.layers||scope.all};
	this.$getElementsByName=function(elm) {
		if(!elm.getElementsByTagName)	elm.getElementsByTagName=document.getElementsByTagName;
		return elm;
	};
	this.encodeURI=function(str){return str.replace(/"/g,"%22").replace(/\\/g,"%5C")};
	this.$encodeURI=function(str){return $JSL.$charCodeAt(str)};
	this.$encodeURIComponent=function(a,b){
		var i=b.charCodeAt(0),str=[];
		if(i<128)		str.push(i);
		else if(i<2048)		str.push(0xC0+(i>>6),0x80+(i&0x3F));
		else if(i<65536)	str.push(0xE0+(i>>12),0x80+(i>>6&0x3F),0x80+(i&0x3F));
		else			str.push(0xF0+(i>>18),0x80+(i>>12&0x3F),0x80+(i>>6&0x3F),0x80+(i&0x3F));
		return "%"+str.map($JSL.$encodeURI).join("%");
	};
	this.$decodeURIComponent=function(a,b,c,d,e){
		var i=0;
		if(e)	  i=parseInt(e.substr(1,2),16);
		else if(d)i=((parseInt(d.substr(1,2),16)-0xC0)<<6)+(parseInt(d.substr(4,2),16)-0x80);
		else if(c)i=((parseInt(c.substr(1,2),16)-0xE0)<<12)+((parseInt(c.substr(4,2),16)-0x80)<<6)+(parseInt(c.substr(7,2),16)-0x80);
		else	  i=((parseInt(b.substr(1,2),16)-0xF0)<<18)+((parseInt(b.substr(4,2),16)-0x80)<<12)+((parseInt(b.substr(7,2),16)-0x80)<<6)+(parseInt(b.substr(10,2),16)-0x80);
		return String.fromCharCode(i);
	};
	var $has=[];
	if(!Object.prototype.toSource){$has[$has.length]="toSource";Object.prototype.toSource=function(){
		var str=[];
		switch(this.constructor) {
			case Boolean:
				str.push("(new Boolean(",this,"))");
				break;
			case Number:
				str.push("(new Number(",this,"))");
				break;
			case String:
				str.push("(new String(\"",$JSL.replace(this),"\"))");
				break;
			case Date:
				str.push("(new Date(",this.getTime(),"))");
				break;
			case Error().constructor:
				str.push("(new Error(",$JSL.$toSource(this.message),",",$JSL.$toSource(this.fileName),",",this.lineNumber,"))");
				break;
			case Function:
				str.push("(",$JSL.$replace(this.toString()),")");
				break;
			case Array:
				var i=0,j=this.length;
				while(i<j)	str.push($JSL.$toInternalSource(this[i++]));
				str=["[",str.join(", "),"]"];
				break;
			default:
				var i=0,tmp;
				for(i in this){if(i!=="toSource")
					str.push($JSL.$toSource(i)+":"+$JSL.$toInternalSource(this[i]));
				};
				str=["{",str.join(", "),"}"];
				break;
		};
		return str.join("");
	}};
	if(!Function.prototype.apply){$has[$has.length]="apply";Function.prototype.apply=function(){
		var i=arguments.length===2?arguments[1].length:0,str,tmp=[],elm=(""+this).replace(/[^\(]+/,"function");
		if(!arguments[0])arguments[0]={};
		while(i)tmp.unshift("arguments[1]["+(--i)+"]");
		do{str="__".concat($JSL.random(arguments[0]).replace(/\./,"_"),"__")}while(new RegExp(str).test(elm));
		eval("var ".concat(str,"=arguments[0];tmp=(",elm.replace(/([^$])\bthis\b([^$])/g,"$1".concat(str,"$2")),")(",tmp.join(","),")"));
		return tmp;
	}};
	if(!Function.prototype.call){$has[$has.length]="call";Function.prototype.call=function(){
		var i=arguments.length,tmp=[];
		while(i>1)tmp.unshift(arguments[--i]);
		return this.apply((i?arguments[0]:{}),tmp);
	}};
	if(!Array.prototype.pop){$has[$has.length]="pop";Array.prototype.pop=function(){
		var a=this.length,r=this[--a];
		if(a>=0)this.length=a;
		return r;
	}};
	if(!Array.prototype.push){$has[$has.length]="push";Array.prototype.push=function(){
		var a=0,b=arguments.length,r=this.length;
		while(a<b)this[r++]=arguments[a++];
		return r;
	}};
	if(!Array.prototype.shift){$has[$has.length]="shift";Array.prototype.shift=function(){
		this.reverse();
		var r=this.pop();
		this.reverse();
		return r;
	}};
	if(!Array.prototype.splice){$has[$has.length]="splice";Array.prototype.splice=function(){
		var a,b,c,d=arguments.length,tmp=[],r=[];
		if(d>1){
			arguments[0]=parseInt(arguments[0]);
			arguments[1]=parseInt(arguments[1]);
			c=arguments[0]+arguments[1];
			for(a=0,b=this.length;a<b;a++){
				if(a<arguments[0]||a>=c){
					if(a===c&&d>2){
						for(a=2;a<d;a++)tmp.push(arguments[a]);
						a=c;
					};
					tmp.push(this[a]);
				}
				else
					r.push(this[a]);
			};
			for(a=0,b=tmp.length;a<b;a++)
				this[a]=tmp[a];
			this.length = a;
		};
		return r;
	}};
	if(!Array.prototype.unshift){$has[$has.length]="unshift";Array.prototype.unshift=function(){
		var i=arguments.length;
		this.reverse();
		while(i>0)this.push(arguments[--i]);
		this.reverse();
		return this.length;
	}};
	if(!Array.prototype.indexOf){$has[$has.length]="indexOf";Array.prototype.indexOf=function(elm,i){
		var j=this.length;
		if(!i)i=0;
		if(i>=0){while(i<j){if(this[i++]===elm){
			i=i-1+j;j=i-j;
		}}}
		else
			j=this.indexOf(elm,j+i);
		return j!==this.length?j:-1;
	}};
	if(!Array.prototype.lastIndexOf){$has[$has.length]="lastIndexOf";Array.prototype.lastIndexOf=function(elm,i){
		var j=-1;
		if(!i)i=this.length;
		if(i>=0){do{if(this[i--]===elm){
			j=i+1;i=0;
		}}while(i>0)}
		else if(i>-this.length)
			j=this.lastIndexOf(elm,this.length+i);
		return j;
	}};
	if(!Array.prototype.every){$has[$has.length]="every";Array.prototype.every=function(callback,elm){
		var b=false,i=0,j=this.length;
		if(!elm){	while(i<j&&!b)	b=!callback(this[i]||this.charAt(i),i++,this)}
		else {		while(i<j&&!b)	b=!callback.apply(elm,[this[i]||this.charAt(i),i++,this]);}
		return !b;
	}};
	if(!Array.prototype.filter){$has[$has.length]="filter";Array.prototype.filter=function(callback,elm){
		var r=[],i=0,j=this.length;
		if(!elm){while(i<j){if(callback(this[i],i++,this))
			r.push(this[i-1]);
		}} else {while(i<j){if(callback.apply(elm,[this[i],i++,this]))
			r.push(this[i-1]);
		}}
		return r;
	}};
	if(!Array.prototype.forEach){$has[$has.length]="forEach";Array.prototype.forEach=function(callback,elm){
		var i=0,j=this.length;
		if(!elm){	while(i<j)	callback(this[i],i++,this)}
		else {		while(i<j)	callback.apply(elm,[this[i],i++,this]);}
	}};
	if(!Array.prototype.map){$has[$has.length]="map";Array.prototype.map=function(callback,elm){
		var r=[],i=0,j=this.length;
		if(!elm){	while(i<j)	r.push(callback(this[i],i++,this))}
		else {		while(i<j)	r.push(callback.apply(elm,[this[i],i++,this]));}
		return r;
	}};
	if(!Array.prototype.some){$has[$has.length]="some";Array.prototype.some=function(callback,elm){
		var b=false,i=0,j=this.length;
		if(!elm){	while(i<j&&!b)	b=callback(this[i],i++,this)}
		else {		while(i<j&&!b)	b=callback.apply(elm,[this[i],i++,this]);}
		return b;
	}};
	if(!String.prototype.lastIndexOf){if(!this.inArray("lastIndexOf",$has))$has[$has.length]="lastIndexOf";String.prototype.lastIndexOf=function(elm,i){
		var str=$JSL.reverse(this),elm=$JSL.reverse(elm),r=str.indexOf(elm,i);
		return r<0?r:this.length-r;
	}};
	if("aa".replace(/\w/g,function(){return arguments[1]+" "})!=="0 1 "){$has[$has.length]="replace";String.prototype.replace=function(replace){return function(reg,func){
		var r="",tmp=$JSL.random(String);
		String.prototype[tmp]=replace;
		if(func.constructor!==Function)
			r=this[tmp](reg,func);
		else {
			function getMatches(reg,pos,a) {
				function io() {
					var a=reg.indexOf("(",pos),b=a;
					while(a>0&&reg.charAt(--a)==="\\"){};
					pos=b!==-1?b+1:b;
					return (b-a)%2===1?1:0;
				};
				do{a+=io()}while(pos!==-1);
				return a;
			};
			function $replace(str){
				var j=str.length-1;
				while(j>0)str[--j]='"'+str[j].substr(1,str[j--].length-2)[tmp](/(\\|")/g,'\\$1')+'"';
				return str.join("");
			};
			var p=-1,i=getMatches(""+reg,0,0),args=[],$match=this.match(reg),elm=$JSL.$random()[tmp](/\./,'_AG_');
			while(this.indexOf(elm)!==-1)elm=$JSL.$random()[tmp](/\./,'_AG_');
			while(i)args[--i]=[elm,'"$',(i+1),'"',elm].join("");
			if(!args.length)r="$match[i],(p=this.indexOf($match[i++],p+1)),this";
			else		r="$match[i],"+args.join(",")+",(p=this.indexOf($match[i++],p+1)),this";
			r=eval('['+$replace((elm+('"'+this[tmp](reg,'"'+elm+',func('+r+'),'+elm+'"')+'"')+elm).split(elm))[tmp](/\n/g,'\\n')[tmp](/\r/g,'\\r')+'].join("")');
		};
		delete String.prototype[tmp];
		return r;
	}}(String.prototype.replace)};
	if((new Date().getYear()).toString().length===4){$has[$has.length]="getYear";Date.prototype.getYear=function(){
		return this.getFullYear()-1900;
	}};
};$JSL=new $JSL();
if(typeof(encodeURI)==="undefined"){function encodeURI(str){
	var elm=/([\x00-\x20]|[\x25|\x3C|\x3E|\x5B|\x5D|\x5E|\x60|\x7F]|[\x7B-\x7D]|[\x80-\uFFFF])/g;
	return $JSL.encodeURI(str.toString().replace(elm,$JSL.$encodeURIComponent));
}};
if(typeof(encodeURIComponent)==="undefined"){function encodeURIComponent(str){
	var elm=/([\x23|\x24|\x26|\x2B|\x2C|\x2F|\x3A|\x3B|\x3D|\x3F|\x40])/g;
	return $JSL.encodeURI(encodeURI(str).replace(elm,function(a,b){return "%"+$JSL.charCodeAt(b)}));
}};
if(typeof(decodeURIComponent)==="undefined"){function decodeURIComponent(str){
	var elm=/(%F[0-9A-F]%E[0-9A-F]%[A-B][0-9A-F]%[8-9A-B][0-9A-F])|(%E[0-9A-F]%[A-B][0-9A-F]%[8-9A-B][0-9A-F])|(%[C-D][0-9A-F]%[8-9A-B][0-9A-F])|(%[0-9A-F]{2})/g;
	return str.toString().replace(elm,$JSL.$decodeURIComponent);
}};
if(typeof(decodeURI)==="undefined"){function decodeURI(str){
	return decodeURIComponent(str);
}};
if(!document.getElementById){document.getElementById=function(elm){
	return $JSL.$getElementsByName($JSL.$getElementsByTagName(this)[elm]);
}};
if(!document.getElementsByTagName){document.getElementsByTagName=function(elm){
	return $JSL.getElementsByTagName(this,0,elm.toUpperCase(),"tagName");
}};
if(!document.getElementsByName){document.getElementsByName=function(elm){
	return $JSL.getElementsByTagName(this,0,elm,"name");
}};
if(typeof(XMLHttpRequest)==="undefined"){XMLHttpRequest=function(){
	var tmp=null,elm=navigator.userAgent;
	if(elm.toUpperCase().indexOf("MSIE 4")<0&&window.ActiveXObject)
		tmp=elm.indexOf("MSIE 5")<0?new ActiveXObject("Msxml2.XMLHTTP"):new ActiveXObject("Microsoft.XMLHTTP");
	return tmp;
}};
if(typeof(Error)==="undefined")Error=function(){};
Error = function(base){return function(message){
	var tmp=new base();
	tmp.message=message||"";
	if(!tmp.fileName)
		tmp.fileName=document.location.href;
	if(!tmp.lineNumber)
		tmp.lineNumber=0;
	if(!tmp.stack)
		tmp.stack="Error()@:0\n(\""+this.message+"\")@"+tmp.fileName+":"+this.lineNumber+"\n@"+tmp.fileName+":"+this.lineNumber;
	if(!tmp.name)
		tmp.name="Error";
	return tmp;
}}(Error);

/**
* return 'p-tb' if id is included in array, else return 'p-cactions';
*/

function chooseBox( id, arr ) {
	return arr.indexOf( id ) == -1 ? 'p-cactions' : 'p-tb';
}

/**
* Will escape a string to be used in a RegExp
*/
RegExp.escape = function( text, space_fix ) {

	if ( !arguments.callee.sRE ) {
		arguments.callee.sRE = /(\/|\.|\*|\+|\?|\||\(|\)|\[|\]|\{|\}|\\|\$|\^)/g;
	}

	text = text.replace( arguments.callee.sRE , '\\$1' );

	// Special Mediawiki escape, underscore/space is the same, often at lest:

	if( space_fix ) {
		text = text.replace( / |_/g, '[_ ]' );
	}

	return text;

}
namespaces	=	{
	'-2':	'Media',
	'-1':	'Special',
	'0'	:	'',
	'1'	:	'Talk',
	'2'	:	'User',
	'3'	:	'User_talk',
	'4'	:	'Project',
	'5'	:	'Project talk',
	'6'	:	'Image',
	'7'	:	'Image talk',
	'8'	:	'MediaWiki',
	'9'	:	'MediaWiki talk',
	'10':	'Template',
	'11':	'Template talk',
	'12':	'Help',
	'13':	'Help talk',
	'14':	'Category',
	'15':	'Category talk',
	'100':	'Portal',
	'101':	'Portal talk'
};
/**
* Helper functions to get the month as a string instead of a number
*/

Date.monthNames = [
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December'
];
Date.monthNamesAbbrev = [
	'Jan',
	'Feb',
	'Mar',
	'Apr',
	'May',
	'Jun',
	'Jul',
	'Aug',
	'Sep',
	'Oct',
	'Nov',
	'Dec'
];

Date.prototype.getMonthName = function() {
	return Date.monthNames[ this.getMonth() ];
}

Date.prototype.getMonthNameAbbrev = function() {
	return Date.monthNamesAbbrev[ this.getMonth() ];
}
Date.prototype.getUTCMonthName = function() {
	return Date.monthNames[ this.getUTCMonth() ];
}

Date.prototype.getUTCMonthNameAbbrev = function() {
	return Date.monthNamesAbbrev[ this.getUTCMonth() ];
}

// Simple helper functions to see what groups a user might belong

function userIsInGroup( group ) {

	return ( wgUserGroups != null && wgUserGroups.indexOf( group ) != -1 ) || ( wgUserGroups == null && group == 'anon' );
}

function userIsAnon() {
	return wgUserGroups == null;
}

// AOL Proxy IP Addresses (2007-02-03)
var AOLNetworks = [
	'64.12.96.0/19',
	'149.174.160.0/20',
	'152.163.240.0/21',
	'152.163.248.0/22',
	'152.163.252.0/23',
	'152.163.96.0/22',
	'152.163.100.0/23',
	'195.93.32.0/22',
	'195.93.48.0/22',
	'195.93.64.0/19',
	'195.93.96.0/19',
	'195.93.16.0/20',
	'198.81.0.0/22',
	'198.81.16.0/20',
	'198.81.8.0/23',
	'202.67.64.128/25',
	'205.188.192.0/20',
	'205.188.208.0/23',
	'205.188.112.0/20',
	'205.188.146.144/30',
	'207.200.112.0/21',
];

// AOL Client IP Addresses (2007-02-03)
var AOLClients = [
	'172.128.0.0/10',
	'172.192.0.0/12',
	'172.208.0.0/14',
	'202.67.66.0/23',
	'172.200.0.0/15',
	'172.202.0.0/15',
	'172.212.0.0/14',
	'172.216.0.0/16',
	'202.67.68.0/22',
	'202.67.72.0/21',
	'202.67.80.0/20',
	'202.67.96.0/19',
];

/**
* ipadress is in the format 1.2.3.4 and network is in the format 1.2.3.4/5
*/

function isInNetwork( ipaddress, network ) {
	var iparr = ipaddress.split('.');
	var ip = (parseInt(iparr[0]) << 24) + (parseInt(iparr[1]) << 16) + (parseInt(iparr[2]) << 8) + (parseInt(iparr[3]));

	var netmask = 0xffffffff << network.split('/')[1];

	var netarr = network.split('/')[0].split('.');
	var net = (parseInt(netarr[0]) << 24) + (parseInt(netarr[1]) << 16) + (parseInt(netarr[2]) << 8) + (parseInt(netarr[3]));

	return (ip & netmask) == net;
}

/* Returns true if given string contains a valid IP-address, that is, from 0.0.0.0 to 255.255.255.255*/
function isIPAddress( string ){
	var res = /(\d{1,4})\.(\d{1,3})\.(\d{1,3})\.(\d{1,4})/.exec( string );
	return res != null && res.slice( 1, 5 ).every( function( e ) { return e < 256; } );
}

/**
* Maps the querystring to an object
*
* Functions:
*
* QueryString.exists(key)
*     returns true if the particular key is set
* QueryString.get(key)
*     returns the value associated to the key
* QueryString.equals(key, value)
*     returns true if the value associated with given key equals given value
* QueryString.toString()
*     returns the query string as a string
* QueryString.create( hash )
*     creates an querystring and encodes strings via encodeURIComponent and joins arrays with | 
*
* In static context, the value of location.search.substring(1), else the value given to the constructor is going to be used. The mapped hash is saved in the object.
*
* Example:
*
* var value = QueryString.get('key');
* var obj = new QueryString('foo=bar&baz=quux');
* value = obj.get('foo');
*/
function QueryString(qString) {
	this.string = qString;
	this.params = {};

	if( qString.length == 0 ) {
		return;
	}

	qString.replace(/\+/, ' ');
	var args = qString.split('&');

	for( var i in args ) {
		if( typeof( args[i] ) != 'string' ) {
			continue;
		}
		var pair = args[i].split( '=' );
		var key = decodeURIComponent( pair[0] ), value = key;

		if( pair.length == 2 ) {
			value = decodeURIComponent( pair[1] );
		}

		this.params[key] = value;
	}
}

QueryString.static = null;

QueryString.staticInit = function() {
	if( QueryString.static == null ) {
		QueryString.static = new QueryString(location.search.substring(1));
	}
}

QueryString.get = function(key) {
	QueryString.staticInit();
	return QueryString.static.get(key);
};

QueryString.prototype.get = function(key) {
	return this.params[key] ? this.params[key] : null;
};

QueryString.exists = function(key) {
	QueryString.staticInit();
	return QueryString.static.exists(key);
}

QueryString.prototype.exists = function(key) {
	return this.params[key] ? true : false;
}

QueryString.equals = function(key, value) {
	QueryString.staticInit();
	return QueryString.static.equals(key, value);
}

QueryString.prototype.equals = function(key, value) {
	return this.params[key] == value ? true : false;
}

QueryString.toString = function() {
	QueryString.staticInit();
	return QueryString.static.toString();
}

QueryString.prototype.toString = function() {
	return this.string ? this.string : null;
}


QueryString.create = function( arr ) {
	var resarr = Array();
	for( var i in arr ) {
		if( typeof arr[i] == 'object' ){
			var v =  Array();
			for(var j in arr[i] ) {
				v[j] = encodeURIComponent( arr[i][j] );
			}
			resarr.push( encodeURIComponent( i ) + '=' +  v.join('|')  );
		} else {
			resarr.push( encodeURIComponent( i ) + '=' + encodeURIComponent( arr[i] ) );
		}
	}

	return resarr.join('&');
}
QueryString.prototype.create = QueryString.create;

/**
* Simple exception handling
*/

Exception = function( str ) {
	this.str = str || '';
}

Exception.prototype.what = function() {
	return this.str;
}

/**
* Status updating class
*/

Status = function() {}

/*
Initiate an element to be a status window, it will remove all it's childs
*/
Status.init = function( elem ) {

	if( !( elem instanceof Element ) ) {
		throw new Exception( 'object not an instance of Element' );
	}

	Status.elem = elem;
	Status.currentNode = null;

	while( elem.hasChildNodes() ) {
		elem.removeChild( elem.firstChild );
	}
}

// Private function
Status.append = function( obj, node ) {

	if( Status.elem == null ) {
		throw new Exception( 'no initialized object found' );
	}

	if ( ! ( obj instanceof Array ) ) {
		obj = [ obj ];
	}

	node = node || Status.currentNode;

	for( var i in obj ) {
		if( typeof obj[i] == 'string' ) {
			node.appendChild( document.createTextNode( obj[i] ) );
		} else if( obj[i] instanceof Element ) {
			node.appendChild( obj[i] );
		}
	}
}

Status.error = function( obj ) {
	Status.currentNode = document.createElement( 'div' );
	Status.currentNode.style.color = 'OrangeRed';
	Status.currentNode.style.fontWeight = '900';
	Status.append( obj );
	Status.elem.appendChild( Status.currentNode );
	return Status.currentNode;
}

Status.warn = function( obj ) {
	Status.currentNode = document.createElement( 'div' );
	Status.currentNode.style.color = 'OrangeRed';
	Status.append( obj );
	Status.elem.appendChild( Status.currentNode );
	return Status.currentNode;
}

Status.info = function( obj ) {
	Status.currentNode = document.createElement( 'div' );
	Status.currentNode.style.color = 'ForestGreen';
	Status.append( obj );
	Status.elem.appendChild( Status.currentNode );
	return Status.currentNode;
}

Status.debug = function( obj , level ) {
	level = level || 1;
	if( Status.debugLevel >= level ) {
		Status.currentNode = document.createElement( 'div' );
		Status.currentNode.style.color = 'DimGray';
		Status.append( "Debug (" + level + "): " );
		Status.append( obj );
		Status.elem.appendChild( Status.currentNode );
		return Status.currentNode;
	} else {
		return null;
	}
}

Status.debugLevel = 0;

Status.status = function( obj ) {
	Status.currentNode = document.createElement( 'div' );
	Status.currentNode.style.color = 'SteelBlue';
	Status.append( obj );
	Status.elem.appendChild( Status.currentNode );
	return Status.currentNode;
}

Status.progress = function ( obj, node ) {
	Status.append( obj, node );	
}

// Simple helper function to create a simple node
function htmlNode( type, content, color ) {
	var node = document.createElement( type );
	if( color ) {
		node.style.color = color;
	}
	node.appendChild( document.createTextNode( content ) );
	return node;
}

// A simple dragable window

function SimpleWindow( width, height ) {
	this.width = width;
	this.height = height;
	this.frame = document.createElement( 'div' );
	SimpleWindow.frames.push( this.frame );
	this.topbar = document.createElement( 'div' );
	this.topbarover = document.createElement( 'div' );
	this.closeButton = document.createElement( 'span' );
	this.frame.appendChild( this.topbar );
	this.topbarover.appendChild( this.closeButton );


	this.frame.style.zIndex = 100;
	this.frame.style.width = width + 'px';
	this.frame.style.height = height + 'px';
	this.frame.style.position = 'fixed';
	this.frame.style.background = 'AliceBlue';
	this.frame.style.border = '2px ridge Black';
	this.frame.simpleWindow = this;
	this.frame.addEventListener( 'mousedown', this.focus, true );

	this.closeButton.appendChild( document.createTextNode( '[close]' ) );
	this.closeButton.style.position = 'absolute';
	this.closeButton.style.fontWeight = '100';
	this.closeButton.style.fontSize = '0.7em';
	this.closeButton.style.top = '0px';
	this.closeButton.style.left = '0px';
	this.closeButton.style.cursor = 'pointer';
	this.closeButton.simpleWindow = this;
	this.closeButton.addEventListener( 'click', this.close, false );

	this.topbar.style.width = '100%';
	this.topbar.style.height = '20px';
	this.topbar.style.background = 'LightSteelBlue';
	this.topbar.style.position = 'absolute';
	this.topbar.style.fontWeight = '900';
	this.topbar.style.fontSize = '1em';
	this.topbar.style.fontFamily = 'sans-serif';
	this.topbar.style.textAlign = 'center';
	this.topbar.style.verticalAlign = 'baseline';
	this.topbar.style.top = '0px';
	this.topbar.style.left = '0px';

	this.topbarover.style.width = '100%';
	this.topbarover.style.height = '24px';
	this.topbarover.style.position = 'absolute';
	this.topbarover.style.top = '0px';
	this.topbarover.style.left = '0px';
	this.topbarover.simpleWindow = this;
	this.topbarover.addEventListener( 'mousedown', this.beginMove, true );

}

SimpleWindow.prototype.focus = function(e) {
	for( var i in SimpleWindow.frames ) {
		SimpleWindow.frames[i].style.zIndex = 99;
	}

	this.simpleWindow.frame.style.zIndex = 100;

}

SimpleWindow.prototype.display = function() {

	this.title = this.title || "Title";
	this.content = this.content || document.createTextNode( "" );
	this.topbar.appendChild( document.createTextNode( this.title ) );

	var content = document.createElement( 'div' );
	content.style.position = 'relative';
	content.style.top = '20px';
	content.style.height = ( this.height -  20 ) + 'px';
	content.style.overflow = 'auto';
	content.appendChild( this.content );
	this.frame.appendChild( content );
	this.frame.appendChild( this.topbarover );
	this.frame.style.width = this.width + 'px';
	this.frame.style.height = this.height + 'px';

	this.frame.style.top = (window.innerHeight - this.height  )/2 + 'px' ;
	this.frame.style.left = (window.innerWidth - this.width  )/2 + 'px';
	document.getElementsByTagName('body')[0].appendChild( this.frame );
}

SimpleWindow.prototype.close = function(e) {
	if( e ) {
		this.simpleWindow.frame.parentNode.removeChild(this.simpleWindow.frame);
	} else {
		this.frame.parentNode.removeChild(this.frame);
	}
}

SimpleWindow.prototype.setWidth = function( width ) {
	this.width = width;
}

SimpleWindow.prototype.setHeight = function( height ) {
	this.height = height;
}

SimpleWindow.prototype.setTitle = function( title ) {
	this.title = title;
}

SimpleWindow.prototype.setContent = function( content ) {
	this.content = content;
}

SimpleWindow.frames = [];

SimpleWindow.prototype.currentObject = null;

SimpleWindow.prototype.beginMove = function(e) {
	if( e.button != 0 ) {
		return;
	}
	this.simpleWindow.initX = e.layerX;
	this.simpleWindow.initY = e.layerY;
	this.simpleWindow.lastX = e.clientX;
	this.simpleWindow.lastY = e.clientY;
	SimpleWindow.currentObject = this.simpleWindow;

	window.addEventListener( 'mousemove', SimpleWindow.moveWindow, false );
	window.addEventListener( 'mouseup', function(e) { 
		SimpleWindow.currentObject = null;
		window.removeEventListener( 'mousemove', SimpleWindow.moveWindow, false );
		window.removeEventListener( 'mouseup', SimpleWindow.moveWindow, false );
	}, false );
}

SimpleWindow.moveWindow = function(e) {
	if( SimpleWindow.currentObject == null ) {
		return;
	}
	var y = e.clientY - SimpleWindow.currentObject.initY;
	var x = e.clientX - SimpleWindow.currentObject.initX;

	SimpleWindow.currentObject.frame.style.top  = y + 'px';
	SimpleWindow.currentObject.frame.style.left = x + 'px';
	e.stopPropagation();
}

//Status.debugLevel = 1;

/**
* Last diff tab, show difference between last revision and the one before
*/

/**
TwinkleConfig.toolboxButtons (string)
If id defined in this array, the button of the action is located inthe toolbox instead of in
the actions bar.
*/
if( typeof( TwinkleConfig.toolboxButtons ) == 'undefined' ) {
	TwinkleConfig.toolboxButtons = [];
}
function lastdiff() 
{
	if( wgNamespaceNumber >= 0 ) {
		var query = {
			'title': wgPageName,
			'diff': 'cur',
			'oldid': 'prev'
		};
		addPortletLink(chooseBox( 'tw-lastdiff', TwinkleConfig.toolboxButtons ), wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), 'last', 'tw-lastdiff', 'Show most reecent diff' );
	}
}
addOnloadHook(lastdiff);

var dataXML;
var ntitle;
var me;

function diffSincePrevUserButton() { 
	if( wgNamespaceNumber < 0 ) {
		return;
	}

	if( !QueryString.exists( 'diff' ) ) {
		// Not diff page
		return;
	} 

	ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];

	if( !ntitle ) {
		// Nothing to see here, move along...
		return;
	}

	var l = addPortletLink(chooseBox( 'tw-since', TwinkleConfig.toolboxButtons ), "javascript:diffSincePrevUserInit(false);", 'since', 'tw-since', 'Show difference between last diff and the revision made by previous user' );

	var l = addPortletLink(chooseBox( 'tw-sincemine', TwinkleConfig.toolboxButtons ), "javascript:diffSincePrevUserInit(true);", 'since mine', 'tw-sincemine', 'Show difference between last diff and my last revision' );
}
addOnloadHook(diffSincePrevUserButton);

function diffSincePrevUserInit(me) {
	try {
		Status.init( document.getElementById('bodyContent') );
		var rev = wgCurRevisionId;
		var user;
		if( me ) {
			Status.debug( '"me" is choosen' );
			user = wgUserName;
		} else if( vandal ) { 
			Status.debug( 'vandal ' + vandal + ' already defined' );
			// twinklefluff or simlar have already parsed this thingi
			user = vandal;
		} else {
			Status.debug( 'testing 3:rd element' );
			user = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue;
			Status.debug( '3:rd element: ' + user );
			if( user == 'Current revision' ) {
				Status.debug( 'testing 6:th element' );
				user = ntitle.getElementsByTagName('a')[6].firstChild.nodeValue;
				Status.debug( '6:th element: ' + user );
			}
		}

		diffSincePrevUser( user, rev, me );
	} catch(e) {
		if(e.what){
			alert(e.what())
		} else {
			alert(e);
		}
	}
}

function diffSincePrevUser( user, rev, pMe ) {

	me = pMe;
	dataXML = sajax_init_object();

	dataXML.overrideMimeType('text/xml');

	var query = {
		'prop': 'revisions',
		'format': 'xml',
		'action': 'query',
		'titles': wgPageName,
		'rvlimit': 50,
		'rvprop': 'user',
		'rvendid': wgCurRevisionId
	};

	Status.debug( "Query string:" + QueryString.create( query ), 1 );
	Status.status( 'Querying revisions' );

	dataXML.open( 'GET', wgServer + wgScriptPath + "/api.php?" + QueryString.create( query ) , true );

	dataXML.onreadystatechange = function() {
		if ( dataXML.readyState != 4 ){
			Status.progress('.');		
			return;
		} 

		if( dataXML.status != 200 ){
			Status.error('Bad status , bailing out');		
			return;
		}
		Status.progress(' Done.');		

		var doc = dataXML.responseXML.documentElement;
		var revisions = doc.getElementsByTagName('rev');
		Status.debug( 'length: ' + revisions.length, 1 );

		var curNode = revisions[0];

		Status.debug( 'curNode: ' + curNode, 1 );
		Status.debug( 'user: ' + user, 1 );
		Status.debug( "rev: " + rev, 1 );

		var last = null;

		Status.status( 'Finding last revision made by user...' );

		while( curNode != null ) {
			Status.debug( 'curNode: ' + curNode.getAttribute( 'revid' ), 1 );


			if( me ) {
				if( curNode.getAttribute( 'revid' ) < rev && curNode.getAttribute( 'user' ) == user ) {
					Status.debug( "found rev and user", 1 );
					last = curNode.getAttribute( 'revid' );
					break;
				} 
			} else {
				if( curNode.getAttribute( 'revid' ) < rev && curNode.getAttribute( 'user' ) != user ) {
					Status.debug( "found rev and user", 1 );
					last = curNode.getAttribute( 'revid' );
					break;
				} 
			}
			curNode = curNode.nextSibling;
		}

		if( last == null ) {
			Status.error( [ 'No previos revision found, or ', user , ' is only contributor ' ] );
			return;
		}

		var query = {
			'title': wgPageName,
			'oldid': last,
			'diff': rev
		};

		window.location = wgServer + wgScriptPath + '/index.php?' + QueryString.create( query );

	};
	dataXML.send( null );

}