Index: modules/menu/menu.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.install,v
retrieving revision 1.5
diff -u -p -r1.5 menu.install
--- modules/menu/menu.install	5 Jun 2007 09:15:02 -0000	1.5
+++ modules/menu/menu.install	18 Jun 2007 22:11:48 -0000
@@ -7,9 +7,16 @@
 function menu_install() {
   // Create tables.
   drupal_install_schema('menu');
-  db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('navigation', 'Navigation', 'The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.')");
-  db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('primary-links', 'Primary links', 'Primary links are often used at the theme layer to show the major sections of a site. A typical representation for primary links would be tabs along the top.')");
-  db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('secondary-links', 'Secondary links', 'Secondary links are often used for pages like legal notices, contact details, and other secondary navigation items that play a lesser role than primary links')");
+
+  $default_menus = array(    
+    array('menu_name' => 'navigation', 'title' => 'Navigation', 'description' => 'The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.'),
+    array('menu_name' => 'primary-links', 'title' => 'Primary links', 'description' => 'Primary links are often used at the theme layer to show the major sections of a site. A typical representation for primary links would be tabs along the top.'),
+    array('menu_name' => 'secondary-links', 'title' => 'Secondary links', 'description' => 'Secondary links are often used for pages like legal notices, contact details, and other secondary navigation items that play a lesser role than primary links'),
+  );
+
+  foreach ($default_menus as $menu) {
+    db_query("INSERT INTO {menu_custom} (menu_name, title, description, mlid) VALUES ('%s', '%s', '%s', %d)", $menu['menu_name'], $menu['title'], $menu['description'], 0);
+  }
 }
 
 /**
Index: modules/menu/menu.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.module,v
retrieving revision 1.116
diff -u -p -r1.116 menu.module
--- modules/menu/menu.module	6 Jun 2007 06:18:47 -0000	1.116
+++ modules/menu/menu.module	18 Jun 2007 22:11:48 -0000
@@ -45,104 +45,119 @@ function menu_menu() {
   $items['admin/build/menu'] = array(
     'title' => 'Menus',
     'description' => "Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items.",
-    'page callback' => 'system_admin_menu_block_page',
-    'file' => 'system.admin.inc',
-    'file path' => drupal_get_path('module', 'system'),
+    'page callback' => 'menu_overview_page',
     'access callback' => 'user_access',
     'access arguments' => array('administer menu'),
   );
-  $result = db_query('SELECT * FROM {menu_custom} ORDER BY title');
-  while ($menu = db_fetch_object($result)) {
-    $items['admin/build/menu/'. $menu->menu_name] = array(
-      'title' => $menu->title,
-      'page callback' => 'menu_overview',
-      'page arguments' => array($menu->menu_name),
-      'description' => $menu->description,
-    );
-    $items['admin/build/menu/'. $menu->menu_name .'/list'] = array(
-      'title' => 'List items',
-      'weight' => -10,
-      'type' => MENU_DEFAULT_LOCAL_TASK,
-    );
-    $items['admin/build/menu/'. $menu->menu_name .'/add'] = array(
-      'title' => 'Add item',
-      'page callback' => 'drupal_get_form',
-      'page arguments' => array('menu_edit_item', 'add', $menu->menu_name),
-      'type' => MENU_LOCAL_TASK);
-    $items['admin/build/menu/'. $menu->menu_name .'/edit'] = array(
-      'title' => 'Edit menu',
-      'page callback' => 'drupal_get_form',
-      'page arguments' => array('menu_edit_menu', 'edit', $menu->menu_name),
-      'type' => MENU_LOCAL_TASK);
-  }
 
   $items['admin/build/menu/list'] = array(
     'title' => 'List menus',
     'type' => MENU_DEFAULT_LOCAL_TASK,
-    'weight' => -10,
-  );
-  $items['admin/build/menu/menu/add'] = array(
+    'weight' => -10);
+  $items['admin/build/menu/add'] = array(
     'title' => 'Add menu',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('menu_edit_menu', 'add'),
     'type' => MENU_LOCAL_TASK);
-  $items['admin/build/menu/item/disable'] = array(
+  $items['admin/build/menu/settings'] = array(
+    'title' => 'Settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('menu_configure'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 5);
+
+  $items['admin/build/menu-customize/%menu'] = array(
+    'title' => 'Customize menu',
+    'page callback' => 'menu_overview',
+    'page arguments' => array(3),
+    'access arguments' => array('administer menu'),
+    'type' => MENU_CALLBACK);
+  $items['admin/build/menu-customize/%menu/list'] = array(
+    'title' => 'List items',
+    'weight' => -10,
+    'type' => MENU_DEFAULT_LOCAL_TASK);
+  $items['admin/build/menu-customize/%menu/add'] = array(
+    'title' => 'Add item',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('menu_edit_item', 'add', array(), 3),
+    'type' => MENU_LOCAL_TASK);
+  $items['admin/build/menu-customize/%menu/edit'] = array(
+    'title' => 'Edit menu',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('menu_edit_menu', 'edit', 3),
+    'type' => MENU_LOCAL_TASK); 
+
+  $items['admin/build/menu/item/%menu_link/disable'] = array(
     'title' => 'Disable menu item',
     'page callback' => 'menu_flip_item',
-    'page arguments' => array(TRUE),
-    'type' => MENU_CALLBACK,
-  );
-  $items['admin/build/menu/item/enable'] = array(
+    'page arguments' => array(TRUE, 4),
+    'type' => MENU_CALLBACK);
+  $items['admin/build/menu/item/%menu_link/enable'] = array(
     'title' => 'Enable menu item',
     'page callback' => 'menu_flip_item',
-    'page arguments' => array(FALSE),
-    'type' => MENU_CALLBACK,
-  );
-  $items['admin/build/menu/item/edit'] = array(
+    'page arguments' => array(FALSE, 4),
+    'type' => MENU_CALLBACK);
+  $items['admin/build/menu/item/%menu_link/edit'] = array(
     'title' => 'Edit menu item',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_edit_item', 'edit', 5),
+    'page arguments' => array('menu_edit_item', 'edit', 4),
     'type' => MENU_CALLBACK);
-  $items['admin/build/menu/item/reset'] = array(
+  $items['admin/build/menu/item/%menu_link/reset'] = array(
     'title' => 'Reset menu item',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_reset_item'),
+    'page arguments' => array('menu_reset_item', 4),
     'type' => MENU_CALLBACK);
-  $items['admin/build/menu/item/delete'] = array(
+  $items['admin/build/menu/item/%menu_link/delete'] = array(
     'title' => 'Delete menu item',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_item_delete_form'),
+    'page arguments' => array('menu_item_delete_form', 4),
     'type' => MENU_CALLBACK);
 
-  $items['admin/build/menu/menu/edit'] = array(
-    'title' => 'Edit menu',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_edit_menu', 'edit'),
-    'type' => MENU_CALLBACK);
-  $items['admin/build/menu/menu/delete'] = array(
-    'title' => 'Delete menu',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_item_delete_form'),
-    'type' => MENU_CALLBACK);
+  return $items;
+}
 
-  $items['admin/build/menu/settings'] = array(
-    'title' => 'Settings',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('menu_configure'),
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 5,
-  );
+/**
+ * Implementation of hook_enable()
+ *
+ *  Add a link for each custom menu.
+ */
+function menu_enable() {
+  menu_rebuild();
+  $result = db_query("SELECT * FROM {menu_custom}");
+  $link['module'] = 'menu';
+  $link['plid'] = db_result(db_query("SELECT mlid from {menu_links} WHERE menu_name = 'navigation' AND link_path = '%s'", 'admin/build/menu'));
+  $link['router_path'] = 'admin/build/menu-customize/%';  
 
-  return $items;
+  while ($menu = db_fetch_array($result)) {
+    $link['mlid'] = 0;
+    $link['link_title'] = $menu['title'];
+    $link['link_path'] = 'admin/build/menu-customize/'. $menu['menu_name'];
+    menu_link_save($link);
+    db_query("UPDATE {menu_custom} SET mlid = %d WHERE menu_name = '%s'", $link['mlid'], $menu['menu_name']);
+  }
+}
 
