--- book.old	Fri Dec 10 00:00:46 2004
+++ book.module	Sat Dec 11 16:22:37 2004
@@ -124,41 +124,95 @@
 
   return $items;
 }
-
 /**
  * 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) {
+function book_block($op = 'list', $delta = 0, $edit = 0) {
   $block = array();
-  if ($op == 'list') {
-    $block[0]['info'] = t('Book navigation');
-    return $block;
-  }
-  else if ($op == 'view') {
-    // Only display this block when the user is browsing a book:
-    if (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;
-
-        $expand = array();
-        foreach ($path as $key => $node) {
-          $expand[] = $node->nid;
+  switch ($op) {
+    case 'list':
+      if ($op == 'list') {
+        $book_blocks[0]['info'] = t('Book navigation');
+        $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' => t('Book:') . ' ' . $books->title);
         }
+        return($book_blocks);
+      }
+    break;
+    case 'view':
+      // Set up static variables and put queries in them to increase speed
+      static $is_node, $node, $path, $enabled_blocks;
+      if (!isset($is_node)) {
+        $is_node = (arg(0) == 'node' && is_numeric(arg(1)));
+        if ($is_node) {
+          $result = book_block_query(arg(1));
+          if (db_num_rows($result) > 0) {
+            $node = db_fetch_object($result);
+            $path = book_get_path($node);
+          }
+        }
+        // Determine which book blocks are enabled so we can avoid duplicating them
+        $enabled_blocks = array();
+        $result = db_query('SELECT b.delta FROM {blocks} b WHERE module = "book" AND status = 1 AND delta != 0');
+        while($row = db_fetch_object($result)) {
+          $enabled_blocks[] = $row->delta;
+        }
+      }
 
-        $block['subject'] = $path[0]->title;
-        $block['content'] = book_tree($expand[0], 5, $expand);
+      // Individual book blocks that are enabled are created here 
+      if (!is_node || $delta) {
+        if ($path[0]->nid != $delta) {
+          $result = book_block_query($delta);
+          $new_node = db_fetch_object($result);
+          $new_path = book_get_path($new_node);
+          $block = book_create_block($new_node, $new_path);
+        } else {
+          $block = book_create_block($node, $path);
+        }
       }
-    }
 
-    return $block;
+      // Book navigation block is created here
+      else {
+        if($path && !$delta && is_node && !in_array($path[0]->nid, $enabled_blocks)) {
+          $block = book_create_block($node, $path);
+        }
+      }
+      return $block;
+    break;
   }
+}
+
+/**
+ * Helper function to book_block to return a block
+ */
+function book_create_block($node, $path) {
+  $expand = array();
+  foreach ($path as $key => $node) {
+    $expand[] = $node->nid;
+  }  
+  $block['subject'] = $path[0]->title;
+  $block['content'] = book_tree($expand[0], 5, $expand);
+  return $block;
+}
+
+/**
+ * Helper function to book_block function to return the path for a node.
+ */
+function book_get_path($node) {
+  $path = book_location($node);
+  $path[] = $node;
+  return $path;
+}
+
+/**
+ * Helper function to book_block function to return results for frequently used queries in the function.
+ */
+function book_block_query($query_arg) {
+  return 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', $query_arg);
 }
 
 /**
