//Script written by TarquinWJ of http://www.howtocreate.co.uk/
function prepdbl() {
if( document.documentElement && ( !location.pathname || !location.pathname.match( /\/edit$/ ) ) ) {
document.documentElement.ondblclick = function () { 
	var url = location.href;
	var url = url.match(/http:\/\/([\w\.]+)\/([\w]+)\/([\w]+)/);
	url = url[0]+'\/edit'; 
	location.href = url;
	};
}
MWcreateButtons();

if( window.XTRonload ) { window.XTRonload(); }
}

//Credit where credit is due;
//load handling adapted from http://www.brothercake.com/site/resources/scripts/onload/
if( window.addEventListener ) {
window.addEventListener( 'load', prepdbl, false );
} else if( document.addEventListener ) {
document.addEventListener('load' , prepdbl, false );
} else if( window.attachEvent ) {
window.attachEvent( 'onload', prepdbl );
} else {
if( window.onload ) { window.XTRonload = window.onload; }
window.onload = prepdbl;
}

/*
Script written by TarquinWJ of http://www.howtocreate.co.uk/
Almost complete rewrite of the scripts from DokuWiki to simplify code,
remove the awful sniffer script, and make it XHTML compatible
*/

var MWbuttonList = [];

function formatButton( imageFile, toolTip, tagOpen, tagClose, sampleText, accessKey ) {
MWbuttonList[MWbuttonList.length] = arguments;
}

function MWcreateButtons() {

//test for DOM browser
if( !document.createElement || !document.getElementById || !document.childNodes ) { return; }
if( !MWbuttonList.length ) { return; } //no buttons needed

var oPara = document.createElement('p');

for( var i = 0; MWbuttonList[i]; i++ ) {

var imageFile = MWbuttonList[i][0];
var toolTip   = MWbuttonList[i][1];
var accessKey = MWbuttonList[i][5];

//create the link with the image inside it
var linkBut = document.createElement('a');
if( accessKey ) {
linkBut.setAttribute('accesskey',accessKey);
toolTip = ( toolTip ? ( toolTip + ' ' ) : '' ) + '[AccessKey: ' + accessKey.toUpperCase() + ']';
}
if( toolTip ) { linkBut.title = toolTip; }
linkBut.setAttribute('href','javascript:insertTags('+i+');');

var imgBut = document.createElement('img');
imgBut.setAttribute('border','0');
imgBut.setAttribute('src',baseURL+imageFile);
if( toolTip ) { 
imgBut.setAttribute('alt',toolTip);
imgBut.title = toolTip;
}
linkBut.appendChild(imgBut);

oPara.appendChild(linkBut);

}

//put it before the form input
var oBefore = document.getElementById('editform').body;
oBefore.parentNode.insertBefore(oPara,oBefore);

}

function insertTags( oEntry ) {

var openText  = MWbuttonList[oEntry][2];
var closeText = MWbuttonList[oEntry][3];
var altText   = MWbuttonList[oEntry][4];

//make sure we get the selection from the correct input
var input = document.getElementById('editform').body
input.focus();

if( input.setSelectionRange ) {

//Mozilla compatible
var slen = input.selectionStart;
var elen = input.selectionEnd;

//ignore trailing spaces
if( openText && ( slen != elen ) && input.value.substr( elen - 1, 1 ) == ' ' ) { elen--; }
var lastBit = input.value.substr( elen );

//put in the extra text (including alt text if they were supposed to select something but didn't)
input.value = input.value.substr( 0, slen ) + openText +
( ( openText && ( slen == elen ) ) ? altText : input.value.substring( slen, elen ) ) +
closeText + lastBit;

if( openText && ( slen == elen ) ) {
//select the default text, ready to be overwritten
input.setSelectionRange(slen+openText.length,slen+openText.length+altText.length);
} else {
//move to the end of the selection
input.setSelectionRange(input.value.length-lastBit.length,input.value.length-lastBit.length);
}

} else if( document.selection && input.createTextRange ) {

//IE compatible
var inpRng = document.selection.createRange();

//ignore trailing spaces
if( openText && inpRng.text && inpRng.moveEnd && inpRng.text.substr( inpRng.text.length - 1, 1 ) == ' ' ) {
//opera can't do this, so I am making sure the Mozilla syntax is used first if available
inpRng.moveEnd( 'character', -1 );
}

var orgTxt = inpRng.text;

//put in the extra text (including alt text if they were supposed to select something but didn't)
inpRng.text = openText + ( ( openText && !orgTxt ) ? altText : orgTxt ) + closeText;

//move to the end of the selection
//(cannot select default text easily because IE screws up the range when the text is changed - too bad. so sad.)
if( inpRng.collapse ) { inpRng.collapse(false); inpRng.select(); }

} else {

//default for older browsers - put it at the end
input.value += openText + altText + closeText;

if( !window.isAlreadyNotSoGood ) {
//add the warning message - first time only
window.isAlreadyNotSoGood = true;
var oPar2 = document.createElement('p');
oPar2.appendChild(document.createTextNode('Formatting tags will be inserted at the end of the text'));
input.parentNode.insertBefore(oPar2,input.previousSibling);
}

}

}