--- image_attach.module.orig	2007-04-19 02:43:32.000000000 -0700
+++ image_attach.module	2007-04-19 02:20:27.000000000 -0700
@@ -124,6 +124,11 @@
           '#type' => 'hidden',
           '#value' => $node->iid
         );
+        if (module_exist('image_gallery')) {
+          $form['image_attach']['image_gallery'] = taxonomy_form(_image_gallery_get_vid());
+          $form['image_attach']['image_gallery']['#weight'] = 0;
+          $form['image_attach']['image_gallery']['#description'] = t('Choose an image gallery to place the new image in.');
+        }
       }
       $form['image_attach']['image'] = array(
         '#type' => 'file',
@@ -170,6 +180,10 @@
         if (!form_get_errors()) {
           $image = node_submit($image);
           node_save($image);
+          $gallery = taxonomy_get_term($_POST['edit']['image_gallery']);
+          if ($gallery) {
+            taxonomy_node_save($image->nid, array($gallery->tid => $gallery));
+          }
           $node->iid = $image->nid;
           $node->new_image = TRUE;
         }
@@ -209,22 +223,6 @@
 }
 
 /**
- * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
- */
-function _image_attach_get_image_nodes() {
-  $rows = array(0 => '');
-  $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status=1 AND type='image' ORDER BY n.sticky DESC, n.title ASC"));
-  if (db_num_rows($result) == 0) {
-    return array();
-  }
-
-  while ($node = db_fetch_object($result)) {
-    $rows[$node->nid] = $node->title;
-  }
-  return $rows;
-}
-
-/**
  * Implementation of hook_views_tables().
  */
 function image_attach_views_tables() {
@@ -317,6 +315,54 @@
 }
 
 /**
+ * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
+ */
+function _image_attach_get_image_nodes() {
+  $rows = array(0 => '');
+  if (module_exists('image_gallery')) {
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n LEFT JOIN {term_node} t ON t.nid=n.nid WHERE n.status=1 AND n.type='image' AND t.tid IS NULL ORDER BY n.sticky DESC, n.title ASC"));   
+    while ($node = db_fetch_object($result)) {
+      $rows[$node->nid] = $node->title;
+    } 
+    $tree = taxonomy_get_tree(_image_gallery_get_vid());
+    foreach ($tree as $gallery) {
+      $prefix = str_repeat('--', $gallery->depth+1).' ';
+      $rows[$prefix.$gallery->name] = array();     
+      
+      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nid WHERE n.status=1 AND n.type='image' AND t.tid=%d ORDER BY n.sticky DESC, n.title ASC"), $gallery->tid);
+      while ($image = db_fetch_object($result)) {
+        $rows[$prefix.$gallery->name][$image->nid] = $image->title;
+      }      
+    }
+  }
+  else {  
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status=1 AND type='image' ORDER BY n.stickty DESC, n.title ASC"));
+    if (db_num_rows($result) == 0) {
+      return array();
+    }
+    while ($node = db_fetch_object($result)) {
+      $rows[$node->nid] = $node->title;
+    }  
+  }
+  return $rows;
+}
+
+function image_attach_view_image($nid=null) {
+  if (empty($nid)) {
+    header('HTTP/1.1 404 Image Not Found');
+    exit;
+  }
+  $node = node_load($nid);
+  if (!$node) {
+    header('HTTP/1.1 404 Image Not Found');
+    exit;
+  }
+
+  print image_display($node, 'thumbnail');
+  exit;
+}
+
+/**
  * Theme the teaser.
  *
  * Override this in template.php to include a case statement if you want different node types to appear differently.
