Index: wp-includes/script-loader.php
===================================================================
--- wp-includes/script-loader.php	(revision 21944)
+++ wp-includes/script-loader.php	(working copy)
@@ -417,7 +417,12 @@
 
 		$scripts->add( 'list-revisions', "/wp-includes/js/wp-list-revisions$suffix.js" );
 
-		$scripts->add( 'media', "/wp-admin/js/media$suffix.js", array( 'jquery-ui-draggable' ), false, 1 );
+		$scripts->add( 'media', "/wp-admin/js/media$suffix.js", array( 'jquery', 'wpdialogs' ), false, 1 );
+		did_action( 'init' ) && $scripts->localize( 'media', 'wpAttachMediaL10n', array(
+			'title' => __('Attach Media'),
+			'save' => __('Attach'),
+			'noMatchesFound' => __('No matches found.')
+		) );
 
 		$scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array('jquery', 'json2', 'imgareaselect'), false, 1 );
 		did_action( 'init' ) && $scripts->localize( 'image-edit', 'imageEditL10n', array(
Index: wp-admin/includes/template.php
===================================================================
--- wp-admin/includes/template.php	(revision 21944)
+++ wp-admin/includes/template.php	(working copy)
@@ -1311,7 +1311,7 @@
  *
  * @param unknown_type $found_action
  */
-function find_posts_div($found_action = '') {
+/*function find_posts_div($found_action = '') {
 ?>
 	<div id="find-posts" class="find-box" style="display:none;">
 		<div id="find-posts-head" class="find-box-head"><?php _e('Find Posts or Pages'); ?></div>
@@ -1346,8 +1346,98 @@
 		</div>
 	</div>
 <?php
+}*/
+
+function wp_attach_media_dialog() {
+?>
+	<div style="display:block;">
+	<form id="wp-attach-media" tabindex="-1">
+	<?php wp_nonce_field( 'attach-media', '_ajax_attach_media_nonce', false ); ?>
+	<div id="link-selector">
+		<div id="search-panel">
+			<div class="link-search-wrapper">
+				<label>
+					<span><?php _e( 'Search' ); ?></span>
+					<input type="search" id="search-field" class="link-search-field" autocomplete="off" />
+					<img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
+				</label>
+			</div>
+			<div id="search-results" class="query-results">
+				<ul></ul>
+				<div class="river-waiting">
+					<img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
+				</div>
+			</div>
+			<div id="most-recent-results" class="query-results">
+				<div class="query-notice"><em><?php _e( 'No search term specified. Showing recent items.' ); ?></em></div>
+				<ul></ul>
+				<div class="river-waiting">
+					<img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
+				</div>
+			</div>
+		</div>
+	</div>
+	<div class="submitbox">
+		<div id="wp-attach-media-cancel">
+			<a class="submitdelete deletion" href="#"><?php _e( 'Cancel' ); ?></a>
+		</div>
+		<div id="wp-attach-media-attach">
+			<input type="submit" value="<?php esc_attr_e( 'Attach' ); ?>" class="button-primary" id="wp-attach-media-submit" name="wp-attach-media-submit">
+		</div>
+	</div>
+	</form>
+	</div>
+<?php
 }
 
+function wp_attach_media_query() {
+	$pts = get_post_types( array( 'public' => true ), 'objects' );
+	$pt_names = array_keys( $pts );
+
+	$query = array(
+		'post_type' => $pt_names,
+		'suppress_filters' => true,
+		'update_post_term_cache' => false,
+		'update_post_meta_cache' => false,
+		'post_status' => 'any',
+		'order' => 'DESC',
+		'orderby' => 'post_date',
+		'posts_per_page' => 20,
+	);
+
+	$args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
+
+	if ( isset( $args['s'] ) )
+		$query['s'] = $args['s'];
+
+	$query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
+
+	// Do main query.
+	$get_posts = new WP_Query;
+	$posts = $get_posts->query( $query );
+	// Check if any posts were found.
+	if ( ! $get_posts->post_count )
+		return false;
+
+	// Build results.
+	$results = array();
+	foreach ( $posts as $post ) {
+		if ( 'post' == $post->post_type )
+			$info = mysql2date( __( 'Y/m/d' ), $post->post_date );
+		else
+			$info = $pts[ $post->post_type ]->labels->singular_name;
+
+		$results[] = array(
+			'ID' => $post->ID,
+			'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
+			'permalink' => get_permalink( $post->ID ),
+			'info' => $info,
+		);
+	}
+
+	return $results;
+}
+
 /**
  * Display the post password.
  *
@@ -1649,7 +1739,7 @@
  */
 function get_submit_button( $text = null, $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = null ) {
 	if ( ! is_array( $type ) )
-		$type = explode( ' ', $type ); 
+		$type = explode( ' ', $type );
 
 	$button_shorthand = array( 'primary', 'small', 'large' );
 	$classes = array( 'button' );
Index: wp-admin/includes/class-wp-media-list-table.php
===================================================================
--- wp-admin/includes/class-wp-media-list-table.php	(revision 21944)
+++ wp-admin/includes/class-wp-media-list-table.php	(working copy)
@@ -314,10 +314,7 @@
 ?>
 			<td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
 			<?php if( $user_can_edit ) {?>
-				<a class="hide-if-no-js"
-					onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
-					href="#the-list">
-					<?php _e( 'Attach' ); ?></a>
+				<a class="hide-if-no-js" href="#" id="attach-media-<?php echo $id; ?>"><?php _e( 'Attach' ); ?></a>
 			<?php } ?></td>
 <?php
 		}
Index: wp-admin/includes/ajax-actions.php
===================================================================
--- wp-admin/includes/ajax-actions.php	(revision 21944)
+++ wp-admin/includes/ajax-actions.php	(working copy)
@@ -1490,6 +1490,26 @@
 
 }
 
+function wp_ajax_wp_attach_media() {
+	check_ajax_referer( 'attach-media', '_ajax_reattach_media_nonce' );
+
+	$args = array();
+
+	if ( isset( $_POST['search'] ) )
+		$args['s'] = stripslashes( $_POST['search'] );
+	$args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
+
+	$results = wp_attach_media_query( $args );
+
+	if ( ! isset( $results ) )
+		wp_die( 0 );
+
+	echo json_encode( $results );
+	echo "\n";
+
+	wp_die();
+}
+
 function wp_ajax_widgets_order() {
 	check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
 
Index: wp-admin/js/media.js
===================================================================
--- wp-admin/js/media.js	(revision 21944)
+++ wp-admin/js/media.js	(working copy)
@@ -1,4 +1,4 @@
-
+/*
 var findPosts;
 (function($){
 	findPosts = {
@@ -91,4 +91,371 @@
 			});
 		});
 	});
+})(jQuery);*/
+
+var wpAttachMedia;
+
+(function($){
+	var inputs = {}, rivers = {}, ed, River, Query;
+
+	wpAttachMedia = {
+		timeToTriggerRiver: 150,
+		minRiverAJAXDuration: 200,
+		riverBottomThreshold: 5,
+		keySensitivity: 100,
+		lastSearch: '',
+
+		init : function() {
+			inputs.dialog = $('#wp-attach-media');
+			inputs.submit = $('#wp-attach-media-submit');
+			inputs.nonce = $('#_ajax_attach_media_nonce');
+			inputs.search = $('#search-field');
+			// Build Rivers
+			rivers.search = new River( $('#search-results') );
+			rivers.recent = new River( $('#most-recent-results') );
+			rivers.elements = $('.query-results', inputs.dialog);
+
+			// Bind event handlers
+			inputs.dialog.keydown( wpAttachMedia.keydown );
+			inputs.dialog.keyup( wpAttachMedia.keyup );
+			inputs.submit.click( function(e){
+				e.preventDefault();
+				wpAttachMedia.update();
+			});
+			$('#wp-link-cancel').click( function(e){
+				e.preventDefault();
+				wpAttachMedia.close();
+			});
+
+			//rivers.elements.bind('river-select', wpAttachMedia.updateFields );
+
+			inputs.search.keyup( wpAttachMedia.searchMedia );
+
+			inputs.dialog.bind('wpdialogrefresh', wpAttachMedia.refresh);
+
+			//inputs.dialog.bind('wpdialogbeforeopen', wpAttachMedia.beforeOpen);
+
+			//inputs.dialog.bind('wpdialogclose', wpAttachMedia.onClose);
+
+		},
+
+		beforeOpen : function() {
+
+		},
+
+		open : function() {
+			// Initialize the dialog if necessary (html mode).
+			if ( ! inputs.dialog.data('wpdialog') ) {
+				inputs.dialog.wpdialog({
+					title: wpAttachMediaL10n.title,
+					width: 480,
+					height: 'auto',
+					modal: true,
+					dialogClass: 'wp-dialog',
+					zIndex: 300000
+				});
+			}
+
+			inputs.dialog.wpdialog('open');
+		},
+
+		refresh : function() {
+			// Refresh rivers (clear links, check visibility)
+			rivers.search.refresh();
+			rivers.recent.refresh();
+
+			// Load the most recent results if this is the first time opening the panel.
+			if ( ! rivers.recent.ul.children().length )
+				rivers.recent.ajax();
+		},
+
+		close : function() {
+			inputs.dialog.wpdialog('close');
+		},
+
+		onClose: function() {
+
+		},
+
+		update : function() {
+			wpAttachMedia.close();
+		},
+
+		setDefaultValues : function() {
+			// Update save prompt.
+			inputs.submit.val( wpAttachMediaL10n.save );
+		},
+
+		searchMedia : function() {
+			var t = $(this), waiting,
+				search = t.val();
+
+			if ( search.length > 2 ) {
+				rivers.recent.hide();
+				rivers.search.show();
+
+				// Don't search if the keypress didn't change the title.
+				if ( wpAttachMedia.lastSearch == search )
+					return;
+
+				wpAttachMedia.lastSearch = search;
+				waiting = t.parent().find('img.waiting').show();
+
+				rivers.search.change( search );
+				rivers.search.ajax( function(){ waiting.hide(); });
+			} else {
+				rivers.search.hide();
+				rivers.recent.show();
+			}
+		},
+
+		next : function() {
+			rivers.search.next();
+			rivers.recent.next();
+		},
+		prev : function() {
+			rivers.search.prev();
+			rivers.recent.prev();
+		},
+
+		keydown : function( event ) {
+			var fn, key = $.ui.keyCode;
+
+			switch( event.which ) {
+				case key.UP:
+					fn = 'prev';
+				case key.DOWN:
+					fn = fn || 'next';
+					clearInterval( wpAttachMedia.keyInterval );
+					wpAttachMedia[ fn ]();
+					wpAttachMedia.keyInterval = setInterval( wpAttachMedia[ fn ], wpAttachMedia.keySensitivity );
+					break;
+				default:
+					return;
+			}
+			event.preventDefault();
+		},
+		keyup: function( event ) {
+			var key = $.ui.keyCode;
+
+			switch( event.which ) {
+				case key.ESCAPE:
+					event.stopImmediatePropagation();
+					if ( ! $(document).triggerHandler( 'wp_CloseOnEscape', [{ event: event, what: 'wpAttachMedia', cb: wpAttachMedia.close }] ) )
+						wpAttachMedia.close();
+
+					return false;
+					break;
+				case key.UP:
+				case key.DOWN:
+					clearInterval( wpAttachMedia.keyInterval );
+					break;
+				default:
+					return;
+			}
+			event.preventDefault();
+		},
+
+		delayedCallback : function( func, delay ) {
+			var timeoutTriggered, funcTriggered, funcArgs, funcContext;
+
+			if ( ! delay )
+				return func;
+
+			setTimeout( function() {
+				if ( funcTriggered )
+					return func.apply( funcContext, funcArgs );
+				// Otherwise, wait.
+				timeoutTriggered = true;
+			}, delay);
+
+			return function() {
+				if ( timeoutTriggered )
+					return func.apply( this, arguments );
+				// Otherwise, wait.
+				funcArgs = arguments;
+				funcContext = this;
+				funcTriggered = true;
+			};
+		},
+
+	}
+
+	River = function( element, search ) {
+		var self = this;
+		this.element = element;
+		this.ul = element.children('ul');
+		this.waiting = element.find('.river-waiting');
+
+		this.change( search );
+		this.refresh();
+
+		element.scroll( function(){ self.maybeLoad(); });
+		element.delegate('li', 'click', function(e){ self.select( $(this), e ); });
+	};
+
+	$.extend( River.prototype, {
+		refresh: function() {
+			this.deselect();
+			this.visible = this.element.is(':visible');
+		},
+		show: function() {
+			if ( ! this.visible ) {
+				this.deselect();
+				this.element.show();
+				this.visible = true;
+			}
+		},
+		hide: function() {
+			this.element.hide();
+			this.visible = false;
+		},
+		// Selects a list item and triggers the river-select event.
+		select: function( li, event ) {
+			var liHeight, elHeight, liTop, elTop;
+
+			if ( li.hasClass('unselectable') || li == this.selected )
+				return;
+
+			this.deselect();
+			this.selected = li.addClass('selected');
+			// Make sure the element is visible
+			liHeight = li.outerHeight();
+			elHeight = this.element.height();
+			liTop = li.position().top;
+			elTop = this.element.scrollTop();
+
+			if ( liTop < 0 ) // Make first visible element
+				this.element.scrollTop( elTop + liTop );
+			else if ( liTop + liHeight > elHeight ) // Make last visible element
+				this.element.scrollTop( elTop + liTop - elHeight + liHeight );
+
+			// Trigger the river-select event
+			this.element.trigger('river-select', [ li, event, this ]);
+		},
+		deselect: function() {
+			if ( this.selected )
+				this.selected.removeClass('selected');
+			this.selected = false;
+		},
+		prev: function() {
+			if ( ! this.visible )
+				return;
+
+			var to;
+			if ( this.selected ) {
+				to = this.selected.prev('li');
+				if ( to.length )
+					this.select( to );
+			}
+		},
+		next: function() {
+			if ( ! this.visible )
+				return;
+
+			var to = this.selected ? this.selected.next('li') : $('li:not(.unselectable):first', this.element);
+			if ( to.length )
+				this.select( to );
+		},
+		ajax: function( callback ) {
+			var self = this,
+				delay = this.query.page == 1 ? 0 : wpAttachMedia.minRiverAJAXDuration,
+				response = wpAttachMedia.delayedCallback( function( results, params ) {
+					self.process( results, params );
+					if ( callback )
+						callback( results, params );
+				}, delay );
+
+			this.query.ajax( response );
+		},
+		change: function( search ) {
+			if ( this.query && this._search == search )
+				return;
+
+			this._search = search;
+			this.query = new Query( search );
+			this.element.scrollTop(0);
+		},
+		process: function( results, params ) {
+			var list = '', alt = true, classes = '',
+				firstPage = params.page == 1;
+
+			if ( !results ) {
+				if ( firstPage ) {
+					list += '<li class="unselectable"><span class="item-title"><em>'
+					+ wpAttachMediaL10n.noMatchesFound
+					+ '</em></span></li>';
+				}
+			} else {
+				$.each( results, function() {
+					classes = alt ? 'alternate' : '';
+					classes += this['title'] ? '' : ' no-title';
+					list += classes ? '<li class="' + classes + '">' : '<li>';
+					list += '<input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
+					list += '<span class="item-title">';
+					list += this['title'] ? this['title'] : wpAttachMediaL10n.noTitle;
+					list += '</span><span class="item-info">' + this['info'] + '</span></li>';
+					alt = ! alt;
+				});
+			}
+
+			this.ul[ firstPage ? 'html' : 'append' ]( list );
+		},
+		maybeLoad: function() {
+			var self = this,
+				el = this.element,
+				bottom = el.scrollTop() + el.height();
+
+			if ( ! this.query.ready() || bottom < this.ul.height() - wpAttachMedia.riverBottomThreshold )
+				return;
+
+			setTimeout(function() {
+				var newTop = el.scrollTop(),
+					newBottom = newTop + el.height();
+
+				if ( ! self.query.ready() || newBottom < self.ul.height() - wpAttachMedia.riverBottomThreshold )
+					return;
+
+				self.waiting.show();
+				el.scrollTop( newTop + self.waiting.outerHeight() );
+
+				self.ajax( function() { self.waiting.hide(); });
+			}, wpAttachMedia.timeToTriggerRiver );
+		}
+	});
+
+	Query = function( search ) {
+		this.page = 1;
+		this.allLoaded = false;
+		this.querying = false;
+		this.search = search;
+	};
+
+	$.extend( Query.prototype, {
+		ready: function() {
+			return !( this.querying || this.allLoaded );
+		},
+		ajax: function( callback ) {
+			var self = this,
+				query = {
+					action : 'wp-attach-media',
+					page : this.page,
+					'_ajax_attach_media_nonce' : inputs.nonce.val()
+				};
+
+			if ( this.search )
+				query.search = this.search;
+
+			this.querying = true;
+
+			$.post( ajaxurl, query, function(r) {
+				self.page++;
+				self.querying = false;
+				self.allLoaded = !r;
+				callback( r, query );
+			}, "json" );
+		}
+	});
+
+	$(document).ready( wpAttachMedia.init );
 })(jQuery);
Index: wp-admin/upload.php
===================================================================
--- wp-admin/upload.php	(revision 21944)
+++ wp-admin/upload.php	(working copy)
@@ -141,8 +141,6 @@
 $title = __('Media Library');
 $parent_file = 'upload.php';
 
-wp_enqueue_script( 'wp-ajax-response' );
-wp_enqueue_script( 'jquery-ui-draggable' );
 wp_enqueue_script( 'media' );
 
 add_screen_option( 'per_page', array('label' => _x( 'Media items', 'items per page (screen options)' )) );
@@ -240,7 +238,7 @@
 <?php $wp_list_table->display(); ?>
 
 <div id="ajax-response"></div>
-<?php find_posts_div(); ?>
+<?php wp_attach_media_dialog(); ?>
 <br class="clear" />
 
 </form>
