Index: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 9143)
+++ wp-admin/includes/media.php	(working copy)
@@ -16,7 +16,7 @@
 function media_upload_tabs() {
 	$_default_tabs = array(
 		'type' => __('Choose File'), // handler action suffix => tab text
-		'gallery' => __('Gallery'),
+		'gallery' => __('Attachments'),
 		'library' => __('Media Library'),
 	);
 
@@ -40,7 +40,7 @@
 	if ( intval($_REQUEST['post_id']) )
 		$attachments = intval($wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $_REQUEST['post_id'])));
 
-	$tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
+	$tabs['gallery'] = sprintf(__('Attachments (%s)'), "<span id='attachments-count'>$attachments</span>");
 
 	return $tabs;
 }
@@ -98,7 +98,7 @@
 	$rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
 
 	if ( $url )
-		$html = '<a href="' . clean_url($url) . "\"$rel>$html</a>";
+		$html = '<a href="' . clean_url($url) . "\" title=\"". attribute_escape($title) ."\"$rel>$html</a>";
 
 	$html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
 
@@ -358,6 +358,8 @@
 			$post['post_title'] = $attachment['post_title'];
 		if ( isset($attachment['post_excerpt']) )
 			$post['post_excerpt'] = $attachment['post_excerpt'];
+		if ( isset($attachment['group_number']) )
+			$post['group_number'] = $attachment['group_number'];
 		if ( isset($attachment['menu_order']) )
 			$post['menu_order'] = $attachment['menu_order'];
 
@@ -376,8 +378,20 @@
 				wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
 	}
 
-	if ( isset($_POST['insert-gallery']) )
-		return media_send_to_editor('[gallery]');
+	if ( isset($_POST['insert-gallery']) ) { 
+		if ( ( isset($_POST['gallery_insert']) ) && ( $_POST['gallery_insert'] > 0 ) ) { 
+			return media_send_to_editor('[gallery group="'.$_POST['gallery_insert'].'"]'); 
+		} else { 
+			return media_send_to_editor('[gallery]'); 
+		} 
+	}
+	
+	if ( isset($_POST['new-gallery']) ) {	
+		wp_new_gallery($_POST['post_id']);
+	}
+		
+	if ( isset($_POST['gallery_delete']) && !is_null($_POST['gallery_delete']) && ($_POST['gallery_delete'] > 0) ) 
+		wp_delete_gallery($post['post_id'], $_POST['gallery_delete']); 
 
 	if ( isset($_POST['send']) ) {
 		$keys = array_keys($_POST['send']);
@@ -426,8 +440,11 @@
 			$align = attribute_escape($_POST['insertonly']['align']);
 			$class = " class='align$align'";
 		}
+		if ( isset($_POST['insertonly']['title']) ) { 
+			$title = " title='".attribute_escape($_POST['insertonly']['title'])."'"; 
+		} 
 		if ( !empty($src) )
-			$html = "<img src='$src' alt='$alt'$class />";
+			$html = "<img src='$src' alt='$alt'$title$class />";
 		return media_send_to_editor($html);
 	}
 
@@ -841,6 +858,8 @@
 			$post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
 		}
 	}
+	
+	$post['group_name'] = $_POST['attachments']['group_name'][$attachment['group_number']];
 
 	return $post;
 }
@@ -969,13 +988,17 @@
  * @param array $errors Errors for attachment, if any.
  * @return string
  */
