--- profile_category_weight.module	2009-05-06 17:47:16.000000000 -0300
+++ profile_category_weight.module2	2010-07-08 13:18:20.000000000 -0300
@@ -50,7 +50,7 @@ function profile_category_weight_setting
     $categories = _profile_category_weight_categories();
     foreach ( $categories as $category => $weight ) {
       $category_form_id = _profile_category_weight_form_id($category);
-      $categories_values[$category] = $form_state['values'][$category_form_id];
+      $categories_values[$category] = $form_state['values']['list'][$category_form_id];
     }
     variable_set('profile_category_weight_categories', $categories_values);
   }
@@ -72,19 +72,100 @@ function profile_category_weight_setting
 
 function profile_category_weight_settings() {
   $categories = _profile_category_weight_categories();
-  foreach ($categories as $category => $weight) {
-    $category_form_id = _profile_category_weight_form_id($category);
-    $form[$category_form_id] = array(
-        '#type' => 'weight',
-        '#title' => $category,
-        '#default_value' => is_numeric($weight) ? $weight : 0,
-      );
+
+  $form = array();
+  $form['list']['#tree'] = TRUE;
+
+  $category_count = count($categories);
+  foreach ( $categories as $category => $weight ) {
+    $category_form_id = _profile_category_weight_form_id(
+      $category);
+    $form['list'][$category_form_id]['title'] = array(
+      '#value' => $category);
+    $form['list'][$category_form_id]['weight'] = array(
+      
+      '#type' => 'weight' , 
+      '#delta' => $category_count , 
+      '#default_value' => $weight);
   }
+  //  $form = system_settings_form($form);
+  // Actions
+  $form['profile_category_weight_submit'] = array(
+    
+    '#type' => 'submit' , 
+    '#value' => t('Save configuration') , 
+    '#submit' => array('profile_category_weight_settings_submit'));
+  
+  $form['profile_category_weight_reset'] = array(
+    
+    '#type' => 'submit' , 
+    '#value' => t('Reset to defaults') , 
+    '#submit' => array('profile_category_weight_settings_reset'));
+  //  $form['#theme'] = 'profile_category_weight_settings';
+  return $form;
+}
 
+/**
+ * Theme the reordering table
+ * @return <type>
+ */
+function theme_profile_category_weight_settings($form) {
+  drupal_add_tabledrag('profile-category-weight-settings-table', 
+    'order', 
+    'sibling', 
+    'profile-category-weight-sort');
+  $header = array(t('Title') , t('Weight'));
+  
+  $rows = array();
+  $tree = &$form['list'];
+  foreach (element_children($tree) as $key) {
+    
+    $tree[$key]['weight']['#attributes']['class'] = 'profile-category-weight-sort';
+    
+    $row = array();
+    $row[] = drupal_render($tree[$key]['title']);
+    $row[] = drupal_render($tree[$key]['weight']);
+    
+    $rows[] = array('data' => $row , 'class' => 'draggable');
+  }
+  
+  $output = theme('table', 
+    $header, 
+    $rows, 
+    array('id' => 'profile-category-weight-settings-table'));
+  $output .= drupal_render($form);
+  return $output;
+}
+
+/**
+ * Implementation of hook_theme
+ *
+ */
+function profile_category_weight_theme() {
+  return array(
+    
+    'profile_category_weight_settings' => array(
+      'arguments' => array('form' => array())));
+}
+
+function _profile_category_weight_settings() {
+  $categories = _profile_category_weight_categories();
+  $tableData = array();
+  foreach ( $categories as $category => $weight ) {
+    $category_form_id = _profile_category_weight_form_id(
+      $category);
+    $form[$category_form_id] = array(
+      
+      '#type' => 'weight' , 
+      '#title' => $category , 
+      '#default_value' => is_numeric($weight) ? $weight : 0);
+  }
+  
   $form = system_settings_form($form);
   $form['#submit'] = array('profile_category_weight_settings_submit');
   //$form['#validate'][] = 'profile_category_weight_settings_validate';
   $form['#theme'] = 'profile_category_weight_settings';
+  
   return $form;
 }
 
