/*************
*** Call any JS file in MediaWiki namespace with &usejs parameter
*** by [[m:user:Pathoschild]]
*************/

/* get parameter */
var usejs = String(window.location.href.match(/(?:\?|&)usejs=[^&]*/i));

/* if used */
if(null != usejs && usejs != "null") {
	function transcludetest() {
		/* get & fix value */
		usejs = usejs.replace(/^(?:\?|&)usejs=(.*)$/i,'$1');
		usejs = usejs.replace(/^MediaWiki:(.*)$/i,'$1');
		usejs = usejs.replace(/^(.*[^\.][^j][^s])$/i,'$1.js');

		/* write to page */
		// CSS
		document.write('<style type="text/css" media="screen, projection">'
		   + '#usejsbox { padding:0.5em; border:1px solid red; background:#000; color:#FFF; }\n'
		   + '#usejsbox a { color:#FFF; text-decoration:underline; }'
		   + '</style>');

		// JavaScript
		var usejsurl = wgServer + wgScript + '?title=MediaWiki:' + usejs + '&action=raw&ctype=text/javascript';
		document.write('<script type="text/javascript" src="' + usejsurl + '"></script>');
	
		// notify user
		var usejslink = document.createElement('a');
		usejslink.setAttribute('title',usejs);
		usejslink.setAttribute('href',usejsurl);
		usejslink.appendChild(document.createTextNode('MediaWiki:'+usejs));

		var usejshelp = document.createElement('a');
		usejshelp.setAttribute('title','What is usejs?');
		usejshelp.setAttribute('href','http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Usejs');
		usejshelp.appendChild(document.createTextNode('what is this?'));

		var usejsbox = document.createElement('div');
		usejsbox.setAttribute('id','usejsbox');

		usejsbox.appendChild(document.createTextNode('Currently transcluding "'));
		usejsbox.appendChild(usejslink);
		usejsbox.appendChild(document.createTextNode('" ('));
		usejsbox.appendChild(usejshelp);
		usejsbox.appendChild(document.createTextNode(').'));

		var usejscontainer = document.getElementById('bodyContent');
		usejscontainer.insertBefore(usejsbox,usejscontainer.firstChild);
	}

	// wait until DOM is available
	addOnloadHook(transcludetest);
}