Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 20375)
+++ wp-admin/includes/media.php	(working copy)
@@ -33,6 +33,7 @@
  * @return unknown
  */
 function update_gallery_tab($tabs) {
+	static $attachments;
 	global $wpdb;
 
 	if ( !isset($_REQUEST['post_id']) ) {
@@ -42,7 +43,7 @@
 
 	$post_id = intval($_REQUEST['post_id']);
 
-	if ( $post_id )
+	if ( $post_id && is_null( $attachments ) )
 		$attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
 
 	if ( empty($attachments) ) {
@@ -1060,7 +1061,7 @@
  * @return string HTML form for attachment.
  */
 function get_media_item( $attachment_id, $args = null ) {
-	global $redir_tab;
+	global $redir_tab, $tab;
 
 	if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
 		$thumb_url = $thumb_url[0];
@@ -1173,16 +1174,17 @@
 	if ( $send )
 		$send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
 	if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
+		$wp_redirect = empty( $redir_tab ) || $redir_tab == $tab ? '' : '&_wp_http_referer=' . urlencode(add_query_arg('tab', $redir_tab));
 		if ( !EMPTY_TRASH_DAYS ) {
-			$delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' ) . '</a>';
+			$delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post={$attachment_id}{$wp_redirect}", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' ) . '</a>';
 		} elseif ( !MEDIA_TRASH ) {
 			$delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
 			 <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
-			 <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
+			 <a href='" . wp_nonce_url( "post.php?action=delete&amp;post={$attachment_id}{$wp_redirect}", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
 			 <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
 			 </div>";
 		} else {
-			$delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
+			$delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post={$attachment_id}{$wp_redirect}", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
 			<a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-attachment_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
 		}
 	} else {
