var editor;
var editMode = "editor";
var uContents;
var uEditor;

function Editor() {
	this.iframe;
	this.doc;
	this.selection;
	this.range;

	this.colorset = "white";

	this.init			= initEditor;
	this.isIE			= isBrowserIE;
	this.focusEditor	= focusEditor;
	this.createRange	= createRange;
	this.getFrame		= getFrame;
	this.getContent		= getContent;
	this.getContentNoCache = getContentNoCache;
	this.getSelection	= getSelection;

	this.setContent		= setContent;
	this.setColorset	= setColorset;
	this.setEditor		= setEditor;
}

function initEditor(width, height) {
	uContents= document.getElementById("contents");
	uEditor  = document.getElementById("EDITOR"); 

	this.frame = this.getFrame(width, height);    
	this.doc = this.frame.document;
	this.doc.designMode = "on";

	this.doc.open("text/html");
	this.doc.write(this.setEditor());
	this.doc.close();

	//this.doc.domain = "<?=$config[domain];?>";
	//document.domain = "<?=$config[domain];?>";

	this.doc.body.style.fontSize = "9pt";
	//this.doc.body.style.fontFamily = "verdana,arial,sans-serif,굴림,돋움";

	this.focusEditor();
}

function getFrame(width, height) {
	if (this.isIE()) {
		//document.getElementById("contents").style.display = "none";
		this.frame = EDITOR;
	} else {
		var textarea = document.getElementById("contents");
		var htmlarea = document.createElement("div");
		textarea.parentNode.insertBefore(htmlarea, textarea);
		textarea.style.display = "none";
		document.getElementById("EDITOR").style.display = "none";

		iframe = document.createElement("iframe");
		htmlarea.appendChild(iframe);

		iframe.style.width = width + "px";
		iframe.style.height = height + "px";
		iframe.className = document.getElementById('EDITOR').className;

		this.frame = iframe.contentWindow;
	}

	return this.frame;
}

function setEditor() {
	var layout	= "";
	var style		= "";
	var body		= "";
	
	/* 2008.01.24 modified */ 
	style = "<link rel='stylesheet' type='text/css' href='../config/style_editor.css' >";
	body = "<body ></body>";

	layout = "<html><head>" + style + "<script>function resizeImage(num){};function popview(){};</script></head>" + body + "</html>";

	return layout;
}

function setColorset(colorset) {
	this.colorset = colorset;
}

function isBrowserIE() {
	var ua = navigator.userAgent.toLowerCase();

	return (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1);
}

function focusEditor() {
	this.frame.focus();
}

function change_mode() {
	if (editMode == "source") {
		if (document.all) { // IE
			syncShow();
			uContents.style.display = "none";
			uEditor.style.display = "inline";
		} else { // Firefox, Netscape, ..
			html = uEditor.contentWindow.document.body.ownerDocument.createRange();
			html.selectNodeContents(uEditor.contentWindow.document.body);
			uEditor.contentWindow.document.body.innerHTML = html.toString();
			uEditor.style.backgroundColor = "#ffffff";
		}
		editMode = "editor";
		document.input_form.edit_mode.value = "editor";
	} else {
		if (document.all) { 
			syncContents();
			uContents.style.display = "inline";
			uEditor.style.display = "none";
		} else { 
			html = document.createTextNode(uEditor.contentWindow.document.body.innerHTML);
			uEditor.contentWindow.document.body.innerHTML = "";
			uEditor.contentWindow.document.body.appendChild(html);
			uEditor.style.backgroundColor = "#eeeeee";
		}
		uContents.focus(); 
		editMode = "source";
		document.input_form.edit_mode.value = "source";
	}
}

function syncContents() {
	//uContents.value = uEditor.contentWindow.document.body.innerHTML;
	uContents.value = editor.getContent(); 
}

function syncShow() {
	//uEditor.contentWindow.document.body.innerHTML = uContents.value;
	editor.setContent(uContents.value);
}

function getContentNoCache() {
	return this.doc.body.innerHTML;
}

function getContent() {
	//if (this.isIE()) {
	//	this.doc.body.createTextRange().execCommand("copy");
	//}

	return this.doc.body.innerHTML;
}

