/* 
	GetWiki 2.0 interactive support | Javascript code for GetWiki 2.0 GUI
	javascript released under CCL terms
*/

	var noOverwrite=false;
	var alertText;
	var clientPC = navigator.userAgent.toLowerCase();
	var is_iemac = ((clientPC.indexOf('msie')!=-1) && (clientInformation.platform.indexOf('Mac')!=-1));
	var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
	var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
	var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));

if (clientPC.indexOf('opera')!=-1) 
	{
	var is_opera = true;
	var is_opera_preseven = (window.opera && !document.childNodes);
	var is_opera_seven = (window.opera && document.childNodes);
	}

// a list of special characters
var SpecialChar = new Array(


		"#191",
		"#192",
		"#193",
		"#194",
		"#195",
		"#196",
		"#197",
		"#198",
		"#199",
		"#200",
		"#201",
		"#202",
		"#203",
		"#204",
		"#205",
		"#206",
		"#207",
		"#208",
		"#209",
		"#210",
		"#211",
		"#212",
		"#213",
		"#214",
		"#215",
		"#216",
		"#217",
		"#218",
		"#219",
		"#220",
		"#221",
		"#222",
		"#223",
		"#224",
		"#225",
		"#226",
		"#227",
		"#228",
		"#229",
		"#230",
		"#231",
		"#232",
		"#233",
		"#234",
		"#235",
		"#236",
		"#237",
		"#238",
		"#239",
		"#240",
		"#241",
		"#242",
		"#243",
		"#244",
		"#245",
		"#246",
		"#247",
		"#248",
		"#249",
		"#250",
		"#251",
		"#252",
		"#253",
		"#254",
		"#255",
		
		"Alpha",
		"Beta",
		"Gamma",
		"Delta",
		"Epsilon",
		"Zeta",
		"Eta",
		"Theta",
		"Iota",
		"Kappa",
		"Lambda",
		"Mu",
		"Nu",
		"Xi",
		"Omicron",
		"Pi",
		"Rho",
		"Sigma",
		"Tau",
		"Upsilon",
		"Phi",
		"Chi",
		"Psi",
		"Omega",

		"alpha",
		"beta",
		"gamma",
		"delta",
		"epsilon",
		"zeta",
		"eta",
		"theta",
		"iota",
		"kappa",
		"lambda",
		"mu",
		"nu",
		"xi",
		"omicron",
		"pi",
		"rho",
		"sigma",
		"tau",
		"upsilon",
		"phi",
		"chi",
		"psi",
		"omega"
		
		);


