var emopanel=null;

//If the browser supports design-mode try to enable the wysiwyg text-area :)
if (document.getElementById && document.designMode) {
	if (window.addEventListener){
		window.addEventListener("load", start_wysiwyg, false);
	} else if (window.attachEvent){
		window.attachEvent("onload", start_wysiwyg);
	} else {
		//WYSIWYG not supported by browser :)
		//Firefox & IE seem compatible, Opera isn't.
	}
}

function updatewysiwyg() {
	var divs=this.getElementsByTagName("div");
	var i;
	for (i=0; i<divs.length; i++) {
		var cur=divs[i];
		if (cur.className=="wysiwyg" && cur.control.contentWindow)
			cur.textarea.value=wysiwig_tree(cur.control.contentWindow.document.body);
	}
}

var chr_bold="\x02";
var chr_italic="\x05";
var chr_underline="\x1F";
var chr_strike="\x06";

function wysiwig_tree(item) {
	if (item.nodeName.toLowerCase()=="#text")
		return item.nodeValue.replace(/[\r\n]/i,"");
	else if (item.nodeName.toLowerCase()=="img") {
		var re=/.*?\?text=(.*)/i;
		var res;
		if ((res=re.exec(item.src))!=null)
		  return res[1];		
	} else {
		var retval="";
		var curchild=item.firstChild;
		while (curchild) {
			retval+=wysiwig_tree(curchild);
			curchild=curchild.nextSibling;
		}
		var nodename=item.nodeName.toLowerCase();
		if (nodename=="p" || nodename=="div" || nodename=="br")
			retval+="\r\n";
		if (nodename!="br") {
			if (nodename=="b" || nodename=="strong" || item.style.fontWeight=="bold")
				retval=chr_bold+retval+chr_bold;
			if (nodename=="i" || nodename=="em" || item.style.fontStyle=="italic")
				retval=chr_italic+retval+chr_italic;
			if (nodename=="u" || item.style.textDecoration.indexOf("underline")!=-1)
				retval=chr_underline+retval+chr_underline;
			if (nodename=="strike" || item.style.textDecoration.indexOf("line-through")!=-1)
				retval=chr_strike+retval+chr_strike;
		}
		return retval;
	}
	return "";
}
			
function createwysiwyg(field) {
	var textarea=document.getElementById(field);
	if (!textarea)
		return;
	//Create new elements
	var editor=document.createElement("div");
	var control=document.createElement("iframe");
	control.src="about:blank";
	editor.id="wysiwyg_"+field;
	editor.className="wysiwyg";
	editor.appendChild(control);
	editor.control=control;
	//Hide text field
	textarea.style.display="none";
	//Replace text-field with the editor
	textarea.parentNode.replaceChild(editor,textarea);
	//Put the text-area in the editor
	editor.appendChild(textarea);
	editor.textarea=textarea;
	//Create toolbar
	createtoolbar(field);
	//Make sure the form knows what to do :)
	var cur=editor;
	while (cur) {
		if (cur.nodeName == "FORM") {
			cur.onsubmit=updatewysiwyg;
			break;
		}
		cur=cur.parentNode;
	}
	//Set timeout to set it editable
	setTimeout("initwysiwyg(\""+field+"\");", 1);
}

function createbutton(toolbar,text,description,editor,field,command) {
	var button=document.createElement("input");
	button.type="button"
	button.value= (text!="E") ? text : "     ";
	button.editor=editor;
	button.field=field;
	button.onclick=command;
	button.className="wysiwyg_button wysiwyg_"+text;
//	button.id="wysiwyg_"+text;
	toolbar.appendChild(button);
}
			
function addemo() {
	//insertemo(this,"http://www.web2messenger.com/images/emoticons/smile.gif",":)");
	var x=0,y=0;
	var obj=this;
	while (obj) {
		x+=obj.offsetLeft;
		y+=obj.offsetTop;
		obj=obj.offsetParent;
	}
	showemopanel(x,y+this.offsetHeight,this.editor);
	return false;
}

function insertemo(editor,url,text) {
  editor.control.contentWindow.document.execCommand("InsertImage",false,url+"?text="+text);
}