+function menu_load($menu_name) {
+  return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
 }
 
+function menu_overview_page() {
+  $result = db_query("SELECT * FROM {menu_custom}");  
+
+  while ($menu = db_fetch_array($result)) {
+    $menu['href'] = 'admin/build/menu-customize/'. $menu['menu_name'];
+    $menu['options'] = array();
+    $content[] = $menu;
+  }
+  return theme('admin_block_content', $content);
+}
 
 /**
  * Menu callback which displays every menu element accessible to the current
  * user and the relevant operations.
  */
-function menu_overview($menu_name) {
+function menu_overview($menu) {
+
   $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
   $sql ="
     SELECT *, ml.weight + 50000 AS weight FROM {menu_links} ml
@@ -150,7 +165,7 @@ function menu_overview($menu_name) {
     WHERE menu_name = '%s' AND hidden >= 0
     ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC";
   $sql_count = "SELECT COUNT(*) FROM {menu_links} ml WHERE menu_name = '%s' AND hidden >= 0";
-  $result = pager_query($sql, 200, 0, $sql_count, $menu_name);
+  $result = pager_query($sql, 200, 0, $sql_count, $menu['menu_name']);
   list(, $tree) = _menu_tree_data($result);
   $rows = _menu_overview_tree($tree);
   $output = theme('table', $header, $rows);
@@ -172,23 +187,23 @@ function _menu_overview_tree($tree) {
       // Populate the operations field.
       $operations = array();
       // Set the edit column.
-      $operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/edit/'. $item['mlid']));
+      $operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/'. $item['mlid'] .'/edit'));
       if ($item['hidden']) {
         $title .= ' ('. t('disabled') .')';
         $class = 'menu-disabled';
-        $operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/enable/'. $item['mlid']));
+        $operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/'. $item['mlid'] .'/enable'));
       }
       else {
         $class = 'menu-enabled';
-        $operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/disable/'. $item['mlid']));
+        $operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/'. $item['mlid'] .'/disable'));
       }
       // Only items created by the menu module can be deleted.
       if ($item['module'] == 'menu') {
-        $operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/delete/'. $item['mlid']));
+        $operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/'. $item['mlid'] .'/delete'));
       }
       // Set the reset column.
-      else if ($item['module'] == 'system') {
-        $operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/reset/'. $item['mlid']));
+      else if ($item['module'] == 'system' && $item['customized']) {
+        $operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/'. $item['mlid'] .'/reset'));
       }
       else {
         $operations[] = array('data' => '');
@@ -216,14 +231,11 @@ function _menu_overview_tree($tree) {
  * @param $hide
  *   TRUE to not show in the menu tree. FALSE to make the item and its children
  *   reappear in menu tree.
- * @param $mlid
- *    mlid of the menu item.
+ * @param $item
+ *    The menu item.
  */
-function menu_flip_item($hide, $mlid) {
-  if (!($item = menu_link_load($mlid))) {
-    drupal_not_found();
-    return;
-  }
+function menu_flip_item($hide, $item) {
+
   $item['hidden'] = (bool)$hide;
   menu_link_save($item);
   drupal_set_message($hide ? t('The menu item has been disabled.') : t('The menu item has been enabled.'));
@@ -233,33 +245,19 @@ function menu_flip_item($hide, $mlid) {
 /**
  * Menu callback; present the menu item editing form.
  */
-function menu_edit_item(&$form_state, $type, $id = 0) {
-  if ($type == 'edit') {
-    if (!($item = menu_link_load($id))) {
-      drupal_not_found();
-      return;
-    }
-  }
-  else {
-    // This is an add form.
-    // The mlid argument (if set) will be the default pid to use.
-    $item = array('mlid' => 0, 'plid' => 0, 'menu_name' => $id, 'weight' => 0, 'link_title' => '', 'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
+function menu_edit_item(&$form_state, $type, $item, $menu) {
+
+  if ($type == 'add' || empty($item)) {
+    // This is an add form, initialize the menu link.
+    $item = array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu['menu_name'], 'weight' => 0,  'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
   }
-  foreach (array('link_path', 'mlid', 'module', 'hidden', 'menu_name', 'has_children') as $key) {
+  foreach (array('link_path', 'mlid', 'module', 'hidden', 'menu_name', 'has_children', 'options') as $key ) {
     $form[$key] = array('#type' => 'value', '#value' => $item[$key]);
   }
+  // Any item created or edited via this interface is considered "customized".
+  $form['customized'] = array('#type' => 'value', '#value' => 1);
+  $form['original_item'] = array('#type' => 'value', '#value' => $item);
 
-  $form['link_title'] = array('#type' => 'textfield',
-    '#title' => t('Title'),
-    '#default_value' => $item['link_title'],
-    '#description' => t('The name of the menu item.'),
-    '#required' => TRUE,
-  );
-  $form['description'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Description'),
-    '#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
-  );
   if ($item['module'] == 'menu') {
     $form['link_path'] = array(
       '#type' => 'textfield',
@@ -276,8 +274,19 @@ function menu_edit_item(&$form_state, $t
       '#description' => l($item['link_title'], $item['href'], $item['options']),
     );
   }
-  $form['original_item'] = array('#type' => 'value', '#value' => $item);
-
+  $form['link_title'] = array('#type' => 'textfield',
+    '#title' => t('Menu link title'),
+    '#default_value' => $item['link_title'],
+    '#description' => t('The link text corresponding to this item that should appear in the menu.'),
+    '#required' => TRUE,
+  );
+  $form['description'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Description'),
+    '#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
+    '#rows' => 1,
+    '#description' => t('The description displayed when hovering over a menu item.'),
+  );
   $form['expanded'] = array(
     '#type' => 'checkbox',
     '#title' => t('Expanded'),
@@ -286,7 +295,7 @@ function menu_edit_item(&$form_state, $t
   );
 
   // Generate a list of possible parents (not including this item or descendants).
-  $options = menu_parent_options($item['mlid'], $item['menu_name']);
+  $options = menu_parent_options($item['menu_name'], $item);
   $form['plid'] = array(
     '#type' => 'select',
     '#title' => t('Parent item'),
@@ -307,8 +316,7 @@ function menu_edit_item(&$form_state, $t
 function menu_edit_item_validate($form, &$form_state) {
   $item = $form_state['values'];
   if (isset($item['link_path']) && !menu_path_is_external($item['link_path'])) {
-    $path = $item['link_path'];
-    $item = menu_get_item($path);
+    $item = menu_valid_path($item);
     if (!$item || !$item['access']) {
       form_set_error('path', t('This path is either invalid or you do not have access to it'));
     }
@@ -319,67 +327,45 @@ function menu_edit_item_validate($form, 
  * Process menu and menu item add/edit form submissions.
  */
 function menu_edit_item_submit($form, &$form_state) {
+  $form_state['values']['options']['attributes']['title'] = $form_state['values']['description'];
   menu_link_save($form_state['values']);
-  $form_state['redirect'] = 'admin/build/menu/'. $form_state['values']['menu_name'];
+  $form_state['redirect'] = 'admin/build/menu-customize/'. $form_state['values']['menu_name'];
 }
 
 /**
  * Return a list of menu items that are valid possible parents for the
  * given menu item. The list excludes the given item and its children.
  *
- * @param $mlid
- *   The menu item id for which to generate a list of parents.
- *   If $mlid == 0 then the complete tree is returned.
  * @param $menu_name
  *   The name of the menu.
- * @param $plid
- *   The menu link item id of the menu item at which to start the tree.
- *   If $pid > 0 then this item will be included in the tree.
- * @param $depth
- *   The current depth in the tree - used when recursing to indent the tree.
+ * @param $item
+ *   The menu item for which to generate a list of parents.
+ *   If $item['mlid'] == 0 or NULL then the complete tree is returned.
  * @return
- *   An array of menu titles keyed on the mlid.
+ *   An array of menu link titles keyed on the mlid.
  */
-function menu_parent_options($mlid, $menu_name, $plid = 0, $depth = 0) {
+function menu_parent_options($menu_name, $item) {
+
+  $tree = menu_tree_all_data($item['menu_name'], NULL, TRUE);
   $options = array(0 => t('Root'));
-  // Exclude $mlid and its children from the list unless $mlid is 0.
-  if ($mlid && $mlid == $plid) {
-    return $options;
-  }
+  _menu_parents_recurse($tree, '--', $options, $item['mlid']);
 
-  $sql = "SELECT * FROM {menu_links} ml LEFT JOIN {menu_router} mr ON ml.router_path = mr.path WHERE menu_name = '%s' AND hidden >= 0";
-  $params = array($menu_name);
-  if ($mlid && ($item = menu_link_load($mlid))) {
-    $parents = array();
-    for ($i = 1; $i <= 6; $i++) {
-      $key = "p$i";
-      $value = $item[$key];
-      if ($value) {
-        $parents[]= "$key != %d";
-        $params[] = $value;
+  return $options;
+}
+
+function _menu_parents_recurse($tree, $indent, &$options, $exclude) {
+  foreach ($tree as $data) {
+    if ($data['link']['mlid'] != $exclude) {
+      $title = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
+      if ($data['link']['hidden']) {
+        $title .= ' ('. t('disabled') .')';
       }
-      else {
-        break;
+      $options[$data['link']['mlid']] = $title;
+      if ($data['below'] && $data['link']['depth'] < MENU_MAX_DEPTH - 1) {
+         _menu_parents_recurse($data['below'], $indent .'--', $options, $exclude);
       }
     }
-    $sql .= ' AND (' . implode(' OR ', $parents) .')';
   }
-
-  $sql .= ' ORDER BY p1, p2, p3, p4, p5';
-  $result = db_query($sql, $params);
-
-  while ($item = db_fetch_array($result)) {
-    _menu_link_translate($item);
-    if (!$item['access']) {
-      continue;
-    }
-    $title = str_repeat('--', $item['depth']) .' '. $item['link_title'];
-    if ($item['hidden']) {
-      $title .= ' ('. t('disabled') .')';
-    }
-    $options[$item['mlid']] = $title;
-  }
-  return $options;
 }
 
 /**
@@ -392,10 +378,10 @@ function menu_node_form_delete($node) {
 /**
  * Menu callback; handle the adding/editing of a new menu.
  */
-function menu_edit_menu(&$form_state, $type, $menu_name = '') {
+function menu_edit_menu(&$form_state, $type, $menu = array()) {
   if ($type == 'edit') {
-    $menu = db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
-    $form['menu_name'] = array('#type' => 'value', '#value' => $menu_name);
+    $form['menu_name'] = array('#type' => 'value', '#value' => $menu['menu_name']);
+    $form['mlid'] = array('#type' => 'value', '#value' => $menu['mlid']);
     $form['#insert'] = FALSE;
   }
   else {
@@ -442,26 +428,29 @@ function menu_edit_menu_validate($form, 
 
 function menu_edit_menu_submit($form, &$form_state) {
   $menu = $form_state['values'];
-  $redirect = 'admin/build/menu/'. $menu['menu_name'];
+  $redirect = 'admin/build/menu-customize/'. $menu['menu_name'];
+  $link['link_title'] = $menu['title'];
+  $link['router_path'] = 'admin/build/menu-customize/%';
+  $link['module'] = 'menu';
+  $link['link_path'] = $redirect;
   if ($form['#insert']) {
-    db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu['menu_name'], $menu['title'], $menu['description']);
+    $link['plid'] = db_result(db_query("SELECT mlid from {menu_links} WHERE menu_name = 'navigation' AND link_path = %s", 'admin/build/menu'));
+    menu_link_save($link);
+    db_query("INSERT INTO {menu_custom} (menu_name, title, description, mlid) VALUES ('%s', '%s', '%s', %d)", $menu['menu_name'], $menu['title'], $menu['description'], $link['mlid']);
   }
   else {
     db_query("UPDATE {menu_custom} SET title = '%s', description = '%s' WHERE menu_name = '%s'", $menu['title'], $menu['description'], $menu['menu_name']);
-    db_query("UPDATE {menu_links} SET link_title = '%s' WHERE link_title = '%s' AND router_path = '%s'", $menu['title'], $form['#title'], $redirect);
+    $link['mlid'] = $menu['mlid'];
+    menu_link_save($link);
   }
-  menu_rebuild();
+
   $form_state['redirect'] = $redirect;
 }
 
 /**
  * Menu callback; delete a single custom item.
  */
-function menu_item_delete_form(&$form_state, $mlid) {
-  if (!$item = menu_link_load($mlid)) {
-    drupal_not_found();
-    return;
-  }
+function menu_item_delete_form(&$form_state, $item) {
   $form['#item'] = $item;
 
   return confirm_form($form, t('Are you sure you want to delete the custom menu item %item?', array('%item' => $item['link_title'])), 'admin/build/menu/'. $item['menu_name'], t('This action cannot be undone.'), t('Delete'));
@@ -482,14 +471,10 @@ function menu_item_delete_form_submit($f
 /**
  * Menu callback; reset a single modified item.
  */
-function menu_reset_item(&$form_state, $mlid) {
-  if (isset($mlid) && $item = db_fetch_array(db_query('SELECT router_path, link_title FROM {menu_links} WHERE mlid = %d', $mlid))) {
-    $form['#router_path'] = $item['router_path'];
-    return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $item['link_title'])), 'admin/build/menu', t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
-  }
-  else {
-    drupal_not_found();
-  }
+function menu_reset_item(&$form_state, $item) {
+  $form['#router_path'] = $item['router_path'];
+  $form['mlid'] = array('#type' => 'value', '#value' => $item['mlid']);
+  return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $item['link_title'])), 'admin/build/menu-customize/'. $item['menu_name'], t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
 }
 
 /**
@@ -497,6 +482,7 @@ function menu_reset_item(&$form_state, $
  */
 function menu_reset_item_submit($form, &$form_state) {
   $new_item = db_fetch_array(db_query("SELECT * FROM {menu_router} WHERE path = '%s'", $form['#router_path']));
+  $new_item['mlid'] = $form_state['values']['mlid'];
   menu_link_save(_menu_link_build($new_item));
   drupal_set_message(t('The menu item was reset to its default settings.'));
   $form_state['redirect'] = 'admin/build/menu/navigation';
@@ -527,7 +513,7 @@ function menu_block($op = 'list', $delta
 /**
  * Implementation of hook_nodeapi().
  */
-function menu_nodeapi($node, $op) {
+function menu_nodeapi(&$node, $op) {
   if (user_access('administer menu') && isset($node->menu)) {
     $item = $node->menu;
     switch ($op) {
@@ -536,16 +522,33 @@ function menu_nodeapi($node, $op) {
         // Deliberate no break.
       case 'insert':
       case 'update':
-        if ($item['delete']) {
-          _menu_delete_item($item);
+        if (!empty($item['delete'])) {
+          menu_link_delete($item['mlid']);
         }
-        else {
+        elseif (trim($item['link_title'])) {
+          $item['link_title'] = trim($item['link_title']);
           $item['link_path'] = "node/$node->nid";
+          if (!$item['customized']) {
+            $item['options']['attributes']['title'] = trim($node->title);
+          }
           menu_link_save($item);
         }
         break;
     }
   }
+  elseif ($op == 'prepare' && empty($node->menu)) {
+    // Prepare the node for the edit form so that $node->emnu always exists
+    $menu_name = variable_get('menu_parent_items', 'navigation');
+    $item = array();
+    if (isset($node->nid)) {
+      $mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s' ORDER BY mlid ASC", $node->nid, $menu_name));
+      if ($mlid) {
+        $item = menu_link_load($mlid);
+      }
+    }
+    // Set default values
+    $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0);
+  }
 }
 
 /**
@@ -553,35 +556,58 @@ function menu_nodeapi($node, $op) {
  * Add menu item fields to the node form.
  */
 function menu_form_alter(&$form, $form_state, $form_id) {
-  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
+  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
+
     $form['menu'] = array(
       '#type' => 'fieldset',
-      '#title' => t('Menu settings'),
+      '#title' => t('Menu settings for this post'),
       '#access' => user_access('administer menu'),
       '#collapsible' => TRUE,
       '#collapsed' => FALSE,
       '#tree' => TRUE,
-      '#weight' => 30,
+      '#weight' => -2,
     );
-    $form_state = array();
-    if ($mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s'", $form['nid']['#value'], variable_get('menu_parent_items', 'navigation')))) {
-      $menu_form = drupal_retrieve_form('menu_edit_item', $form_state, 'edit', $mlid);
-      $menu_form['delete'] = array(
+    $item = $form['#node']->menu;
+
+    if ($item['mlid']) {
+      // There is an existing link
+      $form['menu']['delete'] = array(
         '#type' => 'checkbox',
-        '#title' => t('Check to delete this menu item.'),
+        '#title' => t('Check to remove this item from the menu.'),
       );
     }
-    else {
-      $menu_form = drupal_retrieve_form('menu_edit_item', $form_state, 'add', variable_get('menu_parent_items', 'navigation'));
-      unset($menu_form['link_path']);
-      $menu_form['link_title']['#required'] = FALSE;
+    if (!$item['link_title']) {
       $form['menu']['#collapsed'] = TRUE;
     }
-    unset($menu_form['submit']);
-    $form['menu'] += $menu_form;
+
+    foreach (array('mlid', 'module', 'hidden', 'menu_name', 'has_children', 'customized', 'options', 'expanded', 'hidden') as $key) {
+      $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
+    }
+
+    $form['menu']['link_title'] = array('#type' => 'textfield',
+      '#title' => t('Menu link title'),
+      '#default_value' => $item['link_title'],
+      '#description' => t('The link text corresponding to this item that should appear in the menu.'),
+      '#required' => FALSE,
+    );
+    // Generate a list of possible parents (not including this item or descendants).
+    $options = menu_parent_options($item['menu_name'], $item);
+    $form['menu']['plid'] = array(
+      '#type' => 'select',
+      '#title' => t('Parent item'),
+      '#default_value' => $item['plid'],
+      '#options' => $options,
+    );
+    $form['menu']['weight'] = array(
+      '#type' => 'weight',
+      '#title' => t('Weight'),
+      '#default_value' => $item['weight'],
+      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
+    );
   }
 }
 
+
 function menu_get_menus($all = FALSE) {
   $sql = 'SELECT * FROM {menu_custom}'. ($all ? '' : " WHERE menu_name NOT IN ('navigation', 'primary-links', 'secondary-links')") . ' ORDER BY title';
   $result = db_query($sql);
@@ -613,3 +639,31 @@ function menu_configure() {
 
   return system_settings_form($form);
 }
+
+/**
+ * Return a valid path, 
+ * if path is dynamic (ie 'user/%'  ) check directly against menu_router table and retrieve return access right
+ * otherwise menu_get_item the $path 
+ */
+function menu_valid_path($form_item){
+  $dynamic = FALSE;
+  $path    = $form_item['link_path'];
+
+  if (preg_match('/\/\%/', $path)) {
+    $dynamic = TRUE;
+  }
+  
+  if ($dynamic) {
+    if ($item = db_fetch_array(db_query("SELECT * FROM {menu_router} where path = '%s' ",$path))) {
+      $item['link_path']  = $form_item['link_path'];
+      $item['link_title'] = $form_item['link_title'];
+      $item['external']   = $form_item['original_item']['external'];
+
+      _menu_link_translate($item);
+    }
+  }
+  else {
+    $item = menu_get_item($path);
+  }
+  return $item;
+}
\ No newline at end of file
Index: modules/menu/menu.schema
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu/menu.schema,v
retrieving revision 1.3
diff -u -p -r1.3 menu.schema
--- modules/menu/menu.schema	27 May 2007 20:31:13 -0000	1.3
+++ modules/menu/menu.schema	18 Jun 2007 22:11:48 -0000
@@ -7,6 +7,7 @@ function menu_schema() {
       'menu_name'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
       'title'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
       'description' => array('type' => 'text', 'not null' => FALSE),
+      'mlid'        => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
     ),
     'primary key' => array('menu_name'),
   );
Index: modules/system/system.schema
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.schema,v
retrieving revision 1.6
diff -u -p -r1.6 system.schema
--- modules/system/system.schema	15 Jun 2007 07:15:24 -0000	1.6
+++ modules/system/system.schema	18 Jun 2007 22:11:48 -0000
@@ -110,6 +110,7 @@ function system_schema() {
       'expanded'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
       'weight'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
       'depth'        => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'customized'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
       'p1'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
       'p2'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
       'p3'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
Index: profiles/default/default.profile
===================================================================
RCS file: /cvs/drupal/drupal/profiles/default/default.profile,v
retrieving revision 1.12
diff -u -p -r1.12 default.profile
--- profiles/default/default.profile	16 May 2007 13:45:17 -0000	1.12
+++ profiles/default/default.profile	18 Jun 2007 22:11:49 -0000
@@ -8,7 +8,7 @@
  *  An array of modules to be enabled.
  */
 function default_profile_modules() {
-  return array('color', 'comment', 'help', 'taxonomy', 'dblog');
+  return array('color', 'comment', 'help', 'menu', 'taxonomy', 'dblog');
 }
 
 /**
