/**
 * EditorScript.js
 * Edior °ü·Ã ½ºÅ©¸³Æ®
 *
 * @see     
 * history :
 *          moon jong deok, 1.0, 2008/02/21 - ÃÊ±â ÀÛ¼º
 * @version 1.0
 * @author  moon jong deok
 */

var mySelect = null;
var myBgcolor = null;
			 
function menuClick(obj){

	if (mySelect!=null) {
		mySelect.select();
	}

	if (obj.id == "idBold"){
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("Bold");
	}
	else if (obj.id == "idItalic"){
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("Italic");
	}
	else if (obj.id == "idUnderline"){
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("Underline");
	}
	else if (obj.id == "idStrike"){
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("StrikeThrough");
	}
	else if (obj.id == "idFontcolor") {
		previewColor('1');
	}
	else if (obj.id == "idBgcolor") {
		previewColor('2');
	}
	else if (obj.id == "idBackcolor") {
		previewColor('3');
	}
	else if (obj.id == "idAlignLeft") {
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("JustifyLeft");
	}
	else if (obj.id == "idAlignCenter") {
		HTMLEDITOR.focus();	
		HTMLEDITOR.document.execCommand("JustifyCenter");
	}
	else if (obj.id == "idAlignRight") {
		HTMLEDITOR.focus();	  
		HTMLEDITOR.document.execCommand("JustifyRight");  
	}
	else if (obj.id == "idLink") {
		setLink();	
	}
	else if(obj.id == "idViewSource") {
		HTMLEDITOR.document.body.innerText = HTMLEDITOR.document.body.innerHTML;
		HTMLEDITORTOOL.style.display = "none";
		HTMLSOURCETOOL.style.display = "block";
		if(myBgcolor!=null && myBgcolor.replace(/ /gi,"") != ""){
			setBackColor("#FFFFFF");
		}
	}
	else if(obj.id == "idViewEditor") {
		HTMLEDITOR.document.body.innerHTML = HTMLEDITOR.document.body.innerText;
		HTMLEDITORTOOL.style.display = "block";
		HTMLSOURCETOOL.style.display = "none";
		if(myBgcolor!=null && myBgcolor.replace(/ /gi,"") != ""){
			setBackColor(myBgcolor);
		}
	}
	else if(obj.id == "idImage"){
		if(HTMLEDITORTOOL.style.display=="none"){
			alert('¿¡µðÅÍ È­¸é¿¡¼­ ÆÄÀÏÀ» ¿Ã¸®¼¼¿ä.');
			return;
		}
		imageInput();
	}
	else if(obj.id == "idFile"){
		fileInput();
	}
	else if(obj.id == "idMedia"){
		mediaInput();
	}

}

function setFont(ch){
	if (ch.options[ch.selectedIndex].value != ''){
		HTMLEDITOR.document.execCommand("FontName", null, ch.options[ch.selectedIndex].value);
	}
	ch.selectedIndex = 0;   
}
function setFontSize(ch){

	if (ch.options[ch.selectedIndex].value != ''){
		HTMLEDITOR.document.execCommand("FontSize", null, ch.options[ch.selectedIndex].value);
	}
	ch.selectedIndex = 0;
}


function setFontColor(color){
	HTMLEDITOR.document.execCommand("forecolor", null, color);
}

function setBgColor(color){
	HTMLEDITOR.document.execCommand( "BackColor", null, color );
}

function setBackColor(color){

	var obj = HTMLEDITOR.document.all["backcolor"];

	if (obj != null)
		HTMLEDITOR.document.all["backcolor"].style.backgroundColor = color;
	else
		HTMLEDITOR.document.body.style.backgroundColor = color;
}

function setLink(){

	var mylink = prompt("¸µÅ©¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.", "http://");
	HTMLEDITOR.document.execCommand("CreateLink", null, mylink);
	/*
	mySelect = HTMLEDITOR.document.selection.createRange();
	if (mySelect!=null){
		mySelect.pasteHTML("<a href=\"" + mylink + "\" target=\"_blank\"><font color='blue'>" + mySelect.text + "</font></a> ");
	}
	*/

}

function previewColor(type){
	window.open('/common/editor/colorSelectForm.do?type=' + type, 'Editcolorpicker', 'width=350,height=280,resizable=no,menubar=no');
}

function imageInput(){
	window.open('/common/editor/imageUploadForm.do','iu','width=310,height=330,resizable=yes');
}

function fileInput(){
	window.open('/common/editor/fileUploadForm.do','fu','width=400,height=300,resizable=yes');
}

