Index: wp-admin/includes/post.php
===================================================================
--- wp-admin/includes/post.php	(revision 20061)
+++ wp-admin/includes/post.php	(working copy)
@@ -1152,7 +1152,7 @@
 	if ( empty( $post_id ) )
 		$post_id = $post_ID;
 
-	$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src('image', $post_id) ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>';
+	$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src( array( 'type'=>'image', 'post_id'=>$post_id ) ) ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>';
 	$content = sprintf($set_thumbnail_link, esc_html__( 'Set featured image' ));
 
 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 20061)
+++ wp-admin/includes/media.php	(working copy)
@@ -392,20 +392,29 @@
 	return "<a href='" . esc_url( get_upload_iframe_src($type) ) . "' id='{$id}-add_{$type}' class='thickbox add_$type' title='" . esc_attr( $title ) . "'><img src='" . esc_url( admin_url( $icon ) ) . "' alt='$title' onclick='return false;' /></a>";
 }
 
-function get_upload_iframe_src( $type = null, $post_id = null ) {
+function get_upload_iframe_src( $args = array() ) {
 	global $post_ID;
 
-	if ( empty( $post_id ) )
-		$post_id = $post_ID;
+	$defaults = array( 'type' => '', 'post_id' => $post_ID );
+	// Backwards compat for first parameter being type
+	if ( is_string( $args ) )
+		$args = array(
+			'type' => $args,
+			'post_id'=> $post_ID
+		);
 
-	$uploading_iframe_ID = (int) $post_id;
-	$upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') );
+	$args = wp_parse_args( $args, $defaults );
 
-	if ( $type && 'media' != $type )
-		$upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
+	$upload_iframe_src = add_query_arg( 'post_id', (int) $args['post_id'], admin_url('media-upload.php') );
 
-	$upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
+	if ( $args['type'] && 'media' != $args['type'] )
+		$upload_iframe_src = add_query_arg('type', $args['type'], $upload_iframe_src);
 
+	if ( ! empty( $args['tab'] ) )
+		$upload_iframe_src = add_query_arg('tab', $args['tab'], $upload_iframe_src);
+
+	$upload_iframe_src = apply_filters($args['type'] . '_upload_iframe_src', $upload_iframe_src);
+
 	return add_query_arg('TB_iframe', true, $upload_iframe_src);
 }
 
@@ -486,7 +495,7 @@
 	if ( isset($send_id) ) {
 		$attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
 
-		$html = $attachment['post_title'];
+		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
 		if ( !empty($attachment['url']) ) {
 			$rel = '';
 			if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
Index: wp-admin/js/custom-background.dev.js
===================================================================
--- wp-admin/js/custom-background.dev.js	(revision 20061)
+++ wp-admin/js/custom-background.dev.js	(working copy)
@@ -10,6 +10,14 @@
 		jQuery('#clearcolor').hide();
 }
 
+function send_to_editor( data ) {
+	data = jQuery.parseJSON(data);
+	jQuery.post(ajaxurl, { action: 'set-background-image', attachment_id: data.id, size: data.size }, function(){
+		tb_remove();
+		window.location.reload();
+	});
+}
+
 jQuery(document).ready(function() {
 	jQuery('#pickcolor').click(function() {
 		jQuery('#colorPickerDiv').show();
Index: wp-admin/custom-header.php
===================================================================
--- wp-admin/custom-header.php	(revision 20061)
+++ wp-admin/custom-header.php	(working copy)
@@ -90,6 +90,10 @@
 		add_action("admin_head-$page", array(&$this, 'take_action'), 50);
 		add_action("admin_head-$page", array(&$this, 'js'), 50);
 		add_action("admin_head-$page", $this->admin_header_callback, 51);
+
+		add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
+		add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
+		add_filter( 'media_send_to_editor', array( $this, 'image_send_to_editor' ), 1, 2 );
 	}
 
 	/**
@@ -140,12 +144,16 @@
 	function js_includes() {
 		$step = $this->step();
 
-		if ( 1 == $step )
-			wp_enqueue_script('jquery-masonry');
-		if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
-			wp_enqueue_script('farbtastic');
-		elseif ( 2 == $step )
-			wp_enqueue_script('imgareaselect');
+		if ( ( 1 == $step || 3 == $step ) ) {
+			add_thickbox();
+			wp_enqueue_script('media-upload');
+			if ( $this->header_text() )
+				wp_enqueue_script('farbtastic');
+			if ( 1 == $step )
+				wp_enqueue_script('jquery-masonry');
+		} elseif ( 2 == $step ) {
+			 wp_enqueue_script('imgareaselect');
+		}
 	}
 
 	/**
@@ -327,10 +335,13 @@
 	 */
 	function js() {
 		$step = $this->step();
-		if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
-			$this->js_1();
-		elseif ( 2 == $step )
+		if ( ( 1 == $step || 3 == $step ) ) {
+			$this->js_0();
+			if ( $this->header_text() )
+				$this->js_1();
+		} elseif ( 2 == $step ) {
 			$this->js_2();
+		}
 	}
 
 	/**
@@ -338,6 +349,23 @@
 	 *
 	 * @since 2.6.0
 	 */
+	function js_0() { ?>
+<script type="text/javascript">
+/* <![CDATA[ */
+	step2_url = '<?php echo add_query_arg( array( 'page' => 'custom-header', 'step' => 2, '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload') ), admin_url('themes.php') ); ?>';
+	function send_to_editor( file_id ) {
+		window.location = step2_url + '&file=' + file_id;
+	}
+/* ]]> */
+</script>
+<?php
+	}
+
+	/**
+	 * Display Javascript based on Step 1 and 3 if header text is active.
+	 *
+	 * @since 2.6.0
+	 */
 	function js_1() { ?>
 <script type="text/javascript">
 /* <![CDATA[ */
@@ -583,6 +611,12 @@
 		<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
 	</p>
 	</form>
+	<?php
+		$image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
+		$image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
+		$image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
+	?>
+	<span class="howto"><?php _ex( 'or', 'Custom Header: Choose an image from your computer - or - Choose from image library' ); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
 </td>
 </tr>
 <?php endif; ?>
@@ -706,29 +740,15 @@
 		if ( ! current_theme_supports( 'custom-header-uploads' ) )
 			wp_die( __( 'Cheatin&#8217; uh?' ) );
 
-		$overrides = array('test_form' => false);
-		$file = wp_handle_upload($_FILES['import'], $overrides);
+		if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
+			$id = absint( $_GET['file'] );
+			$file = get_attached_file( $id, true );
+			$url = wp_get_attachment_image_src( $id, 'full');
+			$url = $url[0];
+		} else if ( isset( $_POST ) ) {
+			extract($this->step_2_manage_upload());
+		}
 
-		if ( isset($file['error']) )
-			wp_die( $file['error'],  __( 'Image Upload Error' ) );
-
-		$url = $file['url'];
-		$type = $file['type'];
-		$file = $file['file'];
-		$filename = basename($file);
-
-		// Construct the object array
-		$object = array(
-		'post_title' => $filename,
-		'post_content' => $url,
-		'post_mime_type' => $type,
-		'guid' => $url,
-		'context' => 'custom-header'
-		);
-
-		// Save the data
-		$id = wp_insert_attachment($object, $file);
-
 		list($width, $height, $type, $attr) = getimagesize( $file );
 
 		$header_support = get_theme_support( 'custom-header' );
@@ -785,6 +805,9 @@
 	<input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
 	<input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
+	<?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
+	<input type="hidden" name="new-attachment" value="true" />
+	<?php } ?>
 	<?php wp_nonce_field( 'custom-header-crop-image' ) ?>
 
 	<?php submit_button( __( 'Crop and Publish' ) ); ?>
@@ -794,6 +817,33 @@
 		<?php
 	}
 
+
+	function step_2_manage_upload() {
+		$overrides = array('test_form' => false);
+		$file = wp_handle_upload($_FILES['import'], $overrides);
+
+		if ( isset($file['error']) )
+			wp_die( $file['error'],  __( 'Image Upload Error' ) );
+
+		$url = $file['url'];
+		$type = $file['type'];
+		$file = $file['file'];
+		$filename = basename($file);
+
+		// Construct the object array
+		$object = array(
+			'post_title'     => $filename,
+			'post_content'   => $url,
+			'post_mime_type' => $type,
+			'guid'           => $url,
+			'context'        => 'custom-header'
+		);
+
+		// Save the data
+		$id = wp_insert_attachment( $object, $file );
+		return compact( 'id', 'file', 'filename', 'url', 'type' );
+	}
+
 	/**
 	 * Display third step of custom header image page.
 	 *
@@ -859,9 +909,11 @@
 			'guid' => $url,
 			'context' => 'custom-header'
 		);
+		if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] )
+			unset($object['ID']);
 
 		// Update the attachment
-		wp_insert_attachment($object, $cropped);
+		$attachment_id = wp_insert_attachment( $object, $cropped );
 		wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
 		update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
 
@@ -878,8 +930,10 @@
 
 		// cleanup
 		$medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
-		@unlink( apply_filters( 'wp_delete_file', $medium ) );
-		@unlink( apply_filters( 'wp_delete_file', $original ) );
+		if ( file_exists( $medium ) )
+			@unlink( apply_filters( 'wp_delete_file', $medium ) );
+		if ( empty ( $_POST['new-attachment'] ) )
+			@unlink( apply_filters( 'wp_delete_file', $original ) );
 
 		return $this->finished();
 	}
@@ -911,4 +965,31 @@
 			$this->step_3();
 	}
 
+	function attachment_fields_to_edit( $form_fields, $post ) {
+		if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
+			$form_fields = array(); // Reset
+			$button = get_submit_button( __( 'Use as header image' ), 'button', "send[{$post->ID}]", false );
+			$form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
+			$form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
+		}
+
+		return $form_fields;
+	}
+
+	function image_send_to_editor( $html, $id ) {
+		if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-header' ) {
+			remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
+			return $id;
+		}
+
+		return $html;
+	}
+
+	function filter_upload_tabs( $tabs ) {
+		if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' )
+			return array( 'library' => __('Media Library') );
+
+		return $tabs;
+	}
+
 }
Index: wp-admin/custom-background.php
===================================================================
--- wp-admin/custom-background.php	(revision 20061)
+++ wp-admin/custom-background.php	(working copy)
@@ -53,6 +53,7 @@
 	function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
 		$this->admin_header_callback = $admin_header_callback;
 		$this->admin_image_div_callback = $admin_image_div_callback;
+		add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
 	}
 
 	/**
@@ -69,6 +70,9 @@
 		add_action("load-$page", array(&$this, 'admin_load'));
 		add_action("load-$page", array(&$this, 'take_action'), 49);
 		add_action("load-$page", array(&$this, 'handle_upload'), 49);
+		add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
+		add_filter( 'media_send_to_editor', array( $this, 'image_send_to_editor' ), 1, 3 );
+		add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
 
 		if ( $this->admin_header_callback )
 			add_action("admin_head-$page", $this->admin_header_callback, 51);
@@ -96,6 +100,8 @@
 			'<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
 		);
 
+		add_thickbox();
+		wp_enqueue_script('media-upload');
 		wp_enqueue_script('custom-background');
 		wp_enqueue_style('farbtastic');
 	}
@@ -246,7 +252,13 @@
 <input type="hidden" name="action" value="save" />
 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
-</form>
+<?php
+	$image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
+	$image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
+	$image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
+?>
+	</form>
+	<span class="howto"><?php _ex( 'or', 'Custom Background: Choose an image from your computer - or - Choose from image library' ); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
 </td>
 </tr>
 </tbody>
@@ -366,4 +378,46 @@
 		$this->updated = true;
 	}
 
+	function attachment_fields_to_edit( $form_fields, $post ) {
+		if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
+			$form_fields = array( 'image-size' => $form_fields['image-size'] ); // Reset
+			$button = get_submit_button( __( 'Use as background image' ), 'button', "send[{$post->ID}]", false );
+			$form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
+			$form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
+		}
+
+		return $form_fields;
+	}
+
+	function image_send_to_editor( $html, $id, $size ) {
+		if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-background' ) {
+			remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
+			return json_encode( array( 'id' => $id, 'size' => $size['image-size'] ) );
+		}
+
+		return $html;
+	}
+
+	function filter_upload_tabs ( $tabs ){
+		if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
+			return array( 'library' => __('Media Library') );
+
+		return $tabs;
+	}
+
+	public function wp_set_background_image() {
+		if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
+		$attachment_id = absint($_POST['attachment_id']);
+		$sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
+		$size = 'thumbnail';
+		if ( in_array( $_POST['size'], $sizes ) )
+			$size = esc_attr( $_POST['size'] );
+
+		update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
+		$url = wp_get_attachment_image_src( $attachment_id, $size );
+		$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
+		set_theme_mod( 'background_image', esc_url( $url[0] ) );
+		set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
+		exit;
+	}
 }
