/* Is used for event creation. */
function extractDateTime(rowID) {
    [_, dayCol, hour] = rowID.match("day-(\\d+)-hour-(\\d+)");

    var day = new Date(displayed_week_start);
    day.setHours(24 * dayCol);

    // Return the time precise on the hour.
    return new Date(day.getFullYear(), day.getMonth(),
                    day.getDate(), hour, 0, 0);
}

function updateTime()
{
    var currentTime = new Date();
    var day = mod(currentTime.getDay() - 1, 7);

    var current_day = $("#day-current");
    if (current_day)
    {
        current_day.id = "";
    }

    current_day = $("#week .day-column")[day];
    current_day.id = "day-current";

    if (!withinDisplayedWeek(currentTime))
    {
        $("#time-indicator").remove();
        return;
    }

    $("#time-indicator").remove();
    var indicator = document.createElement("span");
    indicator.id = "time-indicator";

    $("#day-current").append(indicator)

    var ppm = ($("#day-current .hour-row").height() + 1) / 60;
    var minutes = 60 * currentTime.getHours() + currentTime.getMinutes();

    $("#time-indicator")[0].style.top = (minutes * ppm) + "px";
}

function fixDates()
{
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    var week_number = displayed_week_start.getWeekNumber();
    var month_number = displayed_week_start.getMonth();
    var year_number = displayed_week_start.getFullYear();

    if (view == "day")
    {
        var month_number = displayed_day_start.getMonth();
        var year_number = displayed_day_start.getFullYear();
    }

    $(".weekNumber").text("Week: " + week_number);
    $(".date").text(months[month_number] + " " + year_number);

    showCurrentDay();
    updateTime();
    var date_nos = $(".date-no");

    for (i = 0; i < date_nos.length; ++i)
    {
        date_no = date_nos[i];
        date_no.innerHTML = getRelDay(i).getDate();
    }
}

function showCurrentDay()
{
    var currentTime = new Date();
    var day = mod((currentTime.getDay() - 1), 7);

    /* If the displayed week start is more than currentTime,
     * or more than displayed_week_start + 1 week don't make
     * any days blue. */
    var current_color = blue_color;
    if (!withinDisplayedWeek(currentTime))
    {
        current_color = black_color;
    }

    var week_days = document.getElementsByClassName("day-column");
    var day_name = document.getElementsByClassName("day-name");
    for (var i = 0; i < day_name.length; i++)
    {
        week_days[i].style.backgroundColor = "white";
        day_name[i].style.color = black_color;
    }
    day_name[day].style.color = current_color;
}