function setContent(html) {
	this.doc.body.innerHTML = html;
}

function getSelection() {
	if (this.isIE()) 
		return this.doc.selection;
	else
		return this.frame.getSelection();
}

function createRange(sel) {
	if (this.isIE()) {
		return sel.createRange();
	} else {
		this.focusEditor();
		if (typeof sel != "undefined") {
			try {
				return sel.getRangeAt(0);
			} catch(e) {
				return this.doc.createRange();
			}
		} else {
			return this.doc.createRange();
		}
	}
}

// reply, modify contents setting
function decodeContent(str) {
	return str.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&amp;/gi,"&");
}

// ------------------------------------------------------------------

// edit wyswyg html 
function editContent(act, id, url, width, height) {
	// 주석처리 해도 적용됨??? 
	var selection = editor.getSelection();
	var range = editor.createRange(selection);
	editor.range = range;

	switch (act) {
		case "Bold" :
		case "Italic" :
		case "Underline" :
		case "StrikeThrough" :
		case "JustifyLeft" :
		case "JustifyCenter" :
		case "JustifyRight" :
			editor.doc.execCommand(act, false, null);
		break;

		case "fontname" :
		case "fontsize" :
		case "fontcolor" :
		case "bgcolor" :
			display_layer(id, 1); 
		break;
		case "link" :
			if (editMode == "source") {
			} else {
				showLinkBox();
			}
		break;
		case "image" :
			display_layer(id, 1); 
			//showImageBox();
		break;
		case "multi_video" :
		case "multi_flash" :
		case "multi_audio" :
			if (editMode == "source") {
			} else {
				insertMultimediaLink(act, url, width, height);
			}
		break;
	}
}

function editFontName(id, str) {
	if (editor.range != null && editor.isIE()) {
		editor.range.select();
	}
	editor.doc.execCommand("FontName", null, str);
	display_layer(id); 
}

function editFontSize(id, str) {
	if (editor.range != null && editor.isIE()) {
		editor.range.select();
	}
	editor.doc.execCommand("FontSize", null, str);
	display_layer(id); 
}

function editFontColor(id, color) {
	if (editor.range != null && editor.isIE()) {
		editor.range.select();
	}
	editor.doc.execCommand("ForeColor", false, color);
	display_layer(id); 
}

function editBGColor(id, bgcolor, fontcolor) {
	if (editor.range != null && editor.isIE()) {
		editor.range.select();
	}
	editor.doc.execCommand("BackColor", null, bgcolor);
	if (fontcolor != "") editor.doc.execCommand("ForeColor", null, fontcolor);
	display_layer(id); 
}

function showLinkBox() {
	editor.focusEditor();
	editor.doc.execCommand("CreateLink", true, null);
}

function showImageBox() {
	//editor.focusEditor();
	//editor.doc.execCommand("InsertImage", true, null);

	var image_url = document.getElementById("image_url").value;
	var type = "image"; 

	if (false == checkLinkUrl(image_url, type)) {
		return ;
	}

	sContents = "<p><img src='"+image_url+"' border='0'></p>";

	var cWin = uEditor.contentWindow;
	cWin.focus();
	if (document.all) {
		var eEdit = cWin.document.selection.createRange();
		eEdit.pasteHTML(sContents);
	} else {
		p = cWin.document.createElement("p");
		image = cWin.document.createElement("img");
		image.setAttribute("src", image_url);
		image.setAttribute("border", 0);
		p.appendChild(image);
		insertNodeAtSelection(cWin, p);
	}

}

