function render_editor(id, config) {
  if ($("#"+id).val() != null) {

    var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event;

    var newToolbar = new yui_editor_toolbar;
    var myConfig = {
      height: config.height,
      width: config.width,
      dompath: config.dom,
      animate: true,
      //handleSubmit: true,
      markup: config.markup,
      toolbar: newToolbar.toolbar
    };
    myConfig.toolbar.titlebar = config.title;
    myConfig.toolbar.collapse = config.collapse;
    myConfig.toolbar.draggable = config.draggable;
    myConfig.toolbar.buttonType = config.buttonType;

    $(document).ready(function() {
      $(".grippie").hide();
      $(".teaser-checkbox").hide();
      YAHOO.util.Dom.setStyle('toggleEditor-'+id, 'margin-top', '1.7em');
    });

    var myEditor = new YAHOO.widget.Editor(id, myConfig);
    if (! config.titlebar) {
      myEditor._defaultToolbar.titlebar = config.titlebar;
    }

    myEditor.render();

    // JCD: Eventually we probably should just move all of the config functionality into separate functions/files

    if (config.imgUpload == 1) {
      yui_img_uploader(myEditor, '?q=/yui_editor/image_upload', 'files[upload]', config.base_path);
	}

    if (config.resize == 1) {
      myEditor.on('editorContentLoaded', function() {
        resize = new YAHOO.util.Resize(myEditor.get('element_cont').get('element'), {
          handles: ['br'],
          autoRatio: true,
          status: true,
          proxy: true
        });
        resize.on('startResize', function() {
          this.hide();
          this.set('disabled', true);
        }, myEditor, true);
        resize.on('resize', function(args) {
          var h = args.height;
          var th = (this.toolbar.get('element').clientHeight + 2); //It has a 1px border..
          var dh = (this.dompath.clientHeight + 1); //It has a 1px top border..
          var newH = (h - th - dh);
          this.set('width', args.width + 'px');
          this.set('height', newH + 'px');
          this.set('disabled', false);
          this.show();
        }, myEditor, true);
      });
    }

    var plaintextState = 'off';
    if (config.plaintext == 1) {

      $("#"+id).after("<button id=\"toggleEditor-"+id+"\">Switch to Plain Editor</button>");
      var _button = new YAHOO.widget.Button('toggleEditor-'+id);
      _button.addClass('toggleEditor');

      _button.on('click', function(ev) {
        Event.stopEvent(ev);
        if (plaintextState == 'off') {
          plaintextState = 'on';
          myEditor.saveHTML();
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'absolute');
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '-9999px');
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '-9999px');
          myEditor.get('element_cont').removeClass('yui-editor-container');
          Dom.setStyle(myEditor.get('element'), 'visibility', 'visible');
          Dom.setStyle(myEditor.get('element'), 'top', '');
          Dom.setStyle(myEditor.get('element'), 'left', '');
          Dom.setStyle(myEditor.get('element'), 'position', 'static');
          _button.set("label", 'Switch to Rich Text Editor');

          $(".grippie").show();
          $(".teaser-checkbox").show();
        }
        else {
          plaintextState = 'off';
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'static');
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '0');
          Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '0');
          Dom.setStyle(myEditor.get('element'), 'visibility', 'hidden');
          Dom.setStyle(myEditor.get('element'), 'top', '-9999px');
          Dom.setStyle(myEditor.get('element'), 'left', '-9999px');
          Dom.setStyle(myEditor.get('element'), 'position', 'absolute');
          myEditor.get('element_cont').addClass('yui-editor-container');
          myEditor._setDesignMode('on');
          myEditor.setEditorHTML(myEditor.get('textarea').value);
          _button.set("label", 'Switch to Plain Editor');
          $(".grippie").hide();
          $(".teaser-checkbox").hide();
        }
      });
    }

    var coderState = 'off';
    if (config.coder == 1) {

      myEditor.on('toolbarLoaded', function() {
        var codeConfig = {
          type: 'push', label: 'Edit HTML Code', value: 'editcode'
        };
        this.toolbar.addButtonToGroup(codeConfig, 'insertitem');

        this.toolbar.on('editcodeClick', function() {
          var ta = this.get('element'),
            iframe = this.get('iframe').get('element');

          if (coderState == 'on') {
            coderState = 'off';
            this.toolbar.set('disabled', false);
            this.setEditorHTML(ta.value);
            if (!this.browser.ie) {
              this._setDesignMode('on');
            }

            Dom.removeClass(iframe, 'editor-hidden');
            Dom.addClass(ta, 'editor-hidden');
            this.show();
            this._focusWindow();
          }
          else {
            coderState = 'on';
            this.cleanHTML();
            Dom.addClass(iframe, 'editor-hidden');
            Dom.removeClass(ta, 'editor-hidden');
            this.toolbar.set('disabled', true);
            this.toolbar.getButtonByValue('editcode').set('disabled', false);
            this.toolbar.selectButton('editcode');
            this.dompath.innerHTML = 'Editing HTML Code';
            this.hide();
          }

          return false;
        }, this, true);

        this.on('cleanHTML', function(ev) {
          this.get('element').value = ev.html;
        }, this, true);
        
        this.on('afterRender', function() {
          var wrapper = this.get('editor_wrapper');
          wrapper.appendChild(this.get('element'));
          this.setStyle('width', '100%');
          this.setStyle('height', '100%');
          this.setStyle('visibility', '');
          this.setStyle('top', '');
          this.setStyle('left', '');
          this.setStyle('position', '');

          this.addClass('editor-hidden');
        }, this, true);
      }, myEditor, true);
    }

    var toggle = 'off';
    $('#toggleEditor-'+id).bind('click', function () { toggle = 'on'; });
    $('form').bind('submit', function (e) {
      if (toggle == 'on') {
        toggle = 'off';
        return false;
      }
      else if (plaintextState == 'on' || coderState == 'on') {
        myEditor.setEditorHTML(myEditor.get('textarea').value);
      }
       else {
        myEditor.saveHTML();
      }
    });
  }
};

