//
//
//
//

var time_difference;
// US Central Time
var timezone = -6;

// Assume daylight savings time calculations are "simple" ..
var dst_start_month = 2; // March
var dst_start_week = 1;  // Second Sunday
var dst_end_month = 10;  // November
var dst_end_week = 0;    // First Sunday

function set_server_time(s_time)
{
    var client_time = new Date();

    time_difference = (s_time * 1000) - client_time.getTime();
}

function get_server_time()
{
    var client_time = new Date();
    var server_time = new Date(client_time.getTime() + time_difference)

    // Do daylight savings time update at 5am (non-dst) local
    // time to avoid screwing with the time while we're open.
    // The time returned will have the getUTC...() methods return time
    // corrected to local time.

    var dst_hour = 5 - timezone;
    var tz_offset = timezone * 60 * 60 * 1000;

    var cur_month = server_time.getUTCMonth();
    var cur_day = server_time.getUTCDate();
    var cur_weekday = server_time.getUTCDay();
    var cur_hour = server_time.getUTCHours();
        
    var isDST = 0;
    var first_sunday = (cur_day - cur_weekday) % 7;

    if (first_sunday < 1) {
        first_sunday += 7;
    }

    var start_sunday = first_sunday + (dst_start_week * 7);
    var end_sunday = first_sunday + (dst_end_week * 7);

    if (((dst_start_month < dst_end_month) &&
              ((cur_month > dst_start_month) && (cur_month < dst_end_month))) ||
        ((dst_start_month > dst_end_month) && 
              ((cur_month > dst_start_month) || (cur_month < dst_end_month)))) {
        isDST = 1;
    }
    if ((cur_month == dst_start_month) &&
        ((cur_day > start_sunday) ||
         ((cur_day == start_sunday) && (cur_hour > dst_hour)))) {
        isDST = 1;
    }
    if ((cur_month == dst_end_month) &&
        ((cur_day < end_sunday) ||
         ((cur_day == end_sunday) && (cur_hour < dst_hour)))) {
        isDST = 1;
    }

    if (isDST) {
        tz_offset += 3600000;
    }

    return new Date(server_time.getTime() + tz_offset);
}


function initialize_status(server_time)
{
    set_server_time(server_time);
    change_status();
}


function change_status()
{
    calculate_day();

    var img = document.getElementById('open_sign');
    img.src = 'time_' + day_part + '.png';
}

