var easyConfig =
{
	//-----------------------------------------------------
	// default style
	border  : "1px solid #000000",
	bgcolor : "#FFFFFF",
	font    : "normal 9pt ³ª´®°íµñ",
	color   : "#000000",
	margin  : "5px",
	width   : "100%",
	height  : "200px",
	//-----------------------------------------------------
	// default path
	editorpath  : "../common",
	commandpath : "../common/editor",
	buttonpath  : "../common/editor",
	//-----------------------------------------------------
	// button style
	over_bordercolor : "#FACF98",
	over_bgcolor     : "#FFFFEA",
	normal_bgcolor   : "#E7E7E7",
	//-----------------------------------------------------
	// buttons
	Btn     : null,
	BtnList : {
		font      : ["±Û²Ã","font.gif"],
		size      : ["±ÛÀÚÅ©±â","size.gif"],
		undo      : ["µÇµ¹¸®±â","undo.gif"],
		redo      : ["Àç½ÇÇà","redo.gif"],
		bold      : ["±½°Ô","bold.gif"],
		italic    : ["±â¿ï¸®±â","italic.gif"],
		strike    : ["Ãë¼Ò¼±","strike.gif"],
		left      : ["¿ÞÂÊ¸ÂÃã","left.gif"],
		center    : ["°¡¿îµ¥¸ÂÃã","center.gif"],
		right     : ["¿À¸¥ÂÊ¸ÂÃã","right.gif"],
		justify   : ["È¥ÇÕÁ¤·Ä","justify.gif"],
		clean     : ["½ºÅ¸ÀÏÁö¿ò","clean.gif"],
		del       : ["¼±ÅÃ»èÁ¦","del.gif"],
		color     : ["±ÛÀÚ»ö","color.gif"],
		hilite    : ["±ÛÀÚ¹è°æ»ö","hilite.gif"],
		link      : ["¸µÅ©»ðÀÔ","link.gif"],
		unlink    : ["¸µÅ©ÇØÁ¦","unlink.gif"],
		ol        : ["¹øÈ£´Þ±â","ol.gif"],
		ul        : ["±âÈ£´Þ±â","ul.gif"],
		outdent   : ["³»¾î¾²±â","outdent.gif"],
		indent    : ["µé¿©¾²±â","indent.gif"],
		hr        : ["¼öÆò¼±»ðÀÔ","hr.gif"],
		all       : ["ÀüÃ¼¼±ÅÃ","selectall.gif"],
		save      : ["¹®¼­ÀúÀå","save.gif"],
		sup       : ["À­Ã·ÀÚ","sup.gif"],
		sub       : ["¾Æ·¡Ã·ÀÚ","sub.gif"],
		underline : ["¹ØÁÙ","underline.gif"],
		about     : ["ÀÌÁö¿¡µðÅÍ","about.gif"],
		//cut       : ["Àß¶ó³»±â","cut.gif"],
		//copy      : ["º¹»ç","copy.gif"],
		//paste     : ["ºÙ¿©³Ö±â","paste.gif"],
		source    : ["¼Ò½ºº¸±â","source.gif"],
		bar       : ["±¸ºÐ¼±","bar.gif"]
		//br ´ÙÀ½ÁÙ·Î ¹öÆ° ³Ñ±è
	},
	//-----------------------------------------------------
	// button template
	BtnTemplate : {
		all    : [
			"save","preview","bar",
			"all","undo","redo","bar",
			"font","size","bar",
			"color","hilite","bar",
			"bold","italic","underline","strike","bar",
			"sup","sub","bar",
			"clean","del","bar",
			"outdent","indent","bar",
			"ol","ul","bar",
			"left","center","right","justify","bar",
			"hr","link","unlink","bar",
			"table","image","bar",
			"source"
			],
		simple : [
			"source","bar",
			"bold","color","hilite"
			],
		semtle : [
			"source","preview","bar",
			"table","image","bar",
			"link","unlink","hr","bar",
			"all","clean","bar",
			"outdent","indent","bar",
			"ol","ul","bar",
			"left","center","right","justify","br",
			"font","size","bar",
			"color","hilite","bar",
			"bold","italic","underline","strike","bar",
			"sup","sub"
			]
	},
	version : "Beta",
	name : "easyEditor"
}

