Editor.Base = Class.create();
Editor.Base.prototype = {

  initialize: function(element, options) {
    this.hijackForm(element);

    var options  = Editor.Options.from(element, options);
    this.iframe  = new Editor.IFrame(element, options);
    this.iframe.build(element.value);

    this.sweeper = new Editor.Sweeper(this);
    this.sweeper.swapToDeprecated(this.iframe.body());

    Event.observe(this.iframe.document(), "mousedown", this.onFocus.bindAsEventListener(this));
    Event.observe(this.iframe.document(), "keydown",   this.onKeyDown.bindAsEventListener(this));
    Event.observe(this.iframe.document(), "keyup",     this.onKeyUp.bindAsEventListener(this));

    Editor.listeners.push(this);
  },

  execute: function(commandName, options) {
    this.iframe.document().execCommand(commandName, false, options);
    this.sweeper.sweep();
  },

  find: function(tag) {
    return $A(this.iframe.document().getElementsByTagName(tag));
  },

  htmlString: function() {
    return this.sweeper.scrub(this.iframe.body());
  },
  
  hijackForm: function(element) {
    var form    = element.up("form");
    this.hidden = new Element("input", {
      type: "hidden",
      name: element.name,
      id:   element.id
    });
    form.appendChild(this.hidden);
    Event.observe(form, "submit", this.onSubmit.bindAsEventListener(this));
  },

  onSubmit: function(event) {
    Editor.current    = this;
    this.hidden.value = this.htmlString();
    this.sweeper.swapToDeprecated(this.iframe.body());
  },

  onKeyDown: function(event) {
    this.sweeper.sweep();
  },

  onKeyUp: function(event) {
    var key = Prototype.Browser.IE ? event.keyCode : event.which;
    if(key == 13 /*Enter Key*/) this.sweeper.removeDivTagsFromLiTags();
  },

  onFocus: function(event) {
    Editor.current = this;

    //var target = Event.element(event);
    //if (target.nodeName.toLowerCase() == "img") {
    //  Editor.Selection._focusedImage = target;
    //} else {
    //  Editor.Selection._focusedImage = false;
    //}
    this.sweeper.sweep();
  },

  onBlur: function(event) {
  }
}
