--- image/contrib/image_attach/image_attach.module	2010-10-03 10:35:51.000000000 +0200
+++ my_image/contrib/image_attach/image_attach.module	2012-02-22 01:54:12.000000000 +0100
@@ -224,12 +224,33 @@
         }
 
         if ($has_existing_images) {
+          $form['image_attach']['image_thumbnail']['#theme'] = 'image_attach_form_thumbnails';
+          $form['image_attach']['image_thumbnail']['#tree'] = TRUE;
+          $weight = 0;
           foreach ($node->iids as $iid) {
             $image = node_load($iid);
             $form['image_attach']['image_thumbnail'][$iid] = array(
-              '#type' => 'item',
-              '#title' => t('Thumbnail'),
-              '#value' => image_display($image, 'thumbnail'),
+              'id' => array(
+                '#type' => 'hidden',
+                '#value' => $iid,
+              ),
+              'image' => array(
+                '#type' => 'markup',
+                '#value' => image_display($image, 'thumbnail'),
+              ),
+              'delete' => array(
+                '#type' => 'checkbox',
+              ),
+              'title' => array(
+                '#type' => 'markup',
+                '#value' => check_plain($image->title),
+              ),
+              'weight' => array(
+                '#type' => 'weight',
+                '#title' => t('Weight'),
+                '#delta' => count($node->iids),
+                '#default_value' => isset($form_state['values']['image_thumbnail'][$iid]) ? $form_state['values']['image_thumbnail'][$iid]['weight'] : $weight++,
+              ),
             );
           }
         }
@@ -292,6 +313,37 @@
 }
 
 /**
+ * Get image weights and return an iid array ordered by weight
+ *
+ * This private function is called by submit handlers to discover how the user
+ * has ordered their images.
+ * It returns an array with identical keys and values (just like the ordinary 
+ * iid array)
+ *
+ * @param $rawweightdata
+ *   An array of iids and weights from the drag-to-reorder table, derived from
+ *   either the $form_state object or the $node object
+ * @param $selectboxarray
+ *   The array of existing images that the user wants to include. This may be
+ *   different to the iids passed in $rawweightdata
+ * @return
+ *   A weight-sorted (from lightest to heaviest) array of iids, with identical
+ *   keys and (integer) values
+ */
+function _image_attach_set_image_weight($rawweightdata, $selectboxarray) {
+  $weightarray = array();
+  foreach ($selectboxarray as $iid){
+    // check the delete box has not been ticked
+    if($rawweightdata[$iid]['delete'] !== 1) {
+      // if the image exists in the drag-to-reorder array, get its weight, otherwise, assign it the heaviest weight possible
+      $weightarray[$iid] = isset($rawweightdata[$iid]['weight']) ? $rawweightdata[$iid]['weight'] : count($selectboxarray);
+    }
+  }
+  asort($weightarray);
+  return array_combine(array_keys($weightarray),array_keys($weightarray));
+}
+
+/**
  * Save attached image nids and rebuild form.
  *
  * This submit function adds the new images and returns to the
@@ -301,6 +353,10 @@
   // Rebuild the attached image data.
   if (isset($form_state['values']['iids'])) {
     db_query("DELETE FROM {image_attach} WHERE nid = %d", $form['nid']['#value']);
+    // Sort the $form_state['values']['iids'] array by weight
+    if (count($form_state['values']['image_thumbnail'])) {
+      $form_state['values']['iids'] = _image_attach_set_image_weight($form_state['values']['image_thumbnail'], $form_state['values']['iids']);
+    }    
     if (count($form_state['values']['iids'])) {
       $weight = 0;
       foreach ($form_state['values']['iids'] as $iid) {
@@ -417,7 +473,10 @@
     case 'update':
       db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
       if (!empty($node->iids)) {
-        // Populate weight column with placeholder values.
+        if (!empty($node->image_thumbnail)) {
+          // Form submitted, order iids according to drag-to-reorder table
+          $node->iids = _image_attach_set_image_weight($node->image_thumbnail, $node->iids);
+        }
         $weight = 0;
         foreach ($node->iids as $iid) {
           db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++);
@@ -573,10 +632,37 @@
         'image_nodes' => array(),
       ),
     ),
+    'image_attach_form_thumbnails' => array(
+      'arguments' => array('form' => NULL),
+    ),    
   );
 }
 
 /**
+ * Theme image thumbnails in editing ui to a drag-to-reorder table
+ *
+ * @param $form
+ *  The $form object
+ */
+function theme_image_attach_form_thumbnails($form) {
+  drupal_add_tabledrag('image-attach-thumbnails-draggable-table', 'order', 'sibling', 'weight');
+  $header = array(t('Attached Images'), t('Delete'), t('Title'), t('Weight'));
+  foreach (element_children($form) as $key) {
+    $element = &$form[$key];
+    $element['weight']['#attributes']['class'] = 'weight';
+    $row = array();
+    $row[] = drupal_render($element['image']);
+    $row[] = drupal_render($element['delete']);
+    $row[] = drupal_render($element['title']);
+    $row[] = drupal_render($element['weight']) . drupal_render($element['id']);
+    $rows[] = array('data' => $row, 'class' => 'draggable');
+  }
+  $output = theme('table', $header, $rows, array('id' => 'image-attach-thumbnails-draggable-table'));
+  $output .= drupal_render($form);
+  return $output;
+}
+
+/**
  * Theme attached images shown in nodes.
  *
  * @param $nid
@@ -588,8 +674,10 @@
  * @param $teaser
  *   Whether the $node is being shown as a teaser or not.
  *
- * Override this in template.php to include a case statement if you want different node types to appear differently.
- * If you have additional image sizes you defined in image.module, you can use them by theming this function as well.
+ * Override this in template.php to include a case statement if you want 
+ * different node types to appear differently.
+ * If you have additional image sizes you defined in image.module, you can 
+ * use them by theming this function as well.
  */
 function theme_image_attach_attached_images_node($nid, $image_nodes, $img_size, $teaser = FALSE) {
   drupal_add_css(drupal_get_path('module', 'image_attach') . '/image_attach.css');
