HTMLArea.loadPlugin("CSS");

function FIXEDBugOnSubmit() {
	editor._textArea.value = editor.getHTML();
	var a = this.__msh_prevOnSubmit;
	// call previous submit methods if they were there.
	if (typeof a != "undefined") {
		for (var i = a.length; --i >= 0;) {
			a[i]();
		}
	}
}

function emo_tkticon1() {
	editor._popupDialog("insert_emoticons_tkticon1.html", function(url) { 
		if (!url) { 
			return false; 
		} 
		editor._doc.execCommand("insertimage", false, url); 
	}, null); 
}

function emo_tkticon2() {
	editor._popupDialog("insert_emoticons_tkticon2.html", function(url) { 
		if (!url) { 
			return false; 
		} 
		editor._doc.execCommand("insertimage", false, url); 
	}, null); 
}

function emo_tkticon3() {
	editor._popupDialog("insert_emoticons_tkticon3.html", function(url) { 
		if (!url) { 
			return false; 
		} 
		editor._doc.execCommand("insertimage", false, url); 
	}, null); 
}

function emo_tkticon4() {
	editor._popupDialog("insert_emoticons_tkticon4.html", function(url) { 
		if (!url) { 
			return false; 
		} 
		editor._doc.execCommand("insertimage", false, url); 
	}, null); 
}

function insertHTML() {
	var html = prompt("Enter some HTML code here");
	if (html) {
		editor.insertHTML(html);
	}
}

function initEditor() {
  	editor = new HTMLArea(textarea);
	var cfg = editor.config;
	function clickHandler(editor, buttonId) {
		switch (buttonId) {
			case "emo_tkticon1":
				emo_tkticon1();
				break;
			case "emo_tkticon2":
				emo_tkticon2();
				break;
			case "emo_tkticon3":
				emo_tkticon3();
				break;
			case "emo_tkticon4":
				emo_tkticon4();
				break;
			case "text_glow":
				editor.surroundHTML("<span style=\"FILTER: Shadow(Color=#00cc66, Direction=45); WIDTH: 50px\">", "</span>");
				break;
			case "text_shadow":
				editor.surroundHTML("<span style=\"FILTER: Glow(Color=#ff0000, Strength=8); WIDTH: 50px\">", "</span>");
				break;
			case "embed_flash":
				insertHTML();
				break;
		}
	};
	cfg.registerButton("emo_tkticon1", "เลือกอารมณ์เซ็ต 1", "htmlarea/images/ed_smiley.gif", false, clickHandler);
	cfg.registerButton("emo_tkticon2", "เลือกอารมณ์เซ็ต 2", "htmlarea/images/ed_smiley.gif", false, clickHandler);
	cfg.registerButton("emo_tkticon3", "เลือกอารมณ์เซ็ต 3", "htmlarea/images/ed_smiley.gif", false, clickHandler);
	cfg.registerButton("emo_tkticon4", "เลือกอารมณ์เซ็ต 4", "htmlarea/images/ed_smiley.gif", false, clickHandler);
	cfg.registerButton("text_glow", "Text Glow", "htmlarea/images/ed_glow.gif", false, clickHandler);
	cfg.registerButton("text_shadow", "Text Glow", "htmlarea/images/ed_shadow.gif", false, clickHandler);
	cfg.registerButton("embed_flash", "Flash", "htmlarea/images/application_flash.gif", false, clickHandler);
	cfg.toolbar = [
		[ "fontname", "space",
		  "fontsize", "space",
		  "formatblock", "space",
		  "bold", "italic", "underline", "strikethrough", "text_glow", "text_shadow", "separator",
		  "subscript", "superscript" ], 
		[ "justifyleft", "justifycenter", "justifyright", "justifyfull", "separator",
		  "lefttoright", "righttoleft", "separator",
		  "orderedlist", "unorderedlist", "outdent", "indent", "separator",
		  "forecolor", "hilitecolor", "separator",
		  "inserthorizontalrule", "createlink", "insertimage", "separator", "emo_tkticon1", "emo_tkticon2", "emo_tkticon3", "emo_tkticon4", "embed_flash", "htmlmode" ]
	];
	/*
	* add the new button to the toolbar
	*/
 	// cfg.toolbar.push([]);
	editor.config.pageStyle = "@import url(style/subSilver/textarea.css);";
  	editor.generate();
}

function insert_emoticons(emo) {
	var html = '<img src="' + emo + '" />';
	if (html) {
		editor._iframe.contentWindow.focus();
		editor.insertHTML(html);
	}
}