-function get_media_items( $post_id, $errors ) {
+function get_media_items( $post_id, $group_number = null, $errors ) {
 	if ( $post_id ) {
 		$post = get_post($post_id);
-		if ( $post && $post->post_type == 'attachment' )
+		if ( $post && $post->post_type == 'attachment' ) {
 			$attachments = array($post->ID => $post);
-		else
-			$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
+		} else {
+			$attachments = get_attachments( $post_id, array('group_number' => $group_number), get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') ) );
+			
+			if ( ! count($attachments) ) 
+				return "\n<div class='media-item media-no-items child-of-$post_id preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div><div class='filename new'>". __("No images in this group. Move items to this group.") ."</div></div>"; 
+		}
 	} else {
 		if ( is_array($GLOBALS['wp_the_query']->posts) )
 			foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
@@ -985,10 +1008,11 @@
 	if ( empty($attachments) )
 		return '';
 
-	foreach ( $attachments as $id => $attachment )
+	foreach ( $attachments as $id => $attachment ) {	
 		if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
 			$output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
-
+	}
+	
 	return $output;
 }
 
@@ -1026,6 +1050,7 @@
 	$filename = basename($post->guid);
 	$title = attribute_escape($post->post_title);
 	$description = attribute_escape($post->post_content);
+	$group_number = attribute_escape($post->group_number);
 	if ( $_tags = get_the_tags($attachment_id) ) {
 		foreach ( $_tags as $tag )
 			$tags[] = $tag->name;
@@ -1056,6 +1081,8 @@
 	$gallery = ( (isset($_REQUEST['tab']) && 'gallery' == $_REQUEST['tab']) || (isset($redir_tab) && 'gallery' == $redir_tab) ) ? true : false;
 	$order = '';
 
+	$groupnum = '<input type="hidden" class="group-value" name="attachments['.$attachment_id.'][group_number]" value="'.$group_number.'" />';
+
 	foreach ( $form_fields as $key => $val ) {
 		if ( 'menu_order' == $key ) {
 			if ( $gallery )
@@ -1072,6 +1099,7 @@
 	$type
 	$toggle_links
 	$order
+	$groupnum
 	$display_title
 	<table class='slidetoggle describe $class'>
 		<thead class='media-item-info'>
@@ -1355,7 +1383,7 @@
 		html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
 
 		if ( f.url.value )
-			html = '<a href="'+f.url.value+'">'+html+'</a>';
+			html = '<a href="'+f.url.value+'"'+title+'>'+html+'</a>';
 
 		if ( caption )
 			html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
@@ -1425,6 +1453,9 @@
 	media_upload_header();
 
 	$post_id = intval($_REQUEST['post_id']);
+	$groups = intval(get_attachment_groups( $post_id ));
+	$_group_name = get_attachments_groupsname( $post_id );
+	
 	$form_action_url = admin_url("media-upload.php?type={$GLOBALS['type']}&tab=gallery&post_id=$post_id");
 ?>
 
@@ -1434,8 +1465,26 @@
 	var preloaded = $(".media-item.preloaded");
 	if ( preloaded.length > 0 ) {
 		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
-		updateMediaForm();
 	}
+	jQuery("a.del-group").click(function () { 
+		var deleteConfirmation = confirm('<?php _e("Are you sure you want to delete this gallery group?"); ?>'); 
+		
+		if (!deleteConfirmation) { return; } 
+		
+		if (jQuery("#media-group-" + jQuery(this).attr('value') + " .media-item input.group-value").length)
+		{ 
+			jQuery.each(jQuery("#media-group-delete-" + jQuery(this).attr('value') + " .media-item input.group-value"), function(i, id) { 
+				jQuery(id)[0].value = 0;        //      we are going to set all the items to group 0             
+			}); 
+		} 
+		jQuery("#gallery-delete").val( jQuery(this).attr('value') );    //      set the gallery we are deleting 
+		jQuery("#media-group-" + jQuery(this).attr('value')).hide();    //      hide the gallery and then... 
+		jQuery(".savebutton").click(); 
+	}); 
+	jQuery("a.insert-group").click(function () {
+		jQuery("#gallery-insert").val( jQuery(this).attr('value') );    //      set the gallery we are inserting 
+		jQuery(".insert-gallery").click();               
+	});
 });
 -->
 </script>
@@ -1443,23 +1492,59 @@
 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
 <?php wp_nonce_field('media-form'); ?>
 <?php //media_upload_form( $errors ); ?>
+<div id="media-items"> 
+<?php 
+for ( $i = 0; $i < $groups; $i++ ) 
+{ 
+?> 
+<div id="media-group-<?php echo $i; ?>" class="media-groups"> 
+<input type="hidden" id="media-group-value-<?php echo $i; ?>" value="<?php echo $i; ?>" /> 
 <table class="widefat">
 <thead><tr>
-<th><?php _e('Media'); ?></th>
+<th> 
+<?php  
+if ($i > 0) {
+	
+	$delete = "<a href=\"#media-group-delete-value-".$i."\" value=\"".$i."\" class=\"del-group\">" . __('Delete') . "</a>"; 
+	$insert = "<a href=\"#media-group-insert-value-".$i."\" value=\"".$i."\" class=\"insert-group\">" . __('Insert') . "</a>"; 
+	$group_link = "<small>[".$delete." | " .$insert."]</small>";
+	$group_name = "<input type=\"text\" name=\"attachments[group_name][".$i."]\" value=\"" . $_group_name[$i] . "\" size=\"20\" />";
+	
+	printf( __('Group (%1$d) %2$s %3$s'), $i, $group_name, $group_link);
+
+		
+} else { 
+
+	_e('Media: Non-grouped items.'); 
+	
+}
+?> 
+</th>
 <th class="order-head"><?php _e('Order'); ?></th>
 </tr></thead>
 </table>
-<div id="media-items">
-<?php echo get_media_items($post_id, $errors); ?>
+<?php echo get_media_items($post_id, $i, $errors); ?>
 </div>
+<?php 
+}
+?> 
+</div>
 <p class="ml-submit">
 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
-<input type="submit" class="button insert-gallery" name="insert-gallery" value="<?php echo attribute_escape( __( 'Insert gallery into post' ) ); ?>" />
+<input type="submit" class="button new-gallery" name="new-gallery" value="<?php echo attribute_escape( __( 'Create new gallery' ) ); ?>" /> 
+<input type="submit" class="button insert-gallery" name="insert-gallery" value="<?php echo attribute_escape( __( 'Insert full gallery into post' ) ); ?>" /> 
+<input type="hidden" name="gallery_delete" id="gallery-delete" value="" /> 
+<input type="hidden" name="gallery_insert" id="gallery-insert" value="" /> 
 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
 <input type="hidden" name="type" value="<?php echo attribute_escape( $GLOBALS['type'] ); ?>" />
 <input type="hidden" name="tab" value="<?php echo attribute_escape( $GLOBALS['tab'] ); ?>" />
 </p>
 </form>
+<script type="text/javascript"> 
+<!-- 
+	updateMediaForm(); 
+--> 
+</script> 
 <?php
 }
 
@@ -1606,7 +1691,7 @@
 </script>
 
 <div id="media-items">
-<?php echo get_media_items(null, $errors); ?>
+<?php echo get_media_items(null, null, $errors); ?> 
 </div>
 <p class="ml-submit">
 <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
Index: wp-admin/includes/upgrade.php
===================================================================
--- wp-admin/includes/upgrade.php	(revision 9143)
+++ wp-admin/includes/upgrade.php	(working copy)
@@ -906,6 +906,24 @@
 	// Update post_date for unpublished posts with empty timestamp
 	if ( $wp_current_db_version < 8921 )
 		$wpdb->query( "UPDATE $wpdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
+		
+
+	if ( $wp_current_db_version < 9094 ) { 
+		update_option('image_display_type', 'none');    // default display option 
+		$posts = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE (post_type = 'post' || post_type = 'page')"); 
+		foreach ( (array) $posts as $post ) { 
+			$attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = $post->ID");
+			
+			foreach ( (array) $attachments as $attachment ) 
+				$attach[] = $attachment->ID; 
+			
+			$_new_group = array(0 => array ('name' => null, 'items' => $attach)); 
+			update_post_meta( $post->ID, '_wp_post_attachments', $_new_group ); 
+			unset($attach); 
+		}                
+	} 
+	unset($posts, $attachments); 
+		
 }
 
 
Index: wp-admin/js/gallery.js
===================================================================
--- wp-admin/js/gallery.js	(revision 9143)
+++ wp-admin/js/gallery.js	(working copy)
@@ -1,22 +1,44 @@
 jQuery(function($) {
 	var gallerySortable;
+
 	var gallerySortableInit = function() {
 		gallerySortable = $('#media-items').sortable( {
 			items: '.media-item',
 			placeholder: 'sorthelper',
 			axis: 'y',
 			distance: 2,
-			update: galleryReorder
+			update: galleryHandle,
 		} );
 	}
-
-	// When an update has occurred, adjust the order for each item
+	
+	var galleryHandle = function (e, sort) {
+		galleryReorder(e, sort); 
+		gallerySortGroups();
+	}
+	
+	// @todo: fix me!
+	// When an update has occurred, adjust the order for each item.. 
 	var galleryReorder = function(e, sort) {
+		var o = 0;
 		jQuery.each(sort['element'].sortable('toArray'), function(i, id) {
-			jQuery('#' + id + ' .menu_order input')[0].value = (1+i);
+			if (id)
+			{
+				o++;
+				jQuery('#' + id + ' .menu_order input')[0].value = o;
+			}
 		});
 	}
-
+	
+	var gallerySortGroups = function () {
+		var mediaGroups = jQuery(".media-groups").length;
+		for (g = 0; g < mediaGroups; g++)
+		{
+			jQuery.each(jQuery("#media-group-" + g + " .media-item input.group-value"), function(i, id) {
+				jQuery(id)[0].value = g;		
+			});
+		}
+	}
+	
 	// initialize sortable
 	gallerySortableInit();
 });
Index: wp-admin/options-media.php
===================================================================
--- wp-admin/options-media.php	(revision 9143)
+++ wp-admin/options-media.php	(working copy)
@@ -39,6 +39,26 @@
     } 
 ?> 
 </fieldset></td> 
+</tr>
+<tr valign="top"> 
+<th scope="row"><?php _e('Image display') ?></th> 
+<td><fieldset><legend class="hidden"><?php _e('Image display') ?></legend> 
+<?php 
+    $display_types = array('none' => 'Default', 'lightbox' => 'Lightbox', 'shutterset' => 'Shutter Set',
+						'lytebox' => 'Lyte Box', 'greybox' => 'Grey Box', 'thickbox' => 'Thick Box', 'lightview' => 'Light View'
+	); 
+
+    $default_image_display_type = get_option('image_display_type');
+        if ( empty($default_image_display_type) )
+            $default_image_display_type = 'none';
+
+    foreach ($display_types as $type => $name) { ?>
+        <input type="radio" name="image_display_type" id="image_display_type_<?php echo $type; ?>" value="<?php echo $type; ?>"<?php echo ($default_image_display_type == $type ? ' checked="checked"' : ''); ?> />
+        <label for="image_display_type_<?php echo $type; ?>"><?php _e($name); ?></label>
+    <?php 
+    } 
+?> 
+</fieldset></td> 
 </tr> 
 </table>
 
Index: wp-admin/options.php
===================================================================
--- wp-admin/options.php	(revision 9143)
+++ wp-admin/options.php	(working copy)
@@ -25,7 +25,7 @@
 	'general' => array('blogname', 'blogdescription', 'admin_email', 'users_can_register', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'comment_registration', 'default_role' ),
 	'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page' ),
 	'misc' => array( 'hack_file', 'use_linksupdate', 'uploads_use_yearmonth_folders', 'upload_path' ),
-	'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),
+	'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type', 'image_display_type' ),
 	'privacy' => array( 'blog_public' ),
 	'reading' => array( 'posts_per_page', 'posts_per_rss', 'rss_use_excerpt', 'blog_charset', 'gzipcompression', 'show_on_front', 'page_on_front', 'page_for_posts' ),
 	'writing' => array( 'default_post_edit_rows', 'use_smilies', 'ping_sites', 'mailserver_url', 'mailserver_port', 'mailserver_login', 'mailserver_pass', 'default_category', 'default_email_category', 'use_balanceTags', 'default_link_category', 'enable_app', 'enable_xmlrpc' ),