// make the smileybox
function addToolbar()
	{
	// block IE/Mac
	if (is_iemac) return;

	document.writeln("<div id=\"smileybox\">\n<b><em>have fun...<small>but not too much fun</small></em></b><br /><br />\n");

	// just for fun, some smilies!
	addSmiley('images/smileys/smile.gif','smile','[',']','icon=smile');
	addSmiley('images/smileys/grin.gif','grin','[',']','icon=grin');
	addSmiley('images/smileys/blink.gif','blink blink','[',']','icon=blink');
	addSmiley('images/smileys/cool.gif','cool','[',']','icon=cool');
	addSmiley('images/smileys/blush.gif','blush','[',']','icon=blush');
	addSmiley('images/smileys/wink.gif','wink wink','[',']','icon=wink');
	addSmiley('images/smileys/huh.gif','huh','[',']','icon=huh');
	addSmiley('images/smileys/mellow.gif','mellow','[',']','icon=mellow');
	addSmiley('images/smileys/tongue.gif','tongue','[',']','icon=tongue');
	addSmiley('images/smileys/rolleyes.gif','rolleyes','[',']','icon=rolleyes');
	addSmiley('images/smileys/hide.gif','hide','[',']','icon=hide');
	addSmiley('images/smileys/sad.gif','sad','[',']','icon=sad');
	addSmiley('images/smileys/ohmy.gif','oh my!','[',']','icon=ohmy');
	addSmiley('images/smileys/nowink.gif','nowink','[',']','icon=nowink');
	addSmiley('images/smileys/lol.gif','lol','[',']','icon=lol');
	addSmiley('images/smileys/wag.gif','wag','[',']','icon=wag');
	addSmiley('images/smileys/shucks.gif','shucks','[',']','icon=shucks');
	addSmiley('images/smileys/unsure.gif','unsure','[',']','icon=unsure');
	addSmiley('images/smileys/angry.gif','angry','[',']','icon=angry');
	addSmiley('images/smileys/wag.gif','wag','[',']','icon=wag');
	addSmiley('images/smileys/wacko.gif','wacko','[',']','icon=wacko');
	addSmiley('images/smileys/sick.gif','sick','[',']','icon=sick');
	addSmiley('images/smileys/rant.gif','rant','[',']','icon=rant');
	addSmiley('images/smileys/bash.gif','bash','[',']','icon=bash');
	addSmiley('images/smileys/rofl.gif','rofl','[',']','icon=rofl');
	
	document.writeln("<br /><br />\n\n");

	// show the formatting buttons in a row
	addButton('images/icons/button_bold.gif','bold text','\'\'\'','\'\'\'','bold text');
	addButton('images/icons/button_italic.gif','italic text','\'\'','\'\'','italic text');
	addButton('images/icons/button_link.gif','internal link','[[',']]','link title');
	addButton('images/icons/button_extlink.gif','external link (remember http:// prefix)','[',']','http://www.example.com link title');
	addButton('images/icons/button_headline.gif','level2 headline','\n\n== ',' ==\n\n','headline text');
	//addButton('images/icons/button_image.gif','image link','[[Image:','|Image caption]]','example.jpg');
	//addButton('images/icons/button_timage.gif','thumbnail image link','[[Image:','|thumb|right|scale:50|Image caption]]','example.jpg');
	//addButton('images/icons/button_media.gif','media link','[[Media:',']]','example.mp3');
	//addButton('images/icons/button_msg.gif','include message/page','{{msg:','}}','custom message');
	//addButton('images/icons/button_math.gif','logic/math formula (LaTeX)','\<formula\>','\</formula\>','insert formula here');
	//addButton('images/icons/button_nowiki.gif','ignore wiki formatting','\<nowiki\>','\</nowiki\>','insert non-formatted text here');
	//addButton('images/icons/button_sig.gif','signature with timestamp','-~~~~','','');
	addButton('images/icons/button_hr.gif','horizontal line','\n\n----\n','','');
	//addInfobox('click a button for example text','Please enter the text you want to be formatted.\\nIt will be shown in the infobox for copy and pasting.\\nExample:\\n$1\\nwill become:\\n$2');

	document.writeln("<br /><br />\n\n");

	// show the special characters in a box
	document.writeln("\n<div id=\"specialchars\">");
	var l = SpecialChar.length;
	for(i = 0; i < l; i++) 
		{
		if (!SpecialChar[i])
			{
			//document.writeln("\n&nbsp;&nbsp;");
			document.writeln("<br />\n");
			}
		else
			{
			document.writeln("\n<a href=\"javascript:getWikitext('&',';','"+SpecialChar[i]+"')\">&"+SpecialChar[i]+";</a>");
			}
		}

	document.writeln("\n</div>\n");
	
	document.writeln("</div>");
	}

// make toolbar buttons with formatting text
function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) 
	{
	speedTip=escapeQuotes(speedTip);
	tagOpen=escapeQuotes(tagOpen);
	tagClose=escapeQuotes(tagClose);
	sampleText=escapeQuotes(sampleText);
	var mouseOver="";

	// if we can't change the selection, we can try to show examples
	if(!document.selection && !is_gecko) 
		{
		// filter backslashes so it can be shown in the infobox
		var re=new RegExp("\\\\n","g");
		tagOpen=tagOpen.replace(re,"");
		tagClose=tagClose.replace(re,"");
		mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
		}

	document.write("<a class=\"smiley\" href=\"javascript:getWikitext");
	document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");

	document.write("<img class=\"editbutton\" width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\""+mouseOver+">");
	document.write("</a>");
	//return;
	}

