var RichEdit = {

   editors: {},
   scripts: {},

   toolbarConfigs: {
      wide: [
         ['Source','-','Save','-','Templates'],['Cut','Copy','Paste'],
         ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
         '/',
         ['Bold','Italic','Underline','Strike'],
         ['NumberedList','BulletedList','-','Outdent','Indent'],
         ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
         ['Link','Unlink','Anchor'],
         ['Image','Table','HorizontalRule','Smiley','SpecialChar'],
         '/',
         ['Styles','Format','Font','FontSize'],
         ['TextColor','BGColor','ShowBlocks']
      ]      
   },

   loadScript: function(path) {
      if (typeof this.scripts[path] != 'undefined') return;
      new Ajax.Request(path, {
         asynchronous: false,
         onSuccess: function(o) {
            eval(o.responseText);
            RichEdit.scripts[path] = true;
         }
      });
   },
   
   saveInstance: function(areaID, ondone) {
      if (typeof this.editors[areaID] == 'undefined') return false;
      if (!ondone) ondone = function(){ alert('修改功成'); history.back(); };
      var args = this.editors[areaID].args;
      new Ajax.Request('?'+args.optURL, {
         method: 'post',
         parameters: {
            targetID: args.targetID,
            data: this.editors[areaID].getData(),
            act: 'set'
         },
         onSuccess: ondone
      });
   },

   getInstance: function(areaID, targetID, optURL, extraArgs, toolSet) {
      if (typeof this.editors[areaID] != 'undefined') return this.editors[areaID];
      if (!extraArgs) extraArgs = {};
      if (!toolSet) toolSet = 'wide';
      var config = {
         resize_enabled: false,
         toolbar: this.toolbarConfigs[toolSet],
         dialog_backgroundCoverColor: 'black'
      };
      var args = {targetID: targetID, areaID: areaID, optURL: optURL, extra: extraArgs};
      config.filebrowserImageBrowseUrl = '?imageSelector&args='+encodeURIComponent(Object.toJSON(args));
      config.filebrowserImageWindowWidth = 530;
      config.filebrowserImageWindowHeight = 310;
      this.editors[areaID] = CKEDITOR.replace(areaID, config);
      this.editors[areaID].args = args;
      return this.editors[areaID];
   }
   
};
