Index: media.php
===================================================================
--- media.php	(revision 14165)
+++ media.php	(working copy)
@@ -881,6 +881,27 @@
 }
 
 /**
+ * Return the URL of the next adjacent image in a gallery or the URL of the parent post
+ */
+function get_next_image_url() {
+	global $post;
+	$post = get_post($post);
+	$attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
+
+	foreach ( $attachments as $k => $attachment )
+		if ( $attachment->ID == $post->ID )
+			break;
+
+		$k = $k + 1;
+
+		if ( isset($attachments[$k]) ) {
+			return get_attachment_link($attachments[$k]->ID);
+		} else {
+			return get_permalink($post->post_parent);
+		}
+}
+
+/**
  * Retrieve taxonomies attached to the attachment.
  *
  * @since 2.5.0