function easyEditor(id)
{
	if(typeof(document.execCommand)=="undefined") { return; }
	easyUtil.init();
	this.cfg = easyConfig;
	this.cfg.preid = easyConfig.name+"_"+id;
	this.cfg.Btn = easyConfig.BtnTemplate["semtle"];
	this.mode = "wysiwyg";
	this.btn = "";
	this.sel = null;
	this.range = null;
	this.sel_html = "";
	this._doc = null;
	this._textarea = document.getElementById(id);
	this._div = document.createElement("div");
	this._divbtn = document.createElement("div");
	this._iframe = document.createElement("iframe");
	this._text = document.createElement("textarea");
	this._div.id = this.cfg.preid+"_div";
	this._divbtn.id = this.cfg.preid+"_divbtn";
	this._iframe.id = this.cfg.preid+"_iframe";
	this._text.id = this.cfg.preid+"_text";
}

easyEditor.prototype.init = function()
{
	this._textarea.style.display = "none";
	// source
	this._text.style.width = this.cfg.width;
	this._text.style.height = this.cfg.height;
	this._text.style.border = "none";
	this._text.style.display ="none";
	this._text.style.font = "9pt ³ª´®°íµñ";
	this._text.style.background = "#EFEFEF url("+this.cfg.buttonpath+"/source_bg.gif) 0px -2px";
	this._text.style.lineHeight = "150%";
	// iframe
	this._iframe.style.width = this.cfg.width;
	this._iframe.style.height = this.cfg.height;
	//this._iframe.scrolling = "yes";
	this._iframe.frameBorder = "no";
	//this._iframe.onmouseover = easyUtil.hideDiv;
	// editarea div
	this._div.style.border = this.cfg.border;
	this._div.style.width = this.cfg.width;
	// buttonarea div
	this._divbtn.style.padding = "2px";
	this._divbtn.style.backgroundColor = this.cfg.normal_bgcolor;
	this._divbtn.style.borderBottom = this.cfg.border;
	//if(easyUtil.isIE) { this._divbtn.style.width = this.cfg.width; }
	// editor insert
	this._textarea.parentNode.insertBefore(this._div, this._textarea);
	this._div.appendChild(this._divbtn);
	this._div.appendChild(this._iframe);
	this._div.appendChild(this._text);
	// button insert
	this.setBtn();
	// iframe document
	this._doc = this._iframe.contentWindow.document;
	this._doc.designMode = "on";
	// default css
	var css = "BODY { margin:"+this.cfg.margin+"; background-color:"+this.cfg.bgcolor+"; }";
	css += "BODY,TABLE,TD { font:"+this.cfg.font+"; color:"+this.cfg.color+"; }";
	this._iframe.css = css;
	this._doc.open();
	this._doc.write('<HTML><HEAD><STYLE type="text/css">'+css+'</STYLE></HEAD><BODY>'+this._textarea.value+'</BODY></HTML>');
	this._doc.close();
	//
	var self = this;
	easyUtil.addEvent(this._doc, "mousedown", easyUtil.hideDiv);
	if(easyUtil.isIE) {
		easyUtil.addEvent(this._doc, "keydown", function(e) {
			var range = self._doc.selection.createRange();
			if(e.keyCode==13 && range.parentElement().tagName!="LI") {
				e.cancelBubble = true;
				e.returnValue = false;
				range.pasteHTML("<BR />");
				range.select();
				return false;
			}
		});
	}
};

