


var document_ready_listeners = [];
var document_ready_bound = false;
var document_ready_is = false;

function document_ready(fn)
{
	if(document_ready_is) {
		fn();
	}
	else {
		document_ready_listeners.push(fn);
		document_ready_bind();
	}
}

function document_ready_exec()
{
	document_ready_is = true;
	for(var i = 0; i < document_ready_listeners.length; ++i) {
		document_ready_listeners[i]();
	}
}

// jQuery's function bindReady()
function document_ready_bind()
{
	if(document_ready_bound) return;
	document_ready_bound = true;

	var userAgent = navigator.userAgent.toLowerCase();
	var safari = /webkit/.test( userAgent );
	var opera = /opera/.test( userAgent );
	var msie = /msie/.test( userAgent ) && !/opera/.test( userAgent );

	// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
	if(document.addEventListener) {
		document.addEventListener("DOMContentLoaded", document_ready_exec, false );
		return;
	}
	
	// If IE is used and is not in a frame
	// Continually check to see if the document is ready
	if(msie && window == top) {
		(function(){
			if(document_ready_is) return;
			try {
				// If IE is used, use the trick by Diego Perini
				// http://javascript.nwbox.com/IEContentLoaded/
				document.documentElement.doScroll("left");
			}
			catch(error) {
				setTimeout(arguments.callee, 0);
				return;
			}
			// and execute any waiting functions
			document_ready_exec();
		})();
		return;
	}

	if(safari) {
		(function(){
			if(document_ready_is) return;
			if(document.readyState != "loaded" && document.readyState != "complete") {
				setTimeout( arguments.callee, 0 );
				return;
			}
			document_ready_exec();
		})();
		return;
	}

	// A fallback to window.onload, that will always work ???
	if(window.attachEvent) {
		window.attachEvent('load', document_ready_exec);
	}
	else {
		window.onload = document_ready_exec;
	}
}



function prefer_deobfuscate_document()
{
	var el_list = document.getElementsByTagName('span');
	//for(var i = 0; i < el_list.length; ++i) {
	for(var i = el_list.length - 1; i >= 0; --i) {
		if(el_list[i].className.match(/\bprefer-deobfuscate\b/)) {
			var text = el_list[i].textContent ? el_list[i].textContent : el_list[i].innerText;
			if(!text) continue;
			var ea = text.replace(/([^\s]+)\s+på\s+([^\s]+)\s+([^\s]+).*/, "$1@$2$3");
			var a = document.createElement('a');
			a.href = 'mai' + 'lto:' + ea;
			a.appendChild(document.createTextNode(ea));
			el_list[i].parentNode.replaceChild(a, el_list[i]);
		}
	}
}

// jQuery-version of prefer_deobfuscate_document
if(0) {
	$(document).ready(function(){
		$(".prefer-deobfuscate").each(function() {
			var ea = $(this).text().replace(/([^\s]+)\s+på\s+([^\s]+)\s+([^\s]+).*/, "$1@$2$3");
			$(this).replaceWith("<a href=\"mai" + "lto:" + ea + "\">" + ea + "</a>");
		});
	});
}