Index: wp-content/themes/classic/style.css
===================================================================
--- wp-content/themes/classic/style.css	(revision 9143)
+++ wp-content/themes/classic/style.css	(working copy)
@@ -320,7 +320,64 @@
 	text-transform: capitalize;
 }
 
-/* Captions & aligment */
+/* Gallery & Captions & Aligment */
+
+.gallery {
+	display: block;
+	clear: left;
+	overflow: hidden;
+	margin: auto;
+}
+	
+.gallery-item {
+	float: left;
+	margin: 10px 0 0 0;
+	text-align: center;
+}
+	
+.gallery .gallery-item img, .gallery .gallery-item img.thumbnail { 
+	max-width: 89%; 
+	height: auto; 
+	padding: 1%; 
+	margin: 0 
+	auto; 
+}
+
+.gallery div.clear { 
+	width: 0px; 
+	height: 0px;
+}
+
+.col-1 { width: 100%; }
+.col-2 { width: 50%; }
+.col-3 { width: 33%; }
+.col-4 { width: 24.5%; }
+.col-5 { width: 20%; }
+.col-6 { width: 16.5%; }
+.col-7 { width: 14%; }
+.col-8 { width: 12.4%; }
+.col-9 { width: 11%; }
+.col-10 { width: 10%; }
+.col-11 { width: 9%; }
+.col-12 { width: 8.33%; }
+.col-13 { width: 7.6%; }
+.col-14 { width: 7.1%; }
+.col-15 { width: 6.66%; }
+.col-16 { width: 6.25%; }
+.col-17 { width: 5.83%; }
+.col-18 { width: 5.55%; }
+.col-19 { width: 5.25%; }
+.col-20 { width: 4.95%; }
+
+.gallery-caption { 
+	margin-left: 0;
+}
+
+.gallery-space { 
+	clear: both;
+	height: 0;
+}
+
 .aligncenter,
 div.aligncenter {
 	display: block;
@@ -360,4 +417,4 @@
 	padding: 0 4px 5px;
 	margin: 0;
 }
