Index: tinymce.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tinymce/tinymce.module,v
retrieving revision 1.90
diff -u -r1.90 tinymce.module
--- tinymce.module	25 Apr 2006 11:23:47 -0000	1.90
+++ tinymce.module	13 Feb 2007 19:45:23 -0000
@@ -117,69 +117,19 @@
     $disable = t('disable rich-text');
 
 $tinymce_invoke = <<<EOD
-<script type="text/javascript">
+
   tinyMCE.init({
     $tinymce_settings
   });
-</script>
-EOD;
 
-$js_toggle = <<<EOD
-<script type="text/javascript">
-  function mceToggle(id, linkid) {
-    element = document.getElementById(id);
-    link = document.getElementById(linkid);
-    img_assist = document.getElementById('img_assist-link-'+ id);
-
-    if (tinyMCE.getEditorId(element.id) == null) {
-      tinyMCE.addMCEControl(element, element.id);
-      element.togg = 'on';
-      link.innerHTML = '$disable';
-      link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
-      if (img_assist)
-        img_assist.innerHTML = '';
-      link.blur();
-    }
-    else {
-      tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.id));
-      element.togg = 'off';
-      link.innerHTML = '$enable';
-      link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
-      if (img_assist)
-        img_assist.innerHTML = img_assist_default_link;
-      link.blur();
-    }
-  }
-</script>
 EOD;
 
-$status = tinymce_user_get_status($user, $profile);
+    $status = tinymce_user_get_status($user, $profile);
 
-// note we test for string == true because we save our settings as strings
-$link_text = $status == 'true' ? $disable : $enable;
-$img_assist_link = ($status == 'true') ? 'yes' : 'no';
-$no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
-$wysiwyg_link = <<<EOD
-<script type="text/javascript">
-  img_assist = document.getElementById('img_assist-link-edit-$textarea_name');
-  if (img_assist) {
-    var img_assist_default_link = img_assist.innerHTML;
-    if ('$img_assist_link' == 'yes') {
-      img_assist.innerHTML = tinyMCE.getEditorId('edit-$textarea_name') == null ? '' : img_assist_default_link;
-    } 
-    else {
-      img_assist.innerHTML = tinyMCE.getEditorId('edit-$textarea_name') == null ? img_assist_default_link : '';
-    }
-  }
-  if (typeof(document.execCommand) == 'undefined') {
-    img_assist.innerHTML = img_assist_default_link;
-    document.write('<div style="font-size:x-small">$no_wysiwyg</div>');
-  }
-  else {
-    document.write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</a></div>");
-  }
-</script>
-EOD;
+    // note we test for string == true because we save our settings as strings
+    $link_text = $status == 'true' ? $disable : $enable;
+    $img_assist_link = ($status == 'true') ? 'yes' : 'no';
+    $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
 
     // We only load the TinyMCE js file once per request
     if (!$is_running) {
@@ -204,17 +154,24 @@
         // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
         drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce.js');
       }
-      drupal_set_html_head($js_toggle);
+      drupal_add_js($tinymce_mod_path .'/tinymce.js');
+      drupal_add_js(array('tinymce' => array('disable' => $disable, 'enable' => $enable, 'noWysiwyg' => $no_wysiwyg, 'linkText' => $link_text)), 'setting');
       // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
       drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
     }
     // Load a TinyMCE init for each textarea.
-    if ($init) drupal_set_html_head($tinymce_invoke);
+    if ($init) drupal_add_js($tinymce_invoke, 'inline');
 
     //settings are saved as strings, not booleans
     if ($profile->settings['show_toggle'] == 'true') {
-      // Make sure to append to #suffix so it isn't completely overwritten
-      $element['#suffix'] .= $wysiwyg_link;
+      $toggle_js = "
+if (Drupal.jsEnabled) {
+  $(document).ready(function() {
+    Drupal.tinymceToggleLink('$textarea_name', '#". $element['#id'] ."', '$img_assist_link');
+  });
+}
+";
+      drupal_add_js($toggle_js, 'inline');
     }
   }  
   else {
--- modules/tinymce/tinymce.js
+++ modules/tinymce/tinymce.js
@@ -0,0 +1,69 @@
+Drupal.tinymceToggle = function (id, linkId) {
+  var element = $('#' + id).length ? $('#' + id).get(0) : null;
+  var link = $('#' + linkId).length ? $('#' + linkId).get(0) : null;
+  var img_assist = $('#img_assist-link-'+ id).length ? $('#img_assist-link-'+ id).get(0) : null;
+
+  if (tinyMCE.getEditorId(element.id) == null) {
+    tinyMCE.addMCEControl(element, element.id);
+    element.togg = 'on';
+    if (img_assist)
+      $(img_assist).html('');
+    $(link)
+      .html(Drupal.settings.tinymce.disable)
+      .unclick()
+      .click(function () {
+        Drupal.tinymceToggle(id, linkId);
+      })
+      .blur();
+  }
+  else {
+    tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.id));
+    element.togg = 'off';
+    if (img_assist)
+      $(img_assist).each(function () {
+        $(this).html(this.cached);
+      });
+    $(link)
+      .html(Drupal.settings.tinymce.enable)
+      .unclick()
+      .click(function () {
+        Drupal.tinymceToggle(id, linkId);
+      })
+      .blur();
+  }
+}
+
+Drupal.tinymceToggleLink = function (textareaName, selector, imgAssistLink) {
+  if ($('img_assist-link-edit-' + textareaName).length) {
+    $('img_assist-link-edit-' + textareaName).each(function () {
+      this.cached = $(this).html();
+      if (imgAssistLink == 'yes') {
+        $(this).html(tinyMCE.getEditorId(textareaName) == null ? '' : this.cached);
+      } 
+      else {
+        $(this).html(tinyMCE.getEditorId('edit-' + textareaName) == null ? this.cached : '');
+      }
+    });
+  }
+  if (typeof(document.execCommand) == 'undefined') {
+    $('img_assist-link-edit-' + textareaName).each(function() {
+      $(this).html(this.cached);
+    });
+    $(selector).after('<div style="font-size:x-small">' + Drupal.settings.tinymce.noWysiwyg + '</div>');
+  }
+  else {
+    var text = document.createTextNode(Drupal.settings.tinymce.linkText);
+    var a = document.createElement('a');
+    $(a)
+      .click(function() {
+        Drupal.tinymceToggle('edit-' + textareaName, 'wysiwyg4' + textareaName);
+      })
+      .attr('id', 'wysiwyg4' + textareaName)
+      .append(text);
+    var div = document.createElement('div');
+    $(div).append(a);
+    $(selector + ':not(.tinymce-processed)')
+      .after(div)
+      .addClass('tinymce-processed');
+  }
+}
