/**
 * Copyright (c) www.goosol.net
 * Goosol Media
 */

// Basic Setting
var editorConf = {
    bMSIE   : navigator.userAgent.toLowerCase().indexOf("msie")  != -1,
    bGECKO  : navigator.userAgent.toLowerCase().indexOf('gecko') != -1,
    bOPERA  : navigator.userAgent.toLowerCase().indexOf('opera') != -1,
    bSAFARI : (navigator.userAgent.toLowerCase().indexOf('safari') != -1) || (navigator.userAgent.toLowerCase().indexOf('applewebkit') != -1),

	border	: "1px solid #cdcdcd",
	bgcolor	: "#fff",
	font	: "normal 12px/1.4 굴림",
	color	: "#000",
	margin	: "0",
	padding	: "5px",
	width	: "100%",
	height	: "200px",
	imgpath : "./img/editor/easy",
	filepath : "./img/editor/file",
	divbtn_bgcolor : "#fff",
	align	: "left",

	version : "beta_0.1",
	name	: "gEdit"
}

function gEdit(id)
{
	this.conf		= editorConf;
	this.conf.preid	= editorConf.name+"_"+id;
	this._textarea	= document.getElementById(id);
	this._doc		= null;
	this._div		= document.createElement("div");	//전체 div
	this._divbtn1	= document.createElement("div");	//기본 적용셋
	this._divbtn2	= document.createElement("div");	//팝업 적용셋
	this._iframe	= document.createElement("iframe");	//iframe
	this._iframe.setAttribute("id","editorForm");
//	this._iframe.id	= this.conf.preid+"_iframe";
	this._div.id	= this.conf.preid+"_div";
	this._divbtn1.id	= this.conf.preid+"_divbtn";
}
gEdit.prototype.run = function()
{
	this._textarea.style.display="none";
	//전체 div
	this._div.style.border = this.conf.border;
	this._div.style.width = this.conf.width;
	this._div.style.height = this.conf.height;
	this._div.style.textAlign = this.conf.align;
	//iframe
	this._iframe.style.width = this.conf.width;
	this._iframe.style.height= this.conf.height;
	this._iframe.scrolling = "auto";
	this._iframe.frameBorder = "no";
	//버튼 div
	this._divbtn1.style.padding	="2px 0";
	this._divbtn1.style.backgroundColor = this.conf.divbtn_bgcolor;
	this._divbtn1.style.borderBottom = this.conf.border;

	// 본문내용가져오기
	content ='';
	try{
		contentDiv = document.getElementById(this._textarea);
		contentDiv.style.display = 'none';
		content = contentDiv.value;
	}catch(e){}

	//삽입
	this._textarea.parentNode.insertBefore(this._div, this._textarea);
	this._div.appendChild(this._divbtn1);
	this._div.appendChild(this._divbtn2);
	this._div.appendChild(this._iframe);

	//버튼요소 삽입
	this.setBtn();

	this._doc = this._iframe.contentWindow.document;
	//기본 css설정
	var css  = "p{ margin:0px; }";
		css += "body,table,td{margin:"+this.conf.margin+";padding:"+this.conf.padding+";background:"+this.conf.bgcolor+";font:"+this.conf.font+";color:"+this.conf.color+";}";
	var body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">'+css+'</style></head><body>'+this._textarea.value+'</body></html>';

	this._doc.open();
	this._doc.write(body);
	this._doc.close();

	// iframe 을 edit 가능하게 한다.
	if(this.conf.bMSIE) this._doc.body.contentEditable = true; //IE 에서는 contentEditable 로 edit
	else this._doc.designMode="on"; // 그 외의 브라우저에선 iframe 으로

	// 이벤트 처리(내용 상호복제 부분) - 모든 브라우저가 지원하는 attachEvent
	if(document.addEventListener)
	{
		this._doc.addEventListener("mousedown", copyTohtml, false);
		this._doc.addEventListener("blur", copyTohtml, false);
		this._textarea.addEventListener("mousedown", copyToeditor, false);
		this._textarea.addEventListener("blur", copyToeditor, false);
	}
	else if(document.attachEvent)
	{
		this._doc.attachEvent("onmousedown", copyTohtml, false);
		document.getElementById("editorForm").attachEvent("onblur", copyTohtml, false);
		this._textarea.attachEvent("onmousedown", copyToeditor, false);
		this._textarea.attachEvent("onblur", copyToeditor, false);
	}

	var self = this;
}
gEdit.prototype.setBtn = function()
{
	//도구테이블생성
	var ctrlObj  = "<a href=\"javascript:void(0);\" onclick=\"doEdit('bold', false, null); return false;\"><img src=\""+this.conf.imgpath+"/bold.gif\" alt=\"굵게\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('italic', false, null); return false;\"><img src=\""+this.conf.imgpath+"/italic.gif\" alt=\"기울이기\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('underline', false, null); return false;\"><img src=\""+this.conf.imgpath+"/underline.gif\" alt=\"밑줄\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('strikethrough', false, null); return false;\"><img src=\""+this.conf.imgpath+"/strike.gif\" alt=\"취소선\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('cut', false, null); return false;\"><img src=\""+this.conf.imgpath+"/cut.gif\" alt=\"잘라내기\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('copy', false, null); return false;\"><img src=\""+this.conf.imgpath+"/copy.gif\" alt=\"복사\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('paste', false, null); return false;\"><img src=\""+this.conf.imgpath+"/paste.gif\" alt=\"붙여넣기\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('insertorderedlist', false, null); return false;\"><img src=\""+this.conf.imgpath+"/ul1.gif\" alt=\"순서리스트\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('insertunorderedlist', false, null); return false;\"><img src=\""+this.conf.imgpath+"/ul2.gif\" alt=\"비순서리스트\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('justifyfull', false, null); return false;\"><img src=\""+this.conf.imgpath+"/justify.gif\" alt=\"양쪽정렬\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('justifyleft', false, null); return false;\"><img src=\""+this.conf.imgpath+"/left.gif\" alt=\"왼쪽정렬\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('justifycenter', false, null); return false;\"><img src=\""+this.conf.imgpath+"/center.gif\" alt=\"가운데정렬\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('justifyright', false, null); return false;\"><img src=\""+this.conf.imgpath+"/right.gif\" alt=\"오른쪽정렬\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('indent', false, null); return false;\"><img src=\""+this.conf.imgpath+"/indent.gif\" alt=\"들여쓰기\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('outdent', false, null); return false;\"><img src=\""+this.conf.imgpath+"/outdent.gif\" alt=\"내어쓰기\" /></a>&nbsp;";
//		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('removeformat', false, null);\"><img src=\""+this.conf.imgpath+"/clean.gif\" alt=\"속성삭제\" /></a>&nbsp;";
//		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('delete', false, null);\"><img src=\""+this.conf.imgpath+"/del.gif\" alt=\"삭제\" /></a>&nbsp;";
//		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('selectall', false, null);\"><img src=\""+this.conf.imgpath+"/selectall.gif\" alt=\"전체선택\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('superscript', false, null);\"><img src=\""+this.conf.imgpath+"/sup.gif\" alt=\"위첨자\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('subscript', false, null);\"><img src=\""+this.conf.imgpath+"/sub.gif\" alt=\"아래첨자\" /></a>&nbsp;";
		ctrlObj += "<a href=\"javascript:void(0);\" onclick=\"doEdit('inserthorizontalrule', false, null);\"><img src=\""+this.conf.imgpath+"/hr.gif\" alt=\"수평선\" /></a>";

	var tableObj = document.createElement("table");
	if(document.all)
	{
		var trObj = tableObj.insertRow();
		var tdObj = trObj.insertCell(); 
	}
	else
	{
		var trObj = document.createElement('tr');
		var tdObj = document.createElement('td');
		tableObj.setAttribute("cellspacing","0");
		tableObj.setAttribute("cellspacing","0");
		tableObj.setAttribute("border","0");
	}

	if(!document.all)
	{
		trObj.appendChild(tdObj);
		tableObj.appendChild(trObj);
	}
	this._divbtn1.parentNode.insertBefore(tableObj,this._divbtn1);
	tdObj.innerHTML = ctrlObj;
}

function copyTohtml()
{
	document.getElementById("content").value = document.getElementById("editorForm").contentWindow.document.body.innerHTML;
}

function copyToeditor()
{
	document.getElementById("editorForm").contentWindow.document.body.innerHTML = document.getElementById("content").value;
}

function doEdit(excute, bool, value)
{
	document.getElementById("editorForm").contentWindow.focus();
	document.getElementById("editorForm").contentWindow.document.execCommand(excute, bool, value);
	return false;
}

