=== modified file 'plugins/bidi.inc'
--- plugins/bidi.inc	2009-02-27 15:39:44 +0000
+++ plugins/bidi.inc	2009-02-27 17:10:17 +0000
@@ -9,7 +9,7 @@
 
 function yui_editor_bidi_render(&$profile) {
   if ($profile['bidi'] == 1) {
-    drupal_add_js(drupal_get_path("module", "yui_editor") ."/plugins/bidi.js", 'module', 'footer');
-    drupal_add_css(drupal_get_path("module", "yui_editor") ."/plugins/bidi.css", 'module');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/bidi.js');
+    $profile['plugin_css'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/bidi.css');
   }  
 }

=== modified file 'plugins/coder.inc'
--- plugins/coder.inc	2009-02-27 15:39:44 +0000
+++ plugins/coder.inc	2009-02-27 17:10:17 +0000
@@ -17,7 +17,7 @@
 
 function yui_editor_coder_render(&$profile) {
   if ($profile['coder'] == 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/coder.js', 'module', 'footer');
-    drupal_add_css(drupal_get_path('module', 'yui_editor') .'/plugins/coder.css');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/coder.js');
+    $profile['plugin_css'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/coder.css');
   }
 }

=== modified file 'plugins/flickr.inc'
--- plugins/flickr.inc	2009-02-27 15:39:44 +0000
+++ plugins/flickr.inc	2009-02-27 17:10:17 +0000
@@ -62,7 +62,7 @@
 
 function yui_editor_flickr_render(&$profile) {
   if ($profile['flickr'] == 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/flickr.js', 'module', 'footer');
-    drupal_add_css(drupal_get_path('module', 'yui_editor') .'/plugins/flickr.css');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/flickr.js');
+    $profile['plugin_css'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/flickr.css');
   }
 }

=== modified file 'plugins/img_browser.inc'
--- plugins/img_browser.inc	2009-02-27 15:39:44 +0000
+++ plugins/img_browser.inc	2009-02-27 17:10:17 +0000
@@ -19,7 +19,7 @@
   $has_access = module_exists('imce') && function_exists('imce_access') && imce_access();
   $profile['img_browser'] = ($has_access ? $profile['img_browser'] : 0);
   if ($profile['img_browser'] == 1) {
-    drupal_add_js(drupal_get_path("module", "yui_editor")."/plugins/img_browser.js", 'module', 'footer');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor').'/plugins/img_browser.js');
   }
 }
 
@@ -43,4 +43,4 @@
     '#title' => t('Image Browser'),
     '#default_value' => $profile['img_browser'],
     '#description' => t('Allow users to browse images directly from the editor for insertion into the editor. Note: IMCE module must be installed and configured for this to work. Works only with YUI 2.5.x at present.'));
-}
\ No newline at end of file
+}

=== modified file 'plugins/img_upload.inc'
--- plugins/img_upload.inc	2009-02-27 15:39:44 +0000
+++ plugins/img_upload.inc	2009-02-27 17:10:17 +0000
@@ -52,10 +52,12 @@
   if ($profile['img_upload'] == 1) {
     $yui_source = variable_get('yui_source', 'http://yui.yahooapis.com/2.6.0');
     if (preg_match('/2.6.[0-9]$/iU', $yui_source)) {
-      drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.2.6.js', 'module', 'footer');
+      // drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.2.6.js', 'module', 'footer');
+      $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.2.6.js');
     }
     else {
-      drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.js', 'module', 'footer');
+      // drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.js', 'module', 'footer');
+      $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/img_upload.js');
     }
   }
 }

=== modified file 'plugins/plaintext.inc'
--- plugins/plaintext.inc	2009-02-27 15:39:44 +0000
+++ plugins/plaintext.inc	2009-02-27 17:10:17 +0000
@@ -17,6 +17,6 @@
 
 function yui_editor_plaintext_render(&$profile) {
   if ($profile['plaintext'] == 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/plaintext.js', 'module', 'footer');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/plaintext.js');
   }
 }

=== modified file 'plugins/resize.inc'
--- plugins/resize.inc	2009-02-27 15:39:44 +0000
+++ plugins/resize.inc	2009-02-27 17:10:17 +0000
@@ -17,6 +17,6 @@
 
 function yui_editor_resize_render(&$profile) {
   if ($profile['resize'] == 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/resize.js', 'module', 'footer');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/resize.js');
   }
 }

=== modified file 'plugins/table.inc'
--- plugins/table.inc	2009-02-27 15:39:44 +0000
+++ plugins/table.inc	2009-02-27 17:10:17 +0000
@@ -17,7 +17,7 @@
 
 function yui_editor_table_render(&$profile) {
   if (isset($profile['table']) && $profile['table'] == 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/plugins/table.js', 'module', 'footer');
-    drupal_add_css(drupal_get_path('module', 'yui_editor') .'/plugins/table.css', 'module');
+    $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/table.js');
+    $profile['plugin_css'][] = url(drupal_get_path('module', 'yui_editor') .'/plugins/table.css');
   }
 }

