// Functions to create digital and analog clocks
// For GMT and local time
// Based on the computer system's clock setting
//
// Javascript by doug <sweetser@alum.mit.edu>

// Functions: minHand, localTime(), GMT(),
// showDigitalClock(hour, min), showAnalogClock(hour, min),
// showLocalDigitalClock(), showGMTDigitalClock()
// showLocalAnalogClock(), showGMTAnalogClock()
// showLocalGivenGMT(hour, min)

// Returns the closest hand
// so minHand(49) = 10
function minHand(m)
{
    var minOnes =  m % 5;
    var minFives = (m - minOnes) / 5;

    if (minOnes > 2) return minFives + 1;
    
    else return minFives;
}

// local time values
function localTime()
{
  nowLocal = new Date();    

  // Correct for bug in Macs
  var base = new Date(0);
  var macBug = base.getTime();    // should be 0
  if (macBug > 0)
    nowLocal.setTime(nowLocal.getTime() - macBug);

  // Global local time values
  yearLocal  = nowLocal.getYear();
  if (yearLocal > 1000)  yearLocal -= 2000;  	// a year 2100 bug
  monthLocal = nowLocal.getMonth();
  dayLocal   = nowLocal.getDay();
  hourLocal  = nowLocal.getHours();
  minLocal   = nowLocal.getMinutes();
}

/* Calculate GMT
   Note: Avoid function getTimeZoneOffset
   According to the Javascript Bible, p 590
   Navigator 2.0, Navigator 3.0 on the Mac and IE 3.0
   mess up the time zone offset for All time zones east of GMT.
   That is the folks in India, so you'll never see it.
*/
function GMT()
{
  // Global GMT time values
  nowGMT = new Date(Date.UTC(yearLocal, monthLocal, dayLocal, hourLocal, minLocal));
  hourGMT = (hourLocal + (hourLocal - nowGMT.getHours())) % 24;
  if (hourGMT < 0) hourGMT += 24;
  minGMT  = (minLocal  + (minLocal - nowGMT.getMinutes())) % 60;
  if (minGMT < 0) minGMT += 60;

  // IE 2.0 does not do daylight savings time
  if ((parseInt(navigator.appName) < 3))
  {
    if ((navigator.appName.indexOf("Microsoft") != -1))
	{
	  // Daylight savings runs approximately from
	  // the end of October (9) to the end of March (2)
	  if((monthLocal > 3) && (monthLocal < 10))
	  {
	    hourGMT -= 1;
	  }
	}
  }
  
  // test to see GMT
  // document.write('GMT '+nowGMT+'<BR><BR>');
  // document.write('GMT '+hourGMT+'<BR><BR>');
}

// A general digital clock
function showDigitalClock(hour, min)
{
  // AM vs PM
  if (hour < 12)
  {
    if (min < 10)
    {
      document.write(hour + ":0" + min + " AM");
    }
    else
    {
      document.write(hour + ":" + min + " AM");
    }
  }
  else
  {
    if (hour > 12){hour -= 12;}
    if (min < 10)
    {
      document.write(hour + ":0" + min + " PM");
    }
    else
    {
      document.write(hour + ":" + min + " PM");
    }
  }
}

// Get gif of the clock
// 144 gifs must be named "c" + 0-143
// and stored in a file called "clocks"
// MUST document.write the image to get it to work in IE
function showAnalogClock(hour, min)
{
  if (document.images)     	// works for later versions of javascript
  {
    var posInArray = (hour % 12) * 12 + minHand(min);

    var imageLocalClock = new Image(51, 51);
    var clockName = "clocks/c" + posInArray + ".gif";

    document.write('<IMG SRC='+clockName+' BORDER=0 HEIGHT=51 WIDTH=51 ALIGN=LEFT>');
  }
}

// Call for just the right clock (there are 4 types)
function showLocalDigitalClock()
{
  localTime();
  showDigitalClock(hourLocal, minLocal);
}

function showGMTDigitalClock()
{
  GMT();
  showDigitalClock(hourGMT, minGMT);
}

function showLocalAnalogClock()
{
  localTime();
  showAnalogClock(hourLocal, minLocal);
}

function showGMTAnalogClock()
{
  GMT();
  showAnalogClock(hourGMT, minGMT);
}

// Make a digital local clock based on a given GMT time
function showLocalGivenGMT(hourGivenGMT, minGivenGMT)
{
  localTime();
  GMT();
  
  // Calculate offset
  var hourOffset = (hourGMT - hourLocal) % 24;
  if (hourOffset < 0) hourOffset += 24;
  var hour = (hourGivenGMT - hourOffset) % 24;
  if (hour < 0) hour += 24;
  var minOffset = (minGMT - minLocal) % 60;
  if (minOffset < 0) minOffset += 60;
  var min = (minGivenGMT - minOffset) % 60;
  if (min < 0) min += 60;

  showDigitalClock(hour, min);
  
  // test
  /*
  document.write("hourLocal is " + hourLocal + "<BR>");
  document.write("minLocal is  " + minLocal + "<BR><BR>");

  document.write("hourGMT is " + hourGMT + "<BR>");
  document.write("minGMT is  " + minGMT + "<BR><BR>");

  document.write("hourOffset is " + hourOffset + "<BR>");
  document.write("minOffset is  " + minOffset + "<BR><BR>");
  */
}

// Test code
// SURROUND with  /* ... */  to HIDE!!!
/*
localTime();
GMT();

document.write("minHand(49) returns " + minHand(49) + "\n");
document.write("minHand(44) returns " + minHand(44) + "\n\n");

document.write("hourLocal is " + hourLocal + "\n");
document.write("minLocal is  " + minLocal + "\n\n");

document.write("hourGMT is " + hourGMT + "\n");
document.write("minGMT is  " + minGMT + "\n\n");
*/
// end of test code