var contextArray = new Array();

function RichTextContext(mode, element) {
  this.editorMode = mode;
  this.elementName = element;
}

function getTargetContext(element) {
  for (var i = 0; i < contextArray.length; i++) {
    if (contextArray[i].elementName == element) {
      return contextArray[i];
    }
  }
}

function initEditor(popupURL, mode, targetElement) {
  if (!tinyMCE.isSafari) {
    //    tinyMCE.init({mode: 'textarea'});
    tinyMCE.init({
        theme: "advanced", 
        mode: "exact",
        language: "ja_UTF-8",
        elements: ('r' == mode ? targetElement : ""),
        theme_advanced_toolbar_location: "top",
        theme_advanced_buttons1: "fontsizeselect,bold,italic,underline,strikethrough,forecolor,separator,bullist,numlist,snsimage,link,unlink,outdent,indent,emotions",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
        theme_advanced_resize_horizontal : false,
        plugins: "snsimage,emotions",
        sns_image_popup_url: popupURL,
        content_css: "/stylesheets/entry_editor.css",
        relative_urls: false
          });
  }
  var a = new RichTextContext(mode, targetElement)
  contextArray.push(a);
}



function insertImageFromPopup(url, align, alt) {
  //  tinyMCEPopup.restoreSelection();
  tinyMCE.themes['advanced']._insertImage(url, alt, 0, null, null, null, null, align);
  tinyMCEPopup.close();
}

function toggleImageType() {
  var type = $F('type_file') || $F('type_url');
  var anotherType = type == 'file' ? 'url' : 'file';
  Element.hide($('table_for_' + anotherType));
  Element.show($('table_for_' + type));
}

function getEditorContent(targetElement) {
  if (tinyMCE.getInstanceById(targetElement) != null) {
    return tinyMCE.getContent(targetElement);
  } else {
    return $F(targetElement);
  }
}

function setEditorContent(targetElement) {
  if (tinyMCE.getInstanceById(targetElement) != null) {
    return tinyMCE.getContent(targetElement);
  } else {
    return $F(targetElement);
  }
}

// 指定されたモードが HTML かプレーンテキストかを返す
function modeIsHtml(mode) {
  return mode == 'h' || mode == 't';
}

function toggleEditor(mode, targetElement) {
  var shown = tinyMCE.getInstanceById(targetElement) != null;
  var clear = false;
  var editorMode = getTargetContext(targetElement).editorMode;
  if (modeIsHtml(editorMode) != modeIsHtml(mode)) {
    if (getEditorContent(targetElement)) {
      if (confirm('切り替えを行うと編集中の内容は破棄されます。切り替えてもよろしいですか？')) {
        if (!shown) {
          $(targetElement).value = '';
        }
        clear = true;
      } else {
        return;
      }
    }
  }
  if ('r' == mode) {
    if (!shown) {
      tinyMCE.execCommand("mceAddControl",true,"entry_body");
      //tinyMCE.addMCEControl($(targetElement), targetElement);
    }
  } else {
    if (shown) {
      tinyMCE.removeMCEControl(targetElement);
      if (clear) {
        $(targetElement).value = '';
      }
    }
  }
  getTargetContext(targetElement).editorMode = mode;
}

function copyMCE2RichTextArea(bodyElement) {

  var mode = getTargetContext(bodyElement).editorMode;
  if (modeIsHtml(mode)) {
    return;
  }

  var setValue = null;
  if (tinyMCE.isMSIE) {
    setValue = document.getElementsByTagName('iframe')[0].contentWindow.document.body.innerHTML;
  } else {
    setValue = $(bodyElement).nextSibling.getElementsByTagName('iframe')[0].contentWindow.document.body.innerHTML;
  }
  $(bodyElement).value = setValue;
}

function richTextCheck(){
  if($F("entry_editor_p") == "p"){
    setLatLng();return validateEntry();
  } else {
    setLatLng();copyMCE2RichTextArea('entry_body');return validateEntry();
  }
}
