// WYSIWYG Stuff

function initWYSIWYG(type) {
	if (typeof(document.designMode) == "string" && (document.all || document.designMode == "off")) {
		var textareas = getElementsByClassName(document, "textarea", "wysiwyg");
		for (var i = 0; i < textareas.length; i++) {
			convertWYSIWYG(textareas[i],type);
		}
		var urlpattern = /\/adv\/EditProfile$/;
		if (urlpattern.test(document.location.href)) {
			jscolor_init();
		}
	}
	return true;
}

function convertWYSIWYG(textarea,type) {
	var textareaID = textarea.getAttribute("id");
	var textareaName = textarea.getAttribute("name");
	var textareaValue = textarea.value;
	var input = document.createElement("input");
	input.setAttribute("type", "hidden");
	input.setAttribute("id", textareaID);
	input.setAttribute("name", textareaName);
	input.value = textareaValue;
	if (type=='adv') {
		var tcolour = document.getElementById("textcolour").value;
		var bcolour = document.getElementById("backcolour").value;
	}
	var iframe = document.createElement("iframe");
	iframe.className = "wysiwygIframe";
	iframe.setAttribute("id", "wysiwygIframe");
	textarea.parentNode.replaceChild(iframe, textarea);
	if (typeof iframe.document != "undefined" && typeof iframe.contentDocument == "undefined" && typeof iframe.contentWindow == "undefined") {
		iframe.parentNode.replaceChild(textarea, iframe);
		return false;
	}
	var iframeHeader = '<html><head><style type="text/css" media="all">body{font: 0.75em arial, tahoma, sans-serif;';
	if (tcolour) { iframeHeader += ' color: '+tcolour+';'; }
	else { iframeHeader += ' color: #000000;'; }
	if (bcolour) { iframeHeader += ' background-color: '+bcolour+';'; }
	else { iframeHeader += ' background-color: #ffffff;'; }
	iframeHeader += '}</style></head><body>';
	iframe.parentNode.insertBefore(input, iframe);
	iframe.contentWindow.document.open();
	iframe.contentWindow.document.write( iframeHeader + input.value + '</body></html>');
	iframe.contentWindow.document.close();
	iframe.contentWindow.document.body.contentEditable = true;
	iframe.contentWindow.document.designMode = "on";
	var form = iframe.parentNode;
	while (form != null && form.nodeName.toLowerCase() != "form") {
		form = form.parentNode;
	}
	if (form != null) {
		attachEventListener(form, "submit", function() {input.value = iframe.contentWindow.document.body.innerHTML; }, false);
	}
	// setup toolbar
	var toolbar = document.createElement("div");
	toolbar.className = "wysiwygToolbar";
	iframe.parentNode.insertBefore(toolbar, iframe);
	if (type=='adv') {
	/*	var buttonBColour = document.createElement("a");
		buttonBColour.className = "wysiwygButtonBColour";
		buttonBColour.setAttribute("href", "#");
		buttonBColour.setAttribute("title", "Back Colour");
		buttonBColour.appendChild(document.createTextNode("Back Colour"));
		buttonBColour.command = "BackColor";
		buttonBColour.iframe = iframe;
		buttonBColour.onmousedown = mousedownToolbar;
		buttonBColour.onclick = executeWYSIWYG;
		toolbar.appendChild(buttonBColour);
		var buttonFColour = document.createElement("a");
		buttonFColour.className = "wysiwygButtonFColour";
		buttonFColour.setAttribute("href", "#");
		buttonFColour.setAttribute("title", "Text Colour");
		buttonFColour.appendChild(document.createTextNode("Text Colour"));
		buttonFColour.command = "ForeColor";
		buttonFColour.iframe = iframe;
		buttonFColour.onmousedown = mousedownToolbar;
		buttonFColour.onclick = executeWYSIWYG;
		toolbar.appendChild(buttonFColour);
		var buttonLink = document.createElement("a");
		buttonLink.className = "wysiwygButtonLink";
		buttonLink.setAttribute("href", "#");
		buttonLink.setAttribute("title", "Link");
		buttonLink.appendChild(document.createTextNode("Link"));
		buttonLink.command = "CreateLink";
		buttonLink.iframe = iframe;
		buttonLink.onmousedown = mousedownToolbar;
		buttonLink.onclick = executeWYSIWYG;
		toolbar.appendChild(buttonLink); */
	}
	var buttonRedo = document.createElement("a");
	buttonRedo.className = "wysiwygButtonRedo";
	buttonRedo.setAttribute("href", "#");
	buttonRedo.setAttribute("title", "Redo");
	buttonRedo.appendChild(document.createTextNode("Redo"));
	buttonRedo.command = "Redo";
	buttonRedo.iframe = iframe;
	buttonRedo.onmousedown = mousedownToolbar;
	buttonRedo.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonRedo);
	var buttonUndo = document.createElement("a");
	buttonUndo.className = "wysiwygButtonUndo";
	buttonUndo.setAttribute("href", "#");
	buttonUndo.setAttribute("title", "Undo");
	buttonUndo.appendChild(document.createTextNode("Undo"));
	buttonUndo.command = "Undo";
	buttonUndo.iframe = iframe;
	buttonUndo.onmousedown = mousedownToolbar;
	buttonUndo.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonUndo);
	var buttonOL = document.createElement("a");
	buttonOL.className = "wysiwygButtonOL";
	buttonOL.setAttribute("href", "#");
	buttonOL.setAttribute("title", "Numbered List");
	buttonOL.appendChild(document.createTextNode("Numbered List"));
	buttonOL.command = "InsertOrderedList";
	buttonOL.iframe = iframe;
	buttonOL.onmousedown = mousedownToolbar;
	buttonOL.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonOL);
	var buttonUL = document.createElement("a");
	buttonUL.className = "wysiwygButtonUL";
	buttonUL.setAttribute("href", "#");
	buttonUL.setAttribute("title", "Bulleted List");
	buttonUL.appendChild(document.createTextNode("Bulleted List"));
	buttonUL.command = "InsertUnorderedList";
	buttonUL.iframe = iframe;
	buttonUL.onmousedown = mousedownToolbar;
	buttonUL.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonUL);
	var buttonUnderline = document.createElement("a");
	buttonUnderline.className = "wysiwygButtonUnderline";
	buttonUnderline.setAttribute("href", "#");
	buttonUnderline.setAttribute("title", "Underline");
	buttonUnderline.appendChild(document.createTextNode("Underline"));
	buttonUnderline.command = "Underline";
	buttonUnderline.iframe = iframe;
	buttonUnderline.onmousedown = mousedownToolbar;
	buttonUnderline.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonUnderline);
	var buttonItalic = document.createElement("a");
	buttonItalic.className = "wysiwygButtonItalic";
	buttonItalic.setAttribute("href", "#");
	buttonItalic.setAttribute("title", "Italic");
	buttonItalic.appendChild(document.createTextNode("Italic"));
	buttonItalic.command = "Italic";
	buttonItalic.iframe = iframe;
	buttonItalic.onmousedown = mousedownToolbar;
	buttonItalic.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonItalic);
	var buttonBold = document.createElement("a");
	buttonBold.className = "wysiwygButtonBold";
	buttonBold.setAttribute("href", "#");
	buttonBold.setAttribute("title", "Bold");
	buttonBold.appendChild(document.createTextNode("Bold"));
	buttonBold.command = "Bold";
	buttonBold.iframe = iframe;
	buttonBold.onmousedown = mousedownToolbar;
	buttonBold.onclick = executeWYSIWYG;
	toolbar.appendChild(buttonBold);

	return true;
}

