$(document).ready(function () {
  ShowModalWindow(true);
  var isChrome =
    /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  var isFF =
    /Firefox[\/\s](\d+)/.test(navigator.userAgent) &&
    new Number(RegExp.$1) >= 4;

  $("#buttonSendForm").click(function (event) {
    $("#sendForm").submit();
  });

  $("#buttonCancel").click(function (event) {
    if (parent) {
      parent.postMessage("reload-msg", "*");
    }
  });

  $("#sendForm").submit(function (event) {
    if (
      $("#cke_message iframe").contents().find("body").text().trim() == "" &&
      // ei check img
      CKEDITOR.instances.message.document
        .getBody()
        .getHtml()
        .indexOf("<img src=") < 0 &&
      CKEDITOR.instances.message.document
        .getBody()
        .getHtml()
        .indexOf("<img data-cke-saved-src") < 0
    ) {
      alert(GForumEmprtyMessageAlert);
      event.preventDefault();
      return false;
    }
    ShowModalWindow(true);
    $("#buttonSendForm").attr("disabled", "disabled");
    $("#buttonCancel").attr("disabled", "disabled");
  });

  if ($("#message").length) {
    var editor = CKEDITOR.replace("message", {
      language: gShortLang,
      // instanceReady
      on: {
        instanceReady: function (ev) {
          var tags = ["p", "ol", "ul", "li"];
          for (var key in tags) {
            ev.editor.dataProcessor.writer.setRules(tags[key], {
              indent: false,
              breakBeforeOpen: false,
              breakAfterOpen: false,
              breakBeforeClose: false,
              breakAfterClose: false,
            });
          }
          ev.editor.resize("100%", $("#content").height() - 39);
        },
      },
      // instanceReady end
      toolbar: [
        [
          "Bold",
          "Italic",
          "Underline",
          "Strike",
          "-",
          "Link",
          "Unlink",
          "Image",
        ],
      ],
    });
    CKEDITOR.config.autoParagraph = false;
    CKEDITOR.config.linkShowAdvancedTab = false;
    CKEDITOR.config.linkShowTargetTab = false;
    CKEDITOR.config.DefaultLinkTarget = "_blank";
    CKEDITOR.config.defaultLanguage = gShortLang;
    CKEDITOR.config.language = gShortLang;
    CKEDITOR.config.removePlugins = "resize";
    var chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase());
    var edge = /Edge\/\d./i.test(navigator.userAgent.toLowerCase());

    if (chrome || edge) {
      CKEDITOR.config.extraPlugins = "pastebase64";
    } else {
      CKEDITOR.config.extraPlugins = "pastebase64ie";
    }
  }

  $(window).resize(function () {
    CKEDITOR.instances.message.resize("100%", $("#content").height() - 39);
  });

  $("#content").show();
  ShowModalWindow(false);
});