function insertMultimediaLink(type, url, width, height) {

	if (false == checkLinkUrl(url, type)) {
		return ;
	}

	//editor.focusEditor();

	if (type == 'multi_video') {
		sContents = "<embed type='application/x-mplayer2' pluginspage='http://www.microsoft.com/windows/mediaplayer/download/default.asp' filename='{URL}' width='{WIDTH}', height='{HEIGHT}', autostart='false', loop='false', showcontrols='true', showstatusbar='true', showpositioncontrols='false' ></embed>"; 
	} else if (type == 'multi_flash') {
		var sContents = "";
		sContents = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
		sContents +='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ';
		sContents +='WIDTH="{WIDTH}" HEIGHT="{HEIGHT}" >';
		sContents +='<param name="movie" value="{URL}">';
		sContents +='<param name="quality" value="high">';
		sContents +='<param name="wmode" value="transparent">';
		//sContents +='<param name="wmode" value="transparent">';
		sContents +='<embed src="{URL}" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
		sContents +='type="application/x-shockwave-flash" WIDTH="{WIDTH}" HEIGHT="{HEIGHT}"></embed></object>';
		
		sContents = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' WIDTH='{WIDTH}' HEIGHT='{HEIGHT}' ALIGN=''><PARAM NAME=movie VALUE='{URL}'><PARAM NAME=quality VALUE=high><EMBED src='{URL}' quality=high WIDTH='{WIDTH}' HEIGHT='{HEIGHT}' ALIGN='' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'></EMBED></object>";
	} else if (type == 'multi_audio') {
		sContents = "<embed style='width:{WIDTH}px; height:{HEIGHT}px;' autostart='false' src='{URL}'>"; 
	} 

	sContents = sContents.replace(/{URL}/gi, url);
	sContents = sContents.replace(/{WIDTH}/gi, width);
	sContents = sContents.replace(/{HEIGHT}/gi, height);

	var cWin = uEditor.contentWindow;
	cWin.focus();
    if (is_ie) { 
		var eEdit = cWin.document.selection.createRange();
		eEdit.pasteHTML(sContents); 
	} else {
		editor.doc.execCommand("InsertHTML", false, sContents);
	}

	/*
    if (is_ie) { 
		//var range = editor.doc.selection.createRange();
		//range.pasteHTML(sContents);
		editor.doc.body.innerHTML =  editor.doc.body.innerHTML + "<div>" + sContents + "</div>";
    } else {
		editor.doc.execCommand("InsertHTML", false, sContents);
    }
	*/
}


/*
    function insertMultimediaLink(url) {
		var preDefinedUrl = {
			defaultContent : "<embed style='width:500px; height:408px;' autostart='false' src='{URL}'>",
			videoList : [
							["^http://video.google.com/googleplayer.swf","<embed style='width:500px; height:408px;' type='application/x-shockwave-flash' src='{URL}'></embed>"],
							["^http://serviceapi.nmv.naver.com/flash/NFPlayer.swf","<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='400' height='333' id='NFPlayer' align='middle'>\
							<param name='allowScriptAccess' value='always' />\
							<param name='movie' value='{URL}' />\
							<param name='quality' value='high' />\
							<param name='bgcolor' value='#000000' />\
							<param name='wmode' value='transparent' />\
							<embed src='{URL}' quality='high' wmode='transparent' width='500' height='408' name='NFPlayer' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />\
							</object>"]
						],
			getContents : function( url ){
				var sContents = new String();
				var sCase = new String();
	
				var rgUrlExp = null;
				var rTemp = null;
				var vlLength = this.videoList.length;
	
				for( var i = 0; i < vlLength ; i++ ){
					sExpList = this.videoList[i][0];
					rgUrlExp = new RegExp( sExpList,"gi");
					rTemp = rgUrlExp.test(url);
					if( rTemp == true ){
						sCase = this.videoList[i][1];
						break;
					}
				}
	
				if( rTemp == false ){
					sCase = this.defaultContent;
				}
				sContents = sCase.replace(/{URL}/gi,url);
				return sContents;
			}
		};
	
		var getContents = preDefinedUrl.getContents (url);
		oEditor.execCommand('inserthtml',null,getContents);
	}

*/

