if (!path) var path = "";

function init() {
  iText = TextEditor.document;
  iText.designMode = "On";
  TextEditView.style.display="inline";
  iText.execCommand("2D-Position",true);
  iText.execCommand("LiveResize",true);
  initEditor();
  document.form.edit_mode.value = "html_layer";
  iText.oncontextmenu = function() { return false; } 
  if (document.all.DocContent != null) {
     TextEditor.document.body.innerHTML = document.all.DocContent.innerHTML;
  }
}

function initEditor() {
  var bHeader = "";
  bHeader += "<style>";
  bHeader += "P {margin-top:2px;margin-bottom:2px;margin-left:2;margin-right;2}\n";
  bHeader += "span, body, tr, td, th, select, div, form, center, option, pre, blockquote{font-size:9pt; font-family:µ¸¿ò,verdana;color:#333333;line-height:115%;}\n";
  bHeader += "</style>\n";
  bHeader += "<BODY>";
  iText.open();
  iText.write(bHeader);
  iText.close();
  iText.body.style.fontSize = "9pt";
  iText.body.style.fontFamily = "±¼¸²";
}

var oSelSave;
var sSelType;
var edValue;
function saveSelection()
{
  edValue = TextEditor.event.srcElement;  
  oSelSave = TextEditor.document.selection.createRange();
  sSelType = TextEditor.document.selection.type;
}

function TextEditorEvent(){
  if (TextEditor.event.button==2){
    var oSource = TextEditor.event.srcElement ;
    if (!oSource.isTextEdit) oSource = TextEditor.event.srcElement.parentTextEdit;
    var strValue = TextEditor.event.srcElement.tagName; //¼±ÅÃµÈ ºÎºÐÀÇ ÅÂ±× Á¾·ù
    if ((strValue == "IMG" || strValue == "HR") && oSource != null) {
      var oTextRange = oSource.createTextRange();
    }
    var selectedRange = TextEditor.document.selection.createRange();
    var edValue = selectedRange.htmlText;
    
    var strX = TextEditor.event.x;
    var strY = TextEditor.event.y+180;
    
    if (strValue == "IMG") 
      strH = "180px";
    else if (strValue == "HR" || strValue == "TABLE")
      strH = "135px";
    else
      strH = "340px";
    
    var strParam = "dialogLeft:" + strX + ";dialogTop:" + strY + ";"
    strParam = strParam + "center:no;dialogWidth:150px; dialogHeight:" + strH + ";status:0;scroll:0; help:0;unadorned:yes;"
  }
}

//since 2002/03/17
function paste(param){
  TextEditor.focus();
  var sel = TextEditor.document.selection;
  if (sel!=null) {
    var rng = sel.createRange();
      if (rng!=null)
        rng.pasteHTML(param);
  }
}

//since 2003.1.24 Ãß°¡
function layer_change6(hidden_key,view_key){
  if(hidden_key == "html_layer"){
    if (document.form.edit_mode.value != "html_layer")
    {
      TextEditor.document.body.innerHTML = document.form.html_body.value;
      document.form.edit_mode.value = "html_layer";
      document.all["html_layer"].style.display = "none";
      document.all["text_layer"].style.display = "block";
      document.all["preview_layer"].style.display = "none";
    }
  }
  else if(hidden_key == "text_layer"){
    if (document.form.edit_mode.value != "text_layer")
    {
      document.form.html_body.value = TextEditor.document.body.innerHTML;
      document.form.edit_mode.value = "text_layer";
      document.all["html_layer"].style.display = "block";
      document.all["text_layer"].style.display = "none";
      document.all["preview_layer"].style.display = "none";
    }
  }
  else{
    if(document.form.edit_mode.value == "text_layer"){
      preview_layer.innerHTML = document.form.html_body.value;
      TextEditor.document.body.innerHTML = document.form.html_body.value;
    }
    else{
      preview_layer.innerHTML = TextEditor.document.body.innerHTML;
      document.form.html_body.value = TextEditor.document.body.innerHTML;
    }

    document.form.edit_mode.value = "pre_layer";
    document.all["html_layer"].style.display = "none";
    document.all["text_layer"].style.display = "none";
    document.all["preview_layer"].style.display = "block";
  }  
}

function ButtonOut(param) {
  param.style.border="";
  param.style.background="";
}

function ButtonOut2(param) {
  param.style.border="#999999 1px solid";
  param.style.background="";
}

function block_style(o, cmd) {
  var ed=TextEditor.document.selection.createRange();
  ed.execCommand(cmd, false, o.value);
}

//CreateLink
function SelectionCommand(Btn, cmd) {
  TextEditor.focus();
  var EdRange=TextEditor.document.selection.createRange();
  EdRange.execCommand(cmd);
}
// Redo, Undo
function SelectionCommand2(sOption, cmd) {
  TextEditor.focus()
  var oSel  = TextEditor.document.selection.createRange()
  var sType = TextEditor.document.selection.type  
  var oTarget = (sType == "None" ? TextEditor.document : oSel)
  oTarget.execCommand(cmd, false, sOption)
}

