Index: wysiwyg.init.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wysiwyg/wysiwyg.init.js,v
retrieving revision 1.1
diff -u -p -r1.1 wysiwyg.init.js
--- wysiwyg.init.js	11 Oct 2008 19:54:24 -0000	1.1
+++ wysiwyg.init.js	30 Nov 2008 15:58:32 -0000
@@ -1,6 +1,6 @@
 // $Id: wysiwyg.init.js,v 1.1 2008/10/11 19:54:24 sun Exp $
 
-Drupal.wysiwyg = Drupal.wysiwyg || {};
+Drupal.wysiwyg = Drupal.wysiwyg || { 'instances': {} };
 
 Drupal.wysiwyg.editor = Drupal.wysiwyg.editor || { 'init': {}, 'attach': {}, 'detach': {} };
 
Index: wysiwyg.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/wysiwyg/wysiwyg.js,v
retrieving revision 1.2
diff -u -p -r1.2 wysiwyg.js
--- wysiwyg.js	30 Nov 2008 15:48:32 -0000	1.2
+++ wysiwyg.js	30 Nov 2008 17:08:06 -0000
@@ -34,19 +34,7 @@ Drupal.wysiwygInit = function() {
  */
 Drupal.behaviors.attachWysiwyg = function(context) {
   $('.wysiwyg:not(.wysiwyg-processed)', context).each(function() {
-    // Parse the element's CSS classes into parameters.
-    // Format is wysiwyg-name-value.
-    var classes = this.className.split(' ');
-    var params = {};
-    for (var i in classes) {
-      if (classes[i].substr(0, 8) == 'wysiwyg-') {
-        var parts = classes[i].split('-');
-        var value = parts.slice(2).join('-');
-        params[parts[1]] = value;
-      }
-    }
-    // Convert format id to string.
-    params.format = 'format' + params.format;
+    var params = Drupal.wysiwyg.getParams(this);
     $this = $(this);
     // Directly attach this editor, if the input format is enabled or there is
     // only one input format at all.
@@ -55,12 +43,13 @@ Drupal.behaviors.attachWysiwyg = functio
       Drupal.wysiwygAttach(context, params);
     }
     // Attach onChange handlers to input format selector elements.
-    // @todo To support different editors on the same page, we need to store
-    //   the last attached editor of each target element separately.
     if ($this.is(':input')) {
       $this.change(function() {
-        Drupal.wysiwygDetach(context, params);
-        Drupal.wysiwygAttach(context, params);
+        // If not disabled, detach the current and attach a new editor.
+        if (Drupal.wysiwyg.instances[params.field].status) {
+          Drupal.wysiwygDetach(context, params);
+          Drupal.wysiwygAttach(context, params);
+        }
       });
     }
     $this.addClass('wysiwyg-processed');
@@ -84,6 +73,11 @@ Drupal.wysiwygAttach = function(context,
   if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
     // Attach editor.
     Drupal.wysiwyg.editor.attach[params.editor](context, params, (Drupal.settings.wysiwyg.configs[params.editor] ? Drupal.wysiwyg.clone(Drupal.settings.wysiwyg.configs[params.editor][params.format]) : {}));
+    // (Re-)initialize field instance.
+    Drupal.wysiwyg.instances[params.field] = {};
+    // Store new editor name and status for this field.
+    Drupal.wysiwyg.instances[params.field].editor = params.editor;
+    Drupal.wysiwyg.instances[params.field].status = true;
     // Display toggle link.
     $('#wysiwyg-toggle-' + params.field).show();
   }
@@ -96,19 +90,16 @@ Drupal.wysiwygAttach = function(context,
 /**
  * Detach all editors from a target element.
  *
- * Until there is a central registry of target elements storing the currently
- * attached editor, we simply invoke the detach hook of all editors to ensure
- * that no editor is attached to the target element.
- *
  * @param context
  *   A DOM element, supplied by Drupal.attachBehaviors().
  * @param params
  *   An object containing input format parameters.
  */
 Drupal.wysiwygDetach = function(context, params) {
-  jQuery.each(Drupal.wysiwyg.editor.detach, function(editor) {
-    this(context, params);
-  });
+  var editor = Drupal.wysiwyg.instances[params.field].editor;
+  if (jQuery.isFunction(Drupal.wysiwyg.editor.detach[editor])) {
+    Drupal.wysiwyg.editor.detach[editor](context, params);
+  }
 }
 
 /**
@@ -122,19 +113,22 @@ Drupal.wysiwygDetach = function(context,
 Drupal.wysiwygAttachToggleLink = function(context, params) {
   var text = document.createTextNode(Drupal.settings.wysiwyg.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
   var a = document.createElement('a');
-  $(a).toggle(
-    function() {
+  $(a).click(function() {
+    if (Drupal.wysiwyg.instances[params.field].status) {
       Drupal.wysiwygDetach(context, params);
+      Drupal.wysiwyg.instances[params.field].status = false;
       $('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.enable).blur();
       // After disabling the editor, re-attach default behaviors.
       Drupal.wysiwyg.editor.attach.none(context, params);
-    },
-    function() {
+    }
+    else {
       // Before enabling the editor, detach default behaviors.
       Drupal.wysiwyg.editor.detach.none(context, params);
-      Drupal.wysiwygAttach(context, params);
+      // Attach the editor using parameters of the currently selected input format.
+      Drupal.wysiwygAttach(context, Drupal.wysiwyg.getParams($('.wysiwyg-field-' + params.field + ':checked', context).get(0)));
       $('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.disable).blur();
-    })
+    }
+  })
     .attr('id', 'wysiwyg-toggle-' + params.field)
     .attr('href', 'javascript:void(0);')
     .append(text);
@@ -144,6 +138,26 @@ Drupal.wysiwygAttachToggleLink = functio
 }
 
 /**
+ * Parse the CSS classes of an input format DOM element into parameters.
+ *
+ * Format is "wysiwyg-name-value".
+ */
+Drupal.wysiwyg.getParams = function(element) {
+  var classes = element.className.split(' ');
+  var params = {};
+  for (var i in classes) {
+    if (classes[i].substr(0, 8) == 'wysiwyg-') {
+      var parts = classes[i].split('-');
+      var value = parts.slice(2).join('-');
+      params[parts[1]] = value;
+    }
+  }
+  // Convert format id into string.
+  params.format = 'format' + params.format;
+  return params;
+}
+
+/**
  * Clone a configuration object recursively.
  *
  * @param obj
