--- img_assist.module.original	2008-02-24 22:46:08.000000000 +0100
+++ img_assist.module	2008-03-28 10:04:41.000000000 +0100
@@ -24,82 +24,114 @@ function img_assist_help($section) {
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function img_assist_theme() {
+  return array(
+    'img_assist_textarea_link' => array(
+      'arguments' => array('element' => NULL, 'link' => NULL),
+    ),
+    'img_assist_inline' => array(
+      'arguments' => array('node' => NULL, 'size' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_filter' => array(
+      'arguments' => array('text' => NULL),
+    ),
+    'img_assist_popup' => array(
+      'arguments' => array('content' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_page' => array(
+      'arguments' => array('content' => NULL, 'attributes' => NULL),
+    ),
+    'img_assist_legacy' => array(
+    ),
+  );
+}
+
+/**
  * Implementation of hook_menu().
  */
-function img_assist_menu($may_cache) {
+function img_assist_menu() {
   $items = array();
   
-  $access = user_access('access content');
+  $access_callback = 'user_access';
+  $access_arguments = array('access content');
   
-  if ($may_cache) {
-    $items[] = array('path' => 'img_assist/cache/clear',
-      'title' => t('Empty cache'),
-      'callback' => 'img_assist_cache_clear',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/load',
-      'title' => t('Image Assist'),
-      'callback' => 'img_assist_loader',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Page callbacks called internally by img_assist/load.
-    $items[] = array(
-      'path' => 'img_assist/header',
-      'title' => t('Image Assist Header'),
-      'callback' => 'img_assist_header',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/thumbs',
-      'title' => t('Image Assist Thumbnails'),
-      'callback' => 'img_assist_thumbs',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/upload',
-      'title' => t('Image Assist Upload'),
-      'callback' => 'img_assist_upload',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    $items[] = array(
-      'path' => 'img_assist/properties',
-      'title' => t('Image Assist Properties'),
-      'callback' => 'img_assist_properties',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Popup images page.
-    $items[] = array(
-      'path' => 'img_assist/popup',
-      'title' => t('Popup Image'),
-      'callback' => 'img_assist_popup',
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-    );
-    // Insert callback (only for inserting HTML, not filter tag).
-    $items[] = array(
-      'path' => 'img_assist/insert_html',
-      'title' => t('Insert Callback'),
-      'access' => $access,
-      'type' => MENU_CALLBACK,
-      'callback' => 'img_assist_insert_html',
-    );
-    $items[] = array(
-      'path' => 'admin/settings/img_assist',
-      'title' => t('Image assist'),
-      'description' => t('Change settings for the Image assist module.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => 'img_assist_admin_settings',
-      'access' => user_access('administer site configuration'),
-    );
-  }
-  else {
+  $items['img_assist/cache/clear'] = array(
+    'title' => t('Empty cache'),
+    'page callback' => 'img_assist_cache_clear',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/load'] = array(
+    'title' => t('Image Assist'),
+    'page callback' => 'img_assist_loader',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  // Page callbacks called internally by img_assist/load.
+  $items['img_assist/header'] = array(
+    'title' => t('Image Assist Header'),
+    'page callback' => 'img_assist_header',
+    'page arguments' => array(2),
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/thumbs'] = array(
+    'title' => t('Image Assist Thumbnails'),
+    'page callback' => 'img_assist_thumbs',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/upload'] = array(
+    'title' => t('Image Assist Upload'),
+    'page callback' => 'img_assist_upload',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  $items['img_assist/properties'] = array(
+    'title' => t('Image Assist Properties'),
+    'page callback' => 'img_assist_properties',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  // Popup images page.
+  $items['img_assist/popup'] = array(
+    'title' => t('Popup Image'),
+    'page callback' => 'img_assist_popup',
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+  );
+  // Insert callback (only for inserting HTML, not filter tag).
+  $items['img_assist/insert_html'] = array(
+    'title' => t('Insert Callback'),
+    'access callback' => $access_callback,
+    'access arguments' => $access_arguments,
+    'type' => MENU_CALLBACK,
+    'page callback' => 'img_assist_insert_html',
+  );
+  $items['admin/settings/img_assist'] = array(
+    'title' => t('Image assist'),
+    'description' => t('Change settings for the Image assist module.'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => 'img_assist_admin_settings',
+    'access callback' => 'user_access',
+    'access arguments' => 'administer site configuration'
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_init().
+ */
+function img_assist_init() {
     $path = drupal_get_path('module', 'img_assist');
     if (variable_get('img_assist_page_styling', 'yes') == 'yes') {
       drupal_add_css($path .'/img_assist.css');
@@ -107,8 +139,6 @@ function img_assist_menu($may_cache) {
     // Assign base_path to insert in image source by javascript.
     drupal_add_js('var BASE_URL = "'. base_path() .'";', 'inline');
     drupal_add_js($path .'/img_assist.js');
-  }
-  return $items;
 }
 
 /**
@@ -122,8 +152,9 @@ function img_assist_perm() {
  * Implementation of hook_elements().
  */
 function img_assist_elements() {
+  $type = array();
   $type['textarea'] = array(
-    '#process' => array('img_assist_textarea' => array()),
+    '#process' => 'img_assist_textarea'
   );
   return $type;
 }
@@ -372,9 +403,9 @@ function img_assist_admin_settings() {
 /**
  * Validate Image Assist settings.
  */
-function img_assist_admin_settings_validate($form_id, $form_values) {
+function img_assist_admin_settings_validate($form, &$form_state) {
   // img_assist_max_size must contain a value for width and height.
-  if (!preg_match('/\d+x\d+/', $form_values['img_assist_max_size'])) {
+  if (!preg_match('/\d+x\d+/', $form_state['values']['img_assist_max_size'])) {
     form_set_error('img_assist_max_size', t('Allowed maximum inline image size has to indicate width and height, for example %example.', array('%example' => '200x300')));
   }
 }
@@ -434,6 +465,7 @@ function img_assist_nodeapi(&$node, $op,
       img_assist_map_save($node);
       // Store nid globally if this node is an image uploaded with img_assist.
       if ($node->type == 'image' && arg(0) == 'img_assist') {
+        drupal_goto('img_assist/properties/'. $node->nid);
         global $_img_assist_saved_image;
         $_img_assist_saved_image = $node->nid;
       }
@@ -504,14 +536,13 @@ function img_assist_loader() {
   echo $output;
 }
 
-function img_assist_header() {
+function img_assist_header($mode) {
   // Mode may be 'uploading', 'properties' or 'browse'.
-  $mode = arg(2);
   $output = drupal_get_form('img_assist_header_form', $mode);
   echo theme('img_assist_page', $output, array('id' => 'img_assist_header', 'onload' => 'parent.initHeader();', 'class' => 'img_assist'));
 }
 
-function img_assist_header_form($mode) {
+function img_assist_header_form(&$form_state, $mode) {
   global $user;
   
   // Upload image.
@@ -560,7 +591,7 @@ function img_assist_header_form($mode) {
     if (module_exists('taxonomy')) {
       $vocabs = (array)variable_get('img_assist_vocabs', array());
       foreach ($vocabs as $vid) {
-        $vocab = taxonomy_get_vocabulary($vid);
+        $vocab = taxonomy_vocabulary_load($vid);
         $terms = taxonomy_get_tree($vid);
         if ($terms) {
           foreach ($terms as $key => $value) {
@@ -617,7 +648,8 @@ function img_assist_header_form($mode) {
  */
 function img_assist_upload() {
   global $user;
-  
+  module_load_include('inc', 'node', 'node.pages');
+
   if (module_exists('image') && user_access('create images')) {
     // On other img_assist_pages I've added the javascript using the body onload
     // attribute, but for this page will also need the collapse functions and
@@ -652,15 +684,13 @@ function img_assist_upload() {
  * 
  * Add a submit callback to image_node_form which alters the redirect.
  */
-function img_assist_form_alter($form_id, &$form) {
+function img_assist_form_alter(&$form, &$form_state, $form_id) {
   switch ($form_id) {
     case 'image_node_form':
       if (arg(0) != 'img_assist') {
         break;
       }
-      if (!is_array($form['#submit'])) $form['#submit'] = array();
-      $form['#submit'] += array('img_assist_node_form_submit' => array($form));
-      break;
+      $form['#submit'][] = 'img_assist_node_form_submit';
   }
 }
 
@@ -669,10 +699,10 @@ function img_assist_form_alter($form_id,
  * 
  * Change the redirect from node/$nid to img_assist/properties/$nid.
  */
-function img_assist_node_form_submit($form_id, $form_values) {
+function img_assist_node_form_submit($form, &$form_state) {
   // Get the nid of the newly created image (caught by img_assist_nodeapi).
   global $_img_assist_saved_image;
-  drupal_goto('img_assist/properties/'. $_img_assist_saved_image);
+  $form_state['redirect'] = 'img_assist/properties/'. $_img_assist_saved_image;
 }
 
 /**
@@ -726,14 +756,16 @@ function img_assist_thumbs() {
       }
     }
     
+    $num_rows = FALSE;
     $show_amount = variable_get('img_assist_preview_count', 10);
     $result = pager_query($query, $show_amount, $element = 0, $count_query = NULL, $params);
     while ($row = db_fetch_object($result)) {
       $node    = node_load(array('nid' => $row->nid));
       $image   = img_assist_display($node, IMAGE_THUMBNAIL);
-      $output .= l($image, 'img_assist/properties/'. $node->nid, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = TRUE) ."\n";
+      $output .= l($image, 'img_assist/properties/'. $node->nid, array('attributes' => array(), 'query' => NULL, 'fragment' => NULL, 'absolute' => FALSE, 'html' => TRUE)) ."\n";
+      $num_rows = TRUE;
     }
-    if (!db_num_rows($result)) {
+    if (!$num_rows) {
       $output .= t('No images were found. Please upload a new image or browse images by a different category.');
     }
     
@@ -775,7 +807,7 @@ function img_assist_properties() {
 /**
  * Construct the image properties form.
  */
-function img_assist_properties_form($node, $update) {
+function img_assist_properties_form($form_state, $node, $update) {
   $image_info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
   
   // Select (or generate) a preview image.
@@ -1021,8 +1053,8 @@ function img_assist_properties_form($nod
   return $form;
 }
 
-function img_assist_properties_form_validate($form_id, $form_values) {
-  $html = img_assist_render_image($form_values);
+function img_assist_properties_form_validate($form, &$form_state) {
+  $html = img_assist_render_image($form_state['values']);
   img_assist_set_htmlcode($html);
   drupal_goto('img_assist/insert_html');
 }
@@ -1175,7 +1207,7 @@ function img_assist_display(&$node, $siz
   $attributes['class']  = 'image image-'. $label . (isset($attributes['class']) ? ' '. $attributes['class'] : '');
   $attributes['width']  = $info['width'];
   $attributes['height'] = $info['height'];
-  return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes);
+  return theme('image_display', $node, $label, file_create_url($node->images[$label]), array('path' => $attributes));
 }
 
 /**
@@ -1236,14 +1268,15 @@ function _img_assist_build_derivatives(&
 }
 
 function _img_assist_remove($node, $size) {
-  $result = db_query("SELECT * FROM {files} WHERE nid = %d AND filename = '%s'", $node->nid, $size['key']);
+  $result = db_query("SELECT * FROM {files} f INNER JOIN {image} i WHERE f.fid = i.fid AND i.nid = %d AND f.filename = '%s'", $node->nid, $size['key']);
   while ($file = db_fetch_object($result)) {
     // Never delete original image.
     if ($file->filepath != $node->images[IMAGE_ORIGINAL]) {
       // Delete image file.
       file_delete(file_create_path($file->filepath));
       // Delete file reference in database.
-      db_query("DELETE FROM {files} WHERE nid = %d AND filename = '%s'", $node->nid, $size['key']);
+      db_query("DELETE FROM {files} WHERE fid = %d AND filename = '%s'", $file->fid, $size['key']);
+      db_query("DELETE FROM {image} WHERE nid = %d AND fid = '%d'", $node->nid, $file->fid);
     }
   }
 }
@@ -1255,8 +1288,8 @@ function img_assist_render_image($attrib
   global $user;
   
   if ($attributes['nid']) {
-    $node = node_load($attributes['nid']);
     
+    $node = node_load(array(nid=>$attributes['nid']));
     // Get size.
     $width  = $attributes['width'];
     $height = $attributes['height'];
@@ -1471,7 +1504,7 @@ function img_assist_map_delete($node) {
  */
 function img_assist_map_load($nid) {
   $imagemap = array();
-  $result = db_query('SELECT * FROM {files} f INNER JOIN {img_assist_map} i ON f.nid = i.iid WHERE f.nid = %d', $nid);
+  $result = db_query('SELECT * FROM {image} f INNER JOIN {img_assist_map} i ON f.nid = i.iid WHERE f.nid = %d', $nid);
   while ($data = db_fetch_object($result)) {
     $imagemap[] = $data->nid;
   }
@@ -1605,12 +1638,12 @@ function theme_img_assist_textarea_link(
   else {
     $link = t('Add image');
   }
-  $output .= l($link, 'img_assist/load/textarea', array(
+  $output .= l($link, 'img_assist/load/textarea', $options = array('attributes' => array(
     'class' => 'img_assist-link',
     'id' => 'img_assist-link-'. $element['#id'],
     'title' => t('Click here to add images'),
-    'onclick' => 'window.open(this.href, \'img_assist_link\', \'width=600,height=350,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no\'); return false;',
-  ), 'textarea='. $element['#name'], NULL, FALSE, TRUE);
+    'onclick' => 'window.open(this.href, \'img_assist_link\', \'width=600,height=350,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no\'); return false;'),
+    'query' => 'textarea='. $element['#name'], 'html' => TRUE));
   $output .= '</div>';
   return $output;
 }
@@ -1644,7 +1677,7 @@ function theme_img_assist_inline($node, 
   }
   
   if ($link == 'node') {
-    $output .= l($img_tag, 'node/'. $node->nid, array(), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, 'node/'. $node->nid, array('html' => TRUE));
   }
   elseif ($link == 'popup') {
     $popup_size = variable_get('img_assist_popup_label', IMAGE_PREVIEW);
@@ -1652,10 +1685,10 @@ function theme_img_assist_inline($node, 
     $width      = $info['width'];
     $height     = $info['height'];
     $popup_url  = file_create_url($node->images[variable_get('img_assist_popup_label', IMAGE_PREVIEW)]);
-    $output .= l($img_tag, $popup_url, array('onclick' => "launch_popup({$node->nid}, {$width}, {$height}); return false;", 'target' => '_blank'), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, $popup_url, array('attributes' => array('onclick' => "launch_popup({$node->nid}, {$width}, {$height}); return false;", 'target' => '_blank'), 'html' =>TRUE));
   }
   elseif ($link == 'url') {
-    $output .= l($img_tag, $url, array(), NULL, NULL, FALSE, TRUE);
+    $output .= l($img_tag, $url, array('html' => TRUE));
   }
   else {
     $output .= $img_tag;
@@ -1793,7 +1826,7 @@ function img_assist_load_images($tid = N
   }
   
   if (!$image) {
-    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid INNER JOIN {files} f ON n.nid = f.nid AND n.type = 'image' ". $where ." ORDER BY n.changed DESC"));
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid INNER JOIN {image} f ON n.nid = f.nid AND n.type = 'image' ". $where ." ORDER BY n.changed DESC"));
     while ($node = db_fetch_object($result)) {
       $node->filepath = file_create_path($node->filepath);
       $dim = getimagesize($node->filepath, $info);
@@ -1816,7 +1849,7 @@ function img_assist_load_images($tid = N
     // Note: If we didn't use "LIKE 'image/%%'" here we could load other files.
     // Might be interesting to expand on this someday.
     if ($image) {
-      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {files} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND f.filemime LIKE 'image/%%' AND n.nid NOT IN (". implode(array_keys($image), ', ') .") ". $where ." ORDER BY n.changed DESC"));
+      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, r.teaser, f.* FROM {image} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND f.filemime LIKE 'image/%%' AND n.nid NOT IN (". implode(array_keys($image), ', ') .") ". $where ." ORDER BY n.changed DESC"));
       while ($node = db_fetch_object($result)) {
         $node->filepath = file_create_path($node->filepath);
         $dim = getimagesize($node->filepath, $info);
@@ -1847,7 +1880,7 @@ function img_assist_load_images($tid = N
  * Load an image from the database.
  */
 function img_assist_load_image($id, $derivatives = TRUE) {
-  $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, f.* FROM {files} f, {node} n WHERE f.nid = n.nid AND f.fid = %d'), $id));
+  $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, f.* FROM {image} f, {node} n WHERE f.nid = n.nid AND f.fid = %d'), $id));
   $node->filepath = file_create_path($node->filepath);
   
   if (!$derivatives) {
@@ -1862,7 +1895,7 @@ function img_assist_load_image($id, $der
       foreach (image_get_sizes() as $size) {
         if ($size['label'] == $node->filename) {
           $image_module_image = TRUE;
-          $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, r.teaser, f.* FROM {files} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND n.nid = %d'), $node->nid);
+          $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, r.teaser, f.* FROM {image} f, {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.nid WHERE f.nid = n.nid AND n.nid = %d'), $node->nid);
           while ($node = db_fetch_object($result)) {
             $node->filepath = file_create_path($node->filepath);
             $dim = getimagesize($node->filepath, $info);
@@ -1895,7 +1928,7 @@ function img_assist_load_image($id, $der
 function _img_assist_get_thumbnail(&$image) {
   static $thumbs = array();
   if ($thumbs[$image->nid] === NULL) {
-    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {files} WHERE nid = %d AND filename = '%s'", $image->nid, IMAGE_THUMBNAIL)));
+    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {image} WHERE nid = %d AND filename = '%s'", $image->nid, IMAGE_THUMBNAIL)));
     // In old versions of image.module thumbs were named 'thumb_filename.ext'.
     if (!file_exists($thumbpath)) {
       $pos = strrpos($image->filepath, '/') + 1;