easyEditor.prototype.setBtn = function()
{
	//this.cfg.Btn.push("bar","ab"+"out");
	var arr = this.cfg.Btn;
	var len = arr.length;
	var str = order = tmp = "";
	var btn = tag = null;
	var self = this;
	var bgcolor = this.cfg.normal_bgcolor;
	var over_bordercolor = this.cfg.over_bordercolor;
	var over_bgcolor = this.cfg.over_bgcolor;
	for(var i=0; i<len; i++) {
		tmp = "";
		order = easyUtil.trim(arr[i]);
		btn = this.cfg.BtnList[order];

		if(order!="br" && !btn) {
			if(order!="") alert("¾Ë ¼ö ¾ø´Â ¹öÆ°ÀÔ´Ï´Ù ("+order+")");
			continue;
		}
		if(order=="bar") {
			tag = document.createElement("img");
			tag.src = this.cfg.buttonpath+"/"+btn[1];
			tag.width = 2;
			tag.height = 18;
			tag.hspace = 4;
			tag.align ="absmiddle";
			this._divbtn.appendChild(tag);
		}
		else if(order=="br") {
			this._divbtn.appendChild(document.createElement("br"));
		}
		else {
			tag = document.createElement("img");
			tag.id = this.cfg.preid+"_btn_"+order;
			tag.src = this.cfg.buttonpath+"/"+btn[1];
			tag.align = "absmiddle";
			tag.alt = btn[0];
			tag.cmd = order;
			tag.style.cursor = "pointer";
			tag.style.border = "1px solid "+this.cfg.normal_bgcolor;
			tag.onclick = function() { self.cmd(this, this.cmd); };
			tag.onmouseover = function() { this.style.border = "1px solid "+over_bordercolor; this.style.backgroundColor = over_bgcolor; };
			tag.onmouseout = function() { this.style.border = "1px solid "+bgcolor; this.style.backgroundColor = ""; };
			this._divbtn.appendChild(tag);
		}
	}
};

easyEditor.prototype.cmd = function(btn, order, value)
{
	var self = (this) ? this : easyUtil._editor;
	var doc = self._doc;
	if(self.mode=="text" && order!="source") { return; } //alert("'¼Ò½ºº¸±â' ÇØÁ¦ÈÄ »ç¿ëÇØ ÁÖ¼¼¿ä!");
	self.focus();
	easyUtil.hideDiv();
	self.btn = btn;
	try { var create_func = self.cfg.BtnList[order][2]; } catch(e) {}
	if(typeof(create_func)=="function") { order = "create_order"; }
	switch(order) {
		case "create_order": // Ãß°¡¸í·É
			self.setSelection();
			easyUtil._editor = self;
			easyUtil.order = order;
			create_func(self);
			break;
		case "hyperlink":
			var link_text = (self.sel_html) ? self.sel_html : easyUtil._linktxt.value;
			var html = '<A href="'+easyUtil._linktxt.value+'" target="_blank">'+link_text+'</A>';
			self.innerHTML(html);
			break;
		case "color":
		case "hilite":
		case "font":
		case "size":
		case "link":
		case "about":
			var div = null;
			if(order=="color") { order = "forecolor"; easyUtil.tblSet_color(); div = easyUtil._colortbl; }
			else if(order=="hilite"){ order = "hilitecolor"; easyUtil.tblSet_color(); div = easyUtil._colortbl; }
			else if(order=="font") { order = "fontname"; easyUtil.tblSet_font(); div = easyUtil._fonttbl; }
			else if(order=="size") { order = "fontsize"; easyUtil.tblSet_size(); div = easyUtil._sizetbl; }
			else if(order=="about") { easyUtil.tblSet_about(); div = easyUtil._abouttbl; }
			else if(order=="link") { order = "hyperlink"; easyUtil.tblSet_link(); div = easyUtil._linktbl; easyUtil._linktxt.value = "http://"; self.setSelection(); }
			easyUtil._editor = self;
			easyUtil.order = order;
			easyUtil.showDiv(div);
			break;
		case "source":
			if(self.mode=="wysiwyg") {
				self._text.value = self.getHtml();
				self._iframe.style.display = "none";
				self._text.style.display = "";
				btn.onmouseout = null;
				self.mode = "text";
			}
			else if(self.mode=="text") {
				doc.body.innerHTML = self.getHtml();
				self._text.style.display = "none";
				self._iframe.style.display = "";
				bgcolor = self.cfg.normal_bgcolor;
				btn.onmouseout = function() { this.style.border = "1px solid "+bgcolor; this.style.backgroundColor = ""; };
				self.mode = "wysiwyg";
			}
			break;
		default:
			if(order=="strike") { order = "strikethrough"; }
			else if(order=="ol") { order = "insertorderedlist"; }
			else if(order=="ul") { order = "insertunorderedlist"; }
			else if(order=="hr") { order = "inserthorizontalrule"; }
			else if(order=="clean") { order = "removeformat"; }
			else if(order=="save") { order = "saveas"; }
			else if(order=="all") { order = "selectall"; }
			else if(order=="sup") { order = "superscript"; }
			else if(order=="sub") { order = "subscript"; }
			else if(order=="del") { order = "delete"; }
			else if(order=="justify") { order = "justifyfull"; }
			else if(order=="center" || order=="left" || order=="right") { order = "justify"+order; }
			else if(order=="hilitecolor" && easyUtil.isIE) { order = "backcolor"; }
			doc.execCommand(order, false, value);
			break;
	}
};