function yui_img_uploader(rte, upload_url, upload_image_name, base_path) {
  rte.addListener('toolbarLoaded', function() {
    rte.toolbar.addListener('insertimageClick', function(o) {
      try {
        var imgPanel = new YAHOO.util.Element('yui-editor-panel');
        imgPanel.on ('contentReady', function() {
          try {
            var Dom = YAHOO.util.Dom;
            var label = document.createElement('label');
            label.innerHTML = '<strong>Upload:</strong>' +
              '<input type="file" id="insertimage_upload" name="' + upload_image_name +
              '" size="10" style="width: 20%"/>' +
              '<a href="#"  id="insertimage_upload_btn" style="width: 20%; margin-left: 10em;">Upload Image</a>' +
              '</label>';

            var img_elem=Dom.get('insertimage_url');
            Dom.getAncestorByTagName(img_elem, 'form').encoding = 'multipart/form-data';
            Dom.insertAfter(label, img_elem.parentNode);
                                                    
            YAHOO.util.Event.on('insertimage_upload_btn', 'click', function(ev) {
              YAHOO.util.Event.stopEvent(ev); // no default click action
              YAHOO.util.Connect.setForm(img_elem.form, true, true);
              var c = YAHOO.util.Connect.asyncRequest('POST', upload_url, { upload:
			    function(r) {
                  try {
                    resp = r.responseText.replace( /<hints .*/i, '');
                    var o = eval('('+resp+')');
                    if (o.status == 'UPLOADED') {
                      Dom.get('insertimage_upload').value = '';
                      Dom.get('insertimage_url').value=base_path + o.image_url;
                      Dom.get('insertimage_url').focus();
                      Dom.get('insertimage_upload').focus();
                    }
                    else {
                      alert ("Upload Failed: " + o.status);
                    }
                  }
                  catch (eee) {
                    YAHOO.log(eee.message, 'error');
                  }
                }
              });

              return false;
            });
          }
          catch (ee) {
            YAHOO.log(ee.message, 'error');
          }
        });
      }
      catch (e) {
        YAHOO.log(e.message, 'error');
      }
    });
  });
}