=== modified file 'yui_editor.module'
--- yui_editor.module	2009-02-27 15:39:44 +0000
+++ yui_editor.module	2009-02-27 17:10:17 +0000
@@ -127,19 +127,25 @@
  * Add the javascript/CSS needed to render the editor
  */
 function yui_render_editor($profile) {
-  _yui_editor_plugin('render', $profile, $profile);
-
-  yui_editor_load_libs();
-  drupal_add_css(drupal_get_path('module', 'yui_editor') .'/yui_editor.css');
-  drupal_add_js(drupal_get_path('module', 'yui_editor') .'/yui_editor.js');
-
-  if ($profile['button_profile'] != 1) {
-    drupal_add_js(drupal_get_path('module', 'yui_editor') .'/toolbars/'. $profile['button_profile']);
-  }
-  else {
-    $toolbar = "function yui_editor_toolbar() { this.toolbar = { titlebar: '', collapse: true, draggable: false, buttonType: 'advanced', buttons: [". $profile['custom'] ."] }; }";
-    drupal_add_js($toolbar, 'inline', 'footer');
-  }
+    _yui_editor_plugin('render', $profile, $profile);
+
+    yui_editor_load_libs();
+    /**
+     * Do not load any scripts or stylesheets directly, load it only if needed.
+     */
+    // drupal_add_js(drupal_get_path('module', 'yui_editor') .'/yui_editor.js');
+    // drupal_add_css(drupal_get_path('module', 'yui_editor') .'/yui_editor.css');
+    $modPath = drupal_get_path('module', 'yui_editor');
+    $files = '';
+    $profile['plugin_css'][] = url(drupal_get_path('module', 'yui_editor') .'/yui_editor.css');
+
+    if ($profile['button_profile'] != 1) {
+        $profile['plugin_js'][] = url(drupal_get_path('module', 'yui_editor') .'/toolbars/'. $profile['button_profile']);
+    }
+    else {
+        $toolbar = "function yui_editor_toolbar() { this.toolbar = { titlebar: '', collapse: true, draggable: false, buttonType: 'advanced', buttons: [". $profile['custom'] ."] }; }";
+        drupal_add_js($toolbar, 'inline', 'footer');
+    }
 
   $ids = $profile['ids'];
   $ids = str_replace(array("\r\n", "\n\r", "\r"), "\n", $ids);
@@ -161,8 +167,47 @@
   foreach ($textareas as $id) {
     $editors .= "YAHOO.Drupal.editors.push(new YAHOO.Drupal.yui_editor('$id', $settings));\n";
   }
+  
+    /**
+     * Add load commands for all javascript files needed by the YUI Editor plugins.
+     * All supplied paths should already been parsed by url().
+     */
+    foreach ($profile['plugin_js'] as $script) {
+        $files .= "jQuery.getScript('$script');\n";
+    }
+    
+    /**
+     * Add load commands for all CSS files needed by the YUI Editor plugins.
+     * All supplied paths should already been parsed by url().
+     */
+   foreach ($profile['plugin_css'] as $style) {
+        $files .= "jQuery('<link>').appendTo(jQuery('head')).attr({rel:'stylesheet',type:'text/css',href:'$style'});\n";
+    }
 
-  drupal_add_js("YAHOO.util.Event.onDOMReady(function () { $editors YAHOO.Drupal.yui_editor_load.fire();\n });", 'inline', 'footer');
+     
+    
+    /** 
+     * Use jQuery instead of YUI for event handling here, since we only want to 
+     * load YUI if we need it for the editor. Only load the editor code if one of
+     * ids exist that describe an editor's container.
+     */
+    $ids = array();
+    foreach ($textareas as $id) {
+        $ids[] = '#'.$id;
+    }
+    drupal_add_js("
+jQuery(document).ready(function() {
+    if (jQuery('".implode(',', $ids)."').size() > 0) {
+        // Drupal.loadYUI();
+        jQuery.ajaxSetup({async:false});
+        jQuery.getScript('".url($modPath.'/yui_editor.js')."');
+        $files
+        jQuery.ajaxSetup({async:true});
+        $editors
+        YAHOO.Drupal.yui_editor_load.fire();
+    }
+});", 'inline', 'footer');
+    //drupal_add_js("YAHOO.util.Event.onDOMReady(function () { $editors YAHOO.Drupal.yui_editor_load.fire();\n });", 'inline', 'footer');
 }
 
 function yui_editor_load_libs() {

