// JS QuickTags version 1.1
//
// Copyright (c) 2002-2004 Alex King
// http://www.alexking.org/
//
// Licensed under the LGPL license
// http://www.gnu.org/copyleft/lesser.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************
//
// This JavaScript will insert the tags below at the cursor position in IE and
// Gecko-based browsers (Mozilla, Camino, Firefox, Netscape). For browsers that
// do not support inserting at the cursor position (Safari, OmniWeb) it appends
// the tags to the end of the content.
//
// The variable 'edCanvas' must be defined as the <textarea> element you want
// to be editing in. See the accompanying 'index.html' page for an example.

var edButtons = new Array();
var edLinks = new Array();
var edOpenTags = new Array();
function edButton(id, display, tagStart, tagEnd, open) {
	this.id = id;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.open = open;			// set to -1 if tag does not need to be closed
}

edButtons[edButtons.length] = new edButton('ed_bold'
                                          ,'B'
                                          ,'**'
                                          ,'**'
                                          );

edButtons[edButtons.length] = new edButton('ed_italic'
                                          ,'I'
                                          ,'//'
                                          ,'//'
                                          );

edButtons[edButtons.length] = new edButton('ed_under'
                                          ,'U'
                                          ,'__'
                                          ,'__'
                                          );

edButtons[edButtons.length] = new edButton('ed_mono'
                                          ,'mono'
                                          ,"''"
                                          ,"''"
                                          );

edButtons[edButtons.length] = new edButton('ed_strike'
                                          ,'S'
                                          ,'<del>'
                                          ,'</del>'
                                          );

edButtons[edButtons.length] = new edButton('ed_h1'
                                          ,'H1'
                                          ,'====== '
                                          ,' ======'
                                          );

edButtons[edButtons.length] = new edButton('ed_h2'
                                          ,'H2'
                                          ,'===== '
                                          ,' ====='
                                          );

edButtons[edButtons.length] = new edButton('ed_h3'
                                          ,'H3'
                                          ,'==== '
                                          ,' ===='
                                          );

edButtons[edButtons.length] = new edButton('ed_h4'
                                          ,'H4'
                                          ,'=== '
                                          ,' ==='
                                          );

edButtons[edButtons.length] = new edButton('ed_h5'
                                          ,'H5'
                                          ,'== '
                                          ,' =='
                                          );

edButtons[edButtons.length] = new edButton('ed_link'
                                          ,'link'
                                          ,''

                                          ,']]'

                                          ); // special case



edButtons[edButtons.length] = new edButton('ed_img'

                                          ,'img'

                                          ,''

                                          ,''

                                          ,-1

                                          ); // special case



edButtons[edButtons.length] = new edButton('ed_ul'

                                          ,'UL'

                                          ,'  - '

                                          ,'\n'

                                          );



edButtons[edButtons.length] = new edButton('ed_li'

                                          ,'LI'

                                          ,'  * '

                                          ,'\n'

                                          );



edButtons[edButtons.length] = new edButton('ed_pre'

                                          ,'pre'

                                          ,'<code>'

                                          ,'</code>'

                                          );



edButtons[edButtons.length] = new edButton('ed_bug'

                                          ,'bug'

                                          ,'[[bug#'

                                          ,']]'

                                          );



function edLink(display, URL, newWin) {

	this.display = display;

	this.URL = URL;

	if (!newWin) {

		newWin = 0;

	}

	this.newWin = newWin;

}





edLinks[edLinks.length] = new edLink('ticket'

                                    ,'details.php?id=000'

                                    );



edLinks[edLinks.length] = new edLink('project'

                                    ,'project.php?id=000'

                                    );



