*************** function station_schedule_load($node) {
*** 339,353 ****
        }
        $schedule[$day][] = $s;
      }
-   }
  
-   // Load the settings.
-   $settings = db_fetch_array(db_query('SELECT increment, streams, unscheduled_message FROM {station_schedule} WHERE nid = %d', $node->nid));
-   if (isset($settings['streams']) && $streams = unserialize($settings['streams'])) {
-     $settings['streams'] = $streams;
-   }
-   else {
-     $settings['streams'] = array();
    }
  
    return array(
--- 379,401 ----
        }
        $schedule[$day][] = $s;
      }
  
+     // load Sunday morning to show on the end of Saturday night, if the schedule display goes beyond midnight
+     if ($day == 6 and $finish > 7 * MINUTES_IN_DAY) {
+       $result = db_query('SELECT * FROM {station_schedule_item} i WHERE i.schedule_nid = %d AND i.finish > %d AND i.start < %d ORDER BY i.start', $node->nid, 0, $finish - 7 * MINUTES_IN_DAY);
+ 
+       while ($s = db_fetch_object($result)) {
+         // spoof the times to appear beyond the end of the week for the schedule layout
+         $s->finish += 7 * MINUTES_IN_DAY;
+         $s->start  += 7 * MINUTES_IN_DAY;
+ 
+         // only need to trim the end of the show: a show starting before 0 has already been loaded above
+         if ($s->finish > $finish) {
+           $s->finish = $finish;
+         }
+         $schedule[$day][] = $s;
+       }
+     }
    }
  
    return array(
*************** function station_schedule_view($node, $t
*** 1242,1251 ****
      $header[0] = array('data' => t('Time'));
      $row = array();
  
      // First column is hours.
      $row[0]['id'] = 'station-sch-hours';
-     for ($hour = 0; $hour < 24; $hour++) {
-       $row[0]['data'] .= theme('station_schedule_hour', $hour);
      }
  
      // Then a column for each day of the week.
--- 1299,1315 ----
      $header[0] = array('data' => t('Time'));
      $row = array();
  
+     // get the start and end times of the schedule
+     $schedule_start   = $node->settings['start'];
+     $schedule_finish  = $node->settings['finish'];
+     if ($schedule_finish <= $schedule_start) {
+       $schedule_finish += 1440; 
+     } // for wrapping: to come later
+ 
      // First column is hours.
      $row[0]['id'] = 'station-sch-hours';
+     for ($hour = $schedule_start / 60; $hour < $schedule_finish / 60; $hour++) {
+       $row[0]['data'] .= theme('station_schedule_hour', $hour % 24);
      }
  
      // Then a column for each day of the week.
*************** function station_schedule_view($node, $t
*** 1254,1261 ****
        $row[$day + 1]['data'] = '';
  
        // The last finish pointer starts at the beginning of the day.
-       $last_finish = $day * MINUTES_IN_DAY;
-       $day_finish = ($day + 1) * MINUTES_IN_DAY;
        foreach ($items as $item) {
          // Display blocks for unscheduled time periods
          if ($last_finish != $item->start) {
--- 1318,1325 ----
        $row[$day + 1]['data'] = '';
  
        // The last finish pointer starts at the beginning of the day.
+       $last_finish = $day * MINUTES_IN_DAY + $schedule_start;
+       $day_finish  = $day * MINUTES_IN_DAY + $schedule_finish;
        foreach ($items as $item) {
          // Display blocks for unscheduled time periods
          if ($last_finish != $item->start) {
