--- me.module	Wed Jul 01 23:59:11 2009
+++ me.module	Sun Oct 25 18:44:10 2009
@@ -40,13 +40,6 @@
 }
 
 /**
- * Implementaiton of moduleName_preprocess_hook() for theme_menu_item_link.
- */
-function me_preprocess_menu_item_link(&$vars) {
-  _me_check_path($vars['link']);
-}
-
-/**
  * Implementation of theme_menu_item_link().
  */
 function me_theme_menu_item_link($link) {
@@ -84,10 +77,11 @@
  */
 function _me_check_path(&$link) {
   // If the link has been rewritten, and we are not supposed to be handling this
-  // path, then rewite it back to its original.
-  if (me_variable_get('me_rewrite_link') && !_me_handle_path($link['href'])) {
+  // path, then rewrite it back to its original.
+  if ((me_variable_get('me_rewrite_link') && !_me_handle_path($link['href'])) || 
+     (!me_variable_get('me_rewrite_link') && _me_handle_path($link['href']))) {
     $path_parts = explode('/', $link['href'], MENU_MAX_PARTS);
-
+    
     // The wildcarded path will either be in $link['path'], or $link['router_path'].
     $wild_parts = explode('/', (isset($link['path']) ? $link['path'] : $link['router_path']), MENU_MAX_PARTS);
 
@@ -232,6 +226,57 @@
 }
 
 /**
+ * A special menu callback function for page manger pages that either redirects to
+ * a page with the uid in the path, or calls page_manager_page_execute
+ *
+ * @return page_manager_page_execute(args)
+ */
+function me_page_manager_page_execute() {
+  // If we want the uid shown in the address bar, we need to do a redirect.
+  if (me_variable_get('me_redirect') || _me_user_disabled() || !_me_handle_path($_GET['q'])) {
+    $redirect = FALSE;
+    // Get the menu path arguments.
+    $menu_parts = explode('/', $_GET['q'], MENU_MAX_PARTS);
+
+    // Loop over each part. If it's a %me wildcard, then
+    // check the corresponding menu part for the me alias,
+    // if so, replace it out with the user id so we can redirect correctly.
+    // If no changes are required, then call the required function.
+    foreach ($menu_parts as $key => $value) {
+      if (_me_is_alias($value)) {
+        $redirect = TRUE;
+        $menu_parts[$key] = $GLOBALS['user']->uid;
+      }
+    }
+
+    if ($redirect) {
+      $path = implode('/', $menu_parts);
+      // Save on an extra redirect by also checking the anonymous redirect here.
+      $redirect_path = me_variable_get('me_redirect_anonymous');
+      if ($GLOBALS['user']->uid == 0 && !empty($redirect_path)) {
+        $path = $redirect_path;
+      }
+      drupal_goto($path);
+    }
+  }
+
+  // Before going any further, set the current menu router item to include
+  // paths with %user, which allows modules to use menu_get_object() instead
+  // of arg() in blocks and the like.
+  $router_item = menu_get_item();
+  foreach ($router_item['load_functions'] as $index => $function) {
+    // If the function is a me handled function, then swap the handler out with user.
+    if (0 === strpos($function, 'me')) {
+      $router_item['load_functions'][$index] = 'user_load';
+    }
+  }
+  menu_set_item($_GET['q'], $router_item);
+  
+  $args = func_get_args();
+  return call_user_func_array(page_manager_page_execute, $args);
+}
+
+/**
  * Helper function to check if a user can have, and has me disabled.
  *
  * @return boolean
@@ -334,6 +379,11 @@
       unset($callbacks[$path]);
       $processed[] = $path;
     }
+    
+    // Alter the page callback for page manager pages
+    if ($data['page callback'] == 'page_manager_page_execute') {
+      $callbacks[$path]['page callback'] = 'me_page_manager_page_execute';
+    }
   }
 }
 
@@ -388,6 +438,19 @@
 }
 
 /**
+ * Special menu _load() function for the user:uid argument.
+ *
+ * This is just the normal page manager argument. It only exists so that
+ * the to_arg can exist.
+ */
+function me_pm_uid_arg_load($value, $subtask, $argument) {
+  page_manager_get_task('page');
+  $value = _me_check_arg($value);
+  
+  return _pm_arg_load($value, $subtask, $argument);
+}
+
+/**
  * Menu to_arg function for %me.
  */
 function me_to_arg($arg, $map, $index) {
@@ -415,6 +478,19 @@
 }
 
 /**
+ * Menu to_arg function for %me.
+ */
+function me_pm_uid_arg_to_arg($arg) {
+  $uid = user_uid_optional_to_arg($arg);
+  if (me_variable_get('me_rewrite_link') && !_me_user_disabled()) {
+    return $uid == $GLOBALS['user']->uid ? _me_get_me_alias() : $uid;
+  }
+  else {
+    return $uid;
+  }
+}
+
+/**
  * A Helper function to check for the 'me' alias.
  *
  * @param $arg
@@ -721,4 +797,4 @@
   if ($module == 'ctools' && $plugin == 'arguments') {
     return 'plugins';
   }
-}
+}
\ No newline at end of file
