--- jquery_ui.module	Thu May 29 00:16:52 2008
+++ jquery_ui_new.module	Wed Jun 04 14:35:43 2008
@@ -13,2 +13,157 @@
 /**
+ * Implementation of hook_menu().
+ */
+function jquery_ui_menu() {
+  $items = array();
+  $items[] = array(
+    'path' => 'admin/settings/jquery_ui',
+    'title' => t('jQuery UI'),
+    'description' => t('Configure settings for jQuery UI module.'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => array('jquery_ui_admin_settings'),
+    'access' => array('access site configuration'),
+  );
+
+  return $items;
+}
+
+/**
+ * Admin settings form.
+ */
+function jquery_ui_admin_settings() {
+  $form['jquery_ui_compression_type'] = array(
+    '#type' => 'radios',
+    '#title' => t('jQuery UI compression type'),
+    '#options' => drupal_map_assoc(array('packed', 'minified', 'none')),
+    '#default_value' => variable_get('jquery_ui_compression_type', 'minified'),
+    '#description' => t("Type of compression to use. 'Packed' uses Dean Edward's packer to make the file size as small as possible, but may require more browser processing. 'Minified' takes out all comments, whitespace, etc. to reduce the file size to a lesser degree, maintaining performance. 'None' leaves the full source intact."),
+  );
+
+  return system_settings_form($form);
+}
+
+/*
+ * Implementation of hook_form_alter
+ */
+
+function jquery_ui_form_alter($form_id, &$form) {
+  switch ($form_id) {
+    case 'system_theme_settings':
+      // The list of fieldsets which will be made collapsible.
+      $collapsible = array(
+        'color',
+        'logo',
+        'favicon',
+        'specific',
+      );
+      
+      $weight = 1;
+      $key = $form['var']['#value'];
+      
+      if ($key == 'theme_settings') { /* The form is not specific to a theme. */
+        if (isset($form['theme_settings']) && isset($form['node_info'])) {
+          // Wrap the 'theme_settings', and 'node_info' fieldsets in a 
+          // collapsible fieldset.
+          $form['general_settings'] = array(
+            '#type'   => 'fieldset',
+            '#title'  => t('General settings'),
+            '#weight' => -1,
+          );
+          $form['general_settings']['theme_settings'] = $form['theme_settings'];
+          $form['general_settings']['node_info'] = $form['node_info'];
+          
+          // Remove the old filedsets from the form.
+          unset($form['theme_settings']);
+          unset($form['node_info']);
+          
+          // Add the created fieldset to the list of the collapsible ones.
+          $collapsible[] = 'general_settings';
+        }
+        else {
+          // Add the fieldsets to the list of the collapsible ones.
+          $collapsible[] = 'theme_settings';
+          $collapsible[] = 'node_info';
+        }
+        $key = '';
+      }
+      else { /* The page is specific for a theme. */
+        $collapsible[] = 'theme_settings';
+        $key = preg_replace('/(^theme_|_settings$)/', '', $key);
+      }
+      
+      // Get the theme settings.
+      $settings = jquery_ui_get_theme_settings($key);
+      
+      // Make the fieldsets collapsible.
+      foreach ($collapsible as $fieldset) {
+        if (isset($form[$fieldset])) {
+          $form[$fieldset]['#collapsible'] = TRUE;
+          $form[$fieldset]['#collapsed']   = FALSE;
+        }
+      }
+      
+      // Build the list of available jQuery UI themes.
+      $theme_options = jqery_ui_get_themes_info();
+      if (isset($form['engine_specific']) || isset($form['theme_specific']) || count($theme_options)) {
+        // Collapse the fieldsets.
+        foreach ($collapsible as $fieldset) {
+          if (isset($form[$fieldset])) {
+            $form[$fieldset]['#collapsed'] = TRUE;
+          }
+        }
+      }
+      
+      if (count($theme_options)) {
+        // Add jQuery UI settings fields.
+        $form['jquery_ui'] = array(
+          '#type'          => 'fieldset',
+          '#title'         => t('jQuery UI settings'),
+          '#weight'        => $weight++,
+          '#collapsible'   => TRUE,
+          '#collapsed'     => FALSE,
+          '#tree'          => TRUE,
+        );
+        $form['jquery_ui']['theme'] = array(
+          '#type'          => 'select',
+          '#title'         => t('jQuery UI widgets theme'),
+          '#default_value' => $settings['theme'],
+          '#options'       => $theme_options,
+          '#description'   => t('The theme used by jQuery UI for its widgets.'),
+        );
+        
+        //Add a fieldset for the settings of every jQuery themes.
+        $themes_list = jqery_ui_get_themes_info('.*', FALSE);
+        foreach ($themes_list as $id => $info) {
+          if (isset($info['settings_form'])) {
+            $group = call_user_function($info['settings_form'], $settings);
+            if (!empty($group)) {
+              $form['jquery_ui'][$id] = array(
+                '#type'        => 'fieldset',
+                '#title'       => $info['name'],
+                '#collapsible' => TRUE,
+                '#collapsed'   => TRUE,
+                '#tree'        => TRUE,
+              );
+              $form['jquery_ui'][$id] = array_merge($form['jquery_ui'][$id], $group);
+            }
+          }
+        }
+        
+        // Move the buttons, and fieldsets down.
+        foreach (array('specific', 'engine_specific', 'theme_specific', 'buttons') as $id) {
+          if (isset($form[$id])) {
+            $form[$id]['#weight'] = $weight++;
+          }
+        }
+      }
+    break;
+    
+  }
+}
+
+/***********************************************************************
+ * Module public functions.
+ **********************************************************************/
+
+/**
  * Add JQuery interface library to this page.
@@ -67,32 +222,75 @@
 
-/**
- * Implementation of hook_menu().
+/*
+ * Get the list of installed JQuery UI themes.
+ * @param $mask
+ *   A regular expression to filter the themes list (default to '.*').
+ * @param $form_compatibility
+ *   Either TRUE (default) or FALSE; if the parameter is TRUE, then the
+ *   returned array can be used to set the options of a form element.  
+ * @result array
+ *   An array of informations about the installed jQuery UI themes; the
+ *   array can be empty, if there aren't themes which match the regular
+ *   expression.
  */
-function jquery_ui_menu() {
-  $items = array();
-  $items[] = array(
-    'path' => 'admin/settings/jquery_ui',
-    'title' => t('jQuery UI'),
-    'description' => t('Configure settings for jQuery UI module.'),
-    'callback' => 'drupal_get_form',
-    'callback arguments' => array('jquery_ui_admin_settings'),
-    'access' => array('access site configuration'),
-  );
-
-  return $items;
+function jqery_ui_get_themes_info($mask = '.*', $form_compatibility = TRUE) {
+  static $themes_list = NULL;
+  
+  $info = array();
+  
+  if (!$themes_list) {
+    $themes_list = array();
+    
+    // Get the list of files contained in the 'css' folder.
+    $themes_path = drupal_get_path('module', 'jquery_ui') .'/css';
+    $nomask      = array('.', '..', 'CVS');
+    $themes      = file_scan_directory($themes_path, '.*', $nomask, 0, FALSE, 'basename');
+    
+    // Filter out the unwanted items.
+    foreach ($themes as $id => $data) {
+      $dirname     = $data->filename;
+      $helper_file = "{$dirname}/{$id}.inc";
+      require_once $helper_file;
+      if (function_exists($function = "jquery_ui_{$id}_get_info")) {
+        $result = $function($themes_path);
+        
+        // The helper function must return an array containing at least
+        // the keys 'name', and 'css'.
+        if (isset($result['name']) && isset($result['css'])) {
+          $themes_list[$id] = $result;
+        }
+      }
+    }
+  }
+  
+  // Filter the themes list to return only the searched items.
+  foreach ($themes_list as $id => $data) {
+    if (ereg($mask, $id)) {
+      if ($form_compatibility) {
+        $info[$id] = $data['name'];
+      }
+      else {
+        $info[$id] = $data;
+      }
+    }
+  }
+  
+  return $info;
 }
 
-/**
- * Admin settings form.
+/*
+ * Get the jquery_ui settings for the theme, or the default ones.
+ * @param $key
+ *   The theme ID, or NULL, to get the global settings.
  */
-function jquery_ui_admin_settings() {
-  $form['jquery_ui_compression_type'] = array(
-    '#type' => 'radios',
-    '#title' => t('jQuery UI compression type'),
-    '#options' => drupal_map_assoc(array('packed', 'minified', 'none')),
-    '#default_value' => variable_get('jquery_ui_compression_type', 'minified'),
-    '#description' => t("Type of compression to use. 'Packed' uses Dean Edward's packer to make the file size as small as possible, but may require more browser processing. 'Minified' takes out all comments, whitespace, etc. to reduce the file size to a lesser degree, maintaining performance. 'None' leaves the full source intact."),
-  );
-
-  return system_settings_form($form);
+function jquery_ui_get_theme_settings($key) {
+  // Create the default settings array.
+  $default_settings = array('theme' => 'flora');
+  foreach (jqery_ui_get_themes_info() as $id => $name) {
+    $default_settings[$id] = array();
+  }
+  
+  // Merge the settings array into the default settings one, and return
+  // it.
+  $settings = array_merge(array('jquery_ui' => $default_settings), theme_get_settings($key));
+  return $settings['jquery_ui'];
 }