function mediaInput(bptype){
	window.open('/common/editor/mediaInputForm.do','mu','width=267,height=208,resizable=yes');
}

function imageInsert(url,align,fileName,fileNameOrg,size,width,height,subPath){
	try{
		var sText = HTMLEDITOR.document.selection.createRange();
		var iText = "";

		/*
		if (align.toLowerCase()=='center'){
			if(width>530)
				iText = "<p align=center><img src='" + url + "' border=0 width='530' /></p><br>";
			else
				iText = "<p align=center><img src='" + url + "' border=0 width='"+width+"' height='"+height+"' /></p><br>";
		}
		else{
			if(width>530)
				iText = "<img src='" + url + "' align='" + align + "' border=0 width=530 /><br>";
			else
				iText = "<img src='" + url + "' align='" + align + "' border=0 width='"+width+"' height='"+height+"' /><br>";
		}
		if (sText.parentElement().all.HTMLEDITOR == null){
			sText.pasteHTML(iText);
			sText.select();
		}
		else{
			HTMLEDITOR.document.body.innerHTML =  iText + HTMLEDITOR.document.body.innerHTML;
			HTMLEDITOR.focus();
		}
		*/

		/*
		if(width>530){
			if(align == "left"){
				iText = "<img src=\""+url+"\" border=\"0\" width=\"530\" align=\"left\" />";
			}
			else if(align == "right"){
				iText = "<img src=\""+url+"\" border=\"0\" width=\"530\" align=\"right\" />";
			}
			else{
				iText = "<img src=\""+url+"\" border=\"0\" width=\"530\" />";
			}
		}
		else{
			if(align == "left"){
				iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" align=\"left\" />";
			}
			else if(align == "right"){
				iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" align=\"right\" />";
			}
			else{
				iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" />";
			}
		}
		*/

		if(align == "left"){
			iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" align=\"left\" />";
		}
		else if(align == "right"){
			iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" align=\"right\" />";
		}
		else{
			iText = "<img src=\""+url+"\" border=\"0\" width=\""+width+"\" height=\""+height+"\" />";
		}

		if(align == "top"){
			HTMLEDITOR.document.body.innerHTML = "<p align=\"center\">" + iText + "</p><br />" 
												+  HTMLEDITOR.document.body.innerHTML;
		}
		else if(align == "bottom"){
			HTMLEDITOR.document.body.innerHTML = HTMLEDITOR.document.body.innerHTML 
												+ "<br /><p align=\"center\">" + iText + "</p>";
		}
		else if(align == "left"){
			HTMLEDITOR.document.body.innerHTML = iText + HTMLEDITOR.document.body.innerHTML;
		}
		else if(align == "right"){
			HTMLEDITOR.document.body.innerHTML = iText + HTMLEDITOR.document.body.innerHTML;
		}
		else{
			HTMLEDITOR.document.body.innerHTML = HTMLEDITOR.document.body.innerHTML + iText;
		}

	}
	catch(e){
		HTMLEDITOR.document.body.innerHTML =  HTMLEDITOR.document.body.innerHTML+iText;
	}

	if(isObject(document.getElementById("attachFiles"))){
		addAttachFile(fileName,fileNameOrg,size,width,height,subPath,"I",url);
	}
}

function mediaInsert(url){
	try{
		var extchk = false;
		extArray = new Array("gif", "jpg", "bmp", "png");

		var sText = HTMLEDITOR.document.selection.createRange();
		var iText = "";

		ext = url.slice(url.lastIndexOf(".")+1).toLowerCase();

		for(var i=0; i<extArray.length; i++){
			if (extArray[i] == ext){ 
				extchk = true;
				break;
			}else{
				extchk = false;
			}
		}

		if(!extchk){
			iText = "<embed type=\"application/x-mplayer2\" "
					+ "src=\"" + url + "\" "
					+ "name=\"MediaPlayer\" "
					+ "pluginspage=\"http://www.microsoft.com/isapi/redir.dll"
					+ "?prd=windows&amp;sbp=mediaplayer&amp;ar=media&amp;sba=plugin&amp;\" "
					+ "autosize=\"0\" "
					+ "autostart=\"0\" "
					+ "showcontrols=\"1\" "
					+ "showdisplay=\"0\" "
					+ "showstatusbar=\"0\">"
					+ "</embed><br>";
		}else{
			iText = "<img src=\"" + url + "\">"
					+ "<br>";
		}

		if (sText.parentElement().all.HTMLEDITOR == null){
			if (sText.parentElement().outerHTML.indexOf('<BODY') > 0){
				sText.parentElement().innerHTML = iText + sText.parentElement().innerHTML;
			}
			else{
				sText.parentElement().innerHTML = iText + sText.parentElement().innerHTML;
			}    
		}
		else{
			HTMLEDITOR.document.body.innerHTML =  iText + HTMLEDITOR.document.body.innerHTML
		}    
	}
	catch(e){
		HTMLEDITOR.document.body.innerHTML =  HTMLEDITOR.document.body.innerHTML+iText;
	}
}