function addSmiley(imageFile, speedTip, tagOpen, tagClose, sampleText) 
	{
	speedTip=escapeQuotes(speedTip);
	tagOpen=escapeQuotes(tagOpen);
	tagClose=escapeQuotes(tagClose);
	sampleText=escapeQuotes(sampleText);
	var mouseOver="";

	// if we can't change the selection, we can try to show examples
	if(!document.selection && !is_gecko) 
		{
		// filter backslashes so it can be shown in the infobox
		var re=new RegExp("\\\\n","g");
		tagOpen=tagOpen.replace(re,"");
		tagClose=tagClose.replace(re,"");
		mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
		}

	document.write("<a class=\"smiley\" href=\"javascript:getWikitext");
	document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");

	document.write("<img src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\""+mouseOver+">");
	document.write("</a>");
	//return;
	}

function addInfobox(infoText,text_alert) 
	{
	alertText=text_alert;
	var clientPC = navigator.userAgent.toLowerCase(); // Get client info

	var re=new RegExp("\\\\n","g");
	alertText=alertText.replace(re,"\n");

	if(!document.selection && !is_gecko) 
		{
 		infoText=escapeQuotesHTML(infoText);
	 	document.write("<br />\n<form name=\"infoform\" id=\"infoform\">"+
		"<input size=\"60\" id=\"infobox\" name=\"infobox\" value=\""+
		infoText+"\" READONLY></form>");
 		}

	}

function escapeQuotes(text)
	{
	var re=new RegExp("'","g");
	text=text.replace(re,"\\'");
	re=new RegExp('"',"g");
	text=text.replace(re,'&quot;');
	re=new RegExp("\\n","g");
	text=text.replace(re,"\\n");
	return text;
	}

function escapeQuotesHTML(text) 
	{
	var re=new RegExp('"',"g");
	text=text.replace(re,"&quot;");
	return text;
	}

// apply tagOpen/tagClose to selection in textarea
function getWikitext(tagOpen, tagClose, sampleText) 
	{
	var textbox = document.postform.textbox;
	
	// ie
	if(document.selection  && !is_gecko) 
		{
		var theSelection = document.selection.createRange().text;
		if(!theSelection) { theSelection=sampleText;}
		textbox.focus();
		if(theSelection.charAt(theSelection.length - 1) == " ")
			{
			// exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
			} 
		else 
			{
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
			}
		} 

	// mozilla
	else if(textbox.selectionStart || textbox.selectionStart == '0') 
		{
 		var startPos = textbox.selectionStart;
		var endPos = textbox.selectionEnd;
		var scrollTop=textbox.scrollTop;
		var myText = (textbox.value).substring(startPos, endPos);
		if(!myText) { myText=sampleText;}
		if(myText.charAt(myText.length - 1) == " ")
			{ 
			// exclude ending space
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
			} 
		else 
			{
			subst = tagOpen + myText + tagClose;
			}
		textbox.value = textbox.value.substring(0, startPos) + subst + 
		textbox.value.substring(endPos, textbox.value.length);
		textbox.focus();

		var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
		textbox.selectionStart=cPos;
		textbox.selectionEnd=cPos;
		textbox.scrollTop=scrollTop;

		} 

	// all others
	else 
		{
		var copy_alertText=alertText;
		var re1=new RegExp("\\$1","g");
		var re2=new RegExp("\\$2","g");
		copy_alertText=copy_alertText.replace(re1,sampleText);
		copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
		var text;

		if (sampleText) {text=prompt(copy_alertText);} 
		else {text="";}
		if(!text) { text=sampleText;}
		text=tagOpen+text+tagClose;
		document.textbox.value=text;
		// in Safari this causes scrolling
		if(!is_safari) {textbox.focus();}
		noOverwrite=true;
		}

	// reposition cursor if possible
	if (textbox.createTextRange) textbox.caretPos = document.selection.createRange().duplicate();
	}
