﻿var imglist2 = '';
var editor2 = function(instanceName,width,height)
{
	this.id	= instanceName;
	this.mode = "text";
	this.width			= width  		|| '100%';
	this.height			= height		|| '100';	
	this.isIE = (window.showModalDialog) ? true:false;
	this.isIE6 = (navigator.appVersion.indexOf("MSIE 6.0")>0?true:false);
	this.isIE7 = (window.navigator.userAgent.indexOf("MSIE 7") != -1);
	this.border = "solid 1px #d3d3d3";
	this.doc = null;
	this.selectedtext = null;
	this.range	  = null;
	this.maxImgSize = null;
	if(isIE6) this.diff = 4; else this.diff = 2;

	this.objlayer = document.getElementById('objWindow');
	//editorutil2.init(this);
}

editor2.prototype.init = function(id, maxImgSize){
    if(typeof(document.execCommand)=="undefined") return;    
    try{ 
        this.textarea = document.getElementById(id);
        this.outer = document.createElement("div");
   	    this.texteditor	= document.createElement("textarea");
        this.htmleditor = document.createElement("iframe");
        this.mode = "html";

        this.outer.style.border	= "";
	    this.outer.style.width	= this.width + "px";
    	this.outer.style.height = this.height + "px";
    	this.outer.style.margin = "0px";
    	this.outer.style.padding = "0px";
    	this.outer.style.float = "left";
    	
        this.textarea.style.display="none";
	    this.texteditor.style.width = this.width + "px";
	    this.texteditor.style.border= "solid 1px blue";
	    this.texteditor.style.display="none";
	    this.texteditor.style.font = "9pt 굴림";
	    this.texteditor.style.lineHeight = 1.1;
    	
	    this.htmleditor.id = "htmlEditor2";
	    this.htmleditor.setAttribute("border", "0");
	    this.htmleditor.setAttribute("frameBorder", "0");
	    this.htmleditor.setAttribute("marginWidth", "0");
	    this.htmleditor.setAttribute("marginHeight", "0");
	    this.htmleditor.setAttribute("leftMargin", "0");
	    this.htmleditor.setAttribute("topMargin", "0");
	    this.htmleditor.setAttribute("allowtransparency", "true");
	    this.htmleditor.style.margin = "0px";
	    this.htmleditor.style.padding = "0px";
	    this.htmleditor.style.border = this.border;
	    
	    this.htmleditor.style.width = this.width - this.diff + "px";
	    
	    this.texteditor.style.height= this.height - this.diff + "px";
	    this.htmleditor.style.height= this.height - this.diff + "px";
    	
	    this.textarea.parentNode.insertBefore(this.outer, this.textarea);	    
	    this.outer.appendChild(this.htmleditor);
	    this.outer.appendChild(this.texteditor);
    	
	    this.doc = this.htmleditor.contentWindow.document;	    	    
	    this.doc.designMode="on";
	    var css  = "body{margin:0px;padding:5px;background-color:#ffffff;font-size:9pt;font-family:굴림;line-height:1.2;}";
	    css += "P{margin:0;padding:0;}";
	    css += "object{background-color: #eeeeee;}";
	    
	    this.htmleditor.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();
	    //editorutil2.addEvent(this.doc, "mousedown", editorutil2.hidelayer);
	    //this.doc.body.focus();
		//이미지 사이즈 조절로 추가
		this.maxImgSize = maxImgSize;
    }catch(e){
        alert(e.message);
    }
}