easyEditor.prototype.focus = function()
{
	if(this.mode=="text") { this._text.focus(); }
	else { this._iframe.contentWindow.focus(); }
}

easyEditor.prototype.getHtml = function()
{
	var html = "";
	var doc = this._doc;
	if(this.mode=="text") { html = this._text.value; }
	else {
		for(var i in doc.links) { if(!doc.links[i].target) { doc.links[i].target = "_blank"; } }
		html = doc.body.innerHTML;
	}
	this._textarea.value = html;
	return html;
}

easyEditor.prototype.setSelection = function()
{
	var _iframe = this._iframe;
	var sel = null, range = null, html = "";
	if(this._doc.selection) {
		sel = this._doc.selection;
		range = sel.createRange();
		html = range.htmlText;
	}
	else if(_iframe.contentWindow.getSelection) {
		sel = _iframe.contentWindow.getSelection();
		if(typeof(sel)!="undefined") { range=sel.getRangeAt(0); }
		else { range = this._doc.createRange(); }
		if(sel.rangeCount>0 && window.XMLSerializer) { html = new XMLSerializer().serializeToString(range.cloneContents()); }
	}
	this.sel = sel;
	this.range = range;
	this.sel_html = html;
}

easyEditor.prototype.innerHTML = function(html)
{
	if(easyUtil.mode=="text") { return; }
	if(this.range.pasteHTML) { this.range.pasteHTML(html); }
	else { this._doc.execCommand("inserthtml", false, html); }
}

