function whatsOnNowInitSchedule() {
  currentStationSlug = serverCurrentStationSlug;
  onShowUpdate = whatsOnNowShowUpdate;
  onStationUpdate = whatsOnNowStationUpdate;
  initSchedule();
  whatsOnNowStationUpdate(currentStationSlug);
  setupScheduleShowUpdate();
}

function whatsOnNowStationUpdate(slug) { // see onStationUpdate
  var schedule = stations[slug].schedule;
  var row = $("#slide_row");
  row.empty();
  for (var i = 0; i < schedule.length; i++) {
    var div = $("<div/>").text(formatLongTime(schedule[i].start));
    $("<td/>").append(div).appendTo(row);
  }
  var newAlt = "Click to listen to " + stations[slug].name + ".";
  var listen_button = $("#listen_button").attr("alt", newAlt).unbind("click");
  listen_button.click(function(event) {
    event.preventDefault();
    openStation(slug);
  });
  var list = $("#listen_options");
  list.empty();
  var foundAny = false;
  for (var key in stations[slug].alternateURLs) {
    var newRel = "Listen to " + stations[slug].name + " in " + key + " format.";
    var newHref = stations[slug].alternateURLs[key];
    var a = $("<a/>").attr("href", newHref).attr("rel", newRel);
    a.attr("target", "_blank").html(key);
    $("<li/>").append(a).appendTo(list);
    foundAny = true;
  }
  if (foundAny) {
    $("#listen_options_control").show();
  } else {
    $("#listen_options_control").hide();
  }
  closeListenOptions();
}

var displayShowIndex;
function whatsOnNowShowUpdate(i, show) { // see setupScheduleShowUpdate
  if (show) {
    displayShowIndex = i;
    focusOnIndex(i);
  }
}

function focusOnIndex(newIndex) {
  displayShowIndex = newIndex;

  var schedule = stations[currentStationSlug].schedule;

  var targetLeft = -1;
  for (var i = 0; i < displayShowIndex; i++) {
    targetLeft -= $($("#slide_contents td")[displayShowIndex]).outerWidth();
  }
  $("#slide_contents").animate({"left": targetLeft}, animateTime);
  // don't fade all the way out, because then the design collapses for one brief
  // moment before the fade back in. Several elements have .current_info, and
  // each runs this function separately, hence the if statements.
  $(".current_info").fadeTo(animateTime / 2, .01, function() {
    if (this == $("#left_time")[0]) {
      if (displayShowIndex > 0) {
        var start = schedule[displayShowIndex - 1]["start"];
        leftText = "&laquo;" + formatTime(start);
      } else {
        leftText = "&nbsp;";
      }
      $("#left_time").html(leftText);
    } else if (this == $("#right_time")[0]) {
      if (displayShowIndex < schedule.length - 1) {
        var start = schedule[displayShowIndex + 1]["start"];
        rightText = formatTime(start) + "&raquo;";
      } else {
        rightText = "&nbsp;";
      }
      $("#right_time").html(rightText);
    } else if (this == $("#current_show")[0]) {
      var show = displayShowIndex >= 0 ? schedule[displayShowIndex] : null;
      setShowHtml(show);
      if (show && show.url) {
        // Does the show allow comments? That's a complicated thing to figure
        // out. Luckily, producers HATE seeing Comment(0) because it's
        // embarrassing. So now we just show Comment(<count>) if there are
        // comments. Piece of cake.
        var linkText = show.comments ?
            "Comment(" + show.comments + ")" : "Program Details";
        $("#comments").attr("href", show.url).text(linkText);
        $("#comments").show();
      } else {
        $("#comments").hide();
      }
    }
    $(this).fadeTo(animateTime / 2, 1);
  });
}

function shiftSlidingSchedule(direction) {
  var newI = direction + displayShowIndex;
  if (newI >= 0 && newI < stations[currentStationSlug].schedule.length) {
    focusOnIndex(newI);
  }
}

function formatLongTime(dt) {
  return formatTime(dt) + " - " +
      ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"][dt.getDay()] +  ", " +
      ["JAN", "FEB", "MAR", "APR", "MAY", "JUNE",
          "JULY", "AUG", "SEPT", "OCT", "NOV", "DEC"][dt.getMonth()] +
      " " + dt.getDate();
}

var listen_options_visible = false;
function toggleListenOptions() {
  listen_options_visible = !listen_options_visible;
  if (listen_options_visible) {
    var position = $("#listen_options_control").position();
    var top = (position.top + 22) + "px";
    var left = (position.left + 40) + "px";
    $("#listen_options").css({top: top, left: left});
    $("#listen_options").show();
  } else {
    $("#listen_options").hide();
  }
}

function closeListenOptions() {
  listen_options_visible = false;
  $("#listen_options").hide();
}
