// Copyright (c) 2007 Sebastien Grosjean (http://www.zencocoon.com, http://seb.box.re)
//
// Contributor:
//   Neil Rickards
//   Robert Muzslai
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For details, see the inPlaceRichEditor web site: http://inplacericheditor.box.re/

if(typeof Ajax.InPlaceEditor == 'undefined')
  throw("InPlaceRichEditor requires including script.aculo.us' controls.js library");
if(typeof TinyMCE == 'undefined')
  throw("InPlaceRichEditor requires including moxiecode' tiny_mce.js library");

Ajax.InPlaceRichEditor = Class.create();
Object.extend(Ajax.InPlaceRichEditor.prototype, Ajax.InPlaceEditor.prototype);
Ajax.InPlaceRichEditor.prototype._inPlaceEditorInitialize = Ajax.InPlaceEditor.prototype.initialize;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorOnLoadedExternalText = Ajax.InPlaceEditor.prototype.onLoadedExternalText;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorOnSubmit = Ajax.InPlaceEditor.prototype.onSubmit;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorShowSaving = Ajax.InPlaceEditor.prototype.showSaving;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorEnterEditMode = Ajax.InPlaceEditor.prototype.enterEditMode;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorLeaveEditMode = Ajax.InPlaceEditor.prototype.leaveEditMode;
Ajax.InPlaceRichEditor.prototype._inPlaceEditorDispose = Ajax.InPlaceEditor.prototype.dispose;
Object.extend(Ajax.InPlaceRichEditor.prototype, {
  Version: '1.1.2',
  initialize: function(element, url, options) {
    this.tinymce = tinyMCE;
    
    this.element = $(element);
    this.options = Object.extend({
      savingClassName: 'inplacericheditor-saving',
      loadingClassName: 'inplacericheditor-loading',
      formClassName: 'inplacericheditor-form'
    }, options || {});

    if(!this.options.formId && this.element.id) {
      this.options.formId = this.element.id + "-inplacericheditor-form";
      if ($(this.options.formId)) {
        // there's already a form with that name, don't specify an id
        this.options.formId = null;
      }
    }

    this._inPlaceEditorInitialize(element, url, this.options);
  },
  onLoadedExternalText: function(transport) {
    this._inPlaceEditorOnLoadedExternalText(transport);
    this.tinymce.setContent(transport.responseText);
  },
  onSubmit: function() {
    this.tinymce.triggerSave();
    this.editField.value = this.tinymce.getContent();
    this._inPlaceEditorOnSubmit();
  },
  showSaving: function() {
    this.tinymce.execCommand('mceRemoveControl', false, this.element.id);
    this._inPlaceEditorShowSaving();
  },
  enterEditMode: function() {
    this.tinymce.execCommand('mceAddControl', false, this.element.id);
    this._inPlaceEditorEnterEditMode();
    Element.hide(this.editField);
  },
  leaveEditMode: function() {
    if (this.options.loadTextURL) {
        this.tinymce.setContent(this.element.innerHTML);
    }
    this.tinymce.execCommand('mceRemoveControl', false, this.element.id);
    this._inPlaceEditorLeaveEditMode();
  },
  dispose: function() {
    this.tinymce.execCommand('mceRemoveControl', false, this.element.id);
    this._inPlaceEditorDispose();
  }
});