Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.550
diff -u -F^function -r1.550 common.inc
--- includes/common.inc	29 Jul 2006 17:56:41 -0000	1.550
+++ includes/common.inc	1 Aug 2006 20:51:53 -0000
@@ -116,6 +116,7 @@ function drupal_set_html_head($data = NU
 function drupal_get_html_head() {
   $output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
   $output .= theme('stylesheet_import', base_path() .'misc/drupal.css');
+  $output .= drupal_get_js();
   return $output . drupal_set_html_head();
 }
 
@@ -1206,22 +1207,74 @@ function drupal_add_link($attributes) {
 /**
  * Add a JavaScript file to the output.
  *
- * The first time this function is invoked per page request,
- * it adds "misc/drupal.js" to the output. Other scripts
- * depends on the 'killswitch' inside it.
- */
-function drupal_add_js($file, $nocache = FALSE) {
-  static $sent = array();
-
-  $postfix = $nocache ? '?'. time() : '';
-  if (!isset($sent['misc/drupal.js'])) {
-    drupal_set_html_head('<script type="text/javascript" src="'. base_path() .'misc/drupal.js'. $postfix .'"></script>');
-    $sent['misc/drupal.js'] = TRUE;
-  }
-  if (!isset($sent[$file])) {
-    drupal_set_html_head('<script type="text/javascript" src="'. check_url(base_path() . $file) . $postfix .'"></script>');
-    $sent[$file] = TRUE;
+ * When the function is called, it adds an element to the array of JavaScript files
+ * that should be in the page header.
+ *
+ * @param $file
+ *   (optional) The path to the JavaScript file to include.
+ * @param $nocache
+ *   (optional) If set to TRUE, the referenced JavaScript file is loaded every time a new
+ *   page is loaded. This prevents caching.
+ * @return
+ *   An array with all JavaScript files that are queued for addition to the header.
+ */
+function drupal_add_js($file = FALSE, $nocache = FALSE) {
+  static $javascript = array();
+  
+  if ($file) {
+    $javascript[$file] = array(
+      'path' => base_path() . $file,
+      'cache' => !$nocache,
+    );
   }
+  
+  return $javascript;
+}
+
+/**
+ * Add JavaScript configuration settings to a page.
+ *
+ * Often, JavaScript enabled modules need settings to function properly. To prevent modules from
+ * interfering with each other, Drupal offers a centralized place to store this information. The settings
+ * will be accessible at Drupal.settings[$module].
+ *
+ * @param $module
+ *   (optional) The module the JavaScript settings are for.
+ * @param $configuration
+ *   (optional) The array with configuration options as associative array.
+ * @return
+ *   An array with all JavaScript configuration settings that have been added so far.
+ */
+function drupal_add_js_settings($module = FALSE, $configuration = FALSE) {
+  static $javascript;
+  
+  if ($module && $configuration) {
+    $javascript[$module] = array_merge((array)$javascript[$module], $configuration);
+  }
+  
+  return $javascript;
+}
+
+/**
+ * This function returns all JavaScript files that are queued for addition to the header
+ * as HTML code. It adds drupal.js first as some other JavaScript files depend on 
+ * the killswitch inside it
+ */
+function drupal_get_js() {
+  $javascript = drupal_add_js();
+  $settings = drupal_add_js_settings();
+  
+  array_unshift($javascript, array('path' => base_path() .'misc/drupal.js', 'cache' => TRUE));
+  
+  foreach ($javascript as $file) {
+    $output .= '<script type="text/javascript" src="'. check_url($file['path']) . ($file['cache'] ? '' : '?'. time()) .'"></script>';
+  }
+  
+  if ($settings) {
+    $output .= '<script type="text/javascript">if (typeof Drupal == \'undefined\') Drupal = {}; Drupal.settings = '. drupal_to_js($settings) .'</script>';
+  }
+  
+  return $output;
 }
 
 /**
