Index: modules/overlay/overlay.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/overlay/overlay.module,v
retrieving revision 1.31
diff -u -p -r1.31 overlay.module
--- modules/overlay/overlay.module	31 Aug 2010 15:04:09 -0000	1.31
+++ modules/overlay/overlay.module	11 Oct 2010 06:02:42 -0000
@@ -77,6 +77,7 @@ function overlay_form_user_profile_form_
         '#description' => t('Show administrative pages on top of the page you started from.'),
         '#default_value' => isset($account->data['overlay']) ? $account->data['overlay'] : 1,
       );
+      $form['#submit'][] = 'overlay_control_submit';
     }
   }
 }
@@ -91,6 +92,21 @@ function overlay_user_presave(&$edit, $a
 }
 
 /**
+ * Submit handler for the overlay toggle on the user account page.
+ */
+function overlay_control_submit($form, &$form_state) {
+  // If the current user just enabled the overlay for themselves, set a session
+  // variable which causes overlay_init() to redirect into the overlay
+  // immediately on the next page request.
+  global $user;
+  if ($user->uid == $form_state['user']->uid && !empty($form_state['values']['overlay']) && empty($form_state['user']->data['overlay'])) {
+    $_SESSION['overlay_enable_redirect'] = array(
+      'parent_path' => 'user/' . $user->uid,
+    );
+  }
+}
+
+/**
  * Implements hook_init().
  *
  * Determine whether the current page request is destined to appear in the
@@ -108,11 +124,26 @@ function overlay_init() {
   $use_overlay = !isset($user->data['overlay']) || $user->data['overlay'];
   if (empty($mode) && user_access('access overlay') && $use_overlay) {
     $current_path = current_path();
-    // After overlay is enabled on the modules page, redirect to
-    // <front>#overlay=admin/modules to actually enable the overlay.
+    // If the overlay has just been enabled on the current page (for example,
+    // if the Overlay module was just installed at admin/modules), setting
+    // $_SESSION['overlay_enable_redirect'] to a value that evaluates to TRUE
+    // will force the overlay to open immediately. The default behavior if this
+    // session variable is set is to redirect to the front page of the site,
+    // with the current page in an overlay above that. If the calling code
+    // instead sets $_SESSION['overlay_enable_redirect'] to an array with
+    // optional elements 'parent_path' and 'child_path', then the page that
+    // appears underneath the overlay ('parent_path') and/or the page that
+    // appears inside the overlay ('child_path') can be overridden.
     if (isset($_SESSION['overlay_enable_redirect']) && $_SESSION['overlay_enable_redirect']) {
+      $redirect = array(
+        'parent_path' => '<front>',
+        'child_path' => $current_path,
+      );
+      if (is_array($_SESSION['overlay_enable_redirect'])) {
+        $redirect = $_SESSION['overlay_enable_redirect'] + $redirect;
+      }
       unset($_SESSION['overlay_enable_redirect']);
-      drupal_goto('<front>', array('fragment' => 'overlay=' . $current_path));
+      drupal_goto($redirect['parent_path'], array('fragment' => 'overlay=' . $redirect['child_path']));
     }
 
     if (isset($_GET['render']) && $_GET['render'] == 'overlay') {
