--- img_assist.module.original	2008-02-24 22:46:08.000000000 +0100
+++ img_assist.module	2008-03-13 21:44:15.000000000 +0100
@@ -24,82 +24,109 @@ 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',
+//  if ($may_cache) {
+    $items['img_assist/cache/clear'] = array(
       'title' => t('Empty cache'),
-      'callback' => 'img_assist_cache_clear',
-      'access' => $access,
+      'page callback' => 'img_assist_cache_clear',
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
     );
-    $items[] = array(
-      'path' => 'img_assist/load',
+    $items['img_assist/load'] = array(
       'title' => t('Image Assist'),
-      'callback' => 'img_assist_loader',
-      'access' => $access,
+      '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[] = array(
-      'path' => 'img_assist/header',
+    $items['img_assist/header'] = array(
       'title' => t('Image Assist Header'),
-      'callback' => 'img_assist_header',
-      'access' => $access,
+      'page callback' => 'img_assist_header',
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
     );
-    $items[] = array(
-      'path' => 'img_assist/thumbs',
+    $items['img_assist/thumbs'] = array(
       'title' => t('Image Assist Thumbnails'),
-      'callback' => 'img_assist_thumbs',
-      'access' => $access,
+      'page callback' => 'img_assist_thumbs',
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
     );
-    $items[] = array(
-      'path' => 'img_assist/upload',
+    $items['img_assist/upload'] = array(
       'title' => t('Image Assist Upload'),
-      'callback' => 'img_assist_upload',
-      'access' => $access,
+      'page callback' => 'img_assist_upload',
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
     );
-    $items[] = array(
-      'path' => 'img_assist/properties',
+    $items['img_assist/properties'] = array(
       'title' => t('Image Assist Properties'),
-      'callback' => 'img_assist_properties',
-      'access' => $access,
+      'page callback' => 'img_assist_properties',
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
     );
     // Popup images page.
-    $items[] = array(
-      'path' => 'img_assist/popup',
+    $items['img_assist/popup'] = array(
       'title' => t('Popup Image'),
-      'callback' => 'img_assist_popup',
-      'access' => $access,
+      '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[] = array(
-      'path' => 'img_assist/insert_html',
+    $items['img_assist/insert_html'] = array(
       'title' => t('Insert Callback'),
-      'access' => $access,
+      'access callback' => $access_callback,
+      'access arguments' => $access_arguments,
       'type' => MENU_CALLBACK,
-      'callback' => 'img_assist_insert_html',
+      'page callback' => 'img_assist_insert_html',
     );
-    $items[] = array(
-      'path' => 'admin/settings/img_assist',
+    $items['admin/settings/img_assist'] = array(
       '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'),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => 'img_assist_admin_settings',
+      'access callback' => 'user_access',
+      'access arguments' => 'administer site configuration'
     );
-  }
-  else {
+//  }
+//  else {
     $path = drupal_get_path('module', 'img_assist');
     if (variable_get('img_assist_page_styling', 'yes') == 'yes') {
       drupal_add_css($path .'/img_assist.css');
@@ -107,7 +134,7 @@ 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 +149,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 +400,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')));
   }
 }
@@ -394,6 +422,7 @@ function img_assist_filter($op, $delta =
       $processed = FALSE;
       foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) {
         $expanded_macro = img_assist_render_image($macro);
+
         $text           = str_replace($unexpanded_macro, $expanded_macro, $text);
         $processed      = TRUE;
       }
@@ -652,7 +681,7 @@ 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') {
@@ -685,6 +714,7 @@ function img_assist_thumbs() {
   
   if (module_exists('image')) {
     $browse = arg(2);
+
     if ($browse == 'myimages') {
       $myimagesonly = TRUE;
       $tid = 0;
@@ -729,11 +759,12 @@ function img_assist_thumbs() {
     $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)) {
+      $num_rows += 1;
       $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";
     }
-    if (!db_num_rows($result)) {
+    if ($num_rows < 1) {
       $output .= t('No images were found. Please upload a new image or browse images by a different category.');
     }
     
@@ -756,13 +787,11 @@ function img_assist_properties() {
   
   if (is_numeric($nid)) {
     $node = node_load(array('nid' => $nid));
-    
     // Setup a default caption & description.
     $node->title = str_replace("\r", ' ', strip_tags($node->title));
     $node->title = str_replace("\n", ' ', trim($node->title));
     $node->body  = str_replace("\r", ' ', strip_tags($node->body));
     $node->body  = str_replace("\n", ' ', trim($node->body));
-    
     $output = drupal_get_form('img_assist_properties_form', $node, $update);
   }
   else {
@@ -775,7 +804,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.
@@ -790,6 +819,7 @@ function img_assist_properties_form($nod
     $properties_size['key'] = IMAGE_THUMBNAIL;
   }
   $properties_image = img_assist_display($node, $properties_size);
+
   // Get actual image size.
   $properties_size = image_get_info(file_create_path($node->images[$properties_size['key']]));
   
@@ -1021,8 +1051,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 +1205,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 +1266,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);
     }
   }
 }
@@ -1253,10 +1284,9 @@ function _img_assist_remove($node, $size
  */
 function img_assist_render_image($attributes = array()) {
   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'];
@@ -1376,6 +1406,7 @@ function img_assist_render_image($attrib
       $size        = $size[IMAGE_THUMBNAIL];
       $size['key'] = IMAGE_THUMBNAIL;
     }
+
     return theme('img_assist_inline', $node, $size, $attributes);
   }
   // Legacy img_assist filter tag.
@@ -1471,7 +1502,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 +1636,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);
+  ), 'textarea='. $element['#name'], 'html' => TRUE));
   $output .= '</div>';
   return $output;
 }
@@ -1629,7 +1660,6 @@ function theme_img_assist_inline($node, 
   // alt and title.
   $node->title = strip_tags($caption);
   $img_tag = img_assist_display($node, $size);
-  
   // Always define an alignment class, even if it is 'none'.
   $output = '<span class="inline inline-'. $attributes['align'] .'">';
 
@@ -1644,7 +1674,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 +1682,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 +1823,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 +1846,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 +1877,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 +1892,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 +1925,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;
