--- book.module.cvs	Thu Dec  9 01:01:10 2004
+++ book.cvs.orig	Thu Dec  9 01:04:36 2004
@@ -128,18 +128,84 @@
 /**
  * Implementation of hook_block().
  *
- * Displays the book table of contents in a block when the current page is a
- * single-node view of a book node.
+ * Shows one or more table of contents for books in separate blocks.
+ *
  */
 function book_block($op = 'list', $delta = 0) {
+  // These variables protect from having duplicate book menus on the same page
+  // by tracking which blocks have already been generated.
+  static $stop_duplicate_flag;
+  static $completed_deltas = array();
+ 
   $block = array();
   if ($op == 'list') {
-    $block[0]['info'] = t('Book navigation');
-    return $block;
+    $book_blocks[0]['info'] = t('Book navigation');
+    
+    // Add blocks for each book to list
+    $result = db_query('SELECT n.title, b.nid FROM {book} b INNER JOIN {node} n on n.nid = b.nid WHERE b.parent = 0');
+    while ($books = db_fetch_object($result)) {
+      $book_blocks[$books->nid] = array('info' => $books->title . t(' (book)'));
+    }
+    return($book_blocks);
   }
-  else if ($op == 'view') {
-    // Only display this block when the user is browsing a book:
-    if (arg(0) == 'node' && is_numeric(arg(1))) {
+  else {
+    // Expand the book menu appropriately if we are viewing a node that belongs to a book
+    if ($delta  && (arg(0) == 'node' && is_numeric(arg(1)))) {
+
+      $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', arg(1));
+      
+      if (db_num_rows($result) > 0) {
+        $node = db_fetch_object($result);
+        $path = book_location($node);
+        $path[] = $node;
+
+        // See if the node belongs to the book we are currently tyring to create a block for.  If so, use it
+        // to seed the menu.
+        if ($path[0]->nid == $delta && !in_array($delta, $completed_deltas)) {
+          $expand = array();
+          foreach ($path as $key => $node) {
+            $expand[] = $node->nid;
+          }  
+          $block['subject'] = $path[0]->title;
+          $block['content'] = book_tree($expand[0], 5, $expand);
+          $completed_deltas[] = $delta;
+          $stop_duplicate_flag = TRUE;
+        }
+        // If the node doesn't belong to the node we're creating a block for, use the parent node of the book
+        // to seed the menu.
+        else {
+          if(!in_array($delta, $completed_deltas)) {
+            $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', $delta);
+            $node = db_fetch_object($result);
+            $path = book_location($node);
+            $path[] = $node;
+            $expand = array();
+            foreach ($path as $key => $node) {
+              $expand[] = $node->nid;
+            }  
+            $block['subject'] = $path[0]->title;
+            $block['content'] = book_tree($expand[0], 5, $expand);
+            $completed_deltas[] = $delta;
+          }
+        } 
+      }
+    }
+    // If we aren't viewing a node, just diplay the block.  Use the parent node of the book we are currently
+    // trying to generate a block for to seed the menu.
+    elseif ($delta && !(arg(0) == 'node' && is_numeric(arg(1)))  && !$stop_duplicate_flag) {
+      $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', $delta);
+      $node = db_fetch_object($result);
+      $path = book_location($node);
+      $path[] = $node;
+      $expand = array();
+      foreach ($path as $key => $node) {
+        $expand[] = $node->nid;
+      }  
+      $block['subject'] = $path[0]->title;
+      $block['content'] = book_tree($expand[0], 5, $expand);
+    }
+    // Handles book navigation block
+    elseif (!$delta && (arg(0) == 'node' && is_numeric(arg(1)))) {
       $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', arg(1));
       if (db_num_rows($result) > 0) {
         $node = db_fetch_object($result);
@@ -151,14 +217,16 @@
         foreach ($path as $key => $node) {
           $expand[] = $node->nid;
         }
-
-        $block['subject'] = $path[0]->title;
-        $block['content'] = book_tree($expand[0], 5, $expand);
+        if(!in_array($path[0]->nid, $completed_deltas)) {
+          $block['subject'] = $path[0]->title;
+          $block['content'] = book_tree($expand[0], 5, $expand);
+          $completed_deltas[] = $path[0]->nid;
+          $stop_duplicate_flag = TRUE;
+        }
       }
     }
-
-    return $block;
   }
+  return $block;
 }
 
 /**
