Index: freelinking.module
===================================================================
--- freelinking.module	(revision 137)
+++ freelinking.module	(working copy)
@@ -39,7 +39,7 @@
   );
 } // endfunction freelinking_theme
 
-function freelinking_page($thetitle = NULL) {
+function freelinking_page($thetitle = NULL, $gid = NULL) {
   if ($_POST['operation'] == 'delete' && $_POST['links']) {
     return drupal_get_form('freelinking_multiple_delete_confirm', $_POST['links']);
   }
@@ -48,7 +48,7 @@
   }
 
   if ($thetitle) { // find the matching title
-    $freelink = _freelinking_make_link($thetitle);
+    $freelink = _freelinking_make_link($thetitle, $gid);
     drupal_goto($freelink['path'], isset($freelink['options']['query']) ? $freelink['options']['query'] : '');
   }
   else { // no title was passed -- show a list of wikiwords and status
@@ -280,7 +280,14 @@
     '#default_value' => variable_get("freelinking_onceonly", 0) == 1 ? TRUE : FALSE,
     '#description' => t('If desired you can only turn the first occurance of a freelink into a link. This can improve the appearance of content that includes a lot of the same CamelCase words.')
   );
-
+  $form["freelinking_organicgroups"] = array(
+    '#title' => t('Inherit Organic Group'),
+    '#type' => 'checkbox',
+    '#disabled' =>  !function_exists(og_get_group_context),
+    '#default_value' => function_exists(og_get_group_context) ? variable_get("freelinking_organicgroups", 0) : 0,
+    '#description' => t('Should nodes created by new freelinks inherit the Organic Group of the parent node?  (Requires Organic Groups module to be installed)'),
+  );
+  
   return system_settings_form($form);
 }
 
@@ -476,7 +483,10 @@
          $store = FALSE;
       }
       else {
-        $replacement = l(html_entity_decode($phrase), 'freelinking/' . rawurlencode($freelink), array('attributes' => array('class' => 'freelinking')));
+        if (variable_get("freelinking_organicgroups", 0) == 1 && function_exists('og_get_group_context')) {
+          $groups = og_get_group_context();
+        }
+        $replacement = l(html_entity_decode($phrase), 'freelinking/' . rawurlencode($freelink) . ($groups ? '/' . $groups->nid : ''), array('attributes' => array('class' => 'freelinking')));
       }
     }
 
@@ -484,7 +494,10 @@
       $pattern = '/(?<!\/)\b' . $wikiword . '\b(?![^<]*>)/';
       $phrase = $wikiword; // consistency for the db
       $freelink = $wikiword; // also for the db
-      $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword), array('attributes' => array('class' => 'freelinking')));
+      if (variable_get("freelinking_organicgroups", 0) == 1 && function_exists('og_get_group_context')) {
+        $groups = og_get_group_context();
+      }
+      $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword) . ($groups ? '/' . $groups->nid : ''), array('attributes' => array('class' => 'freelinking')));
     }
     $text = preg_replace($pattern, $replacement, $text, variable_get("freelinking_onceonly", 0) ? 1 : -1);
     
@@ -546,12 +559,12 @@
   return (empty($nid) ? 0 : $nid);
 }
 
-function _freelinking_make_link($thetitle) { // helper function for freelinking_page
+function _freelinking_make_link($thetitle, $gid) { // helper function for freelinking_page
   global $user;
   $freelink = array('options' => array()); // ensure that 'options' is an array
   
   // Returns a link to a node named $thetitle if found, or a link to new content otherwise.
-  $nid = _freelinking_exists($thetitle); 
+  $nid = _freelinking_exists($thetitle);
   if ($nid) { // the node exists, set the path to go there
     $freelink['path'] = 'node/' . $nid;
   }
@@ -560,14 +573,14 @@
       case 'create only':
         $freelink['path'] = 'node/add/' . variable_get('freelinking_nodetype', 'story');
         $freelink['options'] = array(
-          'query' => 'edit[title]=' . $thetitle,
+          'query' => 'edit[title]=' . $thetitle . ( $gid ? '&gids[]=' . $gid : '' ),
         );
         break;
       case 'no access search':
         if (node_access('create', variable_get('freelinking_nodetype', 'story'))) {
           $freelink['path'] = 'node/add/' . variable_get('freelinking_nodetype', 'story');
           $freelink['options'] = array(
-            'query' => 'edit[title]=' . $thetitle,
+            'query' => 'edit[title]=' . $thetitle . ( $gid ? '&gids[]=' . $gid : '' ),
           );
         }
         else {