function SelectionCommand3(sOption,cmd) {
  TextEditor.focus()
  var oSel  = TextEditor.document.selection.createRange()
  oSel.execCommand(cmd, true, sOption)
}

//since 2003.1.24 Ãß°¡
function inTable(){
  var value = showModalDialog( "/kBoard/inc/instable.html","","font-family:Verdana; font-size:12;dialogWidth:420px; dialogHeight:420px;status:no;help:no;self-close:no" );
  if( value != null ){
    TextEditor.document.body.focus();
    TextEditor.document.selection.createRange().pasteHTML(value);
  }
  TextEditor.document.body.focus();
}

function inEmotcon(){
  var value = showModalDialog( "/kBoard/inc/icons.jsp","","font-family:Verdana; font-size:12;dialogWidth:520px; dialogHeight:200px;status:no;help:no;self-close:no;scroll:no" );
  if( value != null ){
    TextEditor.document.body.focus();
    TextEditor.document.selection.createRange().pasteHTML(value);
  }
  TextEditor.document.body.focus();
}

function spChar(){
  var value = showModalDialog( "/kBoard/inc/letter.html","","font-family:Verdana; font-size:12;dialogWidth:390px; dialogHeight:270px;status:no;help:no;self-close:no;scroll:no" );
  if( value != null ){
    TextEditor.document.body.focus();
    TextEditor.document.selection.createRange().pasteHTML(value);
  }
  TextEditor.document.body.focus();
}

function colorPicker(which) {
  var value = showModalDialog(path + "../include/html/colorPicker.htm","","font-family:Verdana; font-size:12;dialogWidth:199px; dialogHeight:139px;status:no;help:no;self-close:no;scroll:no" );
  switch (which) {
    case 0:
        SelectionCommand3(value,'forecolor');
        break;
    case 1:
        SelectionCommand3(value,"backcolor")
        break;
  }
}

function addImage(str){
  var imgInfo;
  imgInfo   = document.form.imgInfo.value;
  var saveImage;
  saveImage = document.form.saveImage.value;
  if(saveImage != "" && saveImage != null) imgInfo = saveImage + imgInfo;
  if(imgInfo != "" && imgInfo != null){
    var ImgLen = imgInfo.split(".");
    var ImgLenCnt = parseInt(ImgLen.length);
    if (ImgLenCnt > 10){
      alert("ÀÌ¹ÌÁö¸¦ ´õÀÌ»ó Ãß°¡ÇÏ½Ç¼ö ¾ø½À´Ï´Ù.");
      return;
    }
  }
  OpenWin2(path + "../include/html/bbs_addImg.htm?mode="+str,400,250);
}

function OpenWin2(URL,w_width,h_height) {
  var str;
  var scrWidth = (screen.availWidth / 2 ) - 200;
  var scrHeight = (screen.availHeight / 2) - 150;
  str="'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,";
  str=str+"width="+w_width;
  str=str+",height=" + h_height + "',top="+scrHeight+",left="+scrWidth;
  wopen = window.open(URL,'remote',str);
}

var n   = 0;
function inFind(nettop)
{
  var NS4 = (document.layers);   
  var IE4 = (document.all);
  //var win = window;    
  var win = TextEditor;    
  var txt, i, found;
  var str;
  if(nettop==""){
    str = showModalDialog( "/kBoard/inc/inFind.html","","dialogLeft:250;dialogTop:250;font-family:Verdana; font-size:12;dialogWidth:200px; dialogHeight:120px;status:no;help:no;self-close:no;scroll:no" );
  }else{
    str = showModalDialog( "/kBoard/inc/inFind.html",nettop,"dialogLeft:250;dialogTop:250;font-family:Verdana; font-size:12;dialogWidth:200px; dialogHeight:120px;status:no;help:no;self-close:no;scroll:no" );
  }
  if (str == "" || str == null)
    return false;
  if (NS4) {
    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;
    if (n == 0)
      alert("´Ü¾î¸¦ Ã£À»¼ö°¡ ¾ø½À´Ï´Ù.ns");
    return false;
  }
  if (IE4) {
    txt = win.document.body.createTextRange();

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
  }
    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }
    else {
      if (n > 0) {
        n = 0;
        inFind(str);
      }
      else
        alert("´Ü¾î¸¦ Ã£À»¼ö°¡ ¾ø½À´Ï´Ù.ie");
    return false;
    }
  }
  inFind(str);
  return false;
}

function MoveEditSize(Scroll){
  if(Scroll == "wide_out"){
    TextEditView.style.pixelWidth += 35;
  }else if(Scroll == "wide_in"){
    TextEditView.style.pixelWidth -= 35;
  }else if(Scroll == "height_out"){
    TextEditView.style.pixelHeight += 35;
  }else if(Scroll == "height_in"){
    TextEditView.style.pixelHeight -= 35;
  }else if(Scroll == "fix"){
    TextEditView.style.pixelWidth = 505;
    TextEditView.style.pixelHeight = 250;
  }else{return;}
}


function ButtonUp(param) {
  param.style.border="1px outset";
  param.style.background="#B8B8AB";
}
