Index: fc/relatedcontent/relatedcontent-group.tpl.php
===================================================================
--- fc/relatedcontent/relatedcontent-group.tpl.php	(revision 0)
+++ fc/relatedcontent/relatedcontent-group.tpl.php	(working copy)
@@ -0,0 +1,25 @@
+<?php
+//$Id: relatedcontent-group.tpl.php,v 1.2.2.1.2.1 2009/06/07 13:28:46 peterboeren Exp $
+
+/**
+ * @file relatedcontent-group.tpl.php
+ * Default theme implementation for relatedcontent groups.
+ *
+ * Available variables:
+ * - $group: The group's id.
+ * - $group_title: The group's title.
+ * - $content: The group's content.
+ * - $node_type: The content type of the node whose related content is themed.
+ *
+ * @see template_preprocess_relatedcontent_group()
+ */
+?>
+<div class="relatedcontent-nodes <?php print "$group relatedcontent-$group relatedcontent-$node_type-$group"; ?>">
+
+  <?php if ($group_title) : ?>
+		<h3><?php print $group_title; ?></h3>
+	<?php endif; ?>
+
+  <?php print $content; ?>
+
+</div>
Index: fc/relatedcontent/relatedcontent.module
===================================================================
--- fc/relatedcontent/relatedcontent.module	(revision 1013)
+++ fc/relatedcontent/relatedcontent.module	(working copy)
@@ -54,7 +54,7 @@
 function relatedcontent_get_nodes(&$node) {
   $nodes = $node->nodes; // Keeps $node immutable.
   if (is_array($nodes)) {
-    foreach ($nodes as $viewkey => $node_values) {
+    foreach ($nodes as $rcvkey => $node_values) {
       foreach ($node_values as $nid => $ordinal) {
         $node_array[] = $nid;
       }
@@ -74,13 +74,13 @@
  *   is an array of id numbers (nid) of nodes that are related to $node. The
  *   array should be sorted as described in the documentation of the module.
  */
-function relatedcontent_set_nodes(&$node, $vid, $nodes) {
+function relatedcontent_set_nodes(&$node, $rcvid, $nodes) {
   $ordinal = -count($nodes);
   foreach ($nodes as $nid) {
     $node_nodes[$nid] = $ordinal++;
   }
   $node->nodes = $node_nodes;
-  _relatedcontent_db_insert($node->nid, $vid, $node->nodes);
+  _relatedcontent_db_insert($node->nid, $rcvid, $node->nodes);
 }
 
 /**
@@ -159,7 +159,8 @@
   }
 
   // Get the job done.
-  return _relatedcontent($node, $output_grouped, $content_function, $content_function_args);
+	$relatedcontent = _relatedcontent($node, $output_grouped, $content_function, $content_function_args);
+  return $relatedcontent['output'];
 
 }
 
@@ -192,6 +193,12 @@
   'weight' => -10,
   );
 
+	$items['relatedcontent-add-view-js'] = array(
+    'page callback'    => 'relatedcontent_add_view_js',
+	  'access arguments' => array('access related content'),
+    'type' => MENU_CALLBACK,
+  );
+
   // local task from the views
   _relatedcontent_menu_relatedcontent($items);
 
@@ -212,7 +219,7 @@
  */
 function relatedcontent_form_alter(&$form, $form_state, $form_id) {
   if ($form_id == 'node_type_form' && isset($form['identity']['#type'])) {
-    _relatedcontent_form_alter_node_type($form);
+    _relatedcontent_form_alter_node_type($form, $form_state);
   }
 }
 
@@ -245,11 +252,20 @@
  * Implementation of hook_theme().
  */
 function relatedcontent_theme($existing, $type, $theme, $path) {
+  require_once './'.drupal_get_path('module', 'relatedcontent').'/relatedcontent.theme.inc';
   return array(
   'relatedcontent' => array(
-  'arguments' => array('output' => NULL, 'grouped' => NULL, 'node_type' => NULL, 'teaser' => NULL, 'page' => NULL),
-  'file' => 'relatedcontent.theme.inc'
+		'arguments' => array('output' => NULL, 'grouped' => NULL, 'nodes' => NULL, 'node_type' => NULL, 'teaser' => NULL, 'page' => NULL),
+		'file' => 'relatedcontent.theme.inc',
+		'template' => 'relatedcontent',
+		'pattern' => 'relatedcontent_'
   ),
+  'relatedcontent_group' => array(
+		'arguments' => array('contents' => NULL, 'group' => NULL, 'group_title' => NULL, 'nodes' => NULL),
+		'file' => 'relatedcontent.theme.inc',
+		'template' => 'relatedcontent-group',
+		'pattern' => 'relatedcontent_.*_?group_'
+  ),
   'relatedcontent_form_alter_node_type_views' => array(
   'arguments' => array('form' => NULL),
   'file' => 'relatedcontent.theme.inc'
@@ -294,13 +310,13 @@
   // Add the secondary tab listing all nodes provided by the view.
   if ($views = _relatedcontent_db_enabled_views()) {
     foreach ($views as $view) {
-      $items['node/%_relatedcontent_localtask/relatedcontent/'. $view->vid] = array(
+      $items['node/%_relatedcontent_localtask/relatedcontent/'. $view->rcvid] = array(
       'type' => MENU_LOCAL_TASK,
-      'title' => $view->title ? $view->title : relatedcontent_view_title($view->vid, $type),
-      'load arguments' => array($view->vid),
+      'title' => $view->title ? $view->title : relatedcontent_view_title($view->rcvid, $view->type),
+      'load arguments' => array($view->rcvid),
       'description' => $view->description,
       'page callback' => 'drupal_get_form',
-      'page arguments' => array('_relatedcontent_form_view', 1, $view->vid),
+      'page arguments' => array('_relatedcontent_form_view', 1, $view->rcvid, $view->vid, $view->display),
       'access arguments' => array('access related content'),
       );
     }
@@ -314,7 +330,7 @@
  *
  * @param unknown_type $nid
  */
-function _relatedcontent_localtask_load($nid, $vid = NULL) {
+function _relatedcontent_localtask_load($nid, $rcvid = NULL) {
 
   if (is_numeric($nid)) {
     $type = db_result(_relatedcontent_db_query('_relatedcontent_db_content_type', $nid));
@@ -323,17 +339,17 @@
       return FALSE;
     }
 
-    $views_per_type_query = _relatedcontent_db_query('_relatedcontent_db_get_enabled_views_for_type', $type, 1);
+    $views_per_type_query = _relatedcontent_db_query('_relatedcontent_db_get_views_for_type', $type);
 
-    if (isset($vid)) {
+    if (isset($rcvid)) {
       while ($view_per_type = db_fetch_object($views_per_type_query)) {
-        if ($vid == $view_per_type->vid) {
-          return $nid;
+        if ($rcvid == $view_per_type->rcvid) {
+          return node_load($nid);
         }
       }
     }
     else {
-      return $nid;
+      return node_load($nid);
     }
   }
   return FALSE;
@@ -347,6 +363,7 @@
  * A node is being viewed.
  */
 function _relatedcontent_node_view(&$node, $teaser, $page) {
+  require_once './'.drupal_get_path('module', 'relatedcontent').'/relatedcontent.theme.inc';
   // Get settings.
   $output_placing = relatedcontent_variable_output_placing($node->type);
   $output_teasers = $teaser || relatedcontent_variable_output_teasers($node->type);
@@ -356,11 +373,11 @@
   if (!$output_placing || $teaser && relatedcontent_variable_exclude_teasers($node->type)) return;
 
   // Get the related content. Abort if the node lacks related content.
-  if (!$node->nodes || !($output = _relatedcontent($node, $output_grouped, 'node_view', array($output_teasers)))) return;
+  if (!$node->nodes || !($rc = _relatedcontent($node, $output_grouped, 'node_view', array($output_teasers)))) return;
 
   // Theme the related content.
-  $output = theme('relatedcontent', $output, $output_grouped, $node->type, $teaser, $page);
-  $node->content['relatedcontent']['#value'] = $output;
+  $output = theme(array('relatedcontent_'.$node->type, 'relatedcontent'), $rc['output'], $output_grouped, $rc['nodes'], $node->type, $teaser, $page);
+  $node->content['relatedcontent']['#value'] = $node->relatedcontent_rendered = $output;
   
   // Add the themed output to the node's body.
   switch ($output_placing) {
@@ -424,18 +441,18 @@
 /**
  * Add RelatedContent settings to the node type forms.
  */
-function _relatedcontent_form_alter_node_type(&$form) {
+function _relatedcontent_form_alter_node_type(&$form, $form_state) {
 
   // Get the node type
   $type = $form['#node_type']->type;
   // Build the form.
   $form['relatedcontent'] = _relatedcontent_form_alter_node_type_relatedcontent($type);
   $form['relatedcontent']['settings'] = _relatedcontent_form_alter_node_type_settings($type);
-  $form['relatedcontent']['views'] = _relatedcontent_form_alter_node_type_views($type);
+  $form['relatedcontent']['views'] = _relatedcontent_form_alter_node_type_views($type, $form_state);
 
   // Let our own submit function handle the form before it is passed on to the
-  // default submit function node_type_form_submit().
-  $form['#submit'][]='_relatedcontent_form_alter_node_type_submit';
+  // default submit function node_type_form_submit()
+  array_unshift($form['#submit'], '_relatedcontent_form_alter_node_type_submit');
 }
 
 /**
@@ -542,64 +559,142 @@
  * Build the views selection fieldset of the RelatedContent configuration
  * fieldset.
  */
-function _relatedcontent_form_alter_node_type_views($type) {
-
+function _relatedcontent_form_alter_node_type_views($type, $form_state) {
   // The fieldset.
   $form = array(
-  '#type' => 'fieldset',
-  '#title' => t('Views'),
-  '#description' => t(
-  "The table below lists all available views from which related content can be selected, as well as any deleted view for which nodes has been selected. The <em>View</em> and <em>Description</em> columns contains the names and descriptions, respectively, of the views, as seen in the !views page. When the related content is displayed grouped by view, the values of the <em>Display name</em> column is used as headings of the groups, or, if left blank, the views' names. The related content is displayed view by view. The order of the views is determined from their values in the <em>Weight</em> column. Tick the views to be used by nodes of this content type.",
-  array('!views' => l(t('Administer views'), 'admin/build/views'))
-  ),
-  '#collapsible' => TRUE,
-  '#collapsed' => FALSE,
+		'#type' => 'fieldset',
+		'#title' => t('Views'),
+		'#description' => t('This lists all available views from which related content can be selected, as well as any deleted view for which nodes has been selected. In the table below, the <em>View</em> and <em>Description</em> columns contains the names and descriptions, respectively, of the views, as seen in the !views page. When the related content is displayed grouped by view, the values of the <em>Display name</em> column is used as headings of the groups, or, if left blank, the views\' names. The related content is displayed view by view. The order of the views is determined from positions in the table. Tick the views to be used by nodes of this content type.', array('!views' => l(t('Administer views'), 'admin/build/views'))  ),
+		'#collapsible' => TRUE,
+		'#collapsed' => FALSE,
+		'hs' => array(
+			'#type' => 'hierarchical_select',
+			'#title' => t('Select the views you wish to use'),
+			'#size' => 1,
+			'#config' => array(
+				'module' => 'relatedcontent',
+				'enforce_deepest' => 1
+			)
+		),
+		'add' => array(
+			'#type' => 'submit',
+			'#value' => t('Add'),
+			'#ahah' => array(
+				'path' => 'relatedcontent-add-view-js',
+				'wrapper' => 'relatedcontent_views_table',
+				'method' => 'replace',
+			)
+		),
+		'views_list' => array(
+			'#prefix' => '<div id="relatedcontent_views_table">',
+			'#suffix' => '</div>'
+		)
   );
 
-  // Build a table with a view per row. Each view has a checkbox for enabling
-  // and disabling it, its name an description and a weight selection box. The
-  // actual table is built by a theming function.
-  if ($views = _relatedcontent_db_get_views_for_type($type)) {
-    foreach ($views as $view) {
-      $view_name = $view->name ? l($view->name, "admin/build/views/edit/$view->name") : $view->name;
-      $form['enabled'][$view->vid] = array(
-      '#type' => 'checkbox',
-      '#return_value' => TRUE,
-      '#default_value' => $view->enabled,
-      );
-      $form['name'][$view->vid] = array('#value' => $view_name);
-      $form['description'][$view->vid] = array('#value' => $view->object);
-      $form['title'][$view->vid] = array('#type' => 'textfield', '#default_value' => $view->title, '#size' => 32);
-    }
-  }
+  // Build a table with a view per row. Each view has a checkbox for disabling it, its name, description and weight.
+	// The actual table is built by a theming function.
+	if ($views = _relatedcontent_db_get_views_for_type($type, $form_state['storage']['relatedcontent']['views'])) {
+    foreach ($views as $key => $view) {
+			$views_state = $form_state['values']['views']['views_list'][$key];
+			$form['views_list'][$key] = array(
+				'name' => array('#type' => 'item', '#value' => l($view->vid, 'admin/build/views/edit/'.$view->vid, array('fragment' => $view->display)). ' ('.$view->display_title.')'),
+				'description' => array('#type' => 'item', '#value' => $view->description),
+				'title' => array('#type' => 'textfield', '#default_value' => isset($views_state['title']) ? $views_state['title'] : $view->title, '#size' => 32),
+				'weight' => array('#type' => 'weight', '#default_value' => isset($views_state['weight']) ? isset($views_state['weight']) : $view->weight),
+				'remove' => array('#type' => 'checkbox', '#default_value' => $views_state['remove']),
+				'rcvid' => array('#type' => 'hidden', '#default_value' => $view->rcvid),
+				'vid' => array('#type' => 'hidden', '#default_value' => $view->vid),
+				'display' => array('#type' => 'hidden', '#default_value' => $view->display)
+			);
+		}
+	}
 
-  // Add some directives.
+	// Add some directives.
   $form['#tree'] = TRUE;
-  $form['#theme'] = 'relatedcontent_form_alter_node_type_views';
+  $form['views_list']['#theme'] = 'relatedcontent_form_alter_node_type_views';
 
-  return $form;
+	return $form;
 
 }
 
+function relatedcontent_add_view_js(){
+  module_load_include('inc', 'node', 'content_types');
+
+	//Retrieve the cached form.
+  $form_state = array('storage' => NULL, 'rebuild' => TRUE);
+  $form_build_id = $_POST['form_build_id'];
+  $form = form_get_cache($form_build_id, $form_state);
+
+	$redirect = $form['#redirect'];
+  if (!$form) {
+    drupal_json(array('data' => ''));
+    exit;
+  }
+
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+	drupal_process_form($form_id, $form, $form_state);
+
+	if($form_state['values']['views']['hs']){
+		$view = views_get_view($form['relatedcontent']['views']['hs']['#value']['hierarchical_select']['selects'][0]);
+		$row = new stdClass();
+		$row->type = $form_state['values']['type'];
+		$row->vid = $view->name;
+		$row->display = $form['relatedcontent']['views']['hs']['hierarchy']['#value']->lineage[1];
+		$row->display_title = $view->display[$row->display]->display_title;
+		$row->title = $view->name;
+		$row->description = $view->description;
+		$row->weight = 0;
+
+		if(!$form_state['storage']['relatedcontent']['views'])
+			$form_state['storage']['relatedcontent']['views'] = array();
+
+		if(!in_array($row, $form_state['storage']['relatedcontent']['views']))
+			$form_state['storage']['relatedcontent']['views'][] = $row;
+	}
+
+	$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+
+	$form_element = $form['relatedcontent']['views']['views_list'];
+
+	//Prevent element nesting
+	unset($form_element['#prefix']);
+	unset($form_element['#suffix']);
+
+	$error = theme('status_messages', 'error');
+
+	$output = $error.theme('status_messages').drupal_render($form_element);
+
+	$javascript = drupal_add_js(NULL, NULL, 'header');
+
+  drupal_json(array(
+  	'status' => empty($error),
+  	'data' => $output,
+  	'settings' => call_user_func_array('array_merge_recursive', $javascript['setting']),
+  ));
+  exit;
+}
+
 /**
  * Handles the submission of the views selection in the node type form.
  */
 function _relatedcontent_form_alter_node_type_submit($form, &$form_state) {
   // Abort if there is no views to save.
   if (!$form_state['values']['views']) return;
+	unset($form_state['storage']['relatedcontent']);
 
-  // Save the information about the views.
+	// Save the information about the views.
   $type = $form_state['values']['type'];
-  foreach ($form_state['values']['views']['enabled'] as $vid => $enabled) {
-    $weight = 0;
-    $title = $form_state['values']['views']['title'][$vid];
-    $title ? $title : NULL;
-    _relatedcontent_db_set_view($type, $vid, $title, $weight, $enabled);
+  foreach ($form_state['values']['views']['views_list'] as $view) {
+		if($view['remove'] && !$view['rcvid'])
+			continue;
+		else if($view['remove'])
+			_relatedcontent_db_delete_view($view['rcvid']);
+		else
+	    _relatedcontent_db_set_view($type, $view['vid'], $view['display'], $view['title'], $view['weight'], $view['rcvid']);
   }
-
-  // this needs to be done because local tasks can have an other name
-  // menu caches needs to be rebuild.
-  menu_rebuild();
 }
 
 
@@ -610,11 +705,8 @@
 /**
  * Builds the related content list form.
  */
-function _relatedcontent_form($form, $nid) {
+function _relatedcontent_form($form, $node) {
 
-  //mc this nodeload should not be needed
-  $node = node_load($nid);
-
   $form = array();
 
   // Set the page title.
@@ -648,18 +740,18 @@
 function _relatedcontent_form_table(&$form, $selected_nodes) {
 
   if (count($selected_nodes)) {
-    foreach ($selected_nodes as $vid => $nodes) {
+    foreach ($selected_nodes as $rcvid => $nodes) {
       foreach ($nodes as $nid => $ordinal) {
         $node = node_load($nid);
-        $form['nodes'][$vid][$node->nid] = array(
+        $form['nodes'][$rcvid][$node->nid] = array(
         '#type' => 'checkbox',
         '#return_value' => $ordinal,
         '#default_value' => TRUE,
         );
-        $form['title'][$vid][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
-        $form['name'][$vid][$node->nid] =  array('#value' => node_get_types('name', $node));
-        $form['created'][$vid][$node->nid] = array('#value' => format_date($node->created, 'small'));
-        $form['username'][$vid][$node->nid] = array('#value' => theme('username', $node));
+        $form['title'][$rcvid][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
+        $form['name'][$rcvid][$node->nid] =  array('#value' => node_get_types('name', $node));
+        $form['created'][$rcvid][$node->nid] = array('#value' => format_date($node->created, 'small'));
+        $form['username'][$rcvid][$node->nid] = array('#value' => theme('username', $node));
       }
     }
   }
@@ -694,10 +786,10 @@
   // Get the nid and vid.
   $nid = arg(1);
   // We are saving. Store the selected nodes in the node.
-  foreach ($form_state['values']['nodes'] as $vid => $nodes) {
+  foreach ($form_state['values']['nodes'] as $rcvid => $nodes) {
     $nodes = array_filter($nodes);
-    _relatedcontent_db_delete($nid, $vid);
-    _relatedcontent_db_insert($nid, $vid, $nodes);
+    _relatedcontent_db_delete($nid, $rcvid);
+    _relatedcontent_db_insert($nid, $rcvid, $nodes);
   }
 }
 
@@ -709,13 +801,11 @@
 /**
  * Builds the related content view form.
  */
-function _relatedcontent_form_view($form_values = NULL, &$nid, $view_id) {
+function _relatedcontent_form_view($form_values = NULL, &$node, $rcvid, $view_id, $display_id = 'default') {
   // Two global variables needed by the pager.
   // Taken from pager_query() in pager.inc
   global $pager_page_array, $pager_total, $pager_items_per_page;
 
-  $node = node_load($nid);
-
   $form = array();
   // Get the view that will provide the node list. Abort if the the view has
   // been removed and the content type hasn't been updated accordingly.
@@ -726,7 +816,7 @@
 
   // unset the pager else the result will limit to number of items on a page of the view. We want all te results.
   unset($view->pager);
-  $view->execute();
+  $view->execute($display_id);
   $db_result = $view->result;
 
   // if the first result doesn't have a nid as part of the result
@@ -748,13 +838,13 @@
   $pager_total[0] = $pages;
   $pager_page_array[0] = max(0, min((int)$pager_page_array[0], ( (int)$pager_total[0]) - 1));
 
-  _relatedcontent_track_initialize($node->nid, $node->nodes[$view_id]);
+  _relatedcontent_track_initialize($node->nid, $node->nodes[$rcvid]);
 
   // Build the form.
   _relatedcontent_form_view_introduction($form, $node->type);
-  _relatedcontent_form_view_table($form, $db_result, $node, $view_id);
+  _relatedcontent_form_view_table($form, $db_result, $node, $rcvid);
   _relatedcontent_form_view_buttons($form);
-  _relatedcontent_form_view_hidden_values($form, $view_id);
+  _relatedcontent_form_view_hidden_values($form, $rcvid, $view_id, $display_id);
   _relatedcontent_form_view_directives($form);
 
   return $form;
@@ -776,7 +866,7 @@
   array(
   '!views-url' => url('admin/build/views'),
   '!settings-url' => url("admin/content/types/$type"),
-  '@view-name' => $view_name,
+  '@view-name' => $view,
   '@content-type' => $type,
   )
   );
@@ -830,23 +920,23 @@
 /**
  * Adds the table of nodes with related content to the form.
  */
-function _relatedcontent_form_view_table(&$form, $db_result, $node, $vid) {
+function _relatedcontent_form_view_table(&$form, $db_result, $node, $rcvid) {
 
   $ordinal = 0;
-  $selected_nodes = $node->nodes[$vid];
+  $selected_nodes = $node->nodes[$rcvid];
 
   foreach ($db_result as $key => $node_result) {
     $node = node_load($node_result->nid);
 
-    $form['nodes'][$vid][$node->nid] = array(
+    $form['nodes'][$rcvid][$node->nid] = array(
     '#type' => 'checkbox',
     '#return_value' => ++$ordinal,
     '#attributes' => $selected_nodes[$node->nid] ? array('checked' => 'checked') : NULL, // See http://drupal.org/node/187413.
     );
-    $form['title'][$vid][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
-    $form['name'][$vid][$node->nid] =  array('#value' => node_get_types('name', $node));
-    $form['created'][$vid][$node->nid] = array('#value' => format_date($node->created, 'small'));
-    $form['username'][$vid][$node->nid] = array('#value' => theme('username', $node));
+    $form['title'][$rcvid][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
+    $form['name'][$rcvid][$node->nid] =  array('#value' => node_get_types('name', $node));
+    $form['created'][$rcvid][$node->nid] = array('#value' => format_date($node->created, 'small'));
+    $form['username'][$rcvid][$node->nid] = array('#value' => theme('username', $node));
   }
 }
 
@@ -868,13 +958,21 @@
 /**
  * Adds hidden fields with information needed later.
  */
-function _relatedcontent_form_view_hidden_values(&$form, $view_id) {
+function _relatedcontent_form_view_hidden_values(&$form, $rc_view_id, $view_id, $display_id) {
 
   // Store the view's id.
   $form['view_id'] = array(
   '#type' => 'hidden',
   '#value' => $view_id,
   );
+  $form['display_id'] = array(
+  '#type' => 'hidden',
+  '#value' => $display_id,
+  );
+  $form['rc_view_id'] = array(
+  '#type' => 'hidden',
+  '#value' => $rc_view_id,
+  );
 
 }
 
@@ -894,18 +992,18 @@
 
   // Get the nid and vid.
   $nid = arg(1);
-  $vid = $form_state['values']['view_id'];
+  $rcvid = $form_state['values']['rc_view_id'];
   // Track changes done on the current form page.
   $offset = $form_state['values']['page_number'] * $form_state['values']['page_length'];
-  _relatedcontent_track_update_selected_nodes($nid, $form_state['values']['nodes'][$vid], $offset, $form_state['values']['page_number']);
+  _relatedcontent_track_update_selected_nodes($nid, $form_state['values']['nodes'][$rcvid], $offset, $form_state['values']['page_number']);
 
   // If we are not saving, return to the form again.
   if ($form_state['clicked_button']['#value'] != t('Update')) return FALSE;
 
   // We are saving. Finalize the tracking; and store the tracked changes.
-  $nodes = _relatedcontent_track_finalize($nid, $vid, $form_state['values']['page_length'], $form_state['values']['max_page_number']);
-  _relatedcontent_db_delete($nid, $vid);
-  _relatedcontent_db_insert($nid, $vid, $nodes);
+  $nodes = _relatedcontent_track_finalize($nid, $form_state['values']['view_id'], $form_state['values']['display_id'], $form_state['values']['page_length'], $form_state['values']['max_page_number']);
+  _relatedcontent_db_delete($nid, $rcvid);
+  _relatedcontent_db_insert($nid, $rcvid, $nodes);
 
   drupal_goto("node/$nid/relatedcontent");
 
@@ -1001,7 +1099,7 @@
  * if there are orphan nodes (see the on-line help), we will visit all pages
  * in vain.
  */
-function _relatedcontent_track_finalize($nid, $view_id, $page_length, $max_page_number) {
+function _relatedcontent_track_finalize($nid, $view_id, $display_id, $page_length, $max_page_number) {
 
   // Get the tracked information.
   $nodes = $_SESSION['relatedcontent'][$nid]['nodes'];
@@ -1017,9 +1115,12 @@
   // Visit all pages that has not been displayed, and update the ordinal
   // numbers of those remaining nodes which we find on these pages.
   $view = views_get_view($view_id); // Get the view.
+	unset($view->pager);
+	$view->execute($display_id);
+	$db_result = $view->result;
   $ordinal = ($page + 1) * $page_length; // The last used ordinal number.
   while (++$page <= $max_page_number) { // For each page not yet visited...
-    $info = views_build_view('result', $view, array(), FALSE, $page_length, $page); // Get the nodes on the current page.
+    $info = array_slice($db_result, ($page*$page_length), $page_length); // Get the nodes on the current page.
     while ($node = db_fetch_object($info['result'])) { // For each node on the current page...
       ++$ordinal; // Next ordinal number.
       if ($remaining_nodes[$node->nid]) { // If current node is among the remaining ones...
@@ -1048,17 +1149,18 @@
  */
 function _relatedcontent(&$node, $output_grouped, $content_function, $args) {
   array_unshift($args, 0);
-  foreach ($node->nodes as $vid => $selected_nodes) {
+  foreach ($node->nodes as $rcvid => $selected_nodes) {
     foreach ($selected_nodes as $nid => $ordinal) {
       if ($nid && ($n = node_load($nid)) && node_access ('view', $n)) {
         $n->relatedcontent = $node->nid;
         $args[0] = $n;
-        $key = _relatedcontent_group_key($n, $output_grouped, $vid);
+        $key = _relatedcontent_group_key($n, $output_grouped, $rcvid);
         $output[$key][] = call_user_func_array($content_function, $args);
+				$nodes[$key][] = $n;
       }
     }
   }
-  return $output;
+  return array('output' => $output, 'nodes' => $nodes);
 }
 
 /**
@@ -1066,14 +1168,15 @@
  * are grouped by views, $vid must be the id of the view from which the node
  * was picked.
  */
-function _relatedcontent_group_key($node, $grouped, $vid = NULL) {
+function _relatedcontent_group_key($node, $grouped, $rcvid = NULL) {
   switch ($grouped) {
     case 'type':
       return $node->type;
     case 'author':
       return $node->name;
     case 'view':
-      return "view-$vid"; // See relatedcontent_group_title().
+			$rcv = _relatedcontent_db_get_view_by_rcvid($rcvid);
+      return $rcv->vid.'-'.$rcv->display.'-'.$rcvid; // See relatedcontent_group_title().
     default:
       return 'all';
   }
@@ -1099,8 +1202,8 @@
       return $group;
     case 'view':
       $group = explode('-', $group);
-      $vid = $group[1]; // See _relatedcontent_group_key().
-      return relatedcontent_view_title($vid, $type);
+      $rcvid = $group[2]; // See _relatedcontent_group_key().
+      return relatedcontent_view_title($rcvid, $type);
     default:
       return '';
   }
@@ -1117,7 +1220,7 @@
  * @return
  *   The heading of the view $vid.
  */
-function relatedcontent_view_title($vid, $type) {
+function relatedcontent_view_title($rcvid, $type) {
 
   // On the first call, load information objects about all known views.
   static $views = array();
@@ -1126,15 +1229,18 @@
   }
 
   // Get the view's title.
-  $view = $views[$type][$vid];
-  if ($view->title) {
-    $title = $view->title;
-  }
-  else {
-    $title = $view->name;
-  }
+	foreach($views[$type] as $view){
+		if($view->rcvid == $rcvid){
+			if ($view->title) {
+				$title = $view->title;
+			}
+			else {
+				$title = $view->vid;
+			}
 
-  return $title;
+			return $title;
+		}
+	}
 
 }
 
@@ -1256,67 +1362,71 @@
 //mc function _relatedcontent_db_enabled_views($type) {
 function _relatedcontent_db_enabled_views() {
   //mc if ($db_result = _relatedcontent_db_query(__FUNCTION__, $type, 1)) {
-  if ($db_result = _relatedcontent_db_query(__FUNCTION__, 1)) {
+  if ($db_result = _relatedcontent_db_query(__FUNCTION__)) {
     while ($view = db_fetch_object($db_result)) {
-      $views[$view->vid] = $view;
+      $views[$view->rcvid] = $view;
     }
     return $views;
   }
 }
 
 /**
- * Returns an array of view information objects for all existing views, as well
- * as views that are deleted, but for which there are nodes of the content
- * type $type which have related content picked from the view. The object has
- * following attributes:
+ * Returns an array of view information objects for all views related to the
+ * content type $type. The object has following attributes:
  *
- *   - vid:         the id of the view,
- *   - name:        the name of the view, if existing,
+ *   - rcvid:       the id of the content type's view,
+ *   - vid:         the name of the view,
  *   - description: a description of the view, if existing,
  *   - title:       this module's display name of the view,
- *   - weight:      the weight of the view for the content type $type, and
- *   - enabled:     whether the view is enabled for the content type $type.
+ *   - weight:      the weight of the view for the content type $type,
+ *   - display:			the display of the view
  *
  */
-function _relatedcontent_db_get_views_for_type($type) {
+function _relatedcontent_db_get_views_for_type($type, $additionnal = array()) {
+	static $enabled_views = array();
 
-  $enabled_views = array();
-  $all_views = views_get_all_views();
-  if ($db_result = _relatedcontent_db_query(__FUNCTION__, $type, $type)) {
-    while ($views = db_fetch_object($db_result)) {
-      $relatedviews[$views->vid] = $views;
-    }
-  }
+	if(!$enabled_views[$type]){
+		$enabled_views[$type] = array();
+		if ($db_result = _relatedcontent_db_query(__FUNCTION__, $type)) {
+			while ($view = db_fetch_object($db_result)) {
+				$view_object = views_get_view($view->vid);
+				$view->description = $view_object->description;
+				$view->display_title = $view_object->display[$view->display]->display_title;
+				if(empty($view->title)) $view->title = $view->vid;
+				$enabled_views[$type][] = $view;
+			}
+		}
+	}
+	if(!empty($additionnal)){
+		$enabled_views[$type] = array_merge($enabled_views[$type], $additionnal);
+	}
+	return $enabled_views[$type];
+}
 
+/**
+ * Update the title and weight attributes of the view with the id $rcvid if present
+ * or insert a new view if not
+ */
+function _relatedcontent_db_get_view_by_rcvid($rcvid) {
+  return db_fetch_object(_relatedcontent_db_query(__FUNCTION__, $rcvid));
+}
 
-  foreach ($all_views as $view_key => $view_object) {
-    // check if the view is enabled
-    if (empty($view_object->disabled) && ($view_object->base_table == 'node')) {
-      $view = new stdClass();
-      $view->name = $view_object->name;
-      $view->object = $view_object->description;
-      $view->vid = $view_object->vid ? $view_object->vid : $view_key;
-      if (isset($relatedviews[$view->vid])) {
-        $view->type = $relatedviews[$view->vid]->type;
-        $view->title = $relatedviews[$view->vid]->title;
-        $view->description = $relatedviews[$view->vid]->description;
-        $view->enabled = $relatedviews[$view->vid]->enabled;
-      }
-      $enabled_views[$view->vid] = $view;
-    }
-  }
-  return $enabled_views;
+/**
+ * Update the title and weight attributes of the view with the id $rcvid if present
+ * or insert a new view if not
+ */
+function _relatedcontent_db_set_view($type, $vid, $display, $title, $weight, $rcvid = NULL) {
+	if($rcvid)
+	  _relatedcontent_db_query(__FUNCTION__ .'_update', $title, $weight, $rcvid);
+  else
+    _relatedcontent_db_query(__FUNCTION__ .'_insert', $type, $vid, $display, $title, $weight);
 }
 
 /**
- * Insert/update the enable and weight attributes of the view with the id $vid
- * for the content type $type.
+ * Delete the view with the id $rcvid.
  */
-function _relatedcontent_db_set_view($type, $vid, $title, $weight, $enabled) {
-  _relatedcontent_db_query(__FUNCTION__ .'_update', $enabled, $title, $weight, $vid, $type);
-  if (db_affected_rows() < 1) {
-    _relatedcontent_db_query(__FUNCTION__ .'_insert', $type, $vid, $title, $weight, $enabled);
-  }
+function _relatedcontent_db_delete_view($rcvid) {
+	_relatedcontent_db_query(__FUNCTION__, $rcvid);
 }
 
 /**
@@ -1336,7 +1446,7 @@
   if ($db_result = _relatedcontent_db_query(__FUNCTION__, $nid)) {
     while ($node = db_fetch_object($db_result)) {
       //$nodes[$node->vid][$node->include_nid] = $ordinal++;
-      $nodes[$node->vid][$node->include_nid] = $ordinal++;
+      $nodes[$node->rcvid][$node->include_nid] = $ordinal++;
     }
   }
   return $nodes;
@@ -1345,9 +1455,9 @@
 /**
  * Insert nid of included nodes into database.
  */
-function _relatedcontent_db_insert($nid, $vid, $nodes) {
+function _relatedcontent_db_insert($nid, $rcvid, $nodes) {
   foreach ($nodes as $include_nid => $ordinal) {
-    $values[] = array($nid, $vid, $include_nid, $ordinal);
+    $values[] = array($nid, $rcvid, $include_nid, $ordinal);
   }
   if ($values) {
     _relatedcontent_db_query(__FUNCTION__, $values);
@@ -1357,8 +1467,8 @@
 /**
  * Delete nid of included nodes in database.
  */
-function _relatedcontent_db_delete($nid, $vid = NULL) {
-  _relatedcontent_db_query(__FUNCTION__, $nid, $vid);
+function _relatedcontent_db_delete($nid, $rcvid = NULL) {
+  _relatedcontent_db_query(__FUNCTION__, $nid, $rcvid);
 }
 
 /**
@@ -1444,10 +1554,10 @@
     $nid = arg(1);
     $type_object = db_fetch_object(_relatedcontent_db_query('_relatedcontent_db_load_type', $nid));
     $nodes = _relatedcontent_db_load($nid);
-    foreach ($nodes as $vid => $nids) {
-      $vid = relatedcontent_view_title($vid, $type_object->type);
+    foreach ($nodes as $rcvid => $nids) {
+      $title = relatedcontent_view_title($rcvid, $type_object->type);
       $nids = _related_content_create_links($nids);
-      $output .= theme('item_list', $nids, $vid, 'ul');
+      $output .= theme('item_list', $nids, $title, 'ul');
     }
   }
   return $output;
@@ -1465,4 +1575,106 @@
     $links[$nid] = l($title_object->title, 'node/'. $nid);
   }
   return $links;
-}
\ No newline at end of file
+}
+
+/*****************************************************************************
+* IMPLEMENTATION OF HIERARCHICAL SELECT HOOKS
+****************************************************************************/
+
+/**
+ * Implementation of hook_hierarchical_select_params().
+ */
+function relatedcontent_hierarchical_select_params(){
+  return array();
+}
+
+/**
+ * Implementation of hook_hierarchical_select_root_level().
+ */
+function relatedcontent_hierarchical_select_root_level($params, $dropbox = FALSE){
+	$roots = array();
+  if ($views = _relatedcontent_get_available_views()) {
+    foreach ($views as $view) $roots[$view->name] = $view->name;
+	}
+	return $roots;
+}
+
+/**
+ * Implementation of hook_hierarchical_select_children().
+ */
+function relatedcontent_hierarchical_select_children($parent, $params, $dropbox = FALSE){
+	$children = array();
+
+	//Displays doesn't have children
+	if(count(explode('|', $parent)) == 1 && $views = _relatedcontent_get_available_views()){
+		foreach($views as $view){
+			if($parent == $view->name){
+				foreach($view->displays as $display_id => $display_name){
+					if($view->enabled && $view->display == $display_id) continue;
+					$children[$view->name.'|'.$display_id] = $display_name;
+				}
+				break;
+			}
+		}
+	}
+	return $children;
+}
+
+/**
+ * Implementation of hook_hierarchical_select_lineage().
+ */
+function relatedcontent_hierarchical_select_lineage($item, $params){
+  return explode('|', $item);
+}
+
+/**
+ * Implementation of hook_hierarchical_select_valid_item().
+ */
+function relatedcontent_hierarchical_select_valid_item($item, $params){
+	$views = _relatedcontent_get_available_views();
+	foreach ($views as $view) {
+		$item_parts = explode('|', $item);
+		if($item_parts[0] == $view->name){
+			if(!isset($item_parts[1]) || (isset($item_parts[1]) && in_array($item_parts[1], array_keys((array)$view->displays))))
+				return TRUE;
+			break;
+		}
+	}
+	return FALSE;
+}
+
+/**
+ * Implementation of hook_hierarchical_select_implementation_info().
+ */
+function relatedcontent_hierarchical_select_implementation_info(){
+  return array(
+    'hierarchy type' => t('Views'),
+    'entity type'    => t('Display'),
+  );
+}
+
+/**
+ * Returns an array of view information objects for all existing views
+ */
+function _relatedcontent_get_available_views($reset = FALSE) {
+	static $available_views = array();
+
+	if($reset || count($available_views) == 0){
+		$all_views = views_get_all_views();
+
+		foreach ($all_views as $view_key => $view_object) {
+			// check if the view is enabled
+			if (empty($view_object->disabled) && ($view_object->base_table == 'node')) {
+				$view = new stdClass();
+				$view->vid = $view_object->vid;
+				$view->name = $view_object->name;
+				$view->description = $view_object->description;
+				$view->type = $type;
+				$view->displays = array();
+				foreach($view_object->display as $display_id => $display)	$view->displays[$display_id] = $display->display_title;
+				$available_views[$view->name] = $view;
+			}
+		}
+	}
+  return $available_views;
+}
Index: fc/relatedcontent/relatedcontent.tpl.php
===================================================================
--- fc/relatedcontent/relatedcontent.tpl.php	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.tpl.php	(.../fc/relatedcontent)	(working copy)
@@ -1,7 +1,19 @@
-<?php //$Id: relatedcontent.tpl.php,v 1.2.2.1.2.1 2009/06/07 13:28:46 peterboeren Exp $ ?>
-<?php foreach ($output as $group => $contents) { ?>
-<div class="relatedcontent-nodes <?php echo $group; ?>">
-  <?php if ($grouped) { ?><h3><?php echo relatedcontent_group_title($grouped, $group, $node_type) ?></h3><?php } ?>
-  <?php foreach ($contents as $content) { echo $content; } ?>
-</div>
-<?php } ?>
+<?php
+//$Id: relatedcontent.tpl.php,v 1.2.2.1.2.1 2009/06/07 13:28:46 peterboeren Exp $
+
+/**
+ * @file relatedcontent.tpl.php
+ * Default theme implementation for relatedconten content.
+ *
+ * Available variables:
+ * - $content: The relatedcontent output.
+ * - $node_type: The content type of the node whose related content is themed.
+ * - $teaser: Flag for the teaser state.
+ * - $page: Flag for the full page state.
+ *
+ * @see template_preprocess_relatedcontent_group()
+ */
+?>
+<div class="relatedcontent-<?php print $node_type; ?>">
+	<?php print $content; ?>
+</div>
\ No newline at end of file
Index: fc/relatedcontent/relatedcontent.install
===================================================================
--- fc/relatedcontent/relatedcontent.install	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.install	(.../fc/relatedcontent)	(working copy)
@@ -46,12 +46,12 @@
         'type' => 'int',
         'unsigned' => TRUE,
         'not NULL' => TRUE),
-      'vid' => array(
-        'description' => t('The current view identifier.'),
-        'type' => 'varchar',
-        'length' => 32,
-        'not NULL' => TRUE,
-        'default' => ''),
+      'rcvid' => array(
+        'description' => t('The current relatedcontent view identifier.'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+				'default' => 0),
       'include_nid' => array(
         'description' => t('The nid of the node that is included.'),
         'type' => 'int',
@@ -65,12 +65,17 @@
         'not NULL' => TRUE,
         'default' => 0),
       ),
-    'primary key' => array('nid', 'vid', 'include_nid'),
+    'primary key' => array('nid', 'rcvid', 'include_nid'),
   );
 
   $schema['relatedcontent_view'] = array(
     'description' => t('The base table for nodes.'),
     'fields' => array(
+      'rcvid' => array(
+        'description' => t('The primary identifier for a relatedcontent view.'),
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE),
       'type' => array(
         'description' => t('The node type.'),
         'type' => 'varchar',
@@ -92,16 +97,15 @@
       'weight' => array(
         'description' => t('The weight of the view.'),
         'type' => 'int',
-        'unsigned' => TRUE,
         'not NULL' => TRUE),
-       'enabled' => array(
-        'description' => t('Flag to enable or disable.'),
-        'type' => 'int',
-        'size' => 'small',
-        'unsigned' => TRUE,
-        'not NULL' => TRUE),
-      ),
-    'primary key' => array('type', 'vid'),
+      'display' => array(
+        'description' => t('The display of the view.'),
+        'type' => 'varchar',
+				'length' => 64,
+        'not null' => TRUE,
+        'default' => 'default'),
+		),
+    'primary key' => array('rcvid'),
   );
 
   return $schema;
@@ -156,4 +160,79 @@
  */
 function _relatedcontent_install_module_name() {
   return drupal_substr(__FUNCTION__, 1, strpos(__FUNCTION__, '_', 1) - 1);
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation of hook_update_N()
+ */
+function relatedcontent_update_6000() {
+	$ret = array();
+  $display = array(
+		'description' => t('The display of the view.'),
+		'type' => 'varchar',
+		'length' => 64,
+		'not null' => TRUE,
+		'default' => 'default'
+  );
+
+	$rcvid = array(
+		'description' => t('The primary identifier for a relatedcontent view.'),
+		'type' => 'serial',
+		'unsigned' => TRUE,
+		'not null' => TRUE,
+  );
+
+	$weight = array(
+		'description' => t('The weight of the view.'),
+		'type' => 'int',
+		'not NULL' => TRUE
+	);
+
+	$ret[] = update_sql("DELETE FROM {relatedcontent_view} WHERE enabled = 0");
+  db_drop_field($ret, 'relatedcontent_view', 'enabled');
+	db_drop_primary_key($ret, 'relatedcontent_view');
+	db_add_field($ret, 'relatedcontent_view', 'rcvid', $rcvid, array('primary key' => array('rcvid')));
+	db_add_field($ret, 'relatedcontent_view', 'display', $display);
+	db_change_field($ret, 'relatedcontent_view', 'weight', 'weight', $weight);
+
+  $rcvid = array_merge($rcvid, array('type' => 'int', 'default' => 0, 'description' => t('The current relatedcontent view identifier.')));
+
+	db_drop_primary_key($ret, 'relatedcontent');
+	db_change_field($ret, 'relatedcontent', 'vid', 'rcvid', $rcvid, array('primary key' => array('nid', 'rcvid', 'include_nid')));
+	$ret[] = update_sql("UPDATE {relatedcontent} r SET rcvid = (SELECT rcvid FROM {relatedcontent_view} rv WHERE rv.vid = r.rcvid)");
+	$ret[] = update_sql("UPDATE {relatedcontent_view} r SET vid = (SELECT name FROM {views_view} v WHERE v.vid = r.vid)");
+
+	return $ret;
+}
+
+/**
+ * Implementation of hook_schema_alter()
+ */
+function relatedcontent_schema_alter(&$schema) {
+	unset($schema['relatedcontent_view']['fields']['enabled']);
+	unset($schema['relatedcontent_view']['fields']['weight']['unsigned']);
+	$schema['relatedcontent_view']['fields']['display'] = array(
+		'description' => t('The display of the view'),
+		'type' => 'varchar',
+		'length' => 64,
+		'not null' => TRUE,
+		'default' => 'default'
+  );
+	$schema['relatedcontent_view']['fields']['rcvid'] = array(
+		'description' => t('The primary identifier for a relatedcontent view.'),
+		'type' => 'serial',
+		'unsigned' => TRUE,
+		'not null' => TRUE,
+  );
+	$schema['relatedcontent_view']['primary key'] = array('rcvid');
+
+	$schema['relatedcontent']['fields']['rcvid'] = array(
+		'description' => t('The current relatedcontent view identifier.'),
+		'type' => 'int',
+		'unsigned' => TRUE,
+		'not null' => TRUE,
+		'default' => 0
+	);
+	unset($schema['relatedcontent']['fields']['vid']);
+  $schema['relatedcontent_view']['primary key'] = array('nid', 'rcvid', 'include_nid');
+}
Index: fc/relatedcontent/relatedcontent.css
===================================================================
--- fc/relatedcontent/relatedcontent.css	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.css	(.../fc/relatedcontent)	(working copy)
@@ -2,4 +2,8 @@
 
 .relatedcontent-nodes {
   clear: both;
+}
+
+#relatedcontent_views_table tr .form-item{
+	white-space: normal;
 }
\ No newline at end of file
Index: fc/relatedcontent/relatedcontent.theme.inc
===================================================================
--- fc/relatedcontent/relatedcontent.theme.inc	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.theme.inc	(.../fc/relatedcontent)	(working copy)
@@ -45,36 +45,61 @@
  * @return
  *   The default theming described in the help text.
  */
-function theme_relatedcontent($output, $grouped = NULL, $node_type = NULL, $teaser = NULL, $page = NULL) {
-  global $theme_engine;
+function template_preprocess_relatedcontent($vars){
   global $base_url;
 
-  // Add CSS-file.
-  $base = drupal_get_path('module', 'relatedcontent');
-  drupal_add_css("$base/relatedcontent.css");
+	// Add CSS-file.
+  drupal_add_css(drupal_get_path('module', 'relatedcontent').'/relatedcontent.css');
 
-  // If the current theme engine is PHPTemplate and the current theme has a
-  // RelatedContent template file, process the provided template file, and
-  // return the resulting output. Otherwise, return the default theming.
-  foreach ($output as $group => $contents) {
-    $out .= "<div class=\"relatedcontent-nodes $group\">";
-    if ($grouped) {
-      $out .= '<h3>'. relatedcontent_group_title($group, $grouped, $node_type) .'</h3>';
-    }
-    // replace the internal urls by absolute urls for usage in a newsletter.
-    if (_relatedcontent_variable("relatedcontent_links_absolute_$node_type")) {
-      $contents = str_replace(base_path(), $base_url.'/', $contents);
-    }
+  foreach ($vars['output'] as $group => $contents) {
+    if (_relatedcontent_variable('relatedcontent_links_absolute_'.$vars['node_type'])) $contents = preg_replace("/(href=\"|src=\")/", "$1$base_url", $contents);
 
-    $out .= implode('', $contents);
-    $out .= '</div>';
-  }
+    if ($vars['grouped'])	$group_title = relatedcontent_group_title($group, $vars['grouped'], $vars['node_type']);
 
-  return $out;
+		if($vars['grouped'] == 'view')
+			$group_parts = explode('-', $group);
+		$group_for_themes = strtr($group, '-', '_');
 
+		$themes = array();
+		$themes[] = 'relatedcontent_'.$vars['node_type'].'_group_'.$group_for_themes;
+		if($vars['grouped'] == 'view'){
+			$themes[] = 'relatedcontent_'.$vars['node_type'].'_group_'.$group_parts[0].'_'.$group_parts[1];
+			$themes[] = 'relatedcontent_'.$vars['node_type'].'_group_'.$group_parts[0];
+		}
+		$themes[] = 'relatedcontent_group_'.$group_for_themes;
+		if($vars['grouped'] == 'view'){
+			$themes[] = 'relatedcontent_group_'.$group_parts[0].'_'.$group_parts[1];
+			$themes[] = 'relatedcontent_group_'.$group_parts[0];
+		}
+		$themes[] = 'relatedcontent_'.$vars['node_type'].'_group';
+		$themes[] = 'relatedcontent_group';
+
+		$vars['output_rendered'][$group] = theme($themes, $contents, $group, $group_title, $vars['nodes'][$group]);
+		$vars['content'] .= $vars['output_rendered'][$group];
+  }
 }
 
 /**
+ * Themeable function for the related content.
+ *
+ * @ingroup themeable
+ * @param $contents
+ *   An array with already themed nodes which should be outputted in index
+ *   order as they are.
+ * @param $group
+ *   The id of the relatedcontent's group.
+ * @param $group_title
+ *   The id of the relatedcontent's group.
+ * @param $node_type
+ *   The content type of the node whose related content is themed.
+ * @return
+ *   The default theming described in the help text.
+ */
+function template_preprocess_relatedcontent_group($vars){
+	$vars['content'] = implode('', $vars['contents']);
+}
+
+/**
  * Themeable function for displaying the overview table.
  *
  * @ingroup themeable
@@ -99,24 +124,24 @@
   $header = array(theme('table_select_header_cell'), t('Title'), t('Type'), t('Author'), t('Created'));
 
   // Build the table.
-  $type = $form['#parameters'][1]->type;
+  $type = $form['#parameters'][2]->type;
   if (isset($form['nodes']) && is_array($form['nodes'])) {
-    foreach (element_children($form['nodes']) as $vid) {
+    foreach (element_children($form['nodes']) as $rcvid) {
 
       // Add a row with the title of the view.
       $rows[] = array(array(
-        'data' => '<strong>'. relatedcontent_view_title($vid, $type) .'</strong>',
+        'data' => '<strong>'. relatedcontent_view_title($rcvid, $type) .'</strong>',
         'colspan' => count($header),
       ));
 
       // Add rows of nodes with related content.
-      foreach (element_children($form['nodes'][$vid]) as $nid) {
+      foreach (element_children($form['nodes'][$rcvid]) as $nid) {
         $row = array();
-        $row[] = drupal_render($form['nodes'][$vid][$nid]);
-        $row[] = drupal_render($form['title'][$vid][$nid]);
-        $row[] = drupal_render($form['name'][$vid][$nid]);
-        $row[] = drupal_render($form['username'][$vid][$nid]);
-        $row[] = drupal_render($form['created'][$vid][$nid]);
+        $row[] = drupal_render($form['nodes'][$rcvid][$nid]);
+        $row[] = drupal_render($form['title'][$rcvid][$nid]);
+        $row[] = drupal_render($form['name'][$rcvid][$nid]);
+        $row[] = drupal_render($form['username'][$rcvid][$nid]);
+        $row[] = drupal_render($form['created'][$rcvid][$nid]);
         $rows[] = $row;
       }
 
@@ -157,17 +182,23 @@
  *   The themed table of views.
  */
 function theme_relatedcontent_form_alter_node_type_views($form) {
+
+	drupal_add_css(drupal_get_path('module', 'relatedcontent').'/relatedcontent.css');
+
   // Prepare the table for rendering.
-  $header = array(theme('table_select_header_cell'), t('View'), t('Description'), t('Display name (optional)'));
-  if (isset($form['enabled']) && is_array($form['enabled'])) {
-    foreach (element_children($form['enabled']) as $vid) {
-      $row = array();
-      $row[] = drupal_render($form['enabled'][$vid]);
-      $row[] = drupal_render($form['name'][$vid]);
-      $row[] = drupal_render($form['description'][$vid]);
-      $row[] = drupal_render($form['title'][$vid]);
-      $row[] = drupal_render($form['weight'][$vid]);
-      $rows[] = $row;
+  $header = array(t('Views'), t('Description'), t('Display name (optional)'), t('Remove'), t('Weight'));
+	$rows = array();
+
+	if (count(element_children($form)) > 0) {
+    foreach (element_children($form) as $key) {
+			$form[$key]['weight']['#attributes']['class'] = 'views-weight';
+			$row = array();
+			$row[] = drupal_render($form[$key]['name']);
+			$row[] = drupal_render($form[$key]['description']);
+			$row[] = drupal_render($form[$key]['title']);
+			$row[] = drupal_render($form[$key]['remove']);
+			$row[] = drupal_render($form[$key]['weight']);
+			$rows[] = array('data' => $row, 'class' => 'draggable');
     }
   }
   else  {
@@ -176,10 +207,7 @@
       'colspan' => count($header)
     ));
   }
+	drupal_add_tabledrag('relatedcontent_views', 'order', 'sibling', 'views-weight');
 
-  // Render the form.
-  $output .= theme('table', $header, $rows);
-  $output .= drupal_render($form);
-
-  return $output;
+	return theme('table', $header, $rows, array('id' => 'relatedcontent_views')).drupal_render($form);
 }
\ No newline at end of file
Index: fc/relatedcontent/relatedcontent.info
===================================================================
--- fc/relatedcontent/relatedcontent.info	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.info	(.../fc/relatedcontent)	(working copy)
@@ -3,13 +3,14 @@
 description = Allows privileged users to associate a node with related nodes that can be displayed along with the node.
 package = Related Content
 dependencies[] = views
+dependencies[] = hierarchical_select
 core = 6.x
 
 
-
-; Information added by drupal.org packaging script on 2009-12-09
-version = "6.x-1.0"
-core = "6.x"
-project = "relatedcontent"
-datestamp = "1260364262"
-
+
+; Information added by drupal.org packaging script on 2009-12-09
+version = "6.x-1.0"
+core = "6.x"
+project = "relatedcontent"
+datestamp = "1260364262"
+
Index: fc/relatedcontent/relatedcontent.mysql.inc
===================================================================
--- fc/relatedcontent/relatedcontent.mysql.inc	(.../contrib/relatedcontent)	(revision 1013)
+++ fc/relatedcontent/relatedcontent.mysql.inc	(.../fc/relatedcontent)	(working copy)
@@ -50,39 +50,44 @@
 
 function _relatedcontent_db_enabled_views_query() {
     return <<<SQL
-SELECT * from {relatedcontent_view} r WHERE r.enabled = %d
-ORDER BY weight, title
+SELECT * from {relatedcontent_view} ORDER BY weight, title
 SQL;
 }
 
 function _relatedcontent_db_get_views_for_type_query() {
     return <<<SQL
-SELECT * FROM {relatedcontent_view} where type = '%s'
+SELECT * FROM {relatedcontent_view} WHERE type = '%s' ORDER BY rcvid
 SQL;
 }
 
-function _relatedcontent_db_get_enabled_views_for_type_query() {
-    return <<<SQL
-SELECT * FROM {relatedcontent_view} where type = '%s' and enabled = %d
+function _relatedcontent_db_get_view_by_rcvid_query($values) {
+  return <<<SQL
+SELECT * FROM {relatedcontent_view} WHERE rcvid = %d
 SQL;
 }
 
 function _relatedcontent_db_set_view_update_query() {
   return <<<SQL
-UPDATE {relatedcontent_view} SET enabled = %d, title='%s', weight = %d WHERE vid = '%s' AND type = '%s'
+UPDATE {relatedcontent_view} SET title='%s', weight = %d WHERE rcvid = %d
 SQL;
 }
 
 function _relatedcontent_db_set_view_insert_query() {
   return <<<SQL
-INSERT INTO {relatedcontent_view} (type, vid, title, weight, enabled) VALUES ('%s', '%s', '%s', %d, %d)
+INSERT INTO {relatedcontent_view} (type, vid, display, title, weight) VALUES ('%s', '%s', '%s', '%s', %d)
 SQL;
 }
 
+function _relatedcontent_db_delete_view_query() {
+  return <<<SQL
+DELETE FROM {relatedcontent_view} WHERE rcvid = %d
+SQL;
+}
+
 function _relatedcontent_db_load_query() {
   return <<<SQL
-SELECT r.nid, r.vid, r.include_nid FROM {relatedcontent} r
-  INNER JOIN {relatedcontent_view} v ON r.vid = v.vid AND r.nid = %d
+SELECT r.nid, r.rcvid, r.include_nid FROM {relatedcontent} r
+  INNER JOIN {relatedcontent_view} v ON r.rcvid = v.rcvid AND r.nid = %d
 ORDER BY v.weight, r.ordinal_number
 SQL;
 }
@@ -96,12 +101,12 @@
   $str = drupal_substr($str, 0, -1);
   $values = array();
   return <<<SQL
-INSERT INTO {relatedcontent} (nid, vid, include_nid, ordinal_number) VALUES $str
+INSERT INTO {relatedcontent} (nid, rcvid, include_nid, ordinal_number) VALUES $str
 SQL;
 }
 
 function _relatedcontent_db_delete_query($args) {
-  $and_vid = $args[1] ? "AND vid = '%s'" : '';
+  $and_vid = $args[1] ? "AND rcvid = '%s'" : '';
   return <<<SQL
 DELETE FROM {relatedcontent} WHERE nid = %d $and_vid
 SQL;