// image 
function setImage(image_id, image_url, image_align, comment, image_width, image_height, is_small) {
	if (editMode == "source") {
	} else {
		var cWin = uEditor.contentWindow;
		cWin.focus();
		//var image_url = "<?=$config[file_url].'_temp/';?>" + image_url; 
		if (document.all) {
			var tRange = cWin.document.selection.createRange();
			//tImgStr = "<P><IMG id=" + image_id + " style='CURSOR: hand' onclick='view_image(\"" + image_url + "\")' src='" + image_url + "' width=" + image_width + " height=" + image_height + " onload='setTimeout(\"resizeImage(" + image_id + ")\",200)' align=" + image_align + "></P>"; 
			
			if (is_small) { 
				//tImgStr = "<p><img id=" + image_id + " src='" + image_url + "' align=" + image_align + "></p>"; 
				tImgStr = "<p><img id=" + image_id + " src='" + image_url + "' align=" + image_align + " width=" + image_width + " height=" + image_height + " ></p>"; 
			} else {
				tImgStr = "<p><img id=" + image_id + " src='" + image_url + "' align=" + image_align + " width=" + image_width + " height=" + image_height + " style='CURSOR: hand' onclick=view_image(this) ></p>"; 
			}

			if (image_align=='top') {
				if (comment) {
					tImgStr = "" + tImgStr + "<br>" + comment + "" ;
				}
			} else if (image_align=='bottom') {
				if (comment) {
					tImgStr = "" + comment + "<br>" +  tImgStr + "" ;
				}
			} else {
				tImgStr = tImgStr + comment;
			}
			tRange.pasteHTML(tImgStr);
		} else {
			p = cWin.document.createElement("p");
			image = cWin.document.createElement("img");
			image.setAttribute("src", image_url);
			image.setAttribute("border", 0);
			p.appendChild(image);
			insertNodeAtSelection(cWin, p);
		}
	}
}

function updateImage(image_id, image_align) {
	var image_id = String(image_id); 
	var len = image_id.length; 
	if (len==11) { 
		var image_id = '0' + image_id; 
	}

	var cWin = uEditor.contentWindow;
	cWin.focus();
	if (document.all) { 
		if (cWin.document.getElementById(image_id)) {
			if (cWin.document.getElementById(image_id)) {
				cWin.document.getElementById(image_id).align = image_align;  
			}
		}
	}
}

function updateImage2(image_id, image_align, image_url, image_width, image_height, is_small, sImgStr) {
	var image_id = String(image_id); 
	var len = image_id.length; 
	if (len==11) { 
		var image_id = '0' + image_id; 
	}

	var cWin = uEditor.contentWindow;
	cWin.focus();
	if (document.all) { 
		if (cWin.document.getElementById(image_id)) {
			
			//cWin.document.getElementById(image_id).align = image_align;  
			//cWin.document.getElementById(image_id).src = image_url;  
			//cWin.document.getElementById(image_id).width = image_width;  
			//cWin.document.getElementById(image_id).height = image_height; 

			if (is_small) { 
				tImgStr = "<img id=" + image_id + " src='" + image_url + "' align=" + image_align + " width=" + image_width + " height=" + image_height + " >"; 
			} else {
				tImgStr = "<img id=" + image_id + " src='" + image_url + "' align=" + image_align + " width=" + image_width + " height=" + image_height + " style='CURSOR: hand' onclick=view_image(this) >"; 
			}
			//alert(sImgStr + tImgStr); 
			var tContents = editor.getContent();
			var Replaced = tContents.replace(sImgStr, tImgStr);
			editor.setContent(Replaced); 
			
		} else { 
			setImage(image_id, image_url, image_align, '', image_width, image_height, is_small); 
		} 
	}
}

function delete_image(image_id, str) { 
	//alert(str); 
	//document.write (str); 
	var cWin = uEditor.contentWindow;
	cWin.focus();
	if (cWin.document.getElementById(image_id)) {
		var tContents = editor.getContent();
		var Replaced = tContents.replace(str, '');
		editor.setContent(Replaced); 
	}
}

// ------------------------------------------------------------------


function calcFileSize(filesize, oper)
{
    var objRealSum = document.getElementById("attachsizerealsum");

    if (oper == 1)
    {
        objRealSum.value = parseInt(objRealSum.value) + parseInt(filesize);
    }
    else
    {
        objRealSum.value = parseInt(objRealSum.value) - parseInt(filesize);
    }
    
    document.getElementById("attachsizesum").value = parseInt(objRealSum.value/1024);    
}

