Index: modules/flashvideo/flashvideo.module
===================================================================
--- modules/flashvideo/flashvideo.module	(revision 607)
+++ modules/flashvideo/flashvideo.module	(working copy)
@@ -1549,8 +1549,9 @@
     $cck_original_video_field_db_info = content_database_info($cck_original_video_field);
     $cck_finished_video_field_db_info = content_database_info($cck_finished_video_field);
     $cck_finished_thumbnail_field_db_info = content_database_info($cck_finished_thumbnail_field);
+    $removed_count = db_result(db_query("SELECT COUNT(*) FROM {flashvideo} WHERE fid NOT IN (SELECT fid FROM {files}) AND nid = %d", $node->nid));
     // If they removed at least one file from the node...
-    if (count($node->$cck_original_video_field['field_name']) < count($node->$cck_finished_video_field['field_name'])) {
+    if ($removed_count > 0) {
       // Figure out which file(s) was removed.
       $removed_files = db_query("SELECT * FROM {flashvideo} WHERE fid NOT IN (SELECT fid FROM {files}) AND nid = %d", $node->nid);
       while ($removed_file = db_fetch_object($removed_files)) {
@@ -1565,7 +1566,8 @@
           db_query("DELETE FROM {files} WHERE fid = %d", $oldfile->fid);
           // For single-upload file fields
           if ($cck_original_video_field['db_storage'] == 1) {
-            db_query("UPDATE {". $cck_original_video_field_db_info['table'] ."} SET ". $cck_original_video_field_db_info['columns']['fid']['column'] ." = NULL, ". $cck_original_video_field_db_info['columns']['list']['column'] ." = NULL, ". $cck_finished_video_field_db_info['columns']['fid']['column'] ." = NULL, ". $cck_finished_video_field_db_info['columns']['list']['column'] ." = NULL, ". $cck_finished_thumbnail_field_db_info['columns']['fid']['column'] ." = NULL, ". $cck_finished_thumbnail_field_db_info['columns']['list']['column'] ." = NULL WHERE nid = %d", $node->nid);
+            // expect CCK to have nullified original video field
+            db_query("UPDATE {". $cck_original_video_field_db_info['table'] ."} SET ". $cck_finished_video_field_db_info['columns']['fid']['column'] ." = NULL, ". $cck_finished_video_field_db_info['columns']['list']['column'] ." = NULL, ". $cck_finished_thumbnail_field_db_info['columns']['fid']['column'] ." = NULL, ". $cck_finished_thumbnail_field_db_info['columns']['list']['column'] ." = NULL WHERE nid = %d", $node->nid);
           }
           else { // For multi-upload file fields
             db_query("DELETE FROM {". $cck_original_video_field_db_info['table'] ."} WHERE ". $cck_original_video_field_db_info['columns']['fid']['column'] ." = %d", $oldfile->fid);
@@ -1577,21 +1579,15 @@
         }
         // Need to adjust indexes now that a file has been removed.
         // Look in the flashvideo table for rows that have a video_index that is 1 higher than the one deleted. Update those and decrease by one.
-        $new_index = $removed_file->video_index + 1;
-        $sql = db_query("SELECT * FROM {flashvideo} WHERE nid = %d and video_index = %d", $node->nid, $new_index);
-        while ($one_higher_index_file = db_fetch_object($sql)) {
-          db_query("UPDATE {flashvideo} SET video_index = %d WHERE nid = %d AND fid = %d", $removed_file->video_index, $node->nid, $one_higher_index_file->fid);
-        }
+        db_query("UPDATE {flashvideo} SET video_index = video_index-1 WHERE nid = %d AND video_index > %d", $node->nid, $removed_file->video_index);
       }
       cache_clear_all('content:'. $newfile->nid .':'. $newfile->vid, 'cache_content'); // Clear the CCK cache for this node.
     }
     // Make sure that they uploaded some files to the "original" field.
-    elseif (!empty($node->$cck_original_video_field['field_name'])) {
+    if (!empty($node->$cck_original_video_field['field_name'])) {
       // Initialize new index of the video.
-      $result = db_query("SELECT DISTINCT video_index FROM {flashvideo} WHERE nid = %d ORDER BY video_index ASC", $node->nid);
-      while ($row = db_fetch_object($result)) {
-        $video_index = $row->video_index + 1;
-      }
+      $video_index = db_result(db_query("SELECT MAX(video_index)+1 FROM {flashvideo} WHERE nid = %d", $node->nid));
+      $video_index = ($video_index) ? $video_index : 0;
       // Iterate through all of the files.
       foreach ($node->$cck_original_video_field['field_name'] as $file) {
         // Convert the file to a file object.
@@ -1617,8 +1613,9 @@
       }
     }
   }
+  // Non-FileField  
   elseif (isset($node->files) && count($node->files)) { // Make sure that they uploaded some files.
-
+  
     // Iterate through all of the files.
     foreach ($node->files as $file) {
 
@@ -2557,4 +2554,4 @@
     $files[$file->fid] = $file;
   }
   return $files;
-}
\ No newline at end of file
+}
