Index: schedule/station_schedule.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/schedule/station_schedule.install,v
retrieving revision 1.12
diff -u -r1.12 station_schedule.install
--- schedule/station_schedule.install	21 Feb 2007 16:30:58 -0000	1.12
+++ schedule/station_schedule.install	22 Feb 2007 17:42:03 -0000
@@ -10,13 +10,20 @@
     case 'mysql':
     case 'mysqli':
       db_query("
-        CREATE TABLE {station_schedule} (
-          `sid` int(11) unsigned NOT NULL default '0',
+        CREATE TABLE {station} (
+          `sid` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+          `title` VARCHAR(32) NOT NULL DEFAULT '',
+          PRIMARY KEY(`sid`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;
+      ");
+      db_query("
+        CREATE TABLE {station_schedule_item} (
+          `iid` int(11) unsigned NOT NULL default '0',
           `program_nid` int(10) unsigned NOT NULL default '0',
           `start` int(10) unsigned NOT NULL default '0',
           `finish` int(10) unsigned NOT NULL default '0',
           `may_archive` tinyint(1) NOT NULL default '1',
-          PRIMARY KEY  (`sid`),
+          PRIMARY KEY  (`iid`),
           KEY `station_schedule_start` (`start`),
           KEY `station_schedule_program` (`program_nid`)
         ) /*!40100 DEFAULT CHARACTER SET utf8 */;
@@ -236,3 +243,52 @@
   return $ret;
 }
 
+/**
+ * Rename station_schedule to station_schedule_item and rename sid to iid. Add
+ * a new sid field that refers to the schedule id. Then create a
+ * station_schedule table and update all schedule items to be a member of it.
+ */
+function station_schedule_update_102() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql('ALTER TABLE {station_schedule}
+        RENAME TO {station_schedule_item},
+        CHANGE COLUMN `sid` `iid` INTEGER UNSIGNED NOT NULL DEFAULT 0;
+      ');
+      $ret[] = update_sql('ALTER TABLE {station_schedule_item}
+        ADD COLUMN `sid` INTEGER UNSIGNED NOT NULL DEFAULT 0 FIRST;
+      ');
+      $ret[] = update_sql("
+        CREATE TABLE {station_schedule} (
+          `sid` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+          `title` VARCHAR(32) NOT NULL DEFAULT '',
+          PRIMARY KEY(`sid`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;
+      ");
+
+      // update the sequence
+      db_query('LOCK TABLES {sequences} WRITE');
+      $ret[] = update_sql("UPDATE sequences SET name = '". db_prefix_tables('{station_schedule_item}_iid') ."' WHERE name = '". db_prefix_tables('{station_schedule}_sid') ."';");
+      db_query('UNLOCK TABLES');
+
+      $sid = db_next_id('{station_schedule}_sid');
+      $ret[] = update_sql("INSERT INTO {station_schedule} (sid, title) VALUES ($sid, 'Default')");
+      $ret[] = update_sql("UPDATE {station_schedule_item} SET sid=$sid;");
+
+      $ret[] = update_sql('ALTER TABLE {station_schedule_item}
+        DROP INDEX `station_schedule_start`,
+        ADD INDEX `station_schedule_start`(`sid`, `start`),
+        DROP INDEX `station_schedule_program`,
+        ADD INDEX `station_schedule_program`(`sid`, `program_nid`);
+      ');
+
+
+      variable_set('station_schedule_default', $sid);
+
+    break;
+  }
+  return $ret;
+}
+
Index: schedule/station_schedule.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/schedule/station_schedule.module,v
retrieving revision 1.31
diff -u -r1.31 station_schedule.module
--- schedule/station_schedule.module	12 Feb 2007 20:33:00 -0000	1.31
+++ schedule/station_schedule.module	22 Feb 2007 19:16:16 -0000
@@ -55,21 +55,14 @@
       'path' => 'admin/content/schedule/add',
       'title' => t('Add new'),
       'callback' => 'drupal_get_form',
-      'callback arguments' => array('station_schedule_admin_form', 'add'),
+      'callback arguments' => array('station_schedule_add_form'),
       'type' => MENU_LOCAL_TASK,
     );
     $items[] = array(
-      'path' => 'admin/content/schedule/edit',
-      'title' => t('Edit schedule item'),
+      'path' => 'admin/content/schedule/delete',
+      'title' => t('Delete schedule'),
       'callback' => 'drupal_get_form',
-      'callback arguments' => array('station_schedule_admin_form', 'edit'),
-      'type' => MENU_CALLBACK
-    );
-    $items[] = array(
-      'path' => 'admin/content/schedule/remove',
-      'title' => t('Remove schedule item'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => array('station_schedule_admin_remove'),
+      'callback arguments' => array('station_schedule_delete_form'),
       'type' => MENU_CALLBACK
     );
 
@@ -117,6 +110,48 @@
         'weight' => $day,
       );
     }
+
+    if (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'schedule' && is_numeric(arg(3))) {
+      $sid = arg(3);
+      if ($schedule = station_schedule_load($sid)) {
+        $items[] = array(
+          'path' => 'admin/content/schedule/'. $sid,
+          'title' => t('@title schedule', array('@title' => $schedule->title)),
+          'callback' => 'station_schedule_item_list',
+          'callback arguments' => array($sid),
+          'access' => user_access('administer schedule'),
+          'description' => t("Alter the Station's schedule."),
+          'type' => MENU_CALLBACK,
+        );
+        $items[] = array(
+          'path' => 'admin/content/schedule/'. $sid .'/list',
+          'title' => t('List'),
+          'type' => MENU_DEFAULT_LOCAL_TASK,
+          'weight' => -10,
+        );
+        $items[] = array(
+          'path' => 'admin/content/schedule/'. $sid .'/add',
+          'title' => t('Add new'),
+          'callback' => 'drupal_get_form',
+          'callback arguments' => array('station_schedule_item_edit_form', 'add', $sid),
+          'type' => MENU_LOCAL_TASK,
+        );
+        $items[] = array(
+          'path' => 'admin/content/schedule/'. $sid .'/edit',
+          'title' => t('Edit schedule item'),
+          'callback' => 'drupal_get_form',
+          'callback arguments' => array('station_schedule_item_edit_form', 'edit', $sid),
+          'type' => MENU_CALLBACK
+        );
+        $items[] = array(
+          'path' => 'admin/content/schedule/'. $sid .'/remove',
+          'title' => t('Remove schedule item'),
+          'callback' => 'drupal_get_form',
+          'callback arguments' => array('station_schedule_item_remove_form', $sid),
+          'type' => MENU_CALLBACK
+        );
+      }
+    }
   }
 
   return $items;
@@ -151,7 +186,7 @@
     '#description' => t("This setting lets you determine what you'd like to call the users associated with programs. It should be plural."),
     '#required' => TRUE,
   );
-  
+
   $form['station_schedule_dj_role'] = array(
     '#type' => 'select',
     '#title' => t('DJs role'),
@@ -159,7 +194,7 @@
     '#options' => user_roles(TRUE),
     '#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_program_node_type'] = array(
     '#type' => 'item',
     '#title' => t('Scheduled node type'),
@@ -201,7 +236,7 @@
  */
 function station_schedule_archive_links($program_nid, $short = FALSE) {
   $archive_url = station_get_archive_url();
-  $may_archive = db_result(db_query('SELECT COUNT(*) FROM {station_schedule} s WHERE s.program_nid = %s AND s.may_archive = 1', $program_nid));
+  $may_archive = db_result(db_query('SELECT COUNT(*) FROM {station_schedule_item} s WHERE s.program_nid = %s AND s.may_archive = 1', $program_nid));
 
   if ($archive_url && $may_archive ) {
     $listen_url = $archive_url . $program_nid;
@@ -404,13 +439,18 @@
           '#tree' => TRUE,
           '#description' => t("These are the times the program is currently scheduled."),
         );
-        $times = _station_schedule_program_load_times($node->nid);
-        foreach ($times as $time) {
-          $form['schedule'][$time['sid']] = array(
-            '#type' => 'item',
-            '#value' => l(theme('station_dayhour_range', $time['start'], $time['finish']), 'admin/content/schedule/edit/'. $time['sid']),
-          );
+
+        foreach (_station_schedule_program_load_times($node->nid) as $sid => $times) {
+          $schedule = station_schedule_load($sid);
+          $form['schedule'][$sid]['#title'] = $schedule->title;
+          foreach ($times as $time) {
+            $form['schedule'][$sid][$time['iid']] = array(
+              '#type' => 'item',
+              '#value' => l(theme('station_dayhour_range', $time['start'], $time['finish']), 'admin/content/schedule/edit/'. $time['iid']),
+            );
+          }
         }
+
         // don't encourage adding unpublished stuff to the schedule.
         if ($node->status) {
           $form['schedule']['new'] = array(
@@ -483,14 +523,14 @@
 function _station_schedule_program_load_times($nid) {
   // load the program's scheduled times
   $i = 0;
-  $hours = array();
+  $times = array();
   if ($nid) {
-    $result = db_query('SELECT * FROM {station_schedule} s WHERE s.program_nid = %s ORDER BY s.start', $nid);
-    while ($s = db_fetch_array($result)) {
-      $hours[] = $s;
+    $result = db_query('SELECT * FROM {station_schedule_item} s WHERE s.program_nid = %s ORDER BY s.sid, s.start', $nid);
+    while ($item = db_fetch_array($result)) {
+      $times[$item['sid']][] = $item;
     }
   }
-  return $hours;
+  return $times;
 }
 
 /**
@@ -498,11 +538,13 @@
  */
 function station_schedule_program_get_themed_times($nid) {
   // load the program's scheduled times
-  $times = array();
-  foreach (_station_schedule_program_load_times($nid) as $range) {
-    $times[] = theme('station_dayhour_range', $range['start'], $range['finish']);
+  $ret = array();
+  foreach (_station_schedule_program_load_times($nid) as $sid => $times) {
+    foreach ($times as $time) {
+      $ret[] = theme('station_dayhour_range', $time['start'], $time['finish']);
+    }
   }
-  return $times;
+  return $ret;
 }
 
 /**
@@ -513,7 +555,7 @@
     array(
       'station.program.get.at',
       'station_schedule_program_get_at',
-      array('array', 'int'),
+      array('array', 'ini', 'int'),
       t('Fetch info on the program playing at a day/hour of a given timestamp.')
     ),
   );
@@ -533,30 +575,41 @@
   drupal_goto($path);
 }
 
+
+/**
+ * Return the default schedule id.
+ */
+function station_schedule_get_default() {
+  return variable_get('station_schedule_default', 0);
+}
+
 /**
  * Get the program playing at a certain time. If no time is provide, use the
  * current time.
  *
  * @param $gmt_timestamp
  *   a timestamp used to determine the day of the week an hour.
+ * @param $sid
+ *   Schedule Id, 0 will load the default schedule.
  * @return
  *   program node object if one is scheduled, an empty object if nothing is
  *   scheduled.
  */
-function station_schedule_program_get_at($gmt_timestamp) {
+function station_schedule_program_get_at($gmt_timestamp, $sid = 0) {
   // Load the schedule item based on the time.
   $ts = station_local_ts($gmt_timestamp);
   $day = date('w', $ts);
   $hour = date('G', $ts);
   $minute = station_minute_from_day_hour($day, $hour);
-  $schedule = station_schedule_load_at($minute);
+  $sid = $sid ? (int) $sid : station_schedule_get_default();
+  $schedule_item = station_schedule_load_item_at($sid, $minute);
 
   // If there's an associated program, load it
-  if ($schedule->program_nid) {
-    if ($node = node_load($schedule->program_nid)) {
+  if ($schedule_item->program_nid) {
+    if ($node = node_load($schedule_item->program_nid)) {
       // set this so that if the show is scheduled for multiple times the caller
       // can easily figure out which one.
-      $node->may_archive = $schedule->may_archive;
+      $node->may_archive = $schedule_item->may_archive;
       // put this in so they can use a pretty url
       $node->node_url = url('node/'. $node->nid, NULL, NULL, TRUE);
       return $node;
@@ -572,30 +625,44 @@
  */
 function station_schedule_load($sid) {
   $result = db_query('SELECT * FROM {station_schedule} s WHERE s.sid = %d', $sid);
-  if ($s = db_fetch_object($result)) {
-    return $s;
+  if ($o = db_fetch_object($result)) {
+    return $o;
+  }
+}
+/**
+ * Load the schedule item by its id.
+ * @param $iid schedule item id
+ * @return schedule item object
+ */
+function station_schedule_load_item($iid) {
+  $result = db_query('SELECT * FROM {station_schedule_item} i WHERE i.iid = %d', $iid);
+  if ($o = db_fetch_object($result)) {
+    return $o;
   }
 }
 
 /**
  * Load the schedule by minute of the week.
- * @param $minute the minute of the week...
+ * @param $sid
+ *  Schedule id
+ * @param $minute
+ *  the minute of the week...
  * @return schedule object
  */
-function station_schedule_load_at($minute) {
-  $result = db_query('SELECT * FROM {station_schedule} s WHERE s.start <= %d AND s.finish > %d', $minute, $minute);
+function station_schedule_load_item_at($sid, $minute) {
+  $result = db_query('SELECT * FROM {station_schedule_item} s WHERE s.sid = %d AND s.start <= %d AND s.finish > %d', $sid, $minute, $minute);
   if ($s = db_fetch_object($result)) {
     return $s;
   }
 }
 
-function station_schedule_load_day($day) {
+function station_schedule_load_day($sid, $day) {
   // there are 1440 minutes in a day (60minute * 24hours = 1day)
   $start = $day * 1440;
   $finish = $start + 1440;
 
   $ret = array();
-  $result = db_query('SELECT * FROM {station_schedule} s WHERE s.finish > %d AND s.start < %d ORDER BY s.start', $start, $finish);
+  $result = db_query('SELECT * FROM {station_schedule_item} i WHERE i.sid = %d AND i.finish > %d AND i.start < %d ORDER BY i.start', $sid, $start, $finish);
   while ($s = db_fetch_object($result)) {
     // if a show spans a day, limit its start and finish times to be with-in
     // the day
@@ -611,6 +678,73 @@
 }
 
 function station_schedule_admin_list() {
+  $result = db_query('SELECT * FROM {station_schedule} s');
+  while ($o = db_fetch_object($result)) {
+    $items[] = l($o->title, 'admin/content/schedule/'. $o->sid);
+  }
+  return theme('item_list', $items);
+}
+
+function station_schedule_add_form() {
+  $post_op = $_POST['op'];
+  if ($post_op == t('Cancel')) {
+    drupal_goto('admin/content/schedule/');
+  }
+
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Title'),
+    '#description' => t("The schedule's title. I.e. 'AM', 'FM', callsign."),
+    '#maxlength' => 32,
+    '#required' => TRUE,
+  );
+  $form['buttons']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+  $form['buttons']['cancel'] = array(
+    '#type' => 'submit',
+    '#value' => t('Cancel'),
+  );
+  return $form;
+}
+
+function station_schedule_add_form_submit($form_id, $form_values) {
+  $sid = db_next_id('{station_schedule}_sid');
+  db_query("INSERT INTO {station_schedule} (sid, title) VALUES (%d, '%s')" , $sid, $form_values['title']);
+  //FIXME:  _station_send_notice('schedule', 'add', array('nid' => $program->nid, 'iid' => $iid));
+  drupal_goto('admin/content/schedule/'. $sid);
+}
+
+function station_schedule_delete_form($sid = '') {
+  $schedule = station_schedule_load($sid);
+  if (!$schedule) {
+    return 'admin/content/schedule';
+  }
+
+  $form['sid'] = array('#type' => 'value', '#value' => $schedule->sid);
+  return confirm_form(
+    $form,
+    t('Are you sure you want to delete the %title schedule?', array('%title' => $schedule->title)),
+    $_GET['destination'] ? $_GET['destination'] : 'admin/content/schedule',
+    t('This action cannot be undone.'),
+    t('Remove'),
+    t('Cancel')
+  );
+}
+
+function station_schedule_delete_form_submit($formid, $form) {
+  if ($form['confirm']) {
+    db_query("DELETE FROM {station_schedule_item} WHERE sid = %d", $form['sid']);
+    db_query("DELETE FROM {station_schedule} WHERE sid = %d", $form['sid']);
+    //FIXME: _station_send_notice('schedule', 'remove', array('nid' => $form['nid'], 'iid' => $form['iid']));
+    return 'admin/content/schedule';
+  }
+}
+
+
+
+function station_schedule_item_list($sid) {
   drupal_add_css(drupal_get_path('module', 'station_schedule') .'/station_schedule.css');
 
   $cols = station_day_name();
@@ -621,21 +755,24 @@
     $day_start = $i * 1440;
     $day_finish = $day_start + 1440;
     $last_finish = $day_start;
-    foreach (station_schedule_load_day($i) as $s) {
+    foreach (station_schedule_load_day($sid, $i) as $item) {
       // display blocks for unscheduled time periods
-      if ($last_finish != $s->start) {
-        $cell .= theme('station_schedule_admin_nonitem', $last_finish, $s->start);
+      if ($last_finish != $item->start) {
+        $link = "admin/content/schedule/{$sid}/add/0/{$last_finish}/{$item->start}";
+        $cell .= theme('station_schedule_admin_nonitem', $last_finish, $item->start, $link);
       }
-      $last_finish = $s->finish;
+      $last_finish = $item->finish;
 
       // display the schedule item
-      $height = ($s->finish - $s->start);
-      $program = node_load($s->program_nid);
-      $cell .= theme('station_schedule_admin_item', $s->sid, $s->start, $s->finish, $program);
+      $height = ($item->finish - $item->start);
+      $item->program = node_load($item->program_nid);
+      $link = "admin/content/schedule/{$sid}/edit/{$item->iid}";
+      $cell .= theme('station_schedule_admin_item', $item, $link);
     }
     // any remaining time during the day
     if ($last_finish < $day_finish) {
-      $cell .= theme('station_schedule_admin_nonitem', $last_finish, $day_finish);
+      $link = "admin/content/schedule/{$sid}/add/0/{$last_finish}/{$day_finish}";
+      $cell .= theme('station_schedule_admin_nonitem', $last_finish, $day_finish, $link);
     }
 
     $row[$i] = $cell;
@@ -645,10 +782,9 @@
   return theme('table', $cols, array($row), array('id' => 'station-sch', 'class' => 'station-sch-admin'));
 }
 
-function theme_station_schedule_admin_nonitem($start, $finish) {
+function theme_station_schedule_admin_nonitem($start, $finish, $link) {
   $class = 'station-sch-box station-sch-unscheduled';
   $height = ($finish - $start);
-  $link = "admin/content/schedule/add/0/{$start}/{$finish}";
 
   $output = "<div class='{$class}' style='height:{$height}px;'>";
   $output .= '<div class="station-sch-time">'. theme('station_hour_range', $start, $finish) .'</div>';
@@ -658,14 +794,13 @@
   return l($output, $link, NULL, NULL, NULL, NULL, TRUE);
 }
 
-function theme_station_schedule_admin_item($sid, $start, $finish, $program) {
+function theme_station_schedule_admin_item($item, $link) {
   $class = 'station-sch-box station-sch-scheduled';
-  $height = ($finish - $start);
-  $link = 'admin/content/schedule/edit/'. $sid;
+  $height = ($item->finish - $item->start);
 
   $output = "<div class='{$class}' style='height:{$height}px;'>";
-  $output .= '<div class="station-sch-time">'. theme('station_hour_range', $start, $finish) .'</div>';
-  $output .= check_plain($program->title);
+  $output .= '<div class="station-sch-time">'. theme('station_hour_range', $item->start, $item->finish) .'</div>';
+  $output .= check_plain($item->program->title);
   $output .= "</div>\n";
 
   return l($output, $link, NULL, NULL, NULL, NULL, TRUE);
@@ -673,12 +808,14 @@
 
 /**
  * @param $op 'add' or 'edit'
- * @param $id when adding it's a program id. when editing, it's a schedule id.
+ * @param $sid schedule id.
+ * @param $id when adding it's a program id. when editing, it's a schedule item
+ *  id.
  */
-function station_schedule_admin_form($op, $id = NULL, $start = 0, $finish = 60) {
+function station_schedule_item_edit_form($op, $sid, $id = NULL, $start = 0, $finish = 60) {
   $post_op = $_POST['op'];
   if ($post_op == t('Cancel')) {
-    drupal_goto('admin/content/schedule');
+    drupal_goto('admin/content/schedule/'. $sid);
   }
   if ($post_op == t('Remove')) {
     drupal_goto("admin/content/schedule/remove/$id");
@@ -697,19 +834,20 @@
     if ($id && $program = node_load($id)) {
       $program_title = $program->title;
     }
-    $sch = new stdClass;
-    $sch->sid = $id;
-    $sch->start = (int) $start;
-    $sch->finish = (int) $finish;
-    $sch->may_archive = TRUE;
+    $schedule_item = new stdClass;
+    $schedule_item->sid = $sid;
+    $schedule_item->iid = $id;
+    $schedule_item->start = (int) $start;
+    $schedule_item->finish = (int) $finish;
+    $schedule_item->may_archive = TRUE;
   }
   else if ($op == 'edit') {
-    if ($id && $sch = station_schedule_load($id)) {
-      $program = node_load($sch->program_nid);
+    if ($id && $schedule_item = station_schedule_load_item($id)) {
+      $program = node_load($schedule_item->program_nid);
       $program_title = $program->title;
-      $form['sid'] = array(
+      $form['iid'] = array(
         '#type' => 'value',
-        '#value' => $sch->sid,
+        '#value' => $schedule_item->iid,
       );
     }
     else {
@@ -717,27 +855,31 @@
     }
   }
 
+  $form['sid'] = array(
+    '#type' => 'value',
+    '#value' => $schedule_item->sid,
+  );
   $form['start_day'] = array(
     '#type' => 'select',
     '#title' => t('Starts'),
-    '#default_value' => station_day_from_minute($sch->start),
+    '#default_value' => station_day_from_minute($schedule_item->start),
     '#options' => $day_options,
   );
   $form['start_minutes'] = array(
     '#type' => 'select',
-    '#default_value' => $sch->start % 1440,
+    '#default_value' => $schedule_item->start % 1440,
     '#options' => $minute_options,
   );
   $form['finish_day'] = array(
     '#type' => 'select',
     '#title' => t('Ends'),
-    '#default_value' => station_day_from_minute($sch->finish),
+    '#default_value' => station_day_from_minute($schedule_item->finish),
     '#options' => $day_options,
     '#description' => $description,
   );
   $form['finish_minutes'] = array(
     '#type' => 'select',
-    '#default_value' => $sch->finish % 1440,
+    '#default_value' => $schedule_item->finish % 1440,
     '#options' => $minute_options,
   );
   $form['program_title'] = array(
@@ -751,7 +893,7 @@
   $form['may_archive'] = array(
     '#type' => 'checkbox',
     '#title' => t('Can be saved in Station Archive'),
-    '#default_value' => $sch->may_archive,
+    '#default_value' => $schedule_item->may_archive,
     '#description' => t('Checking this indicates that the Station Archive module can save audio recordings of the program at this timeslot to the archive.'),
   );
 
@@ -759,7 +901,7 @@
     '#type' => 'submit',
     '#value' => t('Save'),
   );
-  if ($sch->sid) {
+  if ($schedule_item->iid) {
     $form['buttons']['remove'] = array(
       '#type' => 'button',
       '#value' => t('Remove'),
@@ -773,7 +915,7 @@
   return $form;
 }
 
-function station_schedule_admin_form_validate($form_id, $form_values) {
+function station_schedule_item_edit_form_validate($form_id, $form_values) {
   $start = ($form_values['start_day'] * 1440) + $form_values['start_minutes'];
   $finish = ($form_values['finish_day'] * 1440) + $form_values['finish_minutes'];
 
@@ -781,7 +923,7 @@
     form_set_error('finish', t("The program must start before it finishes."));
   }
 
-  $result = db_query('SELECT count(*) AS count, min(s.start) AS start, max(s.finish) AS finish FROM {station_schedule} s WHERE s.sid <> %d AND s.finish > %d AND s.start < %d', $form_values['sid'], $start, $finish);
+  $result = db_query('SELECT count(*) AS count, min(s.start) AS start, max(s.finish) AS finish FROM {station_schedule_item} s WHERE s.iid <> %d AND s.finish > %d AND s.start < %d', $form_values['iid'], $start, $finish);
   if ($overlap = db_fetch_object($result)) {
     if ($overlap->count == 1) {
       form_set_error('', t("The program overlaps another scheduled item at %time.", array('%time' => theme('station_dayhour_range', $overlap->start, $overlap->finish))));
@@ -797,51 +939,53 @@
   }
 }
 
-function station_schedule_admin_form_submit($form_id, $form_values) {
+function station_schedule_item_edit_form_submit($form_id, $form_values) {
+  $sid = $form_values['sid'];
   $start = ($form_values['start_day'] * 1440) + $form_values['start_minutes'];
   $finish = ($form_values['finish_day'] * 1440) + $form_values['finish_minutes'];
   $program = node_load(array('type' => 'program', 'title' => $form_values['program_title']));
 
-  if ($form_values['sid']) {
-    $old_sch = station_schedule_load($form_values['sid']);
-    db_query("UPDATE {station_schedule} SET program_nid = %d, start = %d, finish = %d, may_archive = %d WHERE sid = %d",
-      $program->nid, $start, $finish, $form_values['may_archive'], $form_values['sid']);
+  if ($form_values['iid']) {
+    $old_sch = station_schedule_load_item($form_values['iid']);
+    db_query("UPDATE {station_schedule_item} SET program_nid = %d, start = %d, finish = %d, may_archive = %d WHERE iid = %d",
+      $program->nid, $start, $finish, $form_values['may_archive'], $form_values['iid']);
     if ($old_sch->program_nid != $program->nid) {
-    _station_send_notice('schedule', 'remove', array('nid' => $old_sch->program_nid, 'sid' => $form_values['sid']));
-    _station_send_notice('schedule', 'add', array('nid' => $program->nid, 'sid' => $form_values['sid']));
+      _station_send_notice('schedule', 'remove', array('nid' => $old_sch->program_nid, 'iid' => $form_values['iid']));
+      _station_send_notice('schedule', 'add', array('nid' => $program->nid, 'iid' => $form_values['iid']));
     }
     elseif ($old_sch->start != $start || $old_sch->finish != $finish) {
-      _station_send_notice('schedule', 'change', array('nid' => $program->nid, 'sid' => $form_values['sid']));
+      _station_send_notice('schedule', 'change', array('nid' => $program->nid, 'iid' => $form_values['iid']));
     }
   }
   else {
-    $sid = db_next_id('{station_schedule}_sid');
-    db_query("INSERT INTO {station_schedule} (sid, program_nid, start, finish, may_archive) VALUES (%d, %d, %d, %d, %d)",
-      $sid, $program->nid, $start, $finish, $form_values['may_archive']);
-    _station_send_notice('schedule', 'add', array('nid' => $program->nid, 'sid' => $sid));
+    $iid = db_next_id('{station_schedule_item}_iid');
+    db_query("INSERT INTO {station_schedule_item} (sid, iid, program_nid, start, finish, may_archive) VALUES (%d, %d, %d, %d, %d, %d)",
+     $sid, $iid, $program->nid, $start, $finish, $form_values['may_archive']);
+    _station_send_notice('schedule', 'add', array('nid' => $program->nid, 'iid' => $iid));
   }
-  drupal_goto('admin/content/schedule');
+  drupal_goto('admin/content/schedule/'. $sid);
 }
 
 /**
  * Page to confirm the removal of a schedule item.
  */
-function station_schedule_admin_remove($sid = '') {
-  $sch = station_schedule_load($sid);
-  if (!$sch) {
+function station_schedule_item_remove_form($iid = '') {
+  $schedule_item = station_schedule_load_item($iid);
+  if (!$schedule_item) {
     return 'admin/content/schedule';
   }
 
-  $program = node_load($sch->program_nid);
+  $schedule = station_schedule_load($schedule_item->sid);
+  $program = node_load($schedule_item->program_nid);
   $title = $program->title;
-  $time = theme('station_dayhour_range', $sch->start, $sch->finish);
+  $time = theme('station_dayhour_range', $schedule_item->start, $schedule_item->finish);
 
-  $form['sid'] = array('#type' => 'value', '#value' => $sch->sid);
-  $form['nid'] = array('#type' => 'value', '#value' => $sch->program_nid);
+  $form['iid'] = array('#type' => 'value', '#value' => $schedule_item->iid);
+  $form['nid'] = array('#type' => 'value', '#value' => $schedule_item->program_nid);
   return confirm_form(
     $form,
-    t('Are you sure you want to unschedule %title at %time?', array('%title' => $program->title, '%time' => $time)),
-    $_GET['destination'] ? $_GET['destination'] : 'admin/content/schedule',
+    t('Are you sure you want to unschedule %title from %schedule at %time?', array('%title' => $program->title, '%schedule' => $schedule->title, '%time' => $time)),
+    $_GET['destination'] ? $_GET['destination'] : 'admin/content/schedule/'. $schedule->sid,
     t('This action cannot be undone, but you can always re-add the program to the schedule.'),
     t('Remove'),
     t('Cancel')
@@ -851,10 +995,10 @@
 /**
  * Delete a schedule item.
  */
-function station_schedule_admin_remove_submit($formid, $form) {
+function station_schedule_item_remove_form_submit($formid, $form) {
   if ($form['confirm']) {
-    db_query("DELETE FROM {station_schedule} WHERE sid = %d", $form['sid']);
-    _station_send_notice('schedule', 'remove', array('nid' => $form['nid'], 'sid' => $form['sid']));
+    db_query("DELETE FROM {station_schedule_item} WHERE iid = %d", $form['iid']);
+    _station_send_notice('schedule', 'remove', array('nid' => $form['nid'], 'iid' => $form['iid']));
     return 'admin/content/schedule';
   }
 }
@@ -862,7 +1006,7 @@
 /**
  * Print a week's schedule page
  */
-function station_schedule_week_page() {
+function station_schedule_week_page($sid) {
   drupal_add_css(drupal_get_path('module', 'station_schedule') .'/station_schedule.css');
   drupal_set_title(t('Weekly Schedule'));
 
@@ -889,7 +1033,7 @@
     $day_start = $i * 1440;
     $day_finish = $day_start + 1440;
     $last_finish = $day_start;
-    foreach (station_schedule_load_day($i) as $s) {
+    foreach (station_schedule_load_day($sid, $i) as $s) {
       // display blocks for unscheduled time before this program
       if ($last_finish != $s->start) {
         $cell .= theme('station_schedule_spacer', $last_finish, $s->start);
@@ -956,7 +1100,7 @@
 /**
  * Print a day's schedule page.
  */
-function station_schedule_day_page($day) {
+function station_schedule_day_page($sid, $day) {
   $day = station_valid_day($day);
   $dayname = station_day_name($day);
 
@@ -965,7 +1109,7 @@
 
   $rows = array();
   $row = array();
-  foreach (station_schedule_load_day($day) as $hour) {
+x  foreach (station_schedule_load_day($sid, $day) as $hour) {
     if ($hour->program_nid) {
       $program = node_load($hour->program_nid);
       $row['time'] = array('width' => 70,
Index: schedule/views.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/schedule/views.inc,v
retrieving revision 1.6
diff -u -r1.6 views.inc
--- schedule/views.inc	10 Feb 2007 00:11:40 -0000	1.6
+++ schedule/views.inc	22 Feb 2007 18:54:00 -0000
@@ -25,8 +25,8 @@
     ),
   );
 
-  $tables['station_schedule'] = array(
-    'name' => 'station_schedule',
+  $tables['station_schedule_item'] = array(
+    'name' => 'station_schedule_item',
     'join' => array(
       'left' => array(
         'table' => 'node',
@@ -144,10 +144,10 @@
     $query->ensure_table('node');
     $query->add_field('nid', 'node');
     if ($filterdata['operator']) {
-      $query->add_where('EXISTS (SELECT * FROM {station_schedule} WHERE {station_schedule}.program_nid = node.nid)');
+      $query->add_where('EXISTS (SELECT * FROM {station_schedule_item} WHERE {station_schedule_item}.program_nid = node.nid)');
     }
     else {
-      $query->add_where('NOT EXISTS (SELECT * FROM {station_schedule} WHERE {station_schedule}.program_nid = node.nid)');
+      $query->add_where('NOT EXISTS (SELECT * FROM {station_schedule_item} WHERE {station_schedule_item}.program_nid = node.nid)');
     }
     break;
   }
@@ -169,7 +169,7 @@
     $query->ensure_table('node');
     $query->add_field('nid', 'node');
     $minute = '***CURRENT_STATION_MINUTE***';
-    $query->add_where('EXISTS (SELECT * FROM {station_schedule} WHERE {station_schedule}.program_nid = node.nid AND {station_schedule}.start <= %d AND {station_schedule}.finish > %d)', $minute, $minute);
+    $query->add_where('EXISTS (SELECT * FROM {station_schedule_item} WHERE {station_schedule_item}.program_nid = node.nid AND {station_schedule_item}.start <= %d AND {station_schedule_item}.finish > %d)', $minute, $minute);
     break;
   }
   return;
Index: station.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/station.module,v
retrieving revision 1.27
diff -u -r1.27 station.module
--- station.module	12 Feb 2007 07:03:37 -0000	1.27
+++ station.module	22 Feb 2007 17:50:00 -0000
@@ -125,7 +125,7 @@
     // check station url
     if ($url = $edit['station_remote_schedule_url']) {
       $url = check_url($url .'/xmlrpc.php');
-      $ret = xmlrpc($url, 'station.program.get.at', time());
+      $ret = xmlrpc($url, 'station.program.get.at', time(), 0);
       if (xmlrpc_error_msg()) {
         form_set_error('station_remote_schedule_url',
           t('You must provide a a valid URL for a Drupal site with the station schedule module installed.<br />Specific error: %xmlrpc-error', array('%xmlrpc-error' => xmlrpc_error_msg()))
@@ -227,14 +227,16 @@
  *
  * @param $time
  *   A GMT timestamp.
+ * @param $sid
+ *   Schedule Id, 0 will load the default schedule.
  * @return
  *   FALSE if there was an error loading the data, NULL if nothing could be
  *   found, or, a program object if everything worked out.
  */
-function station_get_program_at($timestamp) {
+function station_get_program_at($timestamp, $sid = 0) {
   // Use the locally schedule if one is available
   if (module_exists('station_schedule')) {
-    $program = station_schedule_program_get_at($timestamp);
+    $program = station_schedule_program_get_at($timestamp, $sid);
     if ($program->nid) {
       return $program;
     }
@@ -273,7 +275,7 @@
     }
     else {
       // if it isn't cached get the program info from the server
-      $program = xmlrpc(check_url($url .'/xmlrpc.php'), 'station.program.get.at', $timestamp);
+      $program = xmlrpc(check_url($url .'/xmlrpc.php'), 'station.program.get.at', $timestamp, $sid);
       if (xmlrpc_errno()) {
         watchdog('station', t('Failed to load program info remotely. Error %code : %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg())), WATCHDOG_ERROR);
         return FALSE;
@@ -298,7 +300,7 @@
  */
 function station_block_current_program() {
   // current program
-  $program = station_get_program_at(time());
+  $program = station_get_program_at(time(), 0);
   $unschedule_message = check_plain(variable_get('station_block_unschedule',  t("We're on autopilot.")));
   $high = check_url(variable_get('station_stream_high_url', ''));
   $low = check_url(variable_get('station_stream_low_url', ''));
@@ -375,47 +377,49 @@
 }
 
 /**
- * Function to send notices of station changes via hook_station_notices. 
+ * Function to send notices of station changes via hook_station_notices.
  */
 function _station_send_notice($type, $op, $data) {
   module_invoke_all('station_notice', $type, $op, $data);
 }
 
 /**
- * Implementation of hook_station_notices to display simple notices about 
+ * Implementation of hook_station_notices to display simple notices about
  * station changes.
- * @param $type 'dj' or 'schedule'
- * @param $op 'add', 'remove', 'change' - Change only applies to schedule 
- * 		items.
- * @param $data associative array with details like: nid, sid, uid.
+ * @param $type
+ *  'dj' or 'schedule'
+ * @param $op
+ *  'add', 'remove', 'change' - Change only applies to schedule items.
+ * @param $data
+ *  associative array with details like: nid, sid, uid.
  */
 /*
 function station_station_notice($type, $op, $data) {
   if ($type == 'dj') {
     $uid = $data['uid'];
     $nid = $data['nid'];
-	switch ($op) {
-	  case 'add':
-	    drupal_set_message("adding $uid to $nid");
-	    break;
-	  case 'remove':
-	    drupal_set_message("removing $uid from $nid");
-	    break;
-	}
+    switch ($op) {
+      case 'add':
+        drupal_set_message("adding $uid to $nid");
+        break;
+      case 'remove':
+        drupal_set_message("removing $uid from $nid");
+        break;
+    }
   } elseif ($type == 'schedule') {
     $sid = $data['sid'];
     $nid = $data['nid'];
     switch ($op) {
-	  case 'add':
-	    drupal_set_message("adding $sid to $nid");
-	    break;
-	  case 'change':
-	    drupal_set_message("changing $sid on $nid");
-	    break;
-	  case 'remove':
-	    drupal_set_message("removing $sid from $nid");
-	    break;
-	}
+      case 'add':
+        drupal_set_message("adding $sid to $nid");
+        break;
+      case 'change':
+        drupal_set_message("changing $sid on $nid");
+        break;
+      case 'remove':
+        drupal_set_message("removing $sid from $nid");
+        break;
+    }
   }
 }
 */
\ No newline at end of file

