--- casetracker.module	2010-05-10 18:51:22.000000000 +0100
+++ casetrackerNew.module	2010-07-25 08:21:54.000000000 +0100
@@ -448,6 +448,113 @@ function casetracker_case_form_common(&$
   return $form;
 }
 
+
+/**
+ * Check if the current page is a casetracker node, and return info.
+ *
+ * @return
+ *  Associative array of 'type' ('project'/'case') and 'node', 
+ *  a full load of the node data. 
+ *  If this is not a casetracker page, all elements exist and are FALSE.  
+ */
+function casetracker_page_info() {
+  $path = $_GET['q'];
+  $bits = explode('/', $path);
+  $info = array('type' => FALSE, 'node' => FALSE, 'path_elements' => $bits);
+  if ($bits[0] == 'node') {
+    $info['node'] = node_load($bits[1]);
+    // This is the guarenteed way of knowing.
+    if (casetracker_is_project($info['node'])):
+      $info['type'] = 'project';
+    elseif (casetracker_is_case($info['node'])):
+      $info['type'] = 'case';
+    endif;
+  }
+
+  return $info;
+}
+
+/**
+ * Finds all nodes which are casetracker projects.
+ *
+ * @return
+ *   A resource identifier pointing to the query results.
+ */
+function casetracker_select_projects($order = 'n.title DESC') {
+  $types = variable_get('casetracker_project_node_types', array('casetracker_basic_project'));
+  // $types is a zeroed associative array, not a selection of project node types.
+  $types = array_filter($types);
+
+  $sql = db_rewrite_sql('SELECT n.nid, n.title FROM {node} n WHERE n.type IN (' . db_placeholders($types, 'varchar') . ') ORDER BY ' . $order);
+
+  return db_query($sql, $types);
+}
+
+/**
+ * Format the selection field for choosing terms
+ * (by deafult the default selection field is used).
+ *
+ * @ingroup themeable
+ */
+function theme_casetracker_project_select($element) {
+  return theme('select', $element);
+}
+
+/**
+ * Generate a form element for selecting projects.
+ *
+ * @param $default_nid
+ *  A nid to be used as a default, if within the select options.
+ */
+function _casetracker_project_select($default_nid = 0) {
+  $options = array();
+
+  $result = casetracker_select_projects();
+  while ($node = db_fetch_object($result)) {
+     $options[$node->nid] = t($node->title);
+  }
+
+  return array(
+    '#title' => t('Select a project'),
+    '#type' => 'select',
+    '#required' => TRUE,
+    '#default_value' => in_array($default_nid, array_keys($options)) ? $default_nid : 0,
+    '#options' => $options,
+    '#theme' => 'casetracker_project_select',
+  );
+}
+
+/**
+ * Submit function for our "select project" block.
+ */
+function casetracker_project_select_form_submit($form, $form_state) {
+  drupal_goto('node/'. $form_state['values']['project_id']);
+}
+
+/**
+ * Form for "Select a project" block.
+ *
+ * @param $default_nid
+ *  A nid to be used as a default, if within the select options.
+ * @return
+ *   A form array to select a project.
+ *
+ * @see _casetracker_project_select()
+ */
+function casetracker_project_select_form(&$form_state, $default_nid = 0) {
+  $form = array();
+
+  $form['project_id'] = _casetracker_project_select($default_nid);
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Go'),
+  );
+
+  return $form;
+}
+
+
 /**
 * Implementation of hook_block
 */
@@ -455,7 +562,10 @@ function casetracker_block($op = 'list',
   switch ($op) {
     case 'list':
       $blocks[0] = array(
-        'info' => t('Jump to case'),
+        'info' => t('Jump to casetracker case'),
+      );
+      $blocks[1] =array(
+        'info' => t('Select a casetracker project'),
       );
       return $blocks;
     case 'configure':
@@ -470,6 +580,13 @@ function casetracker_block($op = 'list',
             $block['content'] = drupal_get_form('casetracker_block_jump_to_case_number');
           }
           break;
+        case 1:
+          if (user_access('access content')) {
+            $block['subject'] = t('Select a project');
+            $info = casetracker_page_info();
+            $block['content'] = drupal_get_form('casetracker_project_select_form', $info['type'] == 'project' ? $info['node']->nid : 0);
+          }
+          break;
       }
       return $block;
   }
@@ -740,6 +857,9 @@ function casetracker_theme() {
     'casetracker_case_form_common' => array(),
     'casetracker_case_summary' => array(),
     'casetracker_project_summary' => array(),
+    'casetracker_project_select' => array(
+      'arguments' => array('element' => NULL),
+    ),
   );
 }
 
