// clock.js
//
// clock(true)  prints HTML code that shows Eastern (USA) time.
// clock(false) does the same for Moscow time.
//
// The code reflects the "new" US DST as per Energy Policy Act
// which affects 2007.
//
//  -- Dmitriy Mayorov http://www.dimview.org
//

// Return the time when daylight saving begins or ends 
function ds_time(y, m, h, which) { 
  var x, step;
  if (which == 1) {             // To get 1st Sunday
    x = new Date(y, m, 1);      // start with the 1st of the month
    step = 86400000;            // and go forward in one day increments
  } else if (which == 2) {      // To get 2nd Sunday
    x = new Date(y, m, 8);      // start with the 8st of the month
    step = 86400000;            // and go forward in one day increments
  } else {                      // To get last Sunday
    x = new Date(y, m + 1, 0);  // start with the last day of the month
    step = -86400000;           // and go back in one day increments
  }
  while (x.getDay() !== 0) { // until it's Sunday
    x.setTime(x.getTime() + step);
  }
  x.setUTCHours(h); // do this last because it might change day
  return x; 
} 

// Return true if daylight saving is in effect
function is_ds(u, ny) {
  var dsstart, dsend;
  if (ny) { // US Eastern time
    // 2AM local time on second Sunday in March
    dsstart = ds_time(u.getUTCFullYear(), 2, 2 + 5, 2).getTime();
    // 2AM local time on first Sunday in November
    dsend   = ds_time(u.getUTCFullYear(), 10, 2 + 5, 1).getTime();
  } else { // Moscow time
    return true;
  }
  if ((u.getTime() > dsstart) && 
      (u.getTime() < dsend)) {
    return true;
  } else {
    return false;
  } 
} 

function leading_zero(x) { 
  return (x > 9) ? x : '0' + x; 
} 

function get_time_string(ny) {
  var x = new Date();  // x is initialized to the current time (UTC)
  var dst = is_ds(x, ny);
  var m = x.getUTCHours();
  // Adjust for daylight saving and time zone
  x.setTime(x.getTime() + (dst ? 3600000 : 0) + (ny ? -18000000 : 10800000)); 
  var z;
  if (dst) {
    z = ny ? 'EDT' : 'MSK';
  } else {
    z = ny ? 'EST' : 'MSK';
  }
  // Note that now getUTCHours() returns local time rather than UTC
  return leading_zero(x.getUTCHours()) + ':' + 
         leading_zero(x.getUTCMinutes()) + '&nbsp;' + z;
}

function set_dhtml_elem(id, s) {
  if (document.all) { 
    // IE
    if (document.all(id).innerHTML != s) {
      document.all(id).innerHTML = s;
    }
  } else if (document.getElementById) {
    // Netscape/Mozilla
    if (document.getElementById(id).innerHTML != s) {
      document.getElementById(id).innerHTML = s;
    }
  }
}

function update_time(ny) {
  if (ny) {
    set_dhtml_elem('tPny', get_time_string(true)); 
  } else {
    set_dhtml_elem('tPspb', get_time_string(false)); 
  }
  setTimeout('update_time('+ ny + ')', 1000); 
}

function clock(ny) {
  document.write('<SPAN id="tP' + (ny ? 'ny' : 'spb') + '">' + get_time_string(ny) + '</SPAN>');
  update_time(ny);
} 

