Index: tinymce.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/tinymce/tinymce.module,v
retrieving revision 1.90.4.15
diff -u -r1.90.4.15 tinymce.module
--- tinymce.module	7 Feb 2007 12:33:48 -0000	1.90.4.15
+++ tinymce.module	19 Feb 2007 00:39:51 -0000
@@ -57,173 +57,133 @@
 }
 
 /**
- * Implementation of hook_elements().
- */
-function tinymce_elements() {
-  $type = array();
-
-  if (user_access('access tinymce')) {
-    // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
-    $type['textarea'] = array('#process' => array('tinymce_process_textarea' => array()), '#resizable' => TRUE);
-  }
-
-  return $type;
-}
-
-/**
- * Attach tinymce to a textarea
+ * Implementation of hook_footer().
+ *
+ * Send configuration data after all textareas have been processed.
  */
-function tinymce_process_textarea($element) {
-  static $is_running = FALSE;
+function tinymce_footer() {
   global $user;
-  static $profile_name;
-  
-  //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
-  $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
-
-  // Since tinymce_config() makes a db hit, only call it when we're pretty sure
-  // we're gonna render tinymce.
-  if (!$profile_name) {
-    $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($user->roles))));
-  }
-  $profile = tinymce_profile_load($profile_name);
-  $init = tinymce_config($profile);
-  $init['elements'] = 'edit-'. $textarea_name;
-
-  if (_tinymce_page_match($profile)) {
-    // Merge user-defined TinyMCE settings.
-    $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
-
-    // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
-    if (count($init) < 1) {
-      // we set this textarea to use drupal resizable since tinymce won't be controling this textarea
-      $element['#resizable'] = TRUE;
-      return $element;
-    }
-
-    $settings = array();
-    foreach ($init as $k => $v) {
-      $v = is_array($v) ? implode(',', $v) : $v;
-      // Don't wrap the JS init in quotes for boolean values or functions.
-      if (strtolower($v) != 'true' && strtolower($v) != 'false' && $v[0] != '{') {
-        $v = '"'. $v. '"';
+  static $loaded = FALSE;
+  // We only load the TinyMCE js file once per page request.
+  if (!$loaded) {
+    $profile = tinymce_profile_load(tinymce_current_profile());
+    $themes = _tinymce_get_themes();
+    $configs = array();
+    foreach ($themes as $theme) {
+      $config = tinymce_config($profile, $theme);
+      // Convert the config values into the form expected by tinyMCE.
+      foreach ($config as $key => $value) {
+        switch($value) {
+          case 'true':
+            $config[$key] = TRUE;
+            break;
+          case 'false':
+            $config[$key] = FALSE;
+            break;
+        }
+        if (is_array($value)) {
+          $config[$key] = implode(',', $config[$key]);
+        }
       }
-      $settings[] = $k. ' : '. $v;
+      $configs[$theme] = $config;
     }
-    $tinymce_settings = implode(",\n    ", $settings);
 
     $enable  = t('enable rich-text');
     $disable = t('disable rich-text');
 
-$tinymce_invoke = <<<EOD
+    $status = tinymce_user_get_status($user, $profile);
 
-  tinyMCE.init({
-    $tinymce_settings
-  });
-
-EOD;
-
-$js_toggle = <<<EOD
-
-  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();
+    $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
+
+    $tinymce_mod_path = drupal_get_path('module', 'tinymce');
+
+    if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager') ) {
+      // if tinymce imagemanager is installed
+      drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/jscripts/mcimagemanager.js');
     }
-    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();
+
+    if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager') ) {
+      // if tinymce filemanager is installed
+      drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/jscripts/mcfilemanager.js');
     }
-  }
 
-EOD;
-
-$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;
-    } 
+    // TinyMCE Compressor
+    if (file_exists($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
+      drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
+    }
     else {
-      img_assist.innerHTML = tinyMCE.getEditorId('edit-$textarea_name') == null ? img_assist_default_link : '';
+      // 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_add_js($tinymce_mod_path .'/tinymce.js', 'footer');
+    drupal_add_js(array('tinymce' => array('configs' => $configs, 'showToggle' => ($profile->settings['show_toggle'] == 'true'), 'disable' => $disable, 'enable' => $enable, 'noWysiwyg' => $no_wysiwyg, 'linkText' => $link_text, 'status' => $status)), '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>');
+    $loaded = TRUE;
   }
-  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>");
+}
+
+/**
+ * Implementation of hook_elements().
+ */
+function tinymce_elements() {
+  $type = array();
+
+  if (user_access('access tinymce')) {
+    // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
+    $type['textarea'] = array('#process' => array('tinymce_process_textarea' => array()), '#wysiwyg' => TRUE, '#wysiwyg_style' => 'advanced');
   }
-</script>
-EOD;
 
-    // We only load the TinyMCE js file once per request
-    if (!$is_running) {
-      $is_running = TRUE;
-      $tinymce_mod_path = drupal_get_path('module', 'tinymce');
-      
-      if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager') ) {
-        // if tinymce imagemanager is installed
-        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/jscripts/mcimagemanager.js');
-      }
-      
-      if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager') ) {
-        // if tinymce filemanager is installed
-        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/jscripts/mcfilemanager.js');
+  return $type;
+}
+
+/**
+ * Register a tinymce textarea.
+ */
+function tinymce_process_textarea($element) {
+  tinymce_filter_elements($element);
+
+  if ($element['#wysiwyg']) {
+    $profile = tinymce_profile_load(tinymce_current_profile());
+    if (_tinymce_page_match($profile)) {
+      _tinymce_set_theme($element['#wysiwyg_style']);
+      if (!array_key_exists('#attributes', $element)) {
+        $element['#attributes'] = array();
       }
-      
-      // TinyMCE Compressor
-      if (file_exists($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
-        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
+      if (!array_key_exists('class', $element['#attributes'])) {
+        $element['#attributes']['class'] = 'tinymce tinymce-'. $element['#wysiwyg_style'];
       }
       else {
-        // 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');
+        $element['#attributes']['class'] .= ' tinymce tinymce-'. $element['#wysiwyg_style'];
       }
-      drupal_add_js($js_toggle, 'inline');
-      // 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_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;
+      $element['#resizable'] = FALSE;
     }
-  }  
-  else {
-    $element['#resizable'] = TRUE;
   }
 
   return $element;
 }
 
 /**
+ * Register a theme.
+ */
+function _tinymce_set_theme($theme = NULL) {
+  static $themes = array();
+  if ($theme && !in_array($theme, $themes)) {
+    $themes[] = $theme;
+  }
+  else {
+    return $themes;
+  }
+}
+
+/**
+ * Get the registered themes.
+ */
+function _tinymce_get_themes() {
+  return _tinymce_set_theme();
+}
+
+/**
  * Implementation of hook_user().
  */
 function tinymce_user($type, &$edit, &$user, $category = NULL) {
@@ -257,80 +217,38 @@
 }
 
 /**
- * @addtogroup themeable
- * @{
- */
-
-/**
- * Customize a TinyMCE theme.
- *
- * @param init
- *   An array of settings TinyMCE should invoke a theme. You may override any
- *   of the TinyMCE settings. Details here:
- *
- *    http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
- *
- * @param textarea_name
- *   The name of the textarea TinyMCE wants to enable.
+ * Filter for core text fields.
  *
- * @param theme_name
- *   The default tinymce theme name to be enabled for this textarea. The
- *   sitewide default is 'simple', but the user may also override this.
- *
- * @param is_running
- *   A boolean flag that identifies id TinyMCE is currently running for this
- *   request life cycle. It can be ignored.
- */
-function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
-  switch ($textarea_name) {
-    // Disable tinymce for these textareas
+ * @param $element
+ *   The textarea element to be filtered.
+ */
+function tinymce_filter_elements(&$element) {
+  switch ($element['#name']) {
+    // Disable tinymce for these textareas.
     case 'log': // book and page log
-    case 'img_assist_pages':
     case 'caption': // signature
     case 'pages':
-    case 'access_pages': //TinyMCE profile settings.
     case 'user_mail_welcome_body': // user config settings
     case 'user_mail_approval_body': // user config settings
     case 'user_mail_pass_body': // user config settings
     case 'synonyms': // taxonomy terms
     case 'description': // taxonomy terms
-      unset($init);
+      $element['#wysiwyg'] = FALSE;
       break;
 
     // Force the 'simple' theme for some of the smaller textareas.
     case 'signature':
     case 'site_mission':
-    case 'site_footer':
+    // case 'site_footer':
     case 'site_offline_message':
     case 'page_help':
     case 'user_registration_help':
     case 'user_picture_guidelines':
-      $init['theme'] = 'simple';
-      foreach ($init as $k => $v) {
-        if (strstr($k, 'theme_advanced_')) unset($init[$k]);
-      }
+      $element['#wysiwyg_style'] = 'simple';
       break;
   }
-
-  /* Example, add some extra features when using the advanced theme.
-  
-  // If $init is available, we can extend it
-  if (isset($init)) {
-    switch ($theme_name) {
-     case 'advanced':
-       $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
-       break;
-    }
-  }
-  
-  */
-
-  // Always return $init
-  return $init;
 }
 
-/** @} End of addtogroup themeable */
-
 /**
  * Grab the themes available to TinyMCE.
  *
@@ -342,7 +260,7 @@
  * @return
  *   An array of theme names.
  */
-function _tinymce_get_themes() {
+function _tinymce_get_installed_themes() {
   static $themes = array();
 
   if (!$themes) {
@@ -473,9 +391,9 @@
 /**
  * Return an array of initial tinymce config options from the current role.
  */
-function tinymce_config($profile) {
+function tinymce_config($profile, $theme) {
   global $user;
-  
+
   // Drupal theme path.
   $themepath = path_to_theme() . '/';
   $host = base_path();
@@ -484,11 +402,9 @@
 
   // Build a default list of TinyMCE settings.
 
-  // Is tinymce on by default?
-  $status = tinymce_user_get_status($user, $profile);
-  
-  $init['mode']               = $status == 'true' ? 'exact' : 'none';
-  $init['theme']              = $settings['theme'] ? $settings['theme'] : 'advanced';
+  $init = array();
+  $init['mode']               = 'none';
+  $init['theme']              = $theme;
   $init['relative_urls']      = 'false';
   $init['document_base_url']  = "$host";
   $init['language']           = $settings['language'] ? $settings['language'] : 'en';
@@ -497,9 +413,6 @@
   $init['verify_html']        = $settings['verify_html'] ? $settings['verify_html'] : 'false';
   $init['preformatted']       = $settings['preformatted'] ? $settings['preformatted'] : 'false';
   $init['convert_fonts_to_styles'] = $settings['convert_fonts_to_styles'] ? $settings['convert_fonts_to_styles'] : 'false';
-  $init['theme_advanced_resize_horizontal'] = 'false';
-  $init['theme_advanced_resizing_use_cookie'] = 'false';
-  
 
   $tinymce_mod_path = drupal_get_path('module', 'tinymce');
   if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager')) {
@@ -510,107 +423,113 @@
     // we probably need more security than this
     $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
   }
-  
-  if ($init['theme'] == 'advanced') {
-    $init['plugins'] = array();
-    $init['theme_advanced_toolbar_location']  = $settings['toolbar_loc'] ? $settings['toolbar_loc'] : 'bottom';
-    $init['theme_advanced_toolbar_align']     = $settings['toolbar_align'] ? $settings['toolbar_align'] : 'left';
-    $init['theme_advanced_path_location']     = $settings['path_loc'] ? $settings['path_loc'] : 'bottom';
-    $init['theme_advanced_resizing']          = $settings['resizing'] ? $settings['resizing'] : 'true';
-    $init['theme_advanced_blockformats']      = $settings['block_formats'] ? $settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
-
-    if (is_array($settings['buttons'])) {
-      // This gives us the $plugins variable.
-      $plugins = _tinymce_get_buttons();
-
-      // Find the enabled buttons and the mce row they belong on. Also map the
-      // plugin metadata for each button.
-      $plugin_tracker = array();
-      foreach ($plugins as $rname => $rplugin) { // Plugin name
-        foreach ($rplugin as $mce_key => $mce_value) { // TinyMCE key
-          foreach ($mce_value as $k => $v) { // Buttons
-            if ($settings['buttons'][$rname . '-' . $v]) {
-              // Font isn't a true plugin, rather it's buttons made available by the advanced theme
-              if (!in_array($rname, $plugin_tracker) && $rname != 'font') $plugin_tracker[] = $rname;
-              $init[$mce_key][] = $v;
+
+  if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
+
+  if ($settings['css_setting'] == 'theme') {
+    $css = $themepath . 'style.css';
+    if (file_exists($css)) {
+      $init['content_css'] = $host . $css;
+    }
+  }
+  else if ($settings['css_setting'] == 'self') {
+    $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
+  }
+
+  // Add theme-specific settings.
+  switch ($theme) {
+    case 'advanced':
+
+      $init['theme_advanced_resize_horizontal'] = 'false';
+      $init['theme_advanced_resizing_use_cookie'] = 'false';
+      $init['plugins'] = array();
+      $init['theme_advanced_toolbar_location']  = $settings['toolbar_loc'] ? $settings['toolbar_loc'] : 'bottom';
+      $init['theme_advanced_toolbar_align']     = $settings['toolbar_align'] ? $settings['toolbar_align'] : 'left';
+      $init['theme_advanced_path_location']     = $settings['path_loc'] ? $settings['path_loc'] : 'bottom';
+      $init['theme_advanced_resizing']          = $settings['resizing'] ? $settings['resizing'] : 'true';
+      $init['theme_advanced_blockformats']      = $settings['block_formats'] ? $settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
+
+      if (is_array($settings['buttons'])) {
+        // This gives us the $plugins variable.
+        $plugins = _tinymce_get_buttons();
+
+        // Find the enabled buttons and the mce row they belong on. Also map the
+        // plugin metadata for each button.
+        $plugin_tracker = array();
+        foreach ($plugins as $rname => $rplugin) { // Plugin name
+          foreach ($rplugin as $mce_key => $mce_value) { // TinyMCE key
+            foreach ($mce_value as $k => $v) { // Buttons
+              if ($settings['buttons'][$rname . '-' . $v]) {
+                // Font isn't a true plugin, rather it's buttons made available by the advanced theme
+                if (!in_array($rname, $plugin_tracker) && $rname != 'font') $plugin_tracker[] = $rname;
+                $init[$mce_key][] = $v;
+              }
             }
           }
+          // Some advanced plugins only have an $rname and no buttons
+          if ($settings['buttons'][$rname]) {
+            if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
+          }
         }
-        // Some advanced plugins only have an $rname and no buttons
-        if ($settings['buttons'][$rname]) {
-          if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
-        }
-      }
 
-      // Add the rest of the TinyMCE config options to the $init array for each button.
-      if (is_array($plugin_tracker)) {
-        foreach ($plugin_tracker as $pname) {
-          if ($pname != 'default') $init['plugins'][] = $pname;
-          foreach ($plugins[$pname] as $mce_key => $mce_value) {
-            // Don't overwrite buttons or extended_valid_elements
-            if ($mce_key == 'extended_valid_elements') {
-              // $mce_value is an array for extended_valid_elements so just grab the first element in the array (never more than one)
-              $init[$mce_key][] = $mce_value[0];
-            }
-            else if (!strstr($mce_key, 'theme_advanced_buttons')) {
-              $init[$mce_key] = $mce_value;
+        // Add the rest of the TinyMCE config options to the $init array for each button.
+        if (is_array($plugin_tracker)) {
+          foreach ($plugin_tracker as $pname) {
+            if ($pname != 'default') $init['plugins'][] = $pname;
+            foreach ($plugins[$pname] as $mce_key => $mce_value) {
+              // Don't overwrite buttons or extended_valid_elements
+              if ($mce_key == 'extended_valid_elements') {
+                // $mce_value is an array for extended_valid_elements so just grab the first element in the array (never more than one)
+                $init[$mce_key][] = $mce_value[0];
+              }
+              else if (!strstr($mce_key, 'theme_advanced_buttons')) {
+                $init[$mce_key] = $mce_value;
+              }
             }
           }
         }
-      }
 
-      // Cleanup
-      foreach ($init as $mce_key => $mce_value) {
-        if (is_array($mce_value)) $mce_value = array_unique($mce_value);
-        $init[$mce_key] = $mce_value;
-      }
+        // Cleanup
+        foreach ($init as $mce_key => $mce_value) {
+          if (is_array($mce_value)) $mce_value = array_unique($mce_value);
+          $init[$mce_key] = $mce_value;
+        }
 
-      // Shuffle buttons around so that row 1 always has the most buttons,
-      // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
-      // TinyMCE loads it's own buttons inherited from the theme.
-      if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
-      if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
-      if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
-
-      $min_btns = 5; // Minimum number of buttons per row.
-      $num1 = count($init['theme_advanced_buttons1']);
-      $num2 = count($init['theme_advanced_buttons2']);
-      $num3 = count($init['theme_advanced_buttons3']);
-
-      if ($num3 < $min_btns) {
-        $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
-        $init['theme_advanced_buttons3'] = array();
-        $num2 = count($init['theme_advanced_buttons2']);
-      }
-      if ($num2 < $min_btns) {
-        $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
-        // Squish the rows together, since row 2 is empty
-        $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
-        $init['theme_advanced_buttons3'] = array();
+        // Shuffle buttons around so that row 1 always has the most buttons,
+        // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
+        // TinyMCE loads its own buttons inherited from the theme.
+        if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
+        if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
+        if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
+
+        $min_btns = 5; // Minimum number of buttons per row.
         $num1 = count($init['theme_advanced_buttons1']);
-      }
-      if ($num1 < $min_btns) {
-        $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
-        // Squish the rows together, since row 2 is empty
-        $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
-        $init['theme_advanced_buttons3'] = array();
-      }
+        $num2 = count($init['theme_advanced_buttons2']);
+        $num3 = count($init['theme_advanced_buttons3']);
 
-    }
-  }
+        if ($num3 < $min_btns) {
+          $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
+          $init['theme_advanced_buttons3'] = array();
+          $num2 = count($init['theme_advanced_buttons2']);
+        }
+        if ($num2 < $min_btns) {
+          $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
+          // Squish the rows together, since row 2 is empty
+          $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
+          $init['theme_advanced_buttons3'] = array();
+          $num1 = count($init['theme_advanced_buttons1']);
+        }
+        if ($num1 < $min_btns) {
+          $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
+          // Squish the rows together, since row 2 is empty
+          $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
+          $init['theme_advanced_buttons3'] = array();
+        }
 
-  if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
+      }
 
-  if ($settings['css_setting'] == 'theme') {
-    $css = $themepath . 'style.css';
-    if (file_exists($css)) {
-      $init['content_css'] = $host . $css;
-    }
-  }
-  else if ($settings['css_setting'] == 'self') {
-    $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
+      break;
   }
-
   return $init;
 }
 
@@ -747,7 +666,8 @@
     );
     $form['visibility']['access_pages'] = array(
       '#type' => 'value', 
-      '#value' => $edit->settings['access_pages']
+      '#value' => $edit->settings['access_pages'],
+      '#wysiwyg' => FALSE,
     );
   }
   else {
@@ -982,6 +902,18 @@
 }
 
 /**
+ * Return the name of the current user's default profile.
+ */
+function tinymce_current_profile() {
+  static $profile_name;
+  if (!$profile_name) {
+    global $user;
+    $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($user->roles))));
+  }
+  return $profile_name;
+}
+
+/**
  * Load all profiles. Just load one profile if $name is passed in.
  */
 function tinymce_profile_load($name = '') {
@@ -1129,5 +1061,5 @@
     $status = isset($settings['default']) ? $settings['default'] : 'false';
   }
 
-  return $status;
+  return $status == 'true' ? TRUE : FALSE;
 }
--- modules/tinymce/tinymce.js+++ modules/tinymce/tinymce.js
@@ -0,0 +1,113 @@+// $Id: Exp $
+
+/**
+ * Initialize each tinyMCE configuration.
+ *
+ * This function needs to be called before the page is fully loaded, as
+ * calling tinyMCE.init() after the page is loaded breaks in IE 6.
+ */
+Drupal.tinymceInit = function () {
+  for (var theme in Drupal.settings.tinymce.configs) {
+    // Clone so we are not passing by reference. Otherwise the
+    // settings will get overwritten.
+    var config = Drupal.tinymceCloneObject(Drupal.settings.tinymce.configs[theme]);
+    tinyMCE.init(config);
+  }
+}
+
+/**
+ * Attach tinyMCE to textareas.
+ *
+ * This function can be called to process AJAX-loaded content.
+ */
+Drupal.tinymceAttach = function () {
+  for (var theme in Drupal.settings.tinymce.configs) {
+    var config = Drupal.tinymceCloneObject(Drupal.settings.tinymce.configs[theme]);
+    // Set configuration options for this theme.
+    for (var setting in config) {
+      tinyMCE.settings[setting] = config[setting];
+    }
+    $('textarea.tinymce-' + theme + ':not(.tinymce-processed)').each(function () {
+      // Show toggle link if set.
+      if (Drupal.settings.tinymce.showToggle) {
+        Drupal.tinymceAttachToggleLink(this, theme);
+      }
+      // Attach tinyMCE control if default is on.
+      if (Drupal.settings.tinymce.status) {
+        tinyMCE.execCommand('mceAddControl', true, this.id);
+      }
+      $(this).addClass('tinymce-processed');
+    });
+  }
+}
+
+/**
+ * Toggle the tinyMCE control and related link text for a textarea.
+ */
+Drupal.tinymceToggle = function (id, theme) {
+  if (tinyMCE.getEditorId(id) == null) {
+    var config = Drupal.tinymceCloneObject(Drupal.settings.tinymce.configs[theme]);
+    // Set configuration options for this theme.
+    for (var setting in config) {
+      tinyMCE.settings[setting] = config[setting];
+    }
+    tinyMCE.addMCEControl($('#' + id).get(0), id);
+    $('#wysiwyg4' + id)
+      .html(Drupal.settings.tinymce.disable)
+      .blur();
+  }
+  else {
+    tinyMCE.removeMCEControl(tinyMCE.getEditorId(id));
+    $('#wysiwyg4' + id)
+      .html(Drupal.settings.tinymce.enable)
+      .blur();
+  }
+}
+
+/**
+ * Append toggle link to textarea.
+ */
+Drupal.tinymceAttachToggleLink = function (elt, theme) {
+  if (typeof(document.execCommand) == 'undefined') {
+    $(elt).after('<div style="font-size:x-small">' + Drupal.settings.tinymce.noWysiwyg + '</div>');
+  }
+  else {
+    var text = document.createTextNode(Drupal.settings.tinymce.status ? Drupal.settings.tinymce.disable : Drupal.settings.tinymce.enable);
+    var a = document.createElement('a');
+    $(a)
+      .click(function() {
+        Drupal.tinymceToggle(elt.id, theme);
+      })
+      .attr('id', 'wysiwyg4' + elt.id)
+      .css('cursor', 'pointer')
+      .append(text);
+    var div = document.createElement('div');
+    $(div).append(a);
+    $(elt).after(div);
+  }
+}
+
+Drupal.tinymceCloneObject = function (obj) {
+  var clone = {};
+  for (i in obj) {
+    if ((typeof obj[i] == 'object') || (typeof obj[i] == 'array')) {
+      clone[i] = Drupal.tinymceCloneObject(obj[i]);
+    }
+    else {
+      clone[i] = obj[i];
+    }
+  }
+  return clone;
+}
+
+/**
+ * Global killswitch.
+ */
+if (Drupal.jsEnabled) {
+  $(document).ready(Drupal.tinymceAttach);
+}
+
+/**
+ * Call tinyMCE initialization.
+ */
+Drupal.tinymceInit();


