Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 21377)
+++ wp-admin/includes/media.php	(working copy)
@@ -6,13 +6,77 @@
  * @subpackage Administration
  */
 
+add_action( 'media_buttons', 		'media_buttons' );
+
+add_filter( 'async_upload_image', 	'get_media_item', 10, 2 );
+add_filter( 'async_upload_audio', 	'get_media_item', 10, 2 );
+add_filter( 'async_upload_video', 	'get_media_item', 10, 2 );
+add_filter( 'async_upload_file',  	'get_media_item', 10, 2 );
+
+add_action( 'media_upload_image', 	'wp_media_upload_handler' );
+add_action( 'media_upload_audio', 	'wp_media_upload_handler' );
+add_action( 'media_upload_video', 	'wp_media_upload_handler' );
+add_action( 'media_upload_file',  	'wp_media_upload_handler' );
+
+add_filter( 'media_upload_gallery', 'media_upload_iframe' );
+add_filter( 'media_upload_library', 'media_upload_iframe' );
+
+add_filter( 'media_upload_tabs', 	'update_gallery_tab');
+add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
+
+
 /**
- * {@internal Missing Short Description}}
+ * Echoes the html for media upload buttons
+ * added to the action call for 'media_buttons' 
  *
+ * @uses get_upload_iframe_src()
  * @since 2.5.0
+ */
+function media_buttons($editor_id = 'content') {
+	$context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
+
+	$img = '<img src="' . esc_url( admin_url( 'images/media-button.png?ver=20111005' ) ) . '" width="15" height="15" />';
+
+	echo '<a href="' . esc_url( get_upload_iframe_src() ) . '" class="thickbox add_media" id="' . esc_attr( $editor_id ) . '-add_media" title="' . esc_attr__( 'Add Media' ) . '" onclick="return false;">' . sprintf( $context, $img ) . '</a>';
+}
+
+/**
+ * Determine url of the upload thickbox to be used in the media_buttons() link
  *
- * @return unknown
+ * @uses apply_filters() calls $type . '_upload_iframe_src'
+ * @param string $type
+ * @param int $post_id
+ * @param string $tab
+ * @since unknown
  */
+function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
+	global $post_ID;
+
+	if ( empty( $post_id ) )
+		$post_id = $post_ID;
+
+	$upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
+
+	if ( $type && 'media' != $type )
+		$upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
+
+	if ( ! empty( $tab ) )
+		$upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
+
+	$upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
+
+	return add_query_arg('TB_iframe', true, $upload_iframe_src);
+}
+
+/**
+ * Defines media tabs to be used in thickbox 
+ *
+ * @since 2.5.0
+ *
+ * @uses apply_filters() calls media_upload_tabs
+ *
+ * @return an associative array of tab names in the form of type=>name
+ */
 function media_upload_tabs() {
 	$_default_tabs = array(
 		'type' => __('From Computer'), // handler action suffix => tab text
@@ -25,12 +89,13 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Added to media_upload_tabs filter to define the gallery tab
+ * checks for attachments and removes gallery tab if the current post has none.
  *
  * @since 2.5.0
  *
- * @param unknown_type $tabs
- * @return unknown
+ * @param array $tabs
+ * @return an associative array of tab names in the form of type=>name
  */
 function update_gallery_tab($tabs) {
 	global $wpdb;
@@ -54,10 +119,9 @@
 
 	return $tabs;
 }
-add_filter('media_upload_tabs', 'update_gallery_tab');
 
 /**
- * {@internal Missing Short Description}}
+ * Echoes the thickbox media upload tabs to the screen
  *
  * @since 2.5.0
  */
@@ -65,9 +129,10 @@
 	global $redir_tab;
 	$tabs = media_upload_tabs();
 	$default = 'type';
-
+	$html = '';
+	
 	if ( !empty($tabs) ) {
-		echo "<ul id='sidemenu'>\n";
+		$html = "<ul id='sidemenu'>\n";
 		if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
 			$current = $redir_tab;
 		elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
@@ -83,17 +148,19 @@
 
 			$href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
 			$link = "<a href='" . esc_url($href) . "'$class>$text</a>";
-			echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
+			$html .= "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
 		}
-		echo "</ul>\n";
+		$html .= "</ul>\n";
 	}
+	echo $html;
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Creates the html for an image as it is added to the editor
  *
  * @since 2.5.0
  *
+ * @uses apply_filters() calls image_send_to_editor
  * @param unknown_type $id
  * @param unknown_type $alt
  * @param unknown_type $title
@@ -101,7 +168,7 @@
  * @param unknown_type $url
  * @param unknown_type $rel
  * @param unknown_type $size
- * @return unknown
+ * @return string - the html for an image tag
  */
 function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
 
@@ -118,7 +185,7 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Wraps an image in the caption code
  *
  * @since 2.6.0
  *
@@ -156,7 +223,6 @@
 
 	return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
 }
