


// Startup variables
var imageTag = false;
var theSelection = false;



// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1) && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1) && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);




// Helpline messages
default_help = "<strong>Tip:</strong> Styles can be applied quickly to selected text"
b_help = "<strong>Bold text:</strong> <em>[b]</em>text<em>[/b]</em>";
i_help = "<strong>Italic text:</strong> <em>[i]</em>text<em>[/i]</em>";
u_help = "<strong>Underline text:</strong> <em>[u]</em>text<em>[/u]</em>";
q_help = "<strong>Quote text:</strong> <em>[quote]</em>text<em>[/quote]</em>";
c_help = "<strong>Code display:</strong> <em>[code]</em>code<em>[/code]</em>";
l_help = "<strong>List:</strong> <em>[list]</em>text<em>[/list]</em>";
o_help = "<strong>Ordered list:</strong> <em>[list=]</em>text<em>[/list]</em>";
p_help = "<strong>Insert image:</strong> <em>[img]</em>http://image_url<em>[/img]</em>";
w_help = "<strong>Insert URL:</strong> <em>[url]</em>http://url<em>[/url]</em> <strong>or</strong> <em>[url=http://url]</em>URL text<em>[/url]</em>";
a_help = "<strong>Close all open code tags</strong>";
s_help = "<strong>Font color:</strong> [color=red]text[/color]  Tip: you can also use color=#FF0000";
f_help = "<strong>Font size:</strong> [size=x-small]small text[/size]";




// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]');
imageTag = false;




function initForumEditor() {

	if (!dom) return false;

	var theTextareas = document.getElementsByTagName("textarea");

	for (var i = 0; i < theTextareas.length; i++) {

		var targetTextarea = theTextareas[i];

		if (targetTextarea.className.indexOf("epForumEditor") != -1) {

			if (targetTextarea.id == "") {
				targetTextarea.id = targetTextarea.name;
			}

			setTimeout("new epForumEditor('" + targetTextarea.id + "')", 500 * (i));
		}
	}

	return true;
}







function epForumEditor(instance) {

	var self = this;

	this.theTextarea = document.getElementById(instance);
	this.theContent = this.theTextarea.value;
	this.theTextarea.id = "oldTextarea";
	this.theTextarea.name = "oldTextarea";

	this.theEditPanel = document.createElement("textarea");
	this.theEditPanel.id = instance;
	this.theEditPanel.name = instance;
	this.theEditPanel.value = this.theContent;

	this.theContainer = document.createElement("div");
	this.theContainer.className = "epForumContainer";

	this.theToolbar = new epForumToolbar(this);

	this.theHelpLabel = document.createElement("p");
	this.theHelpLabel.id = "feHelp";

	if (this.theTextarea.form.className == "standardForm") {
		this.theHelpLabel.innerHTML = default_help;
	}
	else {
		this.theHelpLabel.className = "hide";
		this.theHelpLabel.innerHTML = "";
	}

	this.theTextarea.style.display = "none";

	this.theContainer.appendChild(this.theToolbar.theTable);
	this.theContainer.appendChild(this.theHelpLabel);
	this.theContainer.appendChild(this.theEditPanel);

	this.theTextarea.parentNode.replaceChild(this.theContainer, this.theTextarea);

	return true;
}






function epForumToolbar(epForumEditor) {

	var self = this;

	this.epForumEditorObj = epForumEditor;

	this.theTable = document.createElement("table");
	this.theTable.id = this.epForumEditorObj.theEditPanel.id + "Toolbar";
	this.theTable.className = "epForumToolBar"
	this.theTable.epForumToolbarObj = this;

	this.theTBody = document.createElement("tbody");

	this.theRow = document.createElement("tr");
	this.theRow.setAttribute("align","center");
	this.theRow.setAttribute("valign","middle");
	this.addButton('addbbcode0',' B ', 'feButton b', this.theTable.id + '_b');
	this.addButton('addbbcode2',' i ', 'feButton i', this.theTable.id + '_i');
	//this.addButton('addbbcode4',' u ', 'feButton u', this.theTable.id + '_u');
	this.addButton('addbbcode6','Quote', 'feButton w50', this.theTable.id + '_q');
	//this.addButton('addbbcode8','Code', 'feButton w40', this.theTable.id + '_c');
	//this.addButton('addbbcode10','List', 'feButton w40', this.theTable.id + '_ul');
	//this.addButton('addbbcode12','List=', 'feButton w40', this.theTable.id + '_ol');
	//this.addButton('addbbcode14','Img', 'feButton w40', this.theTable.id + '_img');
	this.addButton('addbbcode16','URL', 'feButton w40', this.theTable.id + '_url');
	this.addButton('addbbcode-1','Close tags', 'feButton w40', this.theTable.id + '_close');

	this.theTBody.appendChild(this.theRow);
	this.theTable.appendChild(this.theTBody);

	return true;
}





epForumToolbar.prototype.addButton = function(bName, bValue, bClass, bID) {

	var theCell = document.createElement("td");
	var theButton = document.createElement("input");

	theButton.type = "button";
	theButton.className = bClass;
	theButton.name = bName;
	theButton.id = bID
	theButton.value = bValue;

	theButton.onclick = epForumToolbarAction;
	theButton.onmouseover = epForumToolbarMouseover;
	theButton.onmouseout = epForumToolbarMouseout;

	theCell.appendChild(theButton);
	this.theRow.appendChild(theCell);

	return true;
}




