--- /home/joachim/public_html/drupal-5.1/custom modules/station/schedule/origXstation_schedule.module 
+++ /home/joachim/public_html/drupal-5.1/custom modules/station/schedule/station_schedule.module 
@@ -160,6 +160,13 @@
     '#description' => t("This lets you restrict DJs to members of a specific role. If you don't want any restrictions, select <em>authenticated user</em>s."),
   );
   
+  $form['station_schedule_grid_trim'] = array(
+    '#type' => 'checkbox',
+    '#title' => t("Trim schedule grid"),
+    '#default_value' => variable_get('station_schedule_grid_trim', 0),
+    '#description' => t('Select this to exclude empty hours at the start and end of the day from the full week schedule grid. '),
+  );  
+  
   $form['station_schedule_program_node_type'] = array(
     '#type' => 'item',
     '#title' => t('Scheduled node type'),
@@ -867,6 +874,24 @@
   drupal_set_title(t('Weekly Schedule'));
 
   $today = station_today();
+  
+  # optionally trim the start and end of the grid 
+  if( variable_get('station_schedule_grid_trim', 0) ) {
+    // get the earliest start time and latest finish time, in daily minutes
+    // !!!! these queries work directly on the databse in phpmyadmin,
+    // but appear to return the wrong sort of thing.
+    $earliest = db_result(db_query('SELECT MIN(mod( start ,1440)) FROM {station_schedule}'));
+    $latest   = db_result(db_query('SELECT MAX(mod( finish,1440)) FROM {station_schedule}'));
+    // calculate start and end time in hours, rounded to complete hours
+    // eg turn 09:15 - 15:30 into 09:00 - 16:00
+    $hour_start   = ($earliest - $earliest % 60)/60;
+    $hour_finish  = ($latest   + ($latest % 60 != 0 ? 60 - $latest % 60 : 0))/60; 
+  }
+  else {
+    # default values
+    $hour_start   = 0;
+    $hour_finish  = 24;    
+  }
 
   // build the headers
   $cols = array('data' => t('Time'));
@@ -878,7 +903,7 @@
 
   $row = array();
   // put in a column of hours
-  for ($i = 0; $i < 24; $i++) {
+  for ($i = $hour_start; $i < $hour_finish; $i++) {  
     $cell .= theme('station_schedule_hour', $i);
   }
   $row[] = array('data' => $cell, 'id' => 'station-sch-hours');
@@ -886,8 +911,8 @@
   for ($i = 0; $i < 7; $i++) {
     $cell = '';
     // find the minute that the day starts and finishes
-    $day_start = $i * 1440;
-    $day_finish = $day_start + 1440;
+    $day_start = $i * 1440 + $hour_start * 60;
+    $day_finish = $i * 1440 + $hour_finish * 60;
     $last_finish = $day_start;
     foreach (station_schedule_load_day($i) as $s) {
       // display blocks for unscheduled time before this program