function executeWYSIWYG(event) {

	switch (this.command) {
/*		case "ForeColor":
			if (document.getElementById("wysiwygCP")) {
				hideColorPicker();
			}
			else {
				ColorPicker(event,"ForeColor");
			}
			break;
		case "BackColor":
			if (document.getElementById("wysiwygCP")) {
				hideColorPicker();
			}
			else {
				ColorPicker(event,"BackColor");
			}
			break; */
/*		case "CreateLink":
			LinkMaker(event);
			break; */
		default:
			if(!document.all) this.iframe.contentWindow.document.execCommand('styleWithCSS',false,false);
			this.iframe.contentWindow.document.execCommand(this.command,false, null);
			this.iframe.contentWindow.focus();
	}
	return false;
}

function mousedownToolbar(event) {
	return false;
}
/*
function ColorPicker(event,type) {
	var colors = new Array("#000000","#000033","#000066","#000099","#0000CC","#0000FF","#330000","#330033","#330066","#330099","#3300CC",
							"#3300FF","#660000","#660033","#660066","#660099","#6600CC","#6600FF","#990000","#990033","#990066","#990099",
							"#9900CC","#9900FF","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#FF0000","#FF0033","#FF0066",
							"#FF0099","#FF00CC","#FF00FF","#003300","#003333","#003366","#003399","#0033CC","#0033FF","#333300","#333333",
							"#333366","#333399","#3333CC","#3333FF","#663300","#663333","#663366","#663399","#6633CC","#6633FF","#993300",
							"#993333","#993366","#993399","#9933CC","#9933FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF",
							"#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#006600","#006633","#006666","#006699","#0066CC",
							"#0066FF","#336600","#336633","#336666","#336699","#3366CC","#3366FF","#666600","#666633","#666666","#666699",
							"#6666CC","#6666FF","#996600","#996633","#996666","#996699","#9966CC","#9966FF","#CC6600","#CC6633","#CC6666",
							"#CC6699","#CC66CC","#CC66FF","#FF6600","#FF6633","#FF6666","#FF6699","#FF66CC","#FF66FF","#009900","#009933",
							"#009966","#009999","#0099CC","#0099FF","#339900","#339933","#339966","#339999","#3399CC","#3399FF","#669900",
							"#669933","#669966","#669999","#6699CC","#6699FF","#999900","#999933","#999966","#999999","#9999CC","#9999FF",
							"#CC9900","#CC9933","#CC9966","#CC9999","#CC99CC","#CC99FF","#FF9900","#FF9933","#FF9966","#FF9999","#FF99CC",
							"#FF99FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#33CC00","#33CC33","#33CC66","#33CC99",
							"#33CCCC","#33CCFF","#66CC00","#66CC33","#66CC66","#66CC99","#66CCCC","#66CCFF","#99CC00","#99CC33","#99CC66",
							"#99CC99","#99CCCC","#99CCFF","#CCCC00","#CCCC33","#CCCC66","#CCCC99","#CCCCCC","#CCCCFF","#FFCC00","#FFCC33",
							"#FFCC66","#FFCC99","#FFCCCC","#FFCCFF","#00FF00","#00FF33","#00FF66","#00FF99","#00FFCC","#00FFFF","#33FF00",
							"#33FF33","#33FF66","#33FF99","#33FFCC","#33FFFF","#66FF00","#66FF33","#66FF66","#66FF99","#66FFCC","#66FFFF",
							"#99FF00","#99FF33","#99FF66","#99FF99","#99FFCC","#99FFFF","#CCFF00","#CCFF33","#CCFF66","#CCFF99","#CCFFCC",
							"#CCFFFF","#FFFF00","#FFFF33","#FFFF66","#FFFF99","#FFFFCC","#FFFFFF");
	var total = colors.length;
	var width = 18;
	var cp_contents = "";
	cp_contents += "<table>";
	if (document.getElementById) {
		cp_contents += "<tr><td colspan='"+width+"' style='background-color: #ffffff;height:30px' id='SelectedColor'>&nbsp;</td></tr>";
	}
	var use_highlight = (document.getElementById || document.all)?true:false;
	for (var i=0; i<total; i++) {
		if ((i % width) == 0) { cp_contents += "<tr>"; }
		if (use_highlight) { var mo = 'onMouseOver="showColor(\''+colors[i]+'\',window.document)"'; }
		else { mo = ""; }
		cp_contents += '<td style="background-color: '+colors[i]+';"><a href="#" title="'+colors[i]+'" onClick="setColor(\''+colors[i]+'\',\''+type+'\');return false;" '+mo+' style="text-decoration:none;">&nbsp;&nbsp;&nbsp;</a></td>';
		if ( ((i+1)>=total) || (((i+1) % width) == 0)) { 
			cp_contents += "</tr>";
		}
	}
	cp_contents += "</table>";

	var cp = document.createElement("div");
	cp.className = "wysiwygCP";
	cp.setAttribute("id", "wysiwygCP");
	var cpPos = getCursorPosition(event);
	cp.style.left = cpPos[0] - 390 + "px";
	cp.style.top = cpPos[1] - 120 + "px";
	cp.innerHTML = cp_contents;
	var iframe = document.getElementById("wysiwygIframe");
	iframe.parentNode.insertBefore(cp, iframe);
}

function showColor(c) {
	var thedoc = (arguments.length>1)?arguments[1]:window.document;
	var d = thedoc.getElementById("SelectedColor");
	d.style.backgroundColor = c;
}

function setColor(c,type) {
	hideColorPicker();
	var iframe = document.getElementById("wysiwygIframe");
	if(type=='ForeColor') {
		iframe.contentWindow.document.body.style.color = c;
		document.getElementById("textcolour").value = c;
	}
	else if (type=='BackColor') {
		iframe.contentWindow.document.body.style.backgroundColor = c;
		document.getElementById("backcolour").value = c;
	}
}

function hideColorPicker() {
	var cp = document.getElementById("wysiwygCP");
	cp.parentNode.removeChild(cp);
		return;
}
*/
/*
function LinkMaker(event) {
	var lm_contents = "";
	lm_contents += '<form><select id="wysiwyglinktype" name="wysiwyglinktype"><option value="http://">http://</option><option value="https://">https://</option><option value="mailto:">mailto:</option></select> <input type="text" name="wysiwyglink" id="wysiwyglink" /> <input type="submit" class="wysiwyglinkok" value="OK" onClick="insertLink();return false;" /></form>';

	var lm = document.createElement("div");
	lm.className = "wysiwygLM";
	lm.setAttribute("id", "wysiwygLM");
	var lmPos = getCursorPosition(event);
	lm.style.left = lmPos[0] - 420 + "px";
	lm.style.top = lmPos[1] - 120 + "px";
	lm.innerHTML = lm_contents;
	var iframe = document.getElementById("wysiwygIframe");
	iframe.parentNode.insertBefore(lm, iframe);
}

function insertLink() {
	var hyperLink = document.getElementById('wysiwyglinktype').value + document.getElementById('wysiwyglink').value;
	var iframe = document.getElementById("wysiwygIframe");
	iframe.contentWindow.document.execCommand("CreateLink", false, hyperLink);
	hideLinkMaker();
}

function hideLinkMaker() {
	var lm = document.getElementById("wysiwygLM");
	lm.parentNode.removeChild(lm);
		return;
}
*/