-/* End captions & aligment */
+/* End Gallery & Captions & Aligment */
Index: wp-content/themes/default/style.css
===================================================================
--- wp-content/themes/default/style.css	(revision 9143)
+++ wp-content/themes/default/style.css	(working copy)
@@ -663,9 +663,64 @@
 	}
 /* End Various Tags & Classes*/
 
+/* Gallery & Captions & Aligment */
 
+.gallery {
+	display: block;
+	clear: left;
+	overflow: hidden;
+	margin: auto;
+}
+	
+.gallery-item {
+	float: left;
+	margin: 10px 0 0 0;
+	text-align: center;
+}
+	
+.gallery .gallery-item img, .gallery .gallery-item img.thumbnail { 
+	max-width: 89%; 
+	height: auto; 
+	padding: 1%; 
+	margin: 0 
+	auto; 
+}
 
-/* Captions */
+.gallery div.clear { 
+	width: 0px; 
+	height: 0px;
+}
+
+.col-1 { width: 100%; }
+.col-2 { width: 50%; }
+.col-3 { width: 33%; }
+.col-4 { width: 24.5%; }
+.col-5 { width: 20%; }
+.col-6 { width: 16.5%; }
+.col-7 { width: 14%; }
+.col-8 { width: 12.4%; }
+.col-9 { width: 11%; }
+.col-10 { width: 10%; }
+.col-11 { width: 9%; }
+.col-12 { width: 8.33%; }
+.col-13 { width: 7.6%; }
+.col-14 { width: 7.1%; }
+.col-15 { width: 6.66%; }
+.col-16 { width: 6.25%; }
+.col-17 { width: 5.83%; }
+.col-18 { width: 5.55%; }
+.col-19 { width: 5.25%; }
+.col-20 { width: 4.95%; }
+
+.gallery-caption { 
+	margin-left: 0;
+}
+
+.gallery-space { 
+	clear: both;
+	height: 0;
+}
+
 .aligncenter,
 div.aligncenter {
 	display: block;
@@ -673,6 +728,14 @@
 	margin-right: auto;
 }
 
