--- nodehierarchy.module-orig	2009-11-03 09:57:05.000000000 -0600
+++ nodehierarchy.module	2009-11-30 14:59:05.000000000 -0600
@@ -442,7 +442,14 @@ function nodehierarchy_insert_node(&$nod
       $additions = nodehierarchy_load_node($node);
       $node->order_by = $additions['order_by'];
     }
-  }
+   }
+   elseif ($node->nid && (@$node->old_parent == @$node->parent)) {
+   /// if we have a valid node, and the parent has not changed
+   // load up the order_by field, in case it is needed for later operations
+      $additions = nodehierarchy_load_node($node);
+      $node->order_by = $additions['order_by'];
+   } 
+  
 }
 
 /**
@@ -805,16 +812,27 @@ function nodehierarchy_get_children($nid
 
   if ($nid) {
     $query = "SELECT h.nid FROM {nodehierarchy} h WHERE  h.parent = %d ORDER BY h.order_by ASC";
+    $qparam = $nid;
   }
   else {
-    $query = "SELECT n.nid FROM {node} n LEFT JOIN {nodehierarchy} h ON h.nid = n.nid WHERE h.parent = 0 OR h.parent IS NULL ORDER BY h.order_by ASC";
+    // load list of NH-managed types
+    $typelist=_nodehierarchy_get_types();
+    if (empty($typelist)) {
+       $query = "SELECT n.nid FROM {node} n LEFT JOIN {nodehierarchy} h ON h.nid = n.nid WHERE h.parent = 0 ORDER BY h.order_by ASC";
+       $qparam= $nid;
+    }
+    else {
+       $qplaceholders = db_placeholders($typelist, 'varchar');
+       $query = "SELECT n.nid FROM {node} n LEFT JOIN {nodehierarchy} h ON h.nid = n.nid WHERE n.type IN ($qplaceholders) AND h.parent = 0 ORDER BY h.order_by ASC";
+       $qparam = $typelist;
+    }
   }
 
   if ($pager) {
-    $result = pager_query($query, $pager, 0, NULL, $nid);
+    $result = pager_query($query, $pager, 0, NULL, $qparam);
   }
   else {
-    $result = db_query($query, $nid);
+    $result = db_query($query, $qparam);
   }
 
   while ($node = db_fetch_object($result)) {
@@ -930,7 +948,20 @@ function _nodehierarchy_normalize_child_
     $menu_changed = FALSE;
     // Done either on insert or update of a hierarchy. we want to make order by
     // values into integers to get in the updated one and remain consistent.
-    $result = db_query("SELECT * FROM {nodehierarchy} h WHERE h.parent = %d ORDER BY h.order_by ASC", $parent_nid);
+
+    // load list of NH-managed types.. so we can limit our work to nodes which matter
+    $typelist=_nodehierarchy_get_types();
+    if (empty($typelist)) {
+       $query = "SELECT h.* FROM {nodehierarchy} h WHERE h.parent = %d ORDER BY h.order_by ASC";
+       $qparam= $parent_nid;
+    }
+    else {
+       $qplaceholders = db_placeholders($typelist, 'varchar');
+       $query = "SELECT h.* FROM {nodehierarchy} h join {node} n on h.nid=n.nid WHERE h.parent = %d AND n.type IN ($qplaceholders) ORDER BY h.order_by ASC";
+       $qparam = array_merge(array($parent_nid), $typelist);
+    }
+
+    $result = db_query($query, $qparam);
 
     $i = 1;
     while ($hierarchy = db_fetch_object($result)) {
@@ -983,7 +1014,7 @@ function _nodehierarchy_create_menu(&$no
     $item['link_title'] = trim($node->title);
     $item['link_path'] = "node/$node->nid";
     // Editable menu weight range used to start at -10.
-    $item['weight'] = $node->order_by - 11;
+    $item['weight'] = $node->order_by;
     if (!menu_link_save($item)) {
       drupal_set_message(t('There was an error saving the menu link.'), 'error');
     }
@@ -1109,3 +1140,17 @@ function _nodehierarchy_get_parent_types
   return $types;
 }
 
+/**
+ * Get a list of types which can be parents or children (NH-managed types)
+ */
+function _nodehierarchy_get_types($quoted = FALSE) {
+  $types = array();
+  foreach (node_get_types() as $key => $type) {
+    if (variable_get('nh_parent_'. $key, FALSE) || variable_get('nh_child_'. $key, FALSE)) {
+      $types[] = $quoted ? "'$key'" : $key;
+    }
+  }
+  return $types;
+}
+
+
