diff -urp og.module og.module
--- og.module	2009-01-02 05:58:21.000000000 +0200
+++ og.module	2009-01-15 21:06:11.000000000 +0200
@@ -424,52 +424,103 @@ function og_og($op, $gid, $uid, $args) {
 }
 
 /**
- * Set group context based on URL. Modules may override what we set here.
- * Set a custom theme based on what group is being displayed (if any).
- * Be smart about selecting the 'active' group for ambiguous urls like node/$nid
- * TODOL see http://api.drupal.org/api/function/menu_get_object/6
+ * Set group context using the menu system.
+ *
+ * Modules may override the custom theme and group context set here.
+ * @see get_group_context().
  */
 function og_determine_context() {
-  global $custom_theme;
-  $group_node = NULL; // a node object containing the 'active' group for this request
-  
-  // Recognize context when handling a request from a Views ajax block. See views_ajax().
-  $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL;
+  global $user;
+  $item = menu_get_item();
 
-  if (arg(0, $path) == 'og' && is_numeric(arg(2, $path))) {
-    $group_node = og_set_theme(arg(2, $path));
+  // Recognize context when handling a request from a Views ajax block.
+  // @see views_ajax() in the Views module.
+  if (isset($_REQUEST['view_path'])) {
+    $path = $_REQUEST['view_path'];
   }
-  elseif (arg(0, $path) == 'node' && is_numeric(arg(1, $path))) {
-    $group_node = og_set_theme(arg(1, $path));
-  }
-  elseif (arg(0, $path) == 'node' && arg(1, $path) == 'add' && arg(2, $path) == 'book' && arg(3, $path) == 'parent') {
-    $group_node = og_set_theme(arg(4, $path));
-    $_REQUEST['edit']['og_groups'][] = $group_node->nid; // checks the right box on node form
+  else {
+    // Use the menu system to get the path.
+    $path = $item['path'];
   }
-  elseif (arg(0, $path) == 'node' && arg(1, $path) == 'add' && isset($_REQUEST['gids'])) {
+
+  // Check if are in a node view/ update/ delete.
+  if (strpos($path, 'node/%') === 0) {
+    $path == 'node/%/delete' ? $arg = 1 : $arg = 0;
+    $node = $item['page_arguments'][$arg];
+  }
+  // Check if we are in the node add page.
+  elseif (strpos($path, 'node/add') === 0 && !empty($_REQUEST['gids'])) {
+    // URL pattern: node/add/story?gids[]=1
     $gid = intval(current($_REQUEST['gids']));
-    $group_node = node_load($gid);
-    $custom_theme = $group_node->og_theme;
+    $node = node_load($gid);
   }
-  elseif (arg(0, $path) == 'comment' && in_array(arg(1, $path), array('edit', 'delete')) && is_numeric(arg(2, $path))) {
-    $nid = db_result(db_query('SELECT nid FROM {comments} WHERE cid = %d', arg(2, $path)));
-    $group_node = og_set_theme($nid);
+  elseif ($item['map'][0] == 'og' && !empty($item['map'][2])) {
+    // We are in of OG's menu callbacks (e.g. og/manage/%node).
+    if (is_object($item['map'][2])) {
+      // The menu system passed the object.
+      $node = $item['map'][2];
+    }
+    elseif (is_numeric($item['map'][2])) {
+      // Views module passed the numeric value.
+      $node = node_load(intval($item['map'][2]));
+    }
   }
-  elseif (arg(0, $path) == 'comment' && is_numeric(arg(2, $path))) {
-    // comment/reply/cid/nid URL pattern. 
-    $group_node = og_set_theme(arg(2, $path));
+  elseif ($path == 'comment/reply/%') {
+    $node = $item['page_arguments'][0];
   }
-  elseif (arg(0, $path) == 'comment' && is_numeric(arg(2, $path))) {
-    // comment/reply/cid/nid URL pattern. 
-    $group_node = og_set_theme(arg(2, $path));
+  elseif ($path == 'comment/edit' || $path == 'comment/delete') {
+    // Get the node from the comment object.
+    $comment = _comment_load($item['page_arguments'][0]);
+    $node = node_load($comment->nid);
   }
 
-  return og_set_group_context($group_node);
+  if (!empty($node)) {
+    $group_node = og_determine_context_get_group($node);
+    og_set_theme($group_node);
+    og_set_group_context($group_node);
+  }
+  return !empty($group_node) ? $group_node : NULL;
 }
 
 /**
- * API function for getting the group context (if any) for the current request. Used
- * for things like setting current theme and breadcrumbs. This context is set during og_determine_context()
+ * Helper function; Find the most appropriate group node to be set the group
+ * conext.
+ * @param $node
+ *   The node that was decided by og_determine_context().
+ *   It can be a group node or a group post.
+ * @see
+ * og_determine_context().
+ */
+function og_determine_context_get_group($node) {
+  global $user;
+  if (og_is_group_type($node->type)) {
+    $group_node = $node;
+  }
+  elseif (og_is_group_post_type($node->type)) {
+    if (count($node->og_groups)) {
+      // Group post belongs to multiple groups. Selection order:
+      // 1) The group we showed on the prior page view (if any).
+      // 2) The only or one of the group(s) the current user is a member of.
+      if (isset($_SESSION['og_last']) && in_array($_SESSION['og_last'], $node->og_groups)) {
+        $group_node = node_load($_SESSION['og_last']);
+      }
+      else {
+        $groups = array();
+        // Intersect the node's groups with the user's groups.
+        if (!empty($user->og_groups) && ($groups = array_intersect($node->og_groups, array_keys($user->og_groups)))) {
+          $group = current($groups);
+          $group_node = node_load($group['nid']);
+        }
+      }
+    }
+  }
+  return !empty($group_node) ? $group_node : NULL;
+}
+
+/**
+ * API function for getting the group context (if any) for the current request. 
+ * Used for things like setting current theme and breadcrumbs. 
+ * This context is set during og_determine_context()
  *
  * @return $node object
  */
@@ -478,67 +529,37 @@ function og_get_group_context() {
 }
 
 /**
- * API function for setting the group context for the current request. Modules may set this as needed. 
+ * API function; Set the group context for the current request.
+ * Modules may set this as needed.
  * This context is originally set during og_determine_context().
  *
  * @return $node object
  */
 function og_set_group_context($group_node = NULL) {
   static $stored_group_node;
-  
+
   if (is_object($group_node) && og_is_group_type($group_node->type)) {
     $stored_group_node = $group_node;
   }
   return $stored_group_node;
 }
 
-function og_set_theme($nid) {
+/**
+ * API function; Set the theme for the current request.
+ * @param $node
+ *   Pass the node object or the node id.
+ * @return
+ *   The group node object.
+ */
+function og_set_theme($group_node) {
   global $custom_theme, $user;
-  $node = node_load(intval($nid));
-  if (og_is_group_type($node->type)) {
-    if (!$custom_theme) {
-      $custom_theme = $node->og_theme;
-    }
-    return $node;
+  if (!is_object($group_node)) {
+    $group_node = node_load($group_node);
   }
-  elseif (isset($node->og_groups)) {
-    switch (count($node->og_groups)) {
-      case 0:
-        return NULL;
-      case 1:
-        $group_node = node_load(current($node->og_groups));
-        if (!$custom_theme) {
-          $custom_theme = $group_node->og_theme;
-        }
-        break;
-      default:
-        // Node is in multiple groups. Preference goes to the group we showed on the prior page view (if any),
-        // Then to a group the current user is a member of.
-        if (isset($_SESSION['og_last']) && in_array($_SESSION['og_last'], $node->og_groups)) {
-          $group_node = node_load($_SESSION['og_last']);
-          if (!$custom_theme) {
-            $custom_theme = $group_node->og_theme;
-          }
-        }
-        else {
-          $groups = array();
-          // intersect the node's groups with the user's groups
-          if ($user->uid) {
-            $groups = array_intersect($node->og_groups, array_keys($user->og_groups));
-          }
-          // no user is logged in, or none of the node's groups are the user's groups
-          if (empty($groups)) {
-            $groups = $node->og_groups;
-          }
-          // use array_shift and not [0] because array_intersect preserves keys
-          $group_node = node_load(array_shift($groups));
-          if (!$custom_theme) {
-            $custom_theme = $group_node->og_theme;
-          }
-        }
-    }
-    return $group_node;
+  if (!$custom_theme && !empty($group_node->og_theme)) {
+    $custom_theme = $group_node->og_theme;
   }
+  return $group_node;
 }
 
 /**