function edShowButton(button, i) {

	if (button.id == 'ed_bold') {

		document.write('<img src="images/buttons/bold.png" id="' + button.id + '" alt="' + button.display + '" title="bold" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_italic') {

		document.write('<img src="images/buttons/italic.png" id="' + button.id + '" alt="' + button.display + '" title="italic" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_under') {

		document.write('<img src="images/buttons/underline.png" id="' + button.id + '" alt="' + button.display + '" title="underline" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_strike') {

		document.write('<img src="images/buttons/strike.png" id="' + button.id + '" alt="' + button.display + '" title="strikeout" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_h1') {

		document.write('<img src="images/buttons/fonth1.png" id="' + button.id + '" alt="' + button.display + '" title="h1" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_h2') {

		document.write('<img src="images/buttons/fonth2.png" id="' + button.id + '" alt="' + button.display + '" title="h2" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_h3') {

		document.write('<img src="images/buttons/fonth3.png" id="' + button.id + '" alt="' + button.display + '" title="h3" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_h4') {

		document.write('<img src="images/buttons/fonth4.png" id="' + button.id + '" alt="' + button.display + '" title="h4" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_h5') {

		document.write('<img src="images/buttons/fonth5.png" id="' + button.id + '" alt="' + button.display + '" title="h5" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_link') {

		document.write('<img src="images/buttons/extlink.png" id="' + button.id + '" alt="' + button.display + '" title="link" align="middle" onclick="edInsertLink(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_img') {

		document.write('<img src="images/buttons/image.png" id="' + button.id + '" alt="' + button.display + '" title="image" align="middle" onclick="edInsertImage(edCanvas);" />');

	}

	else if (button.id == 'ed_ul') {

		document.write('<img src="images/buttons/list.png" id="' + button.id + '" alt="' + button.display + '" title="list" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_li') {

		document.write('<img src="images/buttons/list_ul.png" id="' + button.id + '" alt="' + button.display + '" title="bullet list" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_pre') {

		document.write('<img src="images/buttons/code.png" id="' + button.id + '" alt="' + button.display + '" title="code" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_mono') {

		document.write('<img src="images/buttons/monospace.png" id="' + button.id + '" alt="' + button.display + '" title="mono" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_block') {

		document.write('<img src="images/buttons/quote.png" id="' + button.id + '" alt="' + button.display + '" title="quote" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else if (button.id == 'ed_bug') {

		document.write('<img src="images/buttons/bug.png" id="' + button.id + '" alt="' + button.display + '" title="bug" align="middle" onclick="edInsertTag(edCanvas, ' + i + ');" />');

	}

	else {

		document.write('<input type="button" id="' + button.id + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '" />');

	}

}



function edShowLinks() {

	var tempStr = '<select onchange="edQuickLink(this.options[this.selectedIndex].value, this);"><option value="-1" selected>(Quick Links)</option>';

	for (i = 0; i < edLinks.length; i++) {

		tempStr += '<option value="' + i + '">' + edLinks[i].display + '</option>';

	}

	tempStr += '</select>';

	document.write(tempStr);

}



function edAddTag(button) {

	if (edButtons[button].tagEnd != '') {

		edOpenTags[edOpenTags.length] = button;

		document.getElementById(edButtons[button].id).value = '/' + document.getElementById(edButtons[button].id).value;

	}

}



function edRemoveTag(button) {

	for (i = 0; i < edOpenTags.length; i++) {

		if (edOpenTags[i] == button) {

			edOpenTags.splice(i, 1);

			document.getElementById(edButtons[button].id).value = 		document.getElementById(edButtons[button].id).value.replace('/', '');

		}

	}

}



function edCheckOpenTags(button) {

	var tag = 0;

	for (i = 0; i < edOpenTags.length; i++) {

		if (edOpenTags[i] == button) {

			tag++;

		}

	}

	if (tag > 0) {

		return true; // tag found

	}

	else {

		return false; // tag not found

	}

}



function edCloseAllTags() {

	var count = edOpenTags.length;

	for (o = 0; o < count; o++) {

		edInsertTag(edCanvas, edOpenTags[edOpenTags.length - 1]);

	}

}



function edQuickLink(i, thisSelect) {

	if (i > -1) {

		var newWin = '';

		if (edLinks[i].newWin == 1) {

			newWin = ' target="_blank"';

		}

		var tempStr = edLinks[i].display + ' <a href="' + edLinks[i].URL + '"' + newWin + '>'

		            + '#000'

		            + '</a>';

		thisSelect.selectedIndex = 0;

		edInsertContent(edCanvas, tempStr);

	}

	else {

		thisSelect.selectedIndex = 0;

	}

}



function edSpell(myField) {

	var word = '';

	if (document.selection) {

		myField.focus();

	    var sel = document.selection.createRange();

		if (sel.text.length > 0) {

			word = sel.text;

		}

	}

	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;

		var endPos = myField.selectionEnd;

		if (startPos != endPos) {

			word = myField.value.substring(startPos, endPos);

		}

	}

	if (word == '') {

		word = prompt('Enter a word to look up:', '');

	}

	if (word != '') {

		window.open('http://dictionary.reference.com/search?q=' + escape(word));

	}

}



function edToolbar() {

	document.write('<div id="ed_toolbar" style="margin-top: 2px;">');

	for (i = 0; i < edButtons.length; i++) {

		edShowButton(edButtons[i], i);

	}

	document.write(' ');

	document.write('</div>');

}



// insertion code



function edInsertTag(myField, i) {

	//IE support

	if (document.selection) {

		myField.focus();

	    sel = document.selection.createRange();

		if (sel.text.length > 0) {

			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;

		}

		else {

			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {

				sel.text = edButtons[i].tagStart;

				edAddTag(i);

			}

			else {

				sel.text = edButtons[i].tagEnd;

				edRemoveTag(i);

			}

		}

		myField.focus();

	}

	//MOZILLA/NETSCAPE support

	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;

		var endPos = myField.selectionEnd;

		var cursorPos = endPos;

		var scrollTop = myField.scrollTop;



		if (startPos != endPos) {

			myField.value = myField.value.substring(0, startPos)

			              + edButtons[i].tagStart

			              + myField.value.substring(startPos, endPos)

			              + edButtons[i].tagEnd

			              + myField.value.substring(endPos, myField.value.length);

			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;

		}

		else {

			if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {

				myField.value = myField.value.substring(0, startPos)

				              + edButtons[i].tagStart

				              + myField.value.substring(endPos, myField.value.length);

				edAddTag(i);

				cursorPos = startPos + edButtons[i].tagStart.length;

			}

			else {

				myField.value = myField.value.substring(0, startPos)

				              + edButtons[i].tagEnd

				              + myField.value.substring(endPos, myField.value.length);

				edRemoveTag(i);

				cursorPos = startPos + edButtons[i].tagEnd.length;

			}

		}

		myField.focus();

		myField.selectionStart = cursorPos;

		myField.selectionEnd = cursorPos;

		myField.scrollTop = scrollTop;

	}

	else {

		if (!edCheckOpenTags(i) || edButtons[i].tagEnd == '') {

			myField.value += edButtons[i].tagStart;

			edAddTag(i);

		}

		else {

			myField.value += edButtons[i].tagEnd;

			edRemoveTag(i);

		}

		myField.focus();

	}

}



function edInsertContent(myField, myValue) {

	//IE support

	if (document.selection) {

		myField.focus();

		sel = document.selection.createRange();

		sel.text = myValue;

		myField.focus();

	}

	//MOZILLA/NETSCAPE support

	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;

		var endPos = myField.selectionEnd;

		myField.value = myField.value.substring(0, startPos)

		              + myValue

                      + myField.value.substring(endPos, myField.value.length);

		myField.focus();

		myField.selectionStart = startPos + myValue.length;

		myField.selectionEnd = startPos + myValue.length;

	} else {

		myField.value += myValue;

		myField.focus();

	}

}



function edInsertLink(myField, i, defaultValue) {

	if (!defaultValue) {

		defaultValue = 'http://';

	}

	if (!edCheckOpenTags(i)) {

		var URL = prompt('Enter the URL' ,defaultValue);

		if (URL) {

			edButtons[i].tagStart = '[[' + URL + '|';

			edInsertTag(myField, i);

		}

	}

	else {

		edInsertTag(myField, i);

	}

}



function edInsertImage(myField) {

	var myValue = prompt('Enter the URL of the image', 'http://');

	if (myValue) {

		myValue = '{{'

				+ myValue

				+ ' |' + prompt('Enter a description of the image', '')

				+ '}}';

		edInsertContent(myField, myValue);

	}

}