+.alignleft {
+	float: left;
+}
+
+.alignright {
+	float: right;
+}
+
 .wp-caption {
 	border: 1px solid #ddd;
 	text-align: center;
@@ -697,9 +760,8 @@
 	padding: 0 4px 5px;
 	margin: 0;
 }
-/* End captions */
+/* End Gallery & Captions & Aligment */
 
-
 /* "Daisy, Daisy, give me your answer do. I'm half crazy all for the love of you.
 	It won't be a stylish marriage, I can't afford a carriage.
 	But you'll look sweet upon the seat of a bicycle built for two." */
Index: wp-includes/js/swfupload/handlers.js
===================================================================
--- wp-includes/js/swfupload/handlers.js	(revision 9143)
+++ wp-includes/js/swfupload/handlers.js	(working copy)
@@ -126,16 +126,23 @@
 	}
 
 	// Only show Save buttons when there is at least one file.
-	if ( jQuery('#media-items>*').not('.media-blank').length > 0 )
+	if ( jQuery('#media-items .media-item').not('.media-no-items').length > 0 )
 		jQuery('.savebutton').show();
 	else
 		jQuery('.savebutton').hide();
 
+	// Only show new Gallery button when there are at least two files.
+	if ( jQuery('#media-items .media-item').not('.media-no-items').length > 1 )
+		jQuery('.new-gallery').show();
+	else
+		jQuery('.new-gallery').hide();
+
 	// Only show Gallery button when there are at least two files.
-	if ( jQuery('#media-items>*').length > 1 )
+	if ( jQuery('#media-items .media-groups').length > 1 )
 		jQuery('.insert-gallery').show();
 	else
 		jQuery('.insert-gallery').hide();
+		
 }
 
 function uploadSuccess(fileObj, serverData) {
Index: wp-includes/media.php
===================================================================
--- wp-includes/media.php	(revision 9143)
+++ wp-includes/media.php	(working copy)
@@ -617,12 +617,15 @@
 		'icontag'    => 'dt',
 		'captiontag' => 'dd',
 		'columns'    => 3,
+		'class'		 => get_option('image_display_type'),
+		'name'		 => null,
+		'group'		 => null,
 		'size'       => 'thumbnail',
 	), $attr));
 
 	$id = intval($id);
-	$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
-
+	$attachments = get_attachments( $id, array('group_number' => $group, 'name' => $name, 'order' => $order, 'orderby' => $orderby), get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ) );
+	
 	if ( empty($attachments) )
 		return '';
 