-add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
 
 /**
  * Private preg_replace callback used in image_add_caption()
@@ -170,11 +236,11 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Echoes the javascript used to send html back to the editor
  *
  * @since 2.5.0
  *
- * @param unknown_type $html
+ * @param string $html
  */
 function media_send_to_editor($html) {
 ?>
@@ -189,8 +255,6 @@
 }
 
 /**
- * {@internal Missing Short Description}}
- *
  * This handles the file upload POST itself, creating the attachment post.
  *
  * @since 2.5.0
@@ -260,6 +324,11 @@
  *
  * @since 2.6.0
  *
+ * @uses wp_handle_sideload
+ * @uses wp_read_image_metadata
+ * @uses wp_insert_attachment
+ * @uses wp_update_attachment_metadata
+ * @uses wp_generate_attachment_metadata
  * @param array $file_array Array similar to a {@link $_FILES} upload array
  * @param int $post_id The post ID the media is associated with
  * @param string $desc Description of the sideloaded file
@@ -312,8 +381,49 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Pass thru to media_upload_iframe() for backcompat - not actually used anywhere
+ */
+function media_upload_gallery() {
+	media_upload_iframe( 'gallery' );
+}
+
+/**
+ * Pass thru to media_upload_iframe() for backcompat - not actually used anywhere
+ */
+function media_upload_library() {
+	media_upload_iframe( 'library' );
+}
+
+/**
+ * Returns wp_iframe with the proper upload form for media uploads
+ * added to the media_upload filter in media-upload.php
  *
+ * @since 3.5.0
+ *
+ * @return HTML from the wp_iframe function
+ */
+function media_upload_iframe() {
+	$errors = array();
+	$current_filter = current_filter();
+	$type = str_replace( 'media_upload_' , '' , $current_filter );
+	
+	if ( !empty($_POST) || empty($type) ) {
+		$return = media_upload_form_handler();
+		if ( is_string($return) )
+			return $return;
+		if ( is_array($return) )
+			$errors = $return;
+	}
+
+	wp_enqueue_script('admin-gallery');
+
+	wp_iframe( 'media_upload_form_html' , $type , $errors );
+
+}
+
+/**
+ * Generates html for the iframe used in the thickbox window for uploads
+ *
  * Wrap iframe content (produced by $content_func) in a doctype, html head/body
  * etc any additional function args will be passed to content_func.
  *
@@ -324,6 +434,7 @@
 function wp_iframe($content_func /* ... */) {
 	_wp_admin_html_begin();
 ?>
+
 <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
 <?php
 
@@ -372,39 +483,7 @@
 <?php
 }
 
-/**
- * {@internal Missing Short Description}}
- *
- * @since 2.5.0
- */
-function media_buttons($editor_id = 'content') {
-	$context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
 
-	$img = '<img src="' . esc_url( admin_url( 'images/media-button.png?ver=20111005' ) ) . '" width="15" height="15" />';
-
-	echo '<a href="' . esc_url( get_upload_iframe_src() ) . '" class="thickbox add_media" id="' . esc_attr( $editor_id ) . '-add_media" title="' . esc_attr__( 'Add Media' ) . '" onclick="return false;">' . sprintf( $context, $img ) . '</a>';
-}
-add_action( 'media_buttons', 'media_buttons' );
-
-function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
-	global $post_ID;
-
-	if ( empty( $post_id ) )
-		$post_id = $post_ID;
-
-	$upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
-
-	if ( $type && 'media' != $type )
-		$upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
-
-	if ( ! empty( $tab ) )
-		$upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
-
-	$upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
-
-	return add_query_arg('TB_iframe', true, $upload_iframe_src);
-}
-
 /**
  * {@internal Missing Short Description}}
  *
@@ -553,7 +632,7 @@
 
 		return media_send_to_editor($html);
 	}
-
+	
 	if ( !empty($_POST) ) {
 		$return = media_upload_form_handler();
 
@@ -565,16 +644,16 @@
 
 	if ( isset($_POST['save']) ) {
 		$errors['upload_notice'] = __('Saved.');
-		return media_upload_gallery();
+		return media_upload('gallery');
 	}
-
+	
 	if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
-		$type = 'image';
+		$type = 'url';
 		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
 			$type = $_GET['type'];
+		
 		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
 	}
-
 	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
 }
 
@@ -625,50 +704,6 @@
 }
 
 /**
- * {@internal Missing Short Description}}
- *
- * @since 2.5.0
- *
- * @return unknown
- */
-function media_upload_gallery() {
-	$errors = array();
-
-	if ( !empty($_POST) ) {
-		$return = media_upload_form_handler();
-
-		if ( is_string($return) )
-			return $return;
-		if ( is_array($return) )
-			$errors = $return;
-	}
-
-	wp_enqueue_script('admin-gallery');
-	return wp_iframe( 'media_upload_gallery_form', $errors );
-}
-
-/**
- * {@internal Missing Short Description}}
- *
- * @since 2.5.0
- *
- * @return unknown
- */
-function media_upload_library() {
-	$errors = array();
-	if ( !empty($_POST) ) {
-		$return = media_upload_form_handler();
-
-		if ( is_string($return) )
-			return $return;
-		if ( is_array($return) )
-			$errors = $return;
-	}
-
-	return wp_iframe( 'media_upload_library_form', $errors );
-}
-
-/**
  * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  *
  * @since 2.7.0
@@ -1407,87 +1442,49 @@
 	do_action('post-upload-ui');
 }
 
-/**
- * {@internal Missing Short Description}}
- *
- * @since 2.5.0
- *
- * @param unknown_type $type
- * @param unknown_type $errors
- * @param unknown_type $id
+// Start replaced functions
+
+/*
+ * Pass thru for backcompat - cot actually used anywhere
  */
