(function() {
  decko.addEditor('.tinymce-textarea', function() {
    return decko.initTinyMCE(this[0].id);
  }, function() {
    var ed;
    ed = tinyMCE.get(this[0].id);
    return ed && ed.getContent();
  });

  decko.slotDestroy(function(slot) {
    return slot.find("textarea.tinymce-textarea").each(function() {
      var ed;
      ed = tinyMCE.get($(this)[0].id);
      return ed && ed.remove();
    });
  });

  $.extend(decko, {
    setTinyMCEConfig: function(string) {
      var setter;
      setter = function() {
        try {
          return $.parseJSON(string);
        } catch (error) {
          return {};
        }
      };
      return decko.tinyMCEConfig = setter();
    },
    initTinyMCE: function(el_id) {
      var conf, hard_conf, user_conf;
      conf = {
        theme: "silver",
        plugins: 'autoresize',
        autoresize_max_height: 500,
        mobile: {
          theme: 'mobile'
        },
        contextmenu: "deckolink nest"
      };
      user_conf = decko.tinyMCEConfig != null ? decko.tinyMCEConfig : {};
      hard_conf = {
        selector: "#" + el_id,
        branding: false,
        extended_valid_elements: "card-nest[id]",
        content_css: decko.cssPath,
        entity_encoding: 'raw'
      };
      $.extend(conf, user_conf, hard_conf);
      decko.addNestPlugin(conf);
      tinyMCE.baseURL = decko.path('assets/tinymce_editor/tinymce');
      tinyMCE.suffix = '.min';
      return tinyMCE.init(conf);
    },
    addNestPlugin: function(conf) {
      if (conf.plugins != null) {
        conf.plugins += " nest";
      } else {
        conf.plugins = "nest";
      }
      return conf.menu = {
        insert: {
          title: "Insert",
          items: "deckolink nest image | hr"
        }
      };
    }
  });

}).call(this);