@@ -639,47 +642,86 @@
 	$columns = intval($columns);
 	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
 
-	$output = apply_filters('gallery_style', "
-		<style type='text/css'>
-			.gallery {
-				margin: auto;
-			}
-			.gallery-item {
-				float: left;
-				margin-top: 10px;
-				text-align: center;
-				width: {$itemwidth}%;			}
-			.gallery img {
-				border: 2px solid #cfcfcf;
-			}
-			.gallery-caption {
-				margin-left: 0;
-			}
-		</style>
-		<!-- see gallery_shortcode() in wp-includes/media.php -->
-		<div class='gallery'>");
+	if ( is_null($group) ) {
+		$group_area = 0;
+		$group_gallery = "-" . $group_area;
+	} else {
+		$group_area = $group;
+		$group_gallery = "-" . $group;
+	}
+	
+	$output = apply_filters('gallery_style', "<div id='gallery-$post->ID-$group_area' class='gallery'>");
 
 	foreach ( $attachments as $id => $attachment ) {
-		$link = wp_get_attachment_link($id, $size, true);
-		$output .= "<{$itemtag} class='gallery-item'>";
-		$output .= "
-			<{$icontag} class='gallery-icon'>
-				$link
-			</{$icontag}>";
+		
+		if ( !is_null($attachment->group_name) )
+			$group_name = $attachment->group_name;
+		
+		if ( !is_null($group) )
+			if ($attachment->group_number != $group)
+				continue;
+
+		$a_img = wp_get_attachment_url($id);
+		$att_page = get_attachment_link($id);
+		$img = wp_get_attachment_image_src($id, $size);
+		
+		$img = $img[0];
+		$title = $attachment->post_excerpt;
+		
+		if ($title == '')
+			$title = $attachment->post_title;
+		
+		$output .= "\n<{$itemtag} class='gallery-item col-$columns'>\n";
+		$output .= "<{$icontag} class='gallery-icon'>\n";
+		
+		switch ($class)
+		{
+			case 'lightbox':
+				$class_rel = ' class="lightbox" rel="lightbox[gallery-'.$post->ID.$group_gallery.']"';
+				break;
+			case 'shutterset':
+				$class_rel = ' class="shutterset-gallery-'.$post->ID.$group_gallery.'" rel="lightbox[gallery-'.$post->ID.$group_gallery.']"';
+				break;
+			case 'lytebox':
+				$class_rel = ' class="lytebox" rel="lytebox[gallery-'.$post->ID.$group_gallery.']"';
+				break;
+			case 'greybox':
+				$class_rel = ' class="greybox" rel="gb_imageset[gallery-'.$post->ID.$group_gallery.']"';
+				break;
+			case 'thickbox':
+				$class_rel = ' class="thickbox" rel="gallery-'.$post->ID.$group_gallery.'"';
+				break;
+			case 'lightview':
+				$class_rel = ' class="lightview" rel="gallery[gallery-'.$post->ID.$group_gallery.']"';
+				break;
+			default:
+				$class_rel = null;
+				break;
+		}
+
+		if (!is_null($class_rel))
+			$link = $a_img;
+		else
+			$link = $att_page;
+		
+		$output .= "\t<a href=\"$link\" title=\"$title\"$class_rel><img src=\"$img\" alt=\"$title\" /></a>";
+		$output .= "\n</{$icontag}>";
+		
 		if ( $captiontag && trim($attachment->post_excerpt) ) {
-			$output .= "
-				<{$captiontag} class='gallery-caption'>
-				{$attachment->post_excerpt}
-				</{$captiontag}>";
+			$output .= "\n<$captiontag class='gallery-caption'>\n\t";
+			$output .= $attachment->post_excerpt;
+			$output .= "</$captiontag>";
 		}
-		$output .= "</{$itemtag}>";
-		if ( $columns > 0 && ++$i % $columns == 0 )
-			$output .= '<br style="clear: both" />';
+		
+		$output .= "\n</{$itemtag}>";
+		
+		if($columns > 0 && ++$i % $columns == 0)
+			$output .= '<div style="clear:both;" class="gallery-space"><!-- --></div>';
 	}
+	$output .= "\n</div>\n";
 
-	$output .= "
-			<br style='clear: both;' />
-		</div>\n";
+	if ((isset($group_name)) && (!is_null($group)))
+		$output .= "<p style=\"text-align: center;\"><strong>".$group_name."</strong></p>\n";
 
 	return $output;
 }
Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 9143)
+++ wp-includes/post.php	(working copy)
@@ -69,6 +69,272 @@
 }
 
 /**
+ * Get the posts attachments data.
+ *
+ * @since 2.7.0
+ *
+ * @param int $post_id Post ID
+ */
+function _get_attachment_data ( $post_id ) {
+	global $wpdb;
+
+	$_attchments = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_wp_post_attachments' LIMIT 1", $post_id));
+	return unserialize($_attchments->meta_value);
+}
+
+/**
+ * Get the posts attachments.
+ *
+ * @since 2.7.0
+ *
+ * @param int $post_id Post ID
+ * @param mixed $attachment_options Group Options
+ * @param mixed $attachment_childern All the childern's of the current post in an array.
+ */
+function &get_attachments( $post_id, $attachment_options = '', $attachment_childern ) {
+
+	extract($attachment_options, EXTR_SKIP);
+
+	if (!is_null($name)) {
+		
+		if (is_numeric($name))
+			$post_id = $name;
+		
+		if ($page = get_page_by_title($name))
+			$post_id = $page->ID;
+		
+		if ($post = get_post_by_title($name))
+			$post_id = $post->ID;
+		
+		// @todo: This is untested.
+		$attachment_childern = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
+	}
+	
+	$attachment = _get_attachment_data ( $post_id );
+	
+	if ( !is_array($attachment) )
+		return;
+
+	foreach ( $attachment_childern as $id => $image ) {
+		
+		foreach ( $attachment as $key => $group ) {
+		
+			if ( !is_array($group['items']) )
+				continue;
+				
+			if (is_null($group_number)) {
+				if ($key == 0) { continue; }
+			} else {
+				if ($key != $group_number) { continue; }
+			}
+			
+			if (in_array($id, $group['items'])) {
+				
+				$image = $attachment_childern[$id];
+				$image->group_number = $key;
+				$image->group_name = $group['name'];
+				
+				$attach[$id] = $image;
+			
+			}
+
+		}
+		
+	}
+	
+	return $attach;
+}
+
+/**
+ * Get the gallery groups names.
+ *
+ * @since 2.7.0
+ *
+ * @param int $post_id Post ID
+ */
+function &get_attachments_groupsname( $post_id ) {
+
+	$attachment = _get_attachment_data ( $post_id );
+	
+	if ( !is_array($attachment) )
+		return '';
+	
+	foreach ( $attachment as $key => $group ) {
+		$groups[$key] = $group['name'];
+	} 
+	
+	return $groups;
+}
+
+/**
+ * Get the number of groups attached to this post.
+ *
+ * @since 2.7.0
+ *
+ * @param int $post_id Post ID
+ */
+function get_attachment_groups( $post_id ) {
+	
+	return count(_get_attachment_data ( $post_id ));
+}
+
+/**
+ * Insert a new gallery into the post. 
+ * 
+ * @since 2.7.0 
+ * 
+ * @param int $post_id post data 
+*/ 
+function wp_new_gallery ( $post_id ) { 
+
+	$attachment = _get_attachment_data ( $post_id );
+
+	foreach ( $attachment as $key => $group ) {
+		$_group[$key] = array ('name' => $group['name'], 'items' => $group['items']);
+	}
+
+	$_group[] = array ('name' => null, 'items' => null);
+	
+	update_post_meta( $post_id, '_wp_post_attachments', $_group);
+	
+}
+
+/**
+ * We are deleting a gallery and moving 
+ * 
+ * @since 2.7.0 
+ * 
+ * @param int $post_id Post ID
+ * @param int $group_number Group Number
+*/ 
+function wp_delete_gallery ( $post_id, $group_num ) {
+	 
+	$attachment = _get_attachment_data ( $post_id );
+	
+	foreach ( $attachment as $key => $group ) {
+		if ($key != $group_num) {
+			$_group[$key] = array ('name' => $group['name'], 'items' => $group['items']);
+			continue;						
+		}	
+		
+		if ( !is_array($group['items']) )
+			break;
+		
+		foreach ( $group['items'] as $item => $attachment_id ) {
+			$_attach[] = $attachment_id;	
+		}
+	}
+	
+	if ( is_array($_group[0]['items']) && is_array($_attach) )  {
+		$i = array_merge($_group[0]['items'], $_attach);	
+		$_group[0] = array ('name' => null, 'items' => $i);	
+	} else {
+		$_group[0] = array ('name' => null, 'items' => $_group[0]['items']);
+	}
+
+	update_post_meta( $post_id, '_wp_post_attachments', $_group );
+
+}
+
+/**
+ * Updating the postmeta data when saving the gallery information.
+ * 
+ * @since 2.7.0 
+ * 
+ * @param int $attachment_id Attachment ID
+ * @param int $post_id Post ID
+ * @param int $group_name Group Name
+ * @param int $group_number Group Number
+*/ 
+function wp_update_attachment ( $attachment_id, $post_id, $group_name = null, $group_number = null) {
+	
+	$attachment = _get_attachment_data ( $post_id );
+
+	if (!is_array($attachment)) {
+		$_attach[] = array ('name' => null, 'items' => array($attachment_id) );
+		add_post_meta($post_id, '_wp_post_attachments', $_attach, true);
+		return true;
+	}
+
+	//	check to see if this group ($group_number) has a name assignment and is not null
+	
+	/*
+	if (( !isset($attachment[$group_number][name]) ) || ( !is_null($attachment[$group_number][name]) ))
+		if ($group_name != $attachment[$group_number][name])
+			_update_attachment_groupname( $post_id, $group_number, $group_name );	// update group name of group number if they are not the same (should only happen once if it does change)
+	
+	*/
+	
+	//	check to see if this attachment ($attachment_id) is not part of the group. If it's not part of this group add it to the group.
+	$update = false;
+	foreach ( $attachment as $key => $group ) {
+		if ($key == $group_number) {
+			if ((!is_array($group['items'])) || (!in_array($attachment_id, $group['items']) )) {
+				if ($key == $group_number) {
+					$update = true;
+					$i[count($group['items'])] = $attachment_id;
+					
+					if (!is_array($group['items']))
+						$group['items'] = $i;
+					else
+						$group['items'] = array_merge($group['items'], $i);
+					
+					_update_attachment_groupitem( $post_id, $key, $group['items'] );
+				}
+			}
+		}
+	}
+	foreach ( $attachment as $key => $group ) {
+		if (($key != $group_number) && ($update)) {
+			if ( in_array($attachment_id, $group['items']) ) {
+				foreach ($group['items'] as $item => $attach) {
+					if ($attach != $attachment_id)
+						$g[] = $attach;
+				}
+				_update_attachment_groupitem( $post_id, $key, $g );
+			}
+		}	
+	}
+	
+}
+
+/**
+ * Updateing the group name. (Private)
+ * 
+ * @since 2.7.0 
+ * 
+ * @param int $post_id Post ID
+ * @param int $group_number Group Number
+ * @param str $group_name Group Name
+*/ 
+function _update_attachment_groupname ( $post_id, $group_number, $group_name ) {
+		
+	$_attachment = _get_attachment_data ( $post_id );
+	$_attachment[$group_number][name] = stripslashes($group_name);
+	
+	update_post_meta( $post_id, '_wp_post_attachments', $_attachment );
+	
+}
+
+/**
+ * Updateing the group items. (Private)
+ * 
+ * @since 2.7.0 
+ * 
+ * @param int $post_id Post ID
+ * @param int $group_number Group Number
+ * @param array $attachment_data The new item data for this group.
+*/ 
+function _update_attachment_groupitem ( $post_id, $group_number, $attachment_data ) {
+	
+	$_attachment = _get_attachment_data ( $post_id );
+	$_attachment[$group_number][items] = $attachment_data;
+	
+	update_post_meta( $post_id, '_wp_post_attachments', $_attachment );
+
+}
+
+ /**
+ * Retrieve a post given its title.
+ *
+ * @since 2.7.0
+ * @uses $wpdb
+ *
+ * @param string $page_title Page title
+ * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
+ * @return mixed
+ */
+function get_post_by_title($page_title, $output = OBJECT) {
+	global $wpdb;
+	$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
+	if ( $post )
+		return get_post($post, $output);
+
+	return null;
+}
+
+/**
  * Retrieve child pages from list of pages matching page ID.
  *
  * Matches against the pages parameter against the page ID. Also matches all
@@ -2244,7 +2558,7 @@
 
 	// export array as variables
 	extract($object, EXTR_SKIP);
-
+	
 	// Make sure we set a valid category
 	if (0 == count($post_category) || !is_array($post_category)) {
 		$post_category = array(get_option('default_category'));
@@ -2338,6 +2652,8 @@
 		$post_name = sanitize_title($post_title, $post_ID);
 		$wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
 	}
+	
+	wp_update_attachment($post_ID, $post_parent, $group_name, $group_number);	// update or insert attachment into postmeta field
 
 	wp_set_post_categories($post_ID, $post_category);
 