-function media_upload_type_form($type = 'file', $errors = null, $id = null) {
-
-	media_upload_header();
-
-	$post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
-
-	$form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
-	$form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
-	$form_class = 'media-upload-form type-form validate';
-
-	if ( get_user_setting('uploader') )
-		$form_class .= ' html-uploader';
-?>
-
-<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
-<?php submit_button( '', 'hidden', 'save', false ); ?>
-<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
-<?php wp_nonce_field('media-form'); ?>
-
-<h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
-
-<?php media_upload_form( $errors ); ?>
-
-<script type="text/javascript">
-//<![CDATA[
-jQuery(function($){
-	var preloaded = $(".media-item.preloaded");
-	if ( preloaded.length > 0 ) {
-		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
-	}
-	updateMediaForm();
-});
-//]]>
-</script>
-<div id="media-items"><?php
-
-if ( $id ) {
-	if ( !is_wp_error($id) ) {
-		add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
-		echo get_media_items( $id, $errors );
-	} else {
-		echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
-		exit;
-	}
+function media_upload_type_form($type = 'image', $errors = null, $id = null) {
+	media_upload_form_html( $type, $errors, $id);
 }
-?></div>
-
-<p class="savebutton ml-submit">
-<?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
-</p>
-</form>
-<?php
+/*
+ * Pass thru for backcompat - cot actually used anywhere
+ */
+function media_upload_type_url_form($type = 'url', $errors = null, $id = null) {
+	media_upload_form_html( $type , $errors , $id );
 }
-
-/**
- * {@internal Missing Short Description}}
- *
- * @since 2.7.0
- *
- * @param unknown_type $type
- * @param unknown_type $errors
- * @param unknown_type $id
+/*
+ * Pass thru for backcompat - cot actually used anywhere
  */
-function media_upload_type_url_form($type = null, $errors = null, $id = null) {
-	if ( null === $type )
-		$type = 'image';
+function media_upload_gallery_form( $errors ) {
+	media_upload_form_html( 'gallery' , $errors );
+}
+/*
+ * Pass thru for backcompat - cot actually used anywhere
+ */
+function media_upload_library_form($errors) {
+	media_upload_form_html( 'library' , $errors );
+}
 
+// new upload form function
+
+function media_upload_form_html($type = NULL, $errors = null, $id = null) {
+	
+	global $wpdb, $wp_query, $wp_locale, $post_mime_types;
+	
+	if ( $type == 'url' )
+		$action = 'image';
+	else
+		$action = $type;
+		
 	media_upload_header();
-
+	
 	$post_id = intval($_REQUEST['post_id']);
 
-	$form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
+	$form_action_url = admin_url("media-upload.php?type=$action&tab=type&post_id=$post_id");
 	$form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
 	$form_class = 'media-upload-form type-form validate';
 
@@ -1495,434 +1492,425 @@
 		$form_class .= ' html-uploader';
 ?>
 
-<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
-<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
-<?php wp_nonce_field('media-form'); ?>
+	<?php if ( $type == 'gallery' ) { ?>
 
-<h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
+		<div id="sort-buttons" class="hide-if-no-js">
+		<span>
+		<?php _e('All Tabs:'); ?>
+		<a href="#" id="showall"><?php _e('Show'); ?></a>
+		<a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
+		</span>
+		<?php _e('Sort Order:'); ?>
+		<a href="#" id="asc"><?php _e('Ascending'); ?></a> |
+		<a href="#" id="desc"><?php _e('Descending'); ?></a> |
+		<a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
+		</div>
+	
+	<?php }; ?>
 
-<script type="text/javascript">
-//<![CDATA[
-var addExtImage = {
+	<?php if ( $type != 'file' && $type != 'image' ) { ?>
 
-	width : '',
-	height : '',
-	align : 'alignnone',
+		<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="<?php echo $action; ?>-form">
+		<?php submit_button( '', 'hidden', 'save', false ); ?>
+		<?php wp_nonce_field('media-form'); ?>
+		<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
 
-	insert : function() {
-		var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
+	<?php }; ?>
+	
+	<?php if ( $type == 'file' || $type == 'image' ) { ?>
+				
+		<h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
+		
+		<?php 
+		$form_args = array(
+				'post_id'		=> (int) $post_id,
+				'type' 			=> 'file',
+				'inline' 		=> false,
+				'form_class'	=> $form_class,
+				'errors'		=> $errors,		
+		);
+		wp_upload_form( $form_args );
+		?>
 
-		if ( '' == f.src.value || '' == t.width )
-			return false;
+	<?php } elseif ( $type == 'library' )  { ?>
+		
+		<?php
+			$_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
+			if ( $_GET['paged'] < 1 )
+				$_GET['paged'] = 1;
+			$start = ( $_GET['paged'] - 1 ) * 10;
+			if ( $start < 1 )
+				$start = 0;
+			add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
+		
+			list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
+		?>
+		<input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
+		<input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
+		<input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
+		<input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
+		<input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
 
-		if ( f.title.value ) {
-			title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
-			title = ' title="'+title+'"';
+		<p id="media-search" class="search-box">
+			<label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
+			<input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
+			<?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
+		</p>
+		
+		<ul class="subsubsub">
+		<?php
+		$type_links = array();
+		$_num_posts = (array) wp_count_attachments();
+		$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
+		foreach ( $matches as $_type => $reals )
+			foreach ( $reals as $real )
+				if ( isset($num_posts[$_type]) )
+					$num_posts[$_type] += $_num_posts[$real];
+				else
+					$num_posts[$_type] = $_num_posts[$real];
+		// If available type specified by media button clicked, filter by that type
+		if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
+			$_GET['post_mime_type'] = $type;
+			list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
 		}
-
-		if ( f.alt.value )
-			alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
-
-<?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
-		if ( f.caption.value ) {
-			caption = f.caption.value.replace(/\r\n|\r/g, '\n');
-			caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
-				return a.replace(/[\r\n\t]+/, ' ');
-			});
-
-			caption = caption.replace(/\s*\n\s*/g, '<br />');
+		if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
+			$class = ' class="current"';
+		else
+			$class = '';
+		$type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
+		foreach ( $post_mime_types as $mime_type => $label ) {
+			$class = '';
+		
+			if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
+				continue;
+		
+			if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
+				$class = ' class="current"';
+		
+			$type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
 		}
-<?php } ?>
-
-		cls = caption ? '' : ' class="'+t.align+'"';
-
-		html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
-
-		if ( f.url.value ) {
-			url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
-			html = '<a href="'+url+'">'+html+'</a>';
+		echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
+		unset($type_links);
+		?>
+		</ul>
+		
+		<div class="tablenav">
+		
+		<?php
+		$page_links = paginate_links( array(
+			'base' => add_query_arg( 'paged', '%#%' ),
+			'format' => '',
+			'prev_text' => __('&laquo;'),
+			'next_text' => __('&raquo;'),
+			'total' => ceil($wp_query->found_posts / 10),
+			'current' => $_GET['paged']
+		));
+		
+		if ( $page_links )
+			echo "<div class='tablenav-pages'>$page_links</div>";
+		?>
+		
+		<div class="alignleft actions">
+		<?php
+		
+		$arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
+		
+		$arc_result = $wpdb->get_results( $arc_query );
+		
+		$month_count = count($arc_result);
+		
+		if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
+		<select name='m'>
+		<option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
+		<?php
+		foreach ($arc_result as $arc_row) {
+			if ( $arc_row->yyear == 0 )
+				continue;
+			$arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
+		
+			if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
+				$default = ' selected="selected"';
+			else
+				$default = '';
+		
+			echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
+			echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
+			echo "</option>\n";
 		}
+		?>
+		</select>
+		<?php } ?>
+		
+		<?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
+		
+		</div>
+		
+		<br class="clear" />
+		</div>
+		</form>
+		
+		<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="library-form">
+		
+		<?php wp_nonce_field('media-form'); ?>
+		<?php //media_upload_form( $errors ); ?>
+		
+		<?php media_upload_form_js('library'); ?>
 
-		if ( caption )
-			html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
+		
+		<div id="media-items">
+		<?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
+		<?php echo get_media_items(null, $errors); ?>
+		</div>
+		<p class="ml-submit">
+		<?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
+		<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
+		</p>
+		</form>
+		
+	<?php } elseif ( $type == 'gallery' )  { ?>
 
-		var win = window.dialogArguments || opener || parent || top;
-		win.send_to_editor(html);
-		return false;
-	},
+		<?php media_upload_form_js('library'); ?>
 
-	resetImageData : function() {
-		var t = addExtImage;
+		<table class="widefat" cellspacing="0">
+			<thead><tr>
+				<th><?php _e('Media'); ?></th>
+				<th class="order-head"><?php _e('Order'); ?></th>
+				<th class="actions-head"><?php _e('Actions'); ?></th>
+			</tr></thead>
+		</table>
+		<div id="media-items">
+		<?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
+		<?php echo get_media_items($post_id, $errors); ?>
+		</div>
+		
+		<p class="ml-submit">
+		<?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
+		<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
+		<input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
+		<input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
+		</p>
+		
+		<div id="gallery-settings" style="display:none;">
+		<div class="title"><?php _e('Gallery Settings'); ?></div>
+		<table id="basic" class="describe"><tbody>
+			<tr>
+			<th scope="row" class="label">
+				<label>
+				<span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
+				</label>
+			</th>
+			<td class="field">
+				<input type="radio" name="linkto" id="linkto-file" value="file" />
+				<label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
+		
+				<input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
+				<label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
+			</td>
+			</tr>
+		
+			<tr>
+			<th scope="row" class="label">
+				<label>
+				<span class="alignleft"><?php _e('Order images by:'); ?></span>
+				</label>
+			</th>
+			<td class="field">
+				<select id="orderby" name="orderby">
+					<option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
+					<option value="title"><?php _e('Title'); ?></option>
+					<option value="post_date"><?php _e('Date/Time'); ?></option>
+					<option value="rand"><?php _e('Random'); ?></option>
+				</select>
+			</td>
+			</tr>
+		
+			<tr>
+			<th scope="row" class="label">
+				<label>
+				<span class="alignleft"><?php _e('Order:'); ?></span>
+				</label>
+			</th>
+			<td class="field">
+				<input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
+				<label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
+		
+				<input type="radio" name="order" id="order-desc" value="desc" />
+				<label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
+			</td>
+			</tr>
+		
+			<tr>
+			<th scope="row" class="label">
+				<label>
+				<span class="alignleft"><?php _e('Gallery columns:'); ?></span>
+				</label>
+			</th>
+			<td class="field">
+				<select id="columns" name="columns">
+					<option value="1">1</option>
+					<option value="2">2</option>
+					<option value="3" selected="selected">3</option>
+					<option value="4">4</option>
+					<option value="5">5</option>
+					<option value="6">6</option>
+					<option value="7">7</option>
+					<option value="8">8</option>
+					<option value="9">9</option>
+				</select>
+			</td>
+			</tr>
+		</tbody></table>
+		
+		<p class="ml-submit">
+		<input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
+		<input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
+		</p>
+		</div>
+		</form>		
 
-		t.width = t.height = '';
-		document.getElementById('go_button').style.color = '#bbb';
-		if ( ! document.forms[0].src.value )
-			document.getElementById('status_img').innerHTML = '*';
-		else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
-	},
+	<?php } elseif ( $type == 'url' )  { ?>
 
-	updateImageData : function() {
-		var t = addExtImage;
+		<h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
+		
+		<?php media_upload_form_js('url'); ?>
 
-		t.width = t.preloadImg.width;
-		t.height = t.preloadImg.height;
-		document.getElementById('go_button').style.color = '#333';
-		document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
-	},
+		<div id="media-items">
+		<div class="media-item media-blank">
+		<?php echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( 'image' ) ); ?>
+		</div>
+		</div>
+		</form>
+		
+	<?php }; ?>
 
-	getImageData : function() {
-		if ( jQuery('table.describe').hasClass('not-image') )
-			return;
-
-		var t = addExtImage, src = document.forms[0].src.value;
-
-		if ( ! src ) {
-			t.resetImageData();
-			return false;
-		}
-
-		document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />';
-		t.preloadImg = new Image();
-		t.preloadImg.onload = t.updateImageData;
-		t.preloadImg.onerror = t.resetImageData;
-		t.preloadImg.src = src;
-	}
-}
-
-jQuery(document).ready( function($) {
-	$('.media-types input').click( function() {
-		$('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
-	});
-});
-
-//]]>
-</script>
-
-<div id="media-items">
-<div class="media-item media-blank">
-<?php echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) ); ?>
-</div>
-</div>
-</form>
 <?php
 }
 