function epForumToolbarMouseover() {

	var theToolBar = this.parentNode.parentNode.parentNode.parentNode.epForumToolbarObj;
	var theHelpLine = theToolBar.epForumEditorObj.theHelpLabel;

	if (theHelpLine.className == "hide") {
		document.getElementById("feHelp").className = "normal";
	}

	switch (this.value) {
		case " B ":
			document.getElementById("feHelp").innerHTML = b_help;
			break;
		case " i ":
			document.getElementById("feHelp").innerHTML = i_help;
			break;
		case " u ":
			document.getElementById("feHelp").innerHTML = u_help;
			break;
		case "Quote":
			document.getElementById("feHelp").innerHTML = q_help;
			break;
		case "Code":
			document.getElementById("feHelp").innerHTML = c_help;
			break;
		case "List":
			document.getElementById("feHelp").innerHTML = l_help;
			break;
		case "List=":
			document.getElementById("feHelp").innerHTML = o_help;
			break;
		case "Img":
			document.getElementById("feHelp").innerHTML = p_help;
			break;
		case "URL":
			document.getElementById("feHelp").innerHTML = w_help;
			break;
		case "Close tags":
			document.getElementById("feHelp").innerHTML = a_help;
			break;
	}
}






function epForumToolbarMouseout() {

	var theToolBar = this.parentNode.parentNode.parentNode.parentNode.epForumToolbarObj;
	var theHelpLine = theToolBar.epForumEditorObj.theHelpLabel;

	if (theHelpLine.className == "normal") {
		document.getElementById("feHelp").className = "hide";
	}

	document.getElementById("feHelp").innerHTML = default_help;
}






function epForumToolbarAction() {

	var theToolbar = this.parentNode.parentNode.parentNode.parentNode.epForumToolbarObj;
	var theEditPanel = theToolbar.epForumEditorObj.theEditPanel;

	theEditPanel.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;

	bbnumber = getNumberFromValue(this.value);

	if (bbnumber == -1) {
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			theEditPanel.value += bbtags[butnumber + 1];

			activeButtonID = getIDFromNumber(butnumber);
			activeButtonObj = document.getElementById(theToolbar.theTable.id + activeButtonID)
			activeButtonText = activeButtonObj.value;
			activeButtonObj.value = activeButtonText.substr(0,(activeButtonText.length - 1));

		}
		imageTag = false; // All tags are closed including image tags :D
		theEditPanel.focus();
		return;
	}


	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			theEditPanel.focus();
			theSelection = '';
			return;
		}
	}
	else if (theEditPanel.selectionEnd && (theEditPanel.selectionEnd - theEditPanel.selectionStart > 0)) {
		mozWrap(theEditPanel, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}


	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				theEditPanel.value += bbtags[butnumber + 1];

				activeButtonID = getIDFromNumber(butnumber);
				activeButtonObj = document.getElementById(theToolbar.theTable.id + activeButtonID)
				activeButtonText = activeButtonObj.value;
				activeButtonObj.value = activeButtonText.substr(0,(activeButtonText.length - 1));

				imageTag = false;
			}
			theEditPanel.focus();
			return;
	}

	else { // Open tags


		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another

			theEditPanel.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list

			// Return button back to normal state
			imgButton = document.getElementById(theToolbar.theTable.id + "_img");
			imgButton.value = "Img";
			imageTag = false;
		}


		// Open tag
		theEditPanel.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode, bbnumber+1);
		this.value = this.value += "*";
		theEditPanel.focus();
		return;
	}
	storeCaret(theEditPanel);

}



// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray, value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}




function getNumberFromValue(vValue) {
	switch (vValue) {
		case " B ":
			return 0;
			break;
		case " B *":
			return 0;
			break;
		case " i ":
			return 2;
			break;
		case " i *":
			return 2;
			break;
		case " u ":
			return 4;
			break;
		case " u *":
			return 4;
			break;
		case "Quote":
			return 6;
			break;
		case "Quote*":
			return 6;
			break;
		case "Code":
			return 8;
			break;
		case "Code*":
			return 8;
			break;
		case "List":
			return 10;
			break;
		case "List*":
			return 10;
			break;
		case "List=":
			return 12;
			break;
		case "List=*":
			return 12;
			break;
		case "Img":
			return 14;
			break;
		case "Img*":
			return 14;
			break;
		case "URL":
			return 16;
			break;
		case "URL*":
			return 16;
			break;
		case "Close tags":
			return -1;
			break;
	}
}


function getIDFromNumber(nNumber) {
	switch (nNumber) {
		case 0:
			return "_b";
			break;
		case 2:
			return "_i";
			break;
		case 4:
			return "_u";
			break;
		case 6:
			return "_q";
			break;
		case 8:
			return "_c";
			break;
		case 10:
			return "_ul";
			break;
		case 12:
			return "_ol";
			break;
		case 14:
			return "_img";
			break;
		case 16:
			return "_url";
			break;
		case -1:
			return "_close";
			break;
	}
}




// Active the on load event
addLoadEvent(initForumEditor);