var easyUtil =
{
	_editor : null,
	_colortbl : null,
	_fonttbl : null,
	_sizetbl : null,
	_abouttbl : null,
	_linktbl : null,
	_linktxt : null,
	arrtbl : new Array("color","font","size","link","about"),
	order : "",
	is_init : 0,
	isIE : (window.showModalDialog) ? 1 : 0,
	init : function()
	{
		if(easyUtil.is_init) { return; }
		var s = '<STYLE type="text/css">';
		s += "easyWebEditorDiv A { text-decoration:none; color:#000000; }";
		s += "#"+easyConfig.name+"_colortbl A { border:1px solid #F5F5F5; padding:0px 6px; height:9px; font:8px verdana; text-decoration:none; }";
		s += "</STYLE>";
		document.write(s);
		easyUtil.is_init = 1;
	},
	showDiv : function(div)
	{
		var btn = easyUtil._editor.btn;
		div.style.top = easyUtil.curTop(btn)+btn.offsetHeight+"px";
		div.style.left = easyUtil.curLeft(btn)+"px";
		div.style.display = "";
	},
	hideDiv : function()
	{
		if(typeof(easyUtil)!="object") { return; }
		for(var i=0; i<easyUtil.arrtbl.length; i++) {
			try { eval('easyUtil._'+easyUtil.arrtbl[i]+'tbl.style.display="none"'); } catch(e) { }
		}
	},
	curTop : function(el)
	{
		var top = el.offsetTop;
		var parent = el.offsetParent;
		while(parent) { top += parent.offsetTop; parent = parent.offsetParent; }
		return top;
	},
	curLeft : function(el)
	{
		var left = el.offsetLeft;
		var parent = el.offsetParent;
		while(parent) { left += parent.offsetLeft; parent = parent.offsetParent; }
		return left;
	},
	tblSet_about : function()
	{
		if(easyUtil._abouttbl) { return; }
		var s = '<SPAN style="color:#FF6600">ÀÌÁö ¿¡µðÅÍ</SPAN><BR />¹öÀü '+easyConfig.version+" <BR/>";
		s += "µµ¿ò http://cafe.daum.net/easyeditor";
		var d = easyUtil.getDiv(easyConfig.name+"_abouttbl", s);
		d.style.font = "9pt ³ª´®°íµñ";
		d.style.lineHeight = "150%";
		document.body.appendChild(d);
		easyUtil._abouttbl = d;
	},
	tblSet_link : function()
	{
		if(easyUtil._linktbl) { return; }
		var id = easyConfig.name+"_linktxt";
		var s = '<INPUT id="'+id+'" type="text" value="http://" style="width:200px;font:8pt ³ª´®°íµñ;color:gray" /><BR />';
		s += '¿¬°áÇÒ ÁÖ¼Ò(URL)¸¦ ÀÔ·ÂÇÏ¼¼¿ä<BR /><BR />';
		s += '[<A href="È®ÀÎ" onclick="easyUtil._editor.cmd(null,easyUtil.order,\'\');return false;">È®ÀÎ</A>]';
		var div = easyUtil.getDiv(easyConfig.name+"_linktbl", s);
		div.style.padding = "15px";
		div.style.font = "8pt ³ª´®°íµñ";
		document.body.appendChild(div);
		easyUtil._linktbl = div;
		easyUtil._linktxt = document.getElementById(id);
	},
	tblSet_size : function()
	{
		if(easyUtil._sizetbl) { return; }
		var size = new Array(8, 10, 12, 14, 18, 24);
		var s = "";
		for(var i=0; i<size.length; i++) {
			s += '<A href="±Û²Ã¼±ÅÃ" onclick="easyUtil._editor.cmd(null,easyUtil.order,\''+(i+1)+'\');return false;" style="font:'+size[i]+'pt ³ª´®°íµñ;">°¡³ª´Ù¶ó ('+size[i]+')</A><BR />';
		}
		var div = easyUtil.getDiv(easyConfig.name+"_sizetbl", s);
		div.style.padding = "5px";
		document.body.appendChild(div);
		easyUtil._sizetbl = div;
	},
	tblSet_font : function()
	{
		if(easyUtil._fonttbl) { return; }
		var font = new Array("³ª´®°íµñ", "±¼¸²", "µ¸¿ò", "¹ÙÅÁ", "±Ã¼­", "Segoe UI", "Verdana");
		var s = "";
		var pattern = /^[°¡-ÆR]+$/;
		for(var i=0; i<font.length; i++)
		{
			txt = (pattern.test(font[i])) ? "°¡³ª´Ù¶ó¸¶¹Ù»ç" : "abcdefghijkl";
			s += '<A href="±Û²Ã" onclick="easyUtil._editor.cmd(null,easyUtil.order,\''+font[i]+'\');return false;" style="font:10pt '+font[i]+';line-height:170%">'+txt+' ('+font[i]+')</A><BR />';
		}
		var div = easyUtil.getDiv(easyConfig.name+"_fonttbl", s);
		div.style.padding = "5px";
		document.body.appendChild(div);
		easyUtil._fonttbl = div;
	},
	tblSet_color : function()
	{
		if(easyUtil._colortbl) { return; }
		var col= new Array();
		col[0] = new Array("#FFFFFF","#E5E4E4","#D9D8D8","#C0BDBD","#A7A4A4","#8E8A8B","#827E7F","#767173","#5C585A","#000000");
		col[1] = new Array("#FEFCDF","#FEF4C4","#FEED9B","#FEE573","#FFED43","#F6CC0B","#E0B800","#C9A601","#AD8E00","#8C7301");
		col[2] = new Array("#FFDED3","#FFC4B0","#FF9D7D","#FF7A4E","#FF6600","#E95D00","#D15502","#BA4B01","#A44201","#8D3901");
		col[3] = new Array("#FFD2D0","#FFBAB7","#FE9A95","#FF7A73","#FF483F","#FE2419","#F10B00","#D40A00","#940000","#6D201B");
		col[4] = new Array("#FFDAED","#FFB7DC","#FFA1D1","#FF84C3","#FF57AC","#FD1289","#EC0078","#D6006D","#BB005F","#9B014F");
		col[5] = new Array("#FCD6FE","#FBBCFF","#F9A1FE","#F784FE","#F564FE","#F546FF","#F328FF","#D801E5","#C001CB","#8F0197");
		col[6] = new Array("#E2F0FE","#C7E2FE","#ADD5FE","#92C7FE","#6EB5FF","#48A2FF","#2690FE","#0162F4","#013ADD","#0021B0");
		col[7] = new Array("#D3FDFF","#ACFAFD","#7CFAFF","#4AF7FE","#1DE6FE","#01DEFF","#00CDEC","#01B6DE","#00A0C2","#0084A0");
		col[8] = new Array("#EDFFCF","#DFFEAA","#D1FD88","#BEFA5A","#A8F32A","#8FD80A","#79C101","#3FA701","#307F00","#156200");
		col[9] = new Array("#D4C89F","#DAAD88","#C49578","#C2877E","#AC8295","#C0A5C4","#969AC2","#92B7D7","#80ADAF","#9CA53B");
		var s = "";
		for(var i=0; i<10; i++) {
			for(var j=0; j<10; j++) {
				color = col[i][j];
				s += '<A href="" onclick="easyUtil._editor.cmd(null,easyUtil.order,\''+color+'\');return false;" style="background-color:'+color+';">&nbsp;</A>';
			}
			s += '<BR />';
		}
		var div = easyUtil.getDiv(easyConfig.name+"_colortbl", s);
		document.body.appendChild(div);
		easyUtil._colortbl = div;
	},
	getDiv : function(id, html)
	{
		var div = document.createElement("div");
		div.id = id;
		div.className = "easyWebEditorDiv";
		div.style.position = "absolute";
		div.style.backgroundColor = "#F5F5F5";
		div.style.display = "none";
		div.style.border = "1px solid #CCCCCC";
		div.style.padding = "5px";
		div.innerHTML = html;
		return div;
	},
	addEvent : function(object, type, listener)
	{
		if(object.addEventListener) { object.addEventListener(type, listener, false); }
		else if(object.attachEvent) { object.attachEvent("on"+type, listener); }
	},
	trim : function(s)
	{
		return s.replace(/^\s+|\s+$/g, '');
	}
};