function addAttachFile(fileName,fileNameOrg,size,width,height,subPath,fileType,url){

	var obj = document.all.attachFiles;
	var length = obj.options.length;

	obj[length] = new Option(fileNameOrg, fileName+'|'+fileNameOrg+'|'+size+'|'+width+'|'+height+'|'+subPath+'|'+fileType+'|'+url);

	setAttachFileLenth();
}

function delAttachFile(DtFilePath){
	
	var obj = document.all.attachFiles;
	var length = obj.options.length;

	if(!checkAttachFileSelected()) {
		alert('»èÁ¦ÇÒ ÆÄÀÏÀ» ¼±ÅÃÇÏ¼¼¿ä.');
		return;
	}

	for(var i=0; i<length; i++){
		if(!delAttachFileReal(DtFilePath))
			break;
	}

	setAttachFileLenth();
}

function delAttachFileReal(DtFilePath){
	var obj = document.all.attachFiles;
	var length = obj.options.length;

	for(var i=0; i<length; i++){
		if(obj[i].selected==true){
			delContentImage(obj[i], DtFilePath);
			obj[i]=null;
			return true;
		}
	}
	return false;
}

function delContentImage(option, DtFilePath) {
	var splitStr = option.value.split("|");
	var srcStr = splitStr[7];
	var HTMLstr = HTMLEDITOR.document.body.innerHTML;
	var basicIndex = HTMLstr.indexOf(srcStr);
	var startIndex = 0;
	var endIndex = 0;
	var idx = 0;

	for(idx = basicIndex ; idx >= 0 ; idx--) {
		if(HTMLstr.charAt(idx) == '<') {
			startIndex = idx;
			break;
		}
	}
	for(idx = basicIndex ; idx < HTMLstr.length ; idx++) {
		if(HTMLstr.charAt(idx) == '>') {
			endIndex = idx;
			break;
		}
	}
	HTMLEDITOR.document.body.innerHTML = HTMLstr.substring(0, startIndex) + HTMLstr.substring(endIndex + 1, HTMLstr.length);
}

function checkAttachFileSelected(){
	var obj = document.all.attachFiles;
	var length = obj.options.length;

	for(var i=0; i<length; i++){
		if(obj[i].selected==true)
			return true;
	}

	return false;
}

function setAttachFileLenth(){
	var obj = document.all.attachFiles;
    	var length = obj.options.length;
    	var size = 0;
    	var temp ;
 
    	for(var i=0; i<length; i++){
        	if(isEmpty(obj[i].value)){
            		continue;
        	}
        	temp = obj[i].value.split("|");
        	size += parseInt(temp[2]);
    	}
 
    	if( size == null ){
        	site = 0;
    	}
 
    	document.all.attachFileSize.value=size;
}

function resizeEditor() {
	var obj = navigator.appVersion;
	var hei = 29;
	var wid = 556;

	if (navigator.appVersion.indexOf("NT") != -1) {
		os = obj.substr(obj.indexOf("NT"),6);
		if (os > "NT 5.0") {
			hei = 35;
		}
	}
	//hei = HTMLEDITOR.document.body.scrollHeight+hei;
	hei = HTMLEDITOR.document.body.scrollHeight;
	if(hei<300) hei=300;

	setTimeout('resizeEditor()',500);
}

function editorInit(bgcolor) {

	var mySource =  "<body style='margin:5px 5px 5px 5px;'>";
	var editorInitContent = document.getElementById("editorInitContent").value;

	myBgcolor = bgcolor;

	if(editorInitContent != null) {
		mySource += editorInitContent;
	}

	HTMLEDITOR.document.designMode = "on";
	HTMLEDITOR.document.open("text/html; charset=euc-kr");
	HTMLEDITOR.document.write(mySource);
	HTMLEDITOR.document.close();
	HTMLEDITOR.document.body.style.fontSize = "9pt";
	HTMLEDITOR.document.body.style.fontFamily = "±¼¸²";
	HTMLEDITOR.document.body.contentEditable = true;
	//HTMLEDITOR.document.oncontextmenu = new Function("return false;");

	if(myBgcolor!=null && myBgcolor.replace(/ /gi,"") != ""){
		setBackColor(myBgcolor);
	}
}