-/**
- * {@internal Missing Short Description}}
+/* 
+ * Adds javascript for $type of form.
+ * Used to be all split and added by each function
  *
- * @since 2.5.0
- *
- * @param unknown_type $errors
+ * @param string $type - which type of upload form - 'file', 'gallery', 'library', 'url'
  */
-function media_upload_gallery_form($errors) {
-	global $redir_tab, $type;
+function media_upload_form_js( $type=NULL) {
 
-	$redir_tab = 'gallery';
-	media_upload_header();
-
-	$post_id = intval($_REQUEST['post_id']);
-	$form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
-	$form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
-	$form_class = 'media-upload-form validate';
-
-	if ( get_user_setting('uploader') )
-		$form_class .= ' html-uploader';
+	if ( $type == 'file' || $type == 'gallery' || $type == 'library' ) {
 ?>
-
-<script type="text/javascript">
-<!--
-jQuery(function($){
-	var preloaded = $(".media-item.preloaded");
-	if ( preloaded.length > 0 ) {
-		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
+	<script type="text/javascript">
+	//<![CDATA[
+	jQuery(function($){
+		var preloaded = $(".media-item.preloaded");
+		if ( preloaded.length > 0 ) {
+			preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
+		}
 		updateMediaForm();
+	});
+	//]]>
+	</script>
+<?php } elseif ( $type == 'url' ) { ?>
+	<script type="text/javascript">
+	//<![CDATA[
+	var addExtImage = {
+	
+		width : '',
+		height : '',
+		align : 'alignnone',
+	
+		insert : function() {
+			var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
+	
+			if ( '' == f.src.value || '' == t.width )
+				return false;
+	
+			if ( f.title.value ) {
+				title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+				title = ' title="'+title+'"';
+			}
+	
+			if ( f.alt.value )
+				alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+	
+	<?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
+			if ( f.caption.value ) {
+				caption = f.caption.value.replace(/\r\n|\r/g, '\n');
+				caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
+					return a.replace(/[\r\n\t]+/, ' ');
+				});
+	
+				caption = caption.replace(/\s*\n\s*/g, '<br />');
+			}
+	<?php } ?>
+	
+			cls = caption ? '' : ' class="'+t.align+'"';
+	
+			html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
+	
+			if ( f.url.value ) {
+				url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+				html = '<a href="'+url+'">'+html+'</a>';
+			}
+	
+			if ( caption )
+				html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
+	
+			var win = window.dialogArguments || opener || parent || top;
+			win.send_to_editor(html);
+			return false;
+		},
+	
+		resetImageData : function() {
+			var t = addExtImage;
+	
+			t.width = t.height = '';
+			document.getElementById('go_button').style.color = '#bbb';
+			if ( ! document.forms[0].src.value )
+				document.getElementById('status_img').innerHTML = '*';
+			else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
+		},
+	
+		updateImageData : function() {
+			var t = addExtImage;
+	
+			t.width = t.preloadImg.width;
+			t.height = t.preloadImg.height;
+			document.getElementById('go_button').style.color = '#333';
+			document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
+		},
+	
+		getImageData : function() {
+			if ( jQuery('table.describe').hasClass('not-image') )
+				return;
+	
+			var t = addExtImage, src = document.forms[0].src.value;
+	
+			if ( ! src ) {
+				t.resetImageData();
+				return false;
+			}
+	
+			document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />';
+			t.preloadImg = new Image();
+			t.preloadImg.onload = t.updateImageData;
+			t.preloadImg.onerror = t.resetImageData;
+			t.preloadImg.src = src;
+		}
 	}
-});
--->
-</script>
-<div id="sort-buttons" class="hide-if-no-js">
-<span>
-<?php _e('All Tabs:'); ?>
-<a href="#" id="showall"><?php _e('Show'); ?></a>
-<a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
-</span>
-<?php _e('Sort Order:'); ?>
-<a href="#" id="asc"><?php _e('Ascending'); ?></a> |
-<a href="#" id="desc"><?php _e('Descending'); ?></a> |
-<a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
-</div>
-<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
-<?php wp_nonce_field('media-form'); ?>
-<?php //media_upload_form( $errors ); ?>
-<table class="widefat" cellspacing="0">
-<thead><tr>
-<th><?php _e('Media'); ?></th>
-<th class="order-head"><?php _e('Order'); ?></th>
-<th class="actions-head"><?php _e('Actions'); ?></th>
-</tr></thead>
-</table>
-<div id="media-items">
-<?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
-<?php echo get_media_items($post_id, $errors); ?>
-</div>
-
-<p class="ml-submit">
-<?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
-<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
-<input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
-<input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
-</p>
-
-<div id="gallery-settings" style="display:none;">
-<div class="title"><?php _e('Gallery Settings'); ?></div>
-<table id="basic" class="describe"><tbody>
-	<tr>
-	<th scope="row" class="label">
-		<label>
-		<span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
-		</label>
-	</th>
-	<td class="field">
-		<input type="radio" name="linkto" id="linkto-file" value="file" />
-		<label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
-
-		<input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
-		<label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
-	</td>
-	</tr>
-
-	<tr>
-	<th scope="row" class="label">
-		<label>
-		<span class="alignleft"><?php _e('Order images by:'); ?></span>
-		</label>
-	</th>
-	<td class="field">
-		<select id="orderby" name="orderby">
-			<option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
-			<option value="title"><?php _e('Title'); ?></option>
-			<option value="post_date"><?php _e('Date/Time'); ?></option>
-			<option value="rand"><?php _e('Random'); ?></option>
-		</select>
-	</td>
-	</tr>
-
-	<tr>
-	<th scope="row" class="label">
-		<label>
-		<span class="alignleft"><?php _e('Order:'); ?></span>
-		</label>
-	</th>
-	<td class="field">
-		<input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
-		<label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
-
-		<input type="radio" name="order" id="order-desc" value="desc" />
-		<label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
-	</td>
-	</tr>
-
-	<tr>
-	<th scope="row" class="label">
-		<label>
-		<span class="alignleft"><?php _e('Gallery columns:'); ?></span>
-		</label>
-	</th>
-	<td class="field">
-		<select id="columns" name="columns">
-			<option value="1">1</option>
-			<option value="2">2</option>
-			<option value="3" selected="selected">3</option>
-			<option value="4">4</option>
-			<option value="5">5</option>
-			<option value="6">6</option>
-			<option value="7">7</option>
-			<option value="8">8</option>
-			<option value="9">9</option>
-		</select>
-	</td>
-	</tr>
-</tbody></table>
-
-<p class="ml-submit">
-<input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
-<input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
-</p>
-</div>
-</form>
+	
+	jQuery(document).ready( function($) {
+		$('.media-types input').click( function() {
+			$('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
+		});
+	});
+	
+	//]]>
+	</script>
 <?php
+	}; // endif
 }
 
 /**
  * {@internal Missing Short Description}}
  *
- * @since 2.5.0
- *
- * @param unknown_type $errors
- */
-function media_upload_library_form($errors) {
-	global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
-
-	media_upload_header();
-
-	$post_id = intval($_REQUEST['post_id']);
-
-	$form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
-	$form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
-	$form_class = 'media-upload-form validate';
-
-	if ( get_user_setting('uploader') )
-		$form_class .= ' html-uploader';
-
-	$_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
-	if ( $_GET['paged'] < 1 )
-		$_GET['paged'] = 1;
-	$start = ( $_GET['paged'] - 1 ) * 10;
-	if ( $start < 1 )
-		$start = 0;
-	add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
-
-	list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
-
-?>
-
-<form id="filter" action="" method="get">
-<input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
-<input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
-<input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
-<input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
-<input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
-
-<p id="media-search" class="search-box">
-	<label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
-	<input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
-	<?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
-</p>
-
-<ul class="subsubsub">
-<?php
-$type_links = array();
-$_num_posts = (array) wp_count_attachments();
-$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
-foreach ( $matches as $_type => $reals )
-	foreach ( $reals as $real )
-		if ( isset($num_posts[$_type]) )
-			$num_posts[$_type] += $_num_posts[$real];
-		else
-			$num_posts[$_type] = $_num_posts[$real];
-// If available type specified by media button clicked, filter by that type
-if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
-	$_GET['post_mime_type'] = $type;
-	list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
-}
-if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
-	$class = ' class="current"';
-else
-	$class = '';
-$type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
-foreach ( $post_mime_types as $mime_type => $label ) {
-	$class = '';
-
-	if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
-		continue;
-
-	if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
-		$class = ' class="current"';
-
-	$type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
-}
-echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
-unset($type_links);
-?>
-</ul>
-
-<div class="tablenav">
-
-<?php
-$page_links = paginate_links( array(
-	'base' => add_query_arg( 'paged', '%#%' ),
-	'format' => '',
-	'prev_text' => __('&laquo;'),
-	'next_text' => __('&raquo;'),
-	'total' => ceil($wp_query->found_posts / 10),
-	'current' => $_GET['paged']
-));
-
-if ( $page_links )
-	echo "<div class='tablenav-pages'>$page_links</div>";
-?>
-
-<div class="alignleft actions">
-<?php
-
-$arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
-
-$arc_result = $wpdb->get_results( $arc_query );
-
-$month_count = count($arc_result);
-
-if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
-<select name='m'>
-<option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
-<?php
-foreach ($arc_result as $arc_row) {
-	if ( $arc_row->yyear == 0 )
-		continue;
-	$arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
-
-	if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
-		$default = ' selected="selected"';
-	else
-		$default = '';
-
-	echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
-	echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
-	echo "</option>\n";
-}
-?>
-</select>
-<?php } ?>
-
-<?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
-
-</div>
-
-<br class="clear" />
-</div>
-</form>
-
-<form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="<?php echo $form_class; ?>" id="library-form">
-
-<?php wp_nonce_field('media-form'); ?>
-<?php //media_upload_form( $errors ); ?>
-
-<script type="text/javascript">
-<!--
-jQuery(function($){
-	var preloaded = $(".media-item.preloaded");
-	if ( preloaded.length > 0 ) {
-		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
-		updateMediaForm();
-	}
-});
--->
-</script>
-
-<div id="media-items">
-<?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
-<?php echo get_media_items(null, $errors); ?>
-</div>
-<p class="ml-submit">
-<?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
-<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
-</p>
-</form>
-<?php
-}
-
-/**
- * {@internal Missing Short Description}}
- *
  * @since 2.7.0
  *
  * @return unknown
@@ -2095,15 +2083,88 @@
 	echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
 }
 
-add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
-add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
-add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
-add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
+function wp_upload_form( $args ) {
+		
+	$defaults = array(
+		'post_id'		=> NULL,
+		'type' 			=> 'file',
+		'inline' 		=> false,
+		'form_class'	=> 'media-upload-form type-form validate',
+		'errors' 		=> array(),
+	);
+	
+	$args = wp_parse_args( $args, $defaults );
+	
+	extract( $args );
+	
+	wp_enqueue_script('plupload-handlers');
+	wp_enqueue_script('image-edit');
+	wp_enqueue_script('set-post-thumbnail' );
+	wp_enqueue_style('imgareaselect');
+	wp_enqueue_script( 'media-gallery' );
 
-add_action( 'media_upload_image', 'wp_media_upload_handler' );
-add_action( 'media_upload_audio', 'wp_media_upload_handler' );
-add_action( 'media_upload_video', 'wp_media_upload_handler' );
-add_action( 'media_upload_file',  'wp_media_upload_handler' );
+	?>
+	<form enctype="multipart/form-data" method="post" action="<?php echo admin_url('media-upload.php?inline=$inline&amp;upload-page-form='); ?>" class="<?php echo $form_class; ?>" id="file-form">
+	
 
-add_filter( 'media_upload_gallery', 'media_upload_gallery' );
-add_filter( 'media_upload_library', 'media_upload_library' );
+	<?php media_upload_form(); ?>	
+	
+	<?php wp_nonce_field('media-form'); ?>
+	
+	<?php if ( $inline !== false ) { ?>
+
+		<div id="media-items" class="hide-if-no-js"></div>
+		<input type="hidden" name="post_id" id="post_id" value="0" />
+		
+		<script type="text/javascript">
+		jQuery(function($){
+			var preloaded = $(".media-item.preloaded");
+			if ( preloaded.length > 0 ) {
+				preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
+			}
+			updateMediaForm();
+			post_id = 0;
+			shortform = 1;
+		});
+		</script>
+		
+	<?php } else { ?>
+	
+		<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
+		<div id="media-items">
+		
+		<?php
+		if ( $id ) {
+			if ( !is_wp_error($id) ) {
+				add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
+				echo get_media_items( $id, $errors );
+			} else {
+				echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
+				exit;
+			}
+		}
+		?>
+		
+		</div>
+		<p class="savebutton ml-submit">
+		<?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
+		</p>
+		</form>	
+		
+		<script type="text/javascript">
+		//<![CDATA[
+		jQuery(function($){
+			var preloaded = $(".media-item.preloaded");
+			if ( preloaded.length > 0 ) {
+				preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
+			}
+			updateMediaForm();
+		});
+		//]]>
+		</script>
+	
+	<?php }; ?>
+	
+	<?php submit_button( __( 'Save all changes' ), 'button savebutton hidden', 'save' ); ?>
+	<?php
+}
\ No newline at end of file
