Index: popups_reference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/popups_reference/popups_reference.module,v
retrieving revision 1.4.2.2
diff -u -p -r1.4.2.2 popups_reference.module
--- popups_reference.module	21 Mar 2009 01:03:20 -0000	1.4.2.2
+++ popups_reference.module	30 Mar 2009 20:12:55 -0000
@@ -24,24 +24,21 @@ function popups_reference_form_alter(&$f
     );
     $form['#submit'][] = '_popups_reference_manage_fields_submit';
   }
-  elseif (isset($form['type'])) {
+  elseif ((isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) || 
+      variable_get('popups_reference_show_add_link_'. current(array_keys($form)), FALSE) == TRUE) {
     // Add the "Add New: Node Type" links.
-    $node = $form['#node'];
-    if ($form['type']['#value'] .'_node_form' == $form_id) {  
-      $fields = content_fields();
-      foreach ($form as $key => $item) {
-        if (is_array($item)) {
-          $type = $item['#type'];
-          if ($type == 'fieldset') { // Loop through all the subitems.
-            foreach ($form[$key] as $subkey => $subitem) {
-              popups_reference_alter_item($form[$key], $subkey, $subitem, $fields);
-            }
-          }
-          else {
-            popups_reference_alter_item($form, $key, $item, $fields);
+    $fields = content_fields();
+    foreach ($form as $key => $item) {
+      if (is_array($item)) {
+        $type = $item['#type'];
+        if ($type == 'fieldset') { // Loop through all the subitems.
+          foreach ($form[$key] as $subkey => $subitem) {
+            popups_reference_alter_item($form[$key], $subkey, $subitem, $fields);
           }
         }
-        
+        else {
+          popups_reference_alter_item($form, $key, $item, $fields);
+        }
       }
     }
   }
@@ -57,7 +54,7 @@ function popups_reference_nodeapi($node,
   if ($op == 'insert') {
     $five = time()+300; // 5 minutes in the future.
     setcookie("PopupRefNid", $node->nid, $five, '/'); 
-    setrawcookie("PopupRefTitle", rawurlencode($node->title), $five, '/');
+    setrawcookie("PopupRefTitle", rawurlencode($node->title .' [nid:'. $node->nid .']'), $five, '/');
   }
 }
 
@@ -86,14 +83,21 @@ function popups_reference_alter_item(&$f
       variable_get('popups_reference_show_add_link_'. $field_name, TRUE)) {
     $type = $form['type']['#value'];
     $field = content_fields($field_name, $type);
-    
-    $wrapper_id = 'popups-reference-' . _popups_reference_counter();
-    $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']);
-    if ($links) {
-      // Put the nodereference widget and links in an wrapper.
-      // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting.
-      $form[$key]['#prefix'] = '<div class="'. $wrapper_id .'">';
-      $form[$key]['#suffix'] = '<div>Add New: ' . implode(', ', $links) .'</div></div>';
+
+    // we may have multiple fields in case of 'unlimited'
+    foreach ($form[$field_name] as $key => $value) {
+      if (is_numeric($key)) {
+        // add link
+        $wrapper_id = 'popups-reference-' . _popups_reference_counter();
+        $links = _popups_reference_links($field, $type, $wrapper_id, $field['widget']['type']);
+        if ($links) {
+          // Put the nodereference widget and links in an wrapper.
+          // Makes it easy to find for Ahah targeting, and popups_reference behavior selecting.
+          $form[$field_name][$key]['#prefix'] .= '<div class="'. $wrapper_id .'">';
+          $form[$field_name][$key]['#suffix'] = '<div>Add New: ' . implode(', ', $links) .'</div></div>'. (!empty($form[$field_name][$key]['#suffix']) ? $form[$field_name][$key]['#suffix'] : '');
+
+        }
+      }
     }
   }
 }
