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++) {
        $("<td/>").append($("<div/>").text(formatLongTime(schedule[i].start))).appendTo(row);
    }
    var newAlt = "Click to listen to " + stations[slug].name + ".";
    $("#listen_button").attr("alt", newAlt).unbind("click").bind("click", function() { 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 a = $("<a/>").attr("href", stations[slug].alternateURLs[key]).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
    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.
    $(".current_info").fadeTo(animateTime / 2, .01, function() {
            if (this == $("#left_time")[0]) {
                if (displayShowIndex > 0) {
                    leftText = "&laquo;" + formatTime(schedule[displayShowIndex - 1]["start"]);
                } else {
                    leftText = "&nbsp;";
                }
                $("#left_time").html(leftText);
            } else if (this == $("#right_time")[0]) {
                if (displayShowIndex < schedule.length - 1) {
                    rightText = formatTime(schedule[displayShowIndex + 1]["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 formatTime(dt) {
    var hours = dt.getHours();
    var minutes = dt.getMinutes();
    var amPm = "AM";
    if (hours >= 12) {
        if (hours > 12) {
            hours -= 12;
        }
        amPm = "PM";
    } else if (0 == hours) {
        hours = 12;
    }
    minutes = minutes < 10 ? "0" + minutes : minutes;
    return hours + ":" + minutes + amPm;
}

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();
        $("#listen_options").css({top: (position.top + 22) + "px", left: (position.left + 40) + "px"});
        $("#listen_options").show();
    } else {
        $("#listen_options").hide();
    }
}

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