easyConfig.BtnList.table = ["Å×ÀÌºí»ðÀÔ","table.gif",function(self)
{
	//window.open(easyConfig.commandpath+"/table.htm","table","width=400,height=220,status=1");
	SLB(easyConfig.commandpath+'/table.htm','iframe',370,260,false,false);
}];
easyConfig.BtnList.preview = ["¹Ì¸®º¸±â","preview.gif",function(self)
{
	var w = window.open("","preview","width=640,height=600,status=1,scrollbars=1,resizable=1");
	w.document.open();
	w.document.write('<STYLE type="text/css">'+self._iframe.css+'</STYLE>'+self.getHtml());
	w.document.close();
}];
easyConfig.BtnList.image = ["ÀÌ¹ÌÁö»ðÀÔ","image.gif",function(self)
{
	var order = "image";
	var txt_id = easyConfig.name+"_imagetxt";
	var div_id = easyConfig.name+"_imagetbl";
	var div = null;
	if(self.btn) {
		if(!document.getElementById(div_id)) {
			var s = '<INPUT id="'+txt_id+'" type="text" value="http://" style="width:200px;font:8pt ³ª´®°íµñ;color:gray" /><BR />';
			s += 'ÀÌ¹ÌÁö ÁÖ¼Ò(URL)¸¦ ÀÔ·ÂÇÏ¼¼¿ä<BR /><BR />';
			s += '[<A href="È®ÀÎ" onclick="easyUtil._editor.cmd(null,\''+order+'\',\'\');return false;">È®ÀÎ</A>]';
			div = easyUtil.getDiv(div_id, s);
			div.style.padding = "15px";
			div.style.font = "8pt ³ª´®°íµñ";
			document.body.appendChild(div);
			easyUtil._imagetbl = div;
			easyUtil.arrtbl.push(order);
		}
		else
		{
			document.getElementById(txt_id).value = "http://";
			div = document.getElementById(div_id);
		}
		easyUtil.showDiv(div);
	}
	else // ¸í·É½ÇÇà(ÀÌ¹ÌÁö»ðÀÔ)
	{
		var html = '<IMG src="'+document.getElementById(txt_id).value+'" border="0" />';
		self.innerHTML(html);
	}
}];