function createtoolbar(field) {
	var editor=document.getElementById("wysiwyg_"+field);
	var toolbar=document.createElement("div");
	toolbar.className="wysiwyg_toolbar";
	createbutton(toolbar,"B","Bold",editor,field,function() { this.editor.control.contentWindow.document.execCommand("Bold", false, null); return false; });
	createbutton(toolbar,"I","Italic",editor,field,function() { this.editor.control.contentWindow.document.execCommand("Italic", false, null); return false; });
	createbutton(toolbar,"U","Underline",editor,field,function() { this.editor.control.contentWindow.document.execCommand("Underline", false, null); return false; });
	createbutton(toolbar,"S","Strike-trough",editor,field,function() { this.editor.control.contentWindow.document.execCommand("StrikeThrough", false, null); return false; });
	createbutton(toolbar,"E","Emoticons",editor,field,addemo);
	editor.insertBefore(toolbar,editor.control);
}
			
function initwysiwyg(field) {
	var editor=document.getElementById("wysiwyg_"+field);
	if (!editor.control.contentWindow) {
		setTimeout("initwysiwyg(\""+field+"\");", 1);
		return;
	}
	editor.control.contentWindow.document.body.innerHTML=editor.textarea.value;
	editor.control.contentWindow.document.designMode = "on";
}

function createemoticons() {
	/* AG is currently editing this to try and make the emo panel prettier.
		Storing the original in case I fuck up :)
		
	var container=document.createElement("div");
	container.style.position="absolute";
	container.style.display="none";
	container.style.width=(19*10)+"px";
	container.style.textAlign="left";
	container.style.backgroundColor="#FFFFFF";
	container.style.borderColor="#000000";
	container.style.borderStyle="solid";
	container.style.borderWidth="1px";
	var i;
	for (i=0; i<emoticons.length; i+=2) {
		var link=document.createElement("a");
		var emo=document.createElement("img");
		emo.width="19";
		emo.height="19";
		emo.src="http://www.web2messenger.com/images/emoticons/"+emoticons[i+1]+".gif";
		emo.border="0";
		link.href="#";
		link.emo_url=emo.src;
		link.emo_text=emoticons[i];
		link.onclick=function () { hideemopanel(); insertemo(this.parentNode.editor,this.emo_url,this.emo_text); return false; };
		link.appendChild(emo);
		container.appendChild(link);
	}
	
	document.body.appendChild(container);
	emopanel=container;*/
	
	var container=document.createElement("div");
	container.style.position="absolute";
	container.style.display="none";
	container.style.width=((19+8)*10)+"px";
	container.style.borderStyle="none";
	var i;
	var table=document.createElement("table");
	table.cellspacing=6;
	table.cellpadding=0;
	table.id="emot_table";
	var tbody=document.createElement("tbody");
	table.appendChild(tbody);
	var trow=document.createElement("tr");
	tbody.appendChild(trow);
	var cols=1;
	for (i=0; i<emoticons.length; i+=2) {
		var cell=document.createElement("td");
		var link=document.createElement("a");
		link.href="#";
		link.onclick=function() { insertemo(document.getElementById("wysiwyg_messagebox"),this.firstChild.src,this.firstChild.alt); hideemopanel(); return false; };
		var image=document.createElement("img");
		image.src="http://www.web2messenger.com/_system/images/emoticons/"+emoticons[i+1]+".gif";
		image.alt=emoticons[i];
		image.height="19";
		image.width="19";
		image.border="0";
		link.appendChild(image);
		cell.appendChild(link);
		trow.appendChild(cell);
		if (cols==10) { trow=document.createElement("tr"); tbody.appendChild(trow); cols=1; } else { cols++; }
	}
	container.appendChild(table);
	document.body.appendChild(container);
	emopanel=container;
}

function showemopanel(x,y,editor) {
	if (emopanel.style.display!="none")
		return hideemopanel();
	emopanel.style.left=x+"px";
	emopanel.style.top=y+"px";
	emopanel.editor=editor;
	emopanel.style.display="block";
}

function hideemopanel() {
	emopanel.style.display="none";
}