var TinyInit = new Class ({

  /**
   * Контруктор
   *
   * @param string  URL для загрузки данных
   * @param string  Класс тех елементов, которые необходимо модифицировать в tiny-редактор
   *
   */
  initialize: function(className) {
    this.elements = '';
    this.baseUrl;
    this.theme = 'advanced';
    this.className = className;
    if (className) {
      var ids = new Array();
      document.getElements('textarea[class^='+className+']').each(function(element, n) {
        var id = element.getProperty('id');
        if (!id) {
          id = 'textarea'+n;
          element.setProperty('id', id);
        }
        ids[n] = id;
      });
      if (ids.length) this.elements = ids.join(',');
    }
  },
  
  setTheme: function(theme) {
    this.theme = theme;
    return this;
  },

  initTiny: function() {
    var settings = {
      content_css : "/m/css/main.css",
      elements : this.elements,
      language : "ru",
      theme : this.theme,
      relative_urls : false,
      remove_script_host : true,
      document_base_url : this.baseUrl + "/",      
      paste_auto_cleanup_on_paste : true,
      paste_strip_class_attributes : "all",
      inline_styles : false,
      cleanup : true,
      cleanup_on_startup : true,
      verify_css_classes : true,
      theme_advanced_blockformats : "h2,h3,h4",
      plugins : "imageuploader,fileuploader,table,fullscreen,paste",
      theme_advanced_disable : "image,anchor,visualaid,cleanup,removeformat,outdent,indent,strikethrough,justifycenter,justifyfull,help",
      theme_advanced_buttons1_add_before : "pastetext",
      theme_advanced_buttons2_add_before : "uploadfile,uploadimage,blockquote",      
      theme_advanced_buttons2_add : "fullscreen",
      theme_advanced_buttons3_add : "tablecontrols"
    };
    if (this.className) {
      if (this.elements != '') {
        settings.mode = "exact";
        tinyMCE.init(settings);
      }
    } else {
      settings.mode = "textareas";
      tinyMCE.init(settings);
    }
  }

});


var TinyInitContent = new Class({

  Extends: TinyInit,

  initialize: function(url, action, editBtnId, contentAreaId, textareaId) {
    this.url = url;
    this.action = action;
    this.editBtn = $(editBtnId);
    this.contentArea = $(contentAreaId);
    this.elements = textareaId;
    this.editBtn.addEvent('click', function(e){
      new Event(e).stop();
      this.contentArea.addClass('loader');
      this.curContent = this.contentArea.getHTML();
      this.loadData();
    }.bind(this));
  },

  loadData: function() {
    new Request({
      url: this.url,
      onComplete: function(data) {
        this.contentArea.removeClass('loader');
        this.contentArea.setHTML(data);
        this.initTiny();
        $('updateContentBtn').addEvent('click', function(e){
          new Event(e).stop();
          submitForm('contentEditForm', 'updateContentBtn');
        });
        $('cancelContentBtn').addEvent('click', function(e){
          new Event(e).stop();
          this.contentArea.innerHTML = this.curContent;
        });
      }.bind(this)
    }).GET({
      'action' : this.action
    });
  }

});

var TinyInitDD = new Class({

  Extends: TinyInit,

  initialize: function(baseUrl, url, className) {
    this.parent(className);
    this.url = url;
    this.baseUrl = baseUrl;
    this.initTiny();
  }

});