var editorutil2 = {
    editor : null,
    init : function(obj){
        this.editor = obj;
    },
    hidelayer : function(){
        try{
            smalleditor.doc.body.focus(); 
        }catch(e){
            alert(e.message);
        } 
    },
    addEvent : function(object, type, listener)
	{
		if(object.addEventListener) { object.addEventListener(type, listener, false); } 
		else if(object.attachEvent) { object.attachEvent("on"+type, listener); } 
	},
	showcolorpicker : function(func){
        try{ 
            var col= new Array();
		    col[0] = new Array("#ffffff","#e5e4e4","#d9d8d8","#c0bdbd","#a7a4a4","#8e8a8b","#827e7f","#767173","#5c585a","#000000","#fefcdf","#fef4c4","#feed9b","#fee573","#ffed43","#f6cc0b","#e0b800","#c9a601","#ad8e00","#8c7301");		    
		    col[1] = new Array("#ffded3","#ffc4b0","#ff9d7d","#ff7a4e","#ff6600","#e95d00","#d15502","#ba4b01","#a44201","#8d3901","#ffd2d0","#ffbab7","#fe9a95","#ff7a73","#ff483f","#fe2419","#f10b00","#d40a00","#940000","#6d201b");
		    col[2] = new Array("#ffdaed","#ffb7dc","#ffa1d1","#ff84c3","#ff57ac","#fd1289","#ec0078","#d6006d","#bb005f","#9b014f","#fcd6fe","#fbbcff","#f9a1fe","#f784fe","#f564fe","#f546ff","#f328ff","#d801e5","#c001cb","#8f0197");
		    col[3] = new Array("#e2f0fe","#c7e2fe","#add5fe","#92c7fe","#6eb5ff","#48a2ff","#2690fe","#0162f4","#013add","#0021b0","#d3fdff","#acfafd","#7cfaff","#4af7fe","#1de6fe","#01deff","#00cdec","#01b6de","#00a0c2","#0084a0");
		    col[4] = new Array("#edffcf","#dffeaa","#d1fd88","#befa5a","#a8f32a","#8fd80a","#79c101","#3fa701","#307f00","#156200","#d4c89f","#daad88","#c49578","#c2877e","#ac8295","#c0a5c4","#969ac2","#92b7d7","#80adaf","#9ca53b");		    
		    var s="<div style='width:285px'>";
		    for(var i=0; i<5; i++)
		    {
			    for(var j=0; j<20; j++)
			    {
				    color = col[i][j];
				    s += "<a href='javascript:;' onclick=\"" + func + ",'"+color+"');\" style='background-color:"+color+";' class='color'>&nbsp;</a>";
			    }
			    s += "<br />";
		    }
		    s += "</div>";		   
		    this.editor2.objlayer2.innerHTML = s;
        }catch(e){
            alert(e.message);
        } 
    },
    setColor : function(what, color){   
        if(this.mode == "text") {
            alert("소스보기 상태에서는 실행되지 않습니다.");
            return;
        }       
        try{
            if(what == 'doc'){
                smalleditor.doc.body.style.backgroundColor = color;
            }else{                
                smalleditor.format(what, color);
            }
        }catch(e){
            alert("setColor=" + e.message);
        }
        this.editor2.objlayer.innerHTML = '';
        this.editor2.objlayer.style.display = 'none';
       // this.editor.htmleditor.body.focus(); 
        return; 
    }
}


function img_Load2()
{
	//document.all.imgWidth.value = this.width;
	document.getElementById('imgWidth').value = this.width;

	smalleditor.addimg(fileurl2, "");
	
}

editor2.prototype.preLoadImg = function(strfile, arraytype){
	var imgInfo = new Image();
	imgInfo.onload = img_Load2;
	imgInfo.src ="/FileServer/Temp/bbs/" + strfile;
	fileurl2 = strfile;
		
}

editor2.prototype.swapmode = function(t){
    try{ 
        var strcontent = this.getvalue();
        if(this.mode == 'html'){
            t.src = "/images/common/editor/icon_editor.gif";
            this.mode = 'text';
            this.texteditor.style.display = 'block';
            this.htmleditor.style.display = 'none';
            this.texteditor.value = strcontent;
        }else{
            t.src = "/images/common/editor/icon_html.gif";
            this.mode = 'html';
            this.texteditor.style.display = 'none';
            this.htmleditor.style.display = 'block';
            this.doc.body.innerHTML = strcontent;
        }
    }catch(e){alert(e.message);}
    return;
}

editor2.prototype.getvalue = function(){
    var html = "";	
    try{ 
	    if(this.mode=="text"){
	        html = this.texteditor.value;
	    }else{
		    html = this.doc.body.innerHTML;
	    }
    }catch(e){alert(e.message);} 
	return html;
}

editor2.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(this.htmleditor.contentWindow.getSelection)
	{
		sel=this.htmleditor.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;
}




function saveCurrentPos (objTextArea) {
    if (objTextArea.createTextRange) 
        objTextArea.currentPos = document.selection.createRange().duplicate();
}
function insertText (objTextArea, text) {
    if (objTextArea.createTextRange && objTextArea.currentPos) {
        var currentPos = objTextArea.currentPos;
        currentPos.text = currentPos.text.charAt(currentPos.text.length - 1) == ' ' ? text + ' ' : text;
    }
    else
        objTextArea.value += text;
}