/**
 * Å¬·¡½º¸í : WebEditor()
 * ±â    ´É : À¥ ¿¡µðÅÍ (IE Àü¿ë)
 * ÀÛ ¼º ÀÏ : 2008.08.08, Á¤¿ø±¤ (wkwang@yedong.com)
 *
 */

WebEditor = function () {
	this.domain = "http://" + location.host;
	this.topDir = "";
	this.uplDir = "";
	this.visualEditor = true;
	this.buttons = [["BOLD", "ÁøÇÏ°Ô", "/common/editor/img/bold.gif", true]
	               ,["ITALIC", "±â¿ïÀÓ", "/common/editor/img/italic.gif", true]
	               ,["UNDERLINE", "¹ØÁÙ", "/common/editor/img/underline.gif", true]
	               ,["STRIKE", "Ãë¼Ò¼±", "/common/editor/img/strike.gif", true]
	               ,["FCOLOR.SELECT", "±Û²Ã »ö»ó", "/common/editor/img/fore_color.gif", true]
	               ,["BCOLOR.SELECT", "±Û²Ã ¹è°æ»ö", "/common/editor/img/back_color.gif", true]
	               ,["LEFT", "ÁÂÃø Á¤·Ä", "/common/editor/img/left.gif", false]
	               ,["CENTER", "°¡¿îµ¥ Á¤·Ä", "/common/editor/img/center.gif", false]
	               ,["RIGHT", "¿ìÃø Á¤·Ä", "/common/editor/img/right.gif", false]
	               ,["IMAGE.INSERT", "±×¸²»ðÀÔ", "/common/editor/img/image.gif", false]
	               ,["LINK", "ÇÏÀÌÆÛ¸µÅ©", "/common/editor/img/link.gif", false]];

	// ÆùÆ® ¼±ÅÃ
	this.setFont = function (obj) {
		var doc;
		
		doc = "<div class='box' style='float:left; height:20px; padding:1 0 0 1; margin:0 0 0 1;'>"
		    + "<select onchange='editor.visualTag(\"" + obj + "\", \"FACE\", this.options[selectedIndex].value);'>"
		    +  "<option value=''>- ±Û²Ã</option>"
		    +  "<option value='±¼¸²'>±¼¸²</option>"
		    +  "<option value='µ¸¿ò'>µ¸¿ò</option>"
		    +  "<option value='¹ÙÅÁ'>¹ÙÅÁ</option>"
		    +  "<option value='±Ã¼­'>±Ã¼­</option>"
		    +  "</select>"
		    +  "</div>";

		return doc;
	}

	// Å©±â ¼±ÅÃ
	this.setSize = function (obj) {
		var doc;
		
		doc = "<div class='box' style='float:left; height:20px; padding:1 0 0 1; margin:0 0 0 1;'>"
		    +  "<select onchange='editor.visualTag(\"" + obj + "\", \"SIZE\", this.options[selectedIndex].value);'>"
		    +  "<option value=''>- Å©±â</option>"
		    +  "<option value='1'>1</option>"
		    +  "<option value='2'>2</option>"
		    +  "<option value='3'>3</option>"
		    +  "<option value='4'>4</option>"
		    +  "<option value='5'>5</option>"
		    +  "</select>"
		    +  "</div>";

		return doc;
	}

	// ¹öÆ°
	this.setButton = function (obj, minimum) {
		var doc = "<div style='float:left;'>";

		for (var i = 0; i < this.buttons.length; i++) {
			if (minimum == true && this.buttons[i][3] == false)  {
				continue;
			}

			doc += "<img src='" + this.topDir + this.buttons[i][2] + "' onclick='editor.visualTag(\"" + obj + "\", \"" + this.buttons[i][0] + "\");' alt='" + this.buttons[i][1] + "' align='absmiddle' class='box' style='cursor:pointer; margin:0 0 0 1;'>";
		}

		doc += "</div>";

		return doc;
	}

	// ÅÂ±× Àû¿ë
	this.visualTag = function (obj, sTag, opt) {
		var doc = eval("_editor_div_" + obj + "_").document;
		var rng = doc.selection.createRange();
		var tag = "";

		eval("_editor_div_" + obj + "_").focus();

		switch(sTag) {
			case "FACE" :
				doc.execCommand("fontname", false, opt);
				break;
			case "FCOLOR" :
				doc.execCommand("forecolor", false, opt);
				break;
			case "BCOLOR" :
				doc.execCommand("backcolor", false, opt);
				break;
			case "SIZE" :
				doc.execCommand("fontsize", false, opt);
				break;
			case "BOLD" :
				doc.execCommand("Bold", false, null);
				break;
			case "ITALIC" :
				doc.execCommand("Italic", false, null);
				break;
			case "UNDERLINE" :
				doc.execCommand("Underline", false, null);
				break;
			case "STRIKE" :
				doc.execCommand("StrikeThrough", false, null);
				break;
			case "LEFT" :
				doc.execCommand("JustifyLeft", false, null);
				break;
			case "CENTER" :
				doc.execCommand("JustifyCenter", false, null);
				break;
			case "RIGHT" :
				doc.execCommand("JustifyRight", false, null);
				break;
			case "IMAGE" :
				doc.execCommand("InsertImage", false, opt);
				break;
			case "LINK" :
				doc.execCommand("CreateLink");
				break;
			case "FCOLOR.SELECT" :
				window.showModalDialog(this.topDir + "/common/editor/doc/color.html?" + obj + "&FCOLOR", window, "dialogWidth:210px; dialogHeight:150px; resizable:no; help:no; status:no; scroll:no;");
				break;
			case "BCOLOR.SELECT" :
				window.showModalDialog(this.topDir + "/common/editor/doc/color.html?" + obj + "&BCOLOR", window, "dialogWidth:210px; dialogHeight:150px; resizable:no; help:no; status:no; scroll:no;");
				break;
			case "IMAGE.INSERT" :
				window.showModalDialog(this.topDir + "/common/editor/doc/image.html?" + obj, window, "dialogWidth:400px; dialogHeight:340px; resizable:no; help:no; status:no; scroll:no;");
				break;
		}
	}

	// ÆíÁý <-> ¿¡µðÅÍ
	this.visualEditorMode = function (obj) {
		var obj1 = document.getElementById("_editor_div_" + obj + "_");
		var obj2 = document.getElementById("_editor_txt_" + obj + "_");
		var obj3 = document.getElementById("_editor_btn_" + obj + "_");

		if (this.visualEditor) {
			this.visualEditor = !this.visualEditor;

			obj1.style.display = "none";
			obj2.style.display = "";
			obj3.style.display = "none";

			obj2.value = obj1.innerHTML;
			obj2.focus();
		} else {
			this.visualEditor = !this.visualEditor;

			obj1.style.display = "";
			obj2.style.display = "none";
			obj3.style.display = "";

			obj1.innerHTML = obj2.value;
			obj1.focus();
		}

	}

	// ¿£ÅÍÅ° Ã³¸®
	this.captureEnterKey = function () {
		if (event.keyCode == 13) {
			if (event.shiftKey == false) {
				var sel = document.selection.createRange();
	 
				sel.pasteHTML('<br>');
				event.cancelBubble = true;
				event.returnValue = false;
				sel.select();
				sel.moveEnd("character", 1);
				sel.moveStart("character", 1);
				sel.collapse(false);
				return false;
			} else {
				return event.keyCode = 13;
			}
		}
	}

	// ¿¡µðÅÍ
	this.setEditor = function (obj, height, minimum) {
		var doc;
		var isIE = window.navigator.appName == "Microsoft Internet Explorer" ? true : false;
		var font = isIE == true ? this.setFont(obj) : "";
		var size = isIE == true ? this.setSize(obj) : "";
		var btns = isIE == true ? this.setButton(obj, minimum) : "";

		doc = "<div style='clear:both; width:100%;; height:24px;'>\n"
		    +  "	<div style='float:left; padding:5 0 0 0;'><input type='checkbox' id='_editor_vse_" + obj + "_' style='width:14px; height:16px; border:0;' onclick='editor.visualEditorMode(\"" + obj + "\");'><label for='_editor_vse_" + obj + "_'>HTMLÆíÁý</label></div>\n"
		    +  "	<div style='float:right;'>\n"
		    +  "		<div id='_editor_btn_" + obj + "_' style='clear:both; display:;'>" + font + size + btns + "</div>\n"
		    +  "	</div>\n"
		    +  "</div>\n"
		    +  "<div id='_editor_div_" + obj + "_' class='box' style='clear:both; width:100%;' onkeypress='editor.captureEnterKey();' style='text-align:left; height:" + height + "px; overflow:auto;' contentEditable></div>\n"
		    +  "<textarea id='_editor_txt_" + obj + "_' class='box' style='width:100%; height:" + height + "px; display:none;'></textarea>\n";

		document.write(doc);
		document.getElementById("_editor_div_" + obj + "_").innerHTML = document.getElementById(obj).value;

		if (isIE == false) {
			document.getElementById("_editor_vse_" + obj + "_").checked = true;
			document.getElementById("_editor_vse_" + obj + "_").disabled = true;

			this.visualEditorMode(obj);

			try {
				document.getElementById("ableHtml").value = "N";
				document.getElementById("ableBr").value = "Y";
				document.getElementById("ableLink").value = "Y";
			} catch (e) {
			}
		}
	}
}

var editor = new WebEditor();

