Index: E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.module
===================================================================
--- E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.module	(revision 5)
+++ E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.module	(revision 6)
@@ -5,7 +5,7 @@
  * @file
  * Maintains a hierarchy of groups created by the orgainc groups module.
  */
-
+ 
 /**
  * Implementation of hook_perm().
  */
@@ -175,18 +175,54 @@
     if (og_is_group_type($node->type)) {
       // Add the subgroup fields to the node form
       $form = array_merge($form, og_subgroups_form($node));
+      
+      // Set parent if its id has been sent by the link
+      if (!empty($_GET['parents'])) {
+        //$form_state['og_gids'] = $_GET['parents'];
+      }
+
     }
   }
 }
 
 /**
+ * Implenentation of hook_form_FORM_ID_alter() for node type
+ */
+function og_subgroups_form_node_type_form_alter(&$form, &$form_state) {
+  drupal_add_js(drupal_get_path('module', 'og_subgroups'). '/og_subgroups.js');
+
+  $og_types = array();
+  $node_types = node_get_types();
+  foreach ($node_types as $type) {
+    if (og_is_group_type($type->type)) {
+      $og_types[] = $type;
+    }
+  }
+
+  $og_types_list = array();
+  foreach ($og_types as $og_type) {
+    $og_types_list[$og_type->type] = $og_type->name;
+  }
+
+  $form['og']['og_subgroups_allowed_subgroups'] = array(
+    '#prefix' => '<div id="og-group-wrapper">',
+    '#suffix' => '</div>',
+    '#type' => 'checkboxes',
+    '#title' => t('Allowed subgroup types'),
+    '#default_value' => og_subgroups_get_allowed_subgroup_types($form['#node_type']->type),
+    '#options' => $og_types_list,   
+  );
+}
+
+
+/**
  * Implementation of hook_form_FORM_ID_alter().
  */
 function og_subgroups_form_node_delete_confirm_alter(&$form, &$form_state) {
   $node = node_load($form['nid']['#value']);
   if (og_is_group_type($node->type)) {
-    $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
-    $accessibale = og_subgroups_get_eligable_groups('accessibale');
+    $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale', $node);
+    $accessibale = og_subgroups_get_eligable_groups('accessibale', $node);
     $options = og_subgroups_tree($accessibale, $node->nid, $inaccessibale);
     unset($options[0]);
     $form['target']['#options'] = $options;
@@ -213,8 +249,8 @@
     // Check node is a group type.
     if (og_is_group_type($node->type)) {
       // Get all accessible/ inaccesiable groups for the user.
-      $options = og_subgroups_get_eligable_groups('accessibale');
-      $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
+      $options = og_subgroups_get_eligable_groups('accessibale', $node);
+      $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale', $node);
       // Check parent_nid is a valid parent.
       if (array_key_exists($parent_nid, og_subgroups_tree($options, $node, $inaccessibale))) {
         $valid = TRUE;
@@ -282,8 +318,12 @@
     // Only display this block when the user is browsing inside a group:
     $arg = arg(1);
     if ($group_node = og_get_group_context()) {
+      // Get current node
+      $nid = arg(1);
+      $node = node_load($nid);
+      
       $block['subject'] = t('Subgroups');
-      $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
+      $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale', $node);
       $block['content'] = og_subgroups_menu_tree($group_node->nid, $group_node->title, $inaccessibale);
     }
 
@@ -400,6 +440,28 @@
 }
 
 /**
+ * Implementation of hook_og_create_links()
+ */
+function og_subgroups_og_create_links($group) {
+  global $user;
+  $subgroup_types = og_subgroups_get_allowed_subgroup_types($group->type);
+  $post_types = og_get_types('group_post');
+  $types = node_get_types();
+  foreach ($subgroup_types as $subgroup_type) {
+    $type_name = $types[$subgroup_type]->name;
+    $type_url_str = str_replace('_', '-', $subgroup_type);
+    if (module_invoke($types[$subgroup_type]->module, 'access', 'create', $subgroup_type, $user)) {
+      $links["create_$subgroup_type"] = l(t('Create !type', array('!type' => $type_name)), "node/add/$type_url_str", array(
+        'attributes' => array('title' => t('Add a new !type in this group.', array('!type' => $type_name))),
+        'query' => "parents[]=$group->nid",
+        ));
+    }
+  }
+  return isset($links) ? $links : array();
+}
+
+
+/**
  * Get the parent/ children for a given group node id.
  * 
  * @param $gid
@@ -438,8 +500,18 @@
  * @param $op
  *   The operation
  */
-function og_subgroups_get_eligable_groups($op) {
-  $eligable_groups = og_node_groups_distinguish(og_all_groups_options(), FALSE);
+function og_subgroups_get_eligable_groups($op, $node) {
+  // Get all existing groups
+  $groups = og_all_groups_options();
+  foreach ($groups as $gid => $title) {
+    // Remove groups that their content type 
+    // doesn't allow the current group type as a subgroup
+    $allowed_subgroups = og_subgroups_get_allowed_subgroup_types(node_load($gid)->type);
+    if (!in_array($node->type, $allowed_subgroups)) {
+      unset($groups[$gid]);
+    }
+  }
+  $eligable_groups = og_node_groups_distinguish($groups, FALSE);
   $return = array();
   switch ($op) {
     case 'accessibale':
@@ -463,8 +535,8 @@
  */
 function og_subgroups_form($node) {
   // Get all accessible/ inaccesiable groups for the user.
-  $options = og_subgroups_get_eligable_groups('accessibale');
-  $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
+  $options = og_subgroups_get_eligable_groups('accessibale', $node);
+  $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale', $node);
   
   $description = t('The parent group for this group node.');
   if ($inaccessibale) {
@@ -506,7 +578,7 @@
     $form['og_subgroups']['parent'] = array(
       '#type' => 'select',
       '#title' => t('Parent'),
-      '#default_value' => $node->og_subgroups,
+      '#default_value' => $_GET['parents'] ? $_GET['parents'] : $node->og_subgroups,
       '#options' => og_subgroups_tree($options, $node->nid, $inaccessibale),
       '#description' => $description,
     );
@@ -548,7 +620,7 @@
     // Set the top level group. Make sure the gid is part of the accessible groups.
     if (empty($tree[$gid]) && $gid != $exclude) {
       !in_array($gid, $inaccessibale) ? $tree[$gid] = $title : $tree[$gid] = t('<private group>');
-      $tree = og_subgroups_tree_recurse($gid, $exclude, $inaccessibale, $tree, $indent .'--');
+      $tree = og_subgroups_tree_recurse($gid, $groups, $exclude, $inaccessibale, $tree, $indent .'--');
     }
 
   }
@@ -558,13 +630,13 @@
 /**
  * Helper function for og_subgroups_tree().
  */
-function og_subgroups_tree_recurse($gid, $exclude, $inaccessibale, $tree, $indent) {
+function og_subgroups_tree_recurse($gid, $groups, $exclude, $inaccessibale, $tree, $indent) {
   $children = og_subgroups_get_family($gid, 'down'); 
   foreach ($children as $node) {
-    if ($node->gid != $exclude) {
+    if ($node->gid != $exclude && in_array($gid, $groups)) {
       !in_array($node->gid, $inaccessibale) ? $title = $node->title : $title = ('<private group>');
       $tree[$node->gid] = $indent .' '. $title;
-      $tree = og_subgroups_tree_recurse($node->gid, $exclude, $inaccessibale, $tree, $indent .'--');
+      $tree = og_subgroups_tree_recurse($node->gid, $groups, $exclude, $inaccessibale, $tree, $indent .'--');
     }
   }
   return $tree;
@@ -945,3 +1017,7 @@
     return $tokens;
   }
 }
+
+function og_subgroups_get_allowed_subgroup_types($group_type) {
+  return variable_get('og_subgroups_allowed_subgroups_' . $group_type, array());
+}
Index: E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.js
===================================================================
--- E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.js	(revision 0)
+++ E:/xampp/htdocs/itsite/sites/all/modules/og_subgroups/og_subgroups.js	(revision 6)
@@ -0,0 +1,16 @@
+// Content type form
+Drupal.behaviors.og_subgroups_content_type = function() {
+  // Disable the subgroup types selection if the content type usage is not group_node
+  $('input[name="og_content_type_usage"]').click(function(){
+    if (!$('#edit-og-content-type-usage-group').attr('checked')) {
+      $('#og-group-wrapper input[type=checkbox]').attr('disabled','disabled');
+    } else {
+      $('#og-group-wrapper input[type=checkbox]').removeAttr('disabled');
+    }
+  });
+  
+  // Initial check to see if content type is standard group post
+  if (!$('#edit-og-content-type-usage-group-post-standard').attr('checked')) {
+    $('#edit-og-max-groups').attr('disabled','disabled');
+  };
+}
