@@ -3,6 +3,7 @@
 
 /**
  * Implementation of hook_nodeapi().
+ *  
  * Fill in menu section values if user left them empty AND there's a parent
  * menu item selected for this node type and the node is published.
  */
@@ -10,23 +11,44 @@
   switch ($op) {
     case 'insert':
     case 'update':
-      $parent_menu = explode(":", variable_get('parentmenu' . $node->language . '_' . $node->type, '0'));
-      if ($parent_menu[0] != '0') {
-        if (($node->menu['link_title'] == '' || $node->menu['delete']) && $node->status == 1) {
-          $new_menu = array(
-            'menu_name' => $parent_menu[0],
-            'link_path' => 'node/' . $node->nid,
-            'link_title' => $node->title,
-            'plid' => $parent_menu[1],
-            'hidden' => variable_get('automenu_hide_'.$node->type, 0),
-            //'customized' => true, // ?
-          );
+      if ($node->menu['link_title'] == '' || $node->menu['delete']) {
+        $parent_menu = explode(":", variable_get('parentmenu' . $node->language . '_' . $node->type, '0'));
+        // we're working wiht a content type that has the book option enabled.
+        if (variable_get('automenu_book_' . $node->type, 0) && $node->book['mlid'] && $node->book['plid']) {
+          // the link path of the parent
+          $link_path = db_result(db_query("SELECT link_path FROM {menu_links} ml WHERE mlid = %d", $node->book['plid']));
+          // the mlid of the other copy of the parent; the non-book entry
+          $parent = db_fetch_object(db_query("SELECT menu_name, mlid FROM {menu_links} ml 
+            WHERE mlid <> %d AND link_path = '%s' ORDER BY mlid ASC", $node->book['plid'], $link_path));
+          if ($parent) {
+            $new_menu = array(
+              'menu_name' => $parent->menu_name,
+              'link_path' => 'node/' . $node->nid,
+              'link_title' => $node->book['link_title'],
+              'plid' => $parent->mlid,
+              'hidden' => !$node->status,
+            );
+          }
+        }
+        else if ($parent_menu[0] != '0') {
+          if ($node->status == 1) {
+            $new_menu = array(
+              'menu_name' => $parent_menu[0],
+              'link_path' => 'node/' . $node->nid,
+              'link_title' => $node->title,
+              'plid' => $parent_menu[1],
+              'hidden' => variable_get('automenu_hide_'.$node->type, 0),
+              //'customized' => true, // ?
+            );
+          }
+        }
+        if ($new_menu) {
           if (!menu_link_save($new_menu)) {
             drupal_set_message(t('There was an error saving the auto-menu link.'), 'error');
           }
-	  else {
-            drupal_set_message(t('The page was automatically added to: !menu.', array('!menu' => $parent_menu[0])));
-	  }
+      	  else {
+            drupal_set_message(t('The page was automatically added to: !menu.', array('!menu' => $new_menu['menu_name'])));
+      	  }
         }
       }
       break;
@@ -35,6 +57,7 @@
 
 /**
  * Implementation of hook_form_alter().
+ *  
  * Add the "Default Parent Menu" section to content type edit node.
  */
 function automenu_form_alter(&$form, $form_state, $form_id) {
@@ -49,7 +72,15 @@
     // add a selection for "no language" selection... maybe we can find a
     // better way (I think this is what happens in language neutral situations?)
     
-    
+    if (module_exists('book')) {
+      $form['workflow']['automenu_book'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Build menu based on book settings'),
+        '#default_value' => variable_get('automenu_book_' . $form['#node_type']->type, 0),
+        '#description' => t('This will override any automatic settings below.'),
+      );
+    }
+
     $form['workflow']['parentmenu'] = array(
       '#type' => 'select',
       '#title' => t('Default parent menu'),
@@ -75,7 +106,5 @@
       '#description' => t('Set all auto generated menu items of this content type to hidden'),
     );
 
-
   }
-}
-
+}
\ No newline at end of file
