////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
//
//  Nov 2009:
//  File changed by Pauliewog for Product 7 RED Extreme Clock.
//
////////////////////////////////////////////////////////////////////////////////


var clockThemes                = new Array("1r4","r", "r1", "r2","r3");
var clockHighlights            = new Array("1r4","r", "r1", "r2","r3");
var bounceAngle                = 2;
var clockPartsHeight;
var clockPartsTop;
var clockPartsLeft; 
var date;
var displayedDate;
var displayedDay;
var displayedWeek;
var timerIDAnimateDate;
var timerIDAnimateDay;
var timerIDAnimateDigitalTime;
var timerIDAnimateHours;
var timerIDAnimateMinutes;
var timerIDAnimateSeconds;
var timerIDBounceBack;
var timerIDStartAnimation;
var timerIDUpdateClock;
var timerIDUpdateSeconds;
var angleSeconds;
var angleHours;
var angleMinutes;
var animatingHours        = false;
var animatingMinutes      = false;
var animatingSeconds      = false;
var animatingDay          = false;
var animatingDate         = false;
var animatingDigitalTime  = false;
var hours;
var minutes;
var seconds;
var uDate;
var uDay;
var uWeek;

//------------------------------------------------------------------------------

Date.prototype.getWeek = function() {
  var onejan = new Date(this.getFullYear(), 0, 1);

  return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()) / 7);
} 

//------------------------------------------------------------------------------

function loadMain()
{

  settingsChanged();

  System.Gadget.settingsUI        = "settings.html";
  System.Gadget.onSettingsClosed  = settingsClosed;
  
  System.Gadget.visibilityChanged = visibilityChanged;
  
  h.addShadow("grey", 2, 40, 2, 2);
  m.addShadow("grey", 2, 40, 2, 2);
  s.addShadow("grey", 2, 40, 2, 2);
}

//------------------------------------------------------------------------------

function setImages()
{
  var curTheme = clockThemes[mySetting.themeID];
  
  clockBg.src = "url(images/" + curTheme + ".png)";
  
  highlights.src = "images/" + clockHighlights[mySetting.themeID] + "_highlights.png";
  
  h.src   = "images/" + curTheme + "_h.png";
  m.src   = "images/" + curTheme + "_m.png";
  s.src   = "images/" + curTheme + "_s.png";
  dot.src = "images/" + curTheme + "_dot.png";
}


//------------------------------------------------------------------------------

function setGUIDate(aNewDate) {

  displayedDate         = aNewDate;
  dateDisplay.innerText = displayedDate;

}

//------------------------------------------------------------------------------

function setGUIDay(aNewDay) {

  displayedDay         = aNewDay;
  dayDisplay.innerText = L_localized_dayStrings[displayedDay];

}

//------------------------------------------------------------------------------

function setGUIWeek(aNewWeek) {

  displayedWeek       = aNewWeek;
  clockName.innerText = 'Week ' + displayedWeek;

}

//------------------------------------------------------------------------------

function dblclickHandler(aObject, aEvent) {

  // Select next theme if ctrl key was pressed on click.
  if (aEvent.ctrlKey) {
    selectNextTheme();
  }
  else {
    startClock();
  }
  
}

//------------------------------------------------------------------------------

function selectNextTheme() {

  if (mySetting.themeID >= clockThemes.length) {
    mySetting.themeID = 1;
  }
  else {
    mySetting.themeID++;
  }
  
  mySetting.save();
  
  settingsChanged();
  
}

//------------------------------------------------------------------------------

function clearAllTimers() {

  clearTimeout(timerIDBounceBack);
  clearTimeout(timerIDStartAnimation);
  clearTimeout(timerIDUpdateClock);
  clearTimeout(timerIDUpdateSeconds);
  clearTimeout(timerIDAnimateHours);
  clearTimeout(timerIDAnimateMinutes);
  clearTimeout(timerIDAnimateSeconds);
  clearTimeout(timerIDAnimateDate);
  clearTimeout(timerIDAnimateDay);
  
  // Timer handles need to set to null to allow checking if a timer is running.
  timerIDBounceBack     = null;
  timerIDStartAnimation = null;
  timerIDUpdateClock    = null;
  timerIDUpdateSeconds  = null; 
  timerIDAnimateHours   = null;
  timerIDAnimateMinutes = null;
  timerIDAnimateSeconds = null;
  timerIDAnimateDate    = null;
  timerIDAnimateDay     = null;
  
}

//------------------------------------------------------------------------------

function startClock() {

  // Start animation
  clearAllTimers();

  animatingSeconds      = true;
  animatingHours        = true;
  animatingMinutes      = true;
  animatingDay          = true;
  animatingDate         = true;
  animatingDigitalTime  = true;

  // Move all hands and displays to zero position
  setClockToZero();

  // Wait one second before starting animation
  timerIDStartAnimation = setTimeout("startAnimation()", 1000);

  // Start clock
  updateClock();
  updateClockSeconds();
  
}

//------------------------------------------------------------------------------

function startAnimation() {

  AnimateMinutes(0);
  AnimateHours(360);

  if (mySetting.showDigital)
  {
    AnimateDigitalTime(0, 0, 0);
  }

  if (mySetting.secondsEnabled)
  {
    AnimateSeconds(0);
  }

  if (mySetting.showDate)
  {
    AnimateDate(1, 0);
  }

  if (mySetting.showDay)
  {
    AnimateDay(0, 0);
  }

}

//------------------------------------------------------------------------------

function setClockToZero() {

  s.Rotation            = 0;
  h.Rotation            = 0;
  m.Rotation            = 0;

  dateDisplay.style.top = 0;
  setGUIDate(1);

  dayDisplay.style.top  = 0;
  setGUIDay(0);

  updateDigitalTime(0, 0, 0);

}

//------------------------------------------------------------------------------

function updateClock()
{
  if (mySetting.timeZoneIndex != -1 && getValidTimeZone(mySetting.timeZoneIndex) != -1)
  {
    date = new Date(System.Time.getLocalTime(zones.item(mySetting.timeZoneIndex)));
  }
  else
  {
    date = new Date(System.Time.getLocalTime(System.Time.currentTimeZone));
  }

  hours   = date.getHours();
  minutes = date.getMinutes();
  seconds = date.getSeconds();
  uDate   = date.getDate();
  uDay    = date.getDay();
  uWeek   = date.getWeek();

  angleHours   = (hours * 30 + (minutes / 2)) % 360;
  angleMinutes = (minutes * 6) + (seconds / 10);

  if (!animatingHours) 
  {
    h.Rotation = angleHours;
  }

  if (!animatingMinutes) 
  {
    m.Rotation = angleMinutes;
  }        

  updateDateAndDay();

  if (mySetting.showDigital && 
      !animatingDigitalTime) 
  {
    updateDigitalTime(hours, minutes, seconds);
  }

  // Set new timeout. Calculate time until next minute
  var lSecondOffset = 60 - seconds;

  if (lSecondOffset < 5)
  {
    timerIDUpdateClock = setTimeout("updateClock()", lSecondOffset * 1000);
  }
  else
  {
    // Update Once per 5 seconds
    timerIDUpdateClock = setTimeout("updateClock()", 5000);
  }

  
}

//------------------------------------------------------------------------------

function updateClockSeconds()
{  
   
  // Set new timeout
  if (mySetting.secondsEnabled || 
     (mySetting.showDigital && mySetting.secondsEnabledDigital)) {

     
    // System date does not return milliseconds, so we use the local date function
    // We cannot use minutes and hours here because they may be different from the local time.
    var lDate         = new Date();
    seconds           = lDate.getSeconds();
    var lMilliSeconds = lDate.getMilliseconds();
    
    angleSeconds  = seconds * 6;

    if (mySetting.showDigital && 
        !animatingDigitalTime) 
    {
      updateDigitalTime(hours, minutes, seconds);
    }

    // Update seconds hand
    if (mySetting.secondsEnabled && 
       (! animatingSeconds))
    {

      if (mySetting.smoothSeconds ) 
      {
        s.Rotation = angleSeconds + (6 * lMilliSeconds / 1000);
      }
      else
      {
        s.Rotation = angleSeconds + bounceAngle;
        timerIDBounceBack = setTimeout("BounceSecondBack()", 50);
      }
      
    }
    
    if (mySetting.smoothSeconds) 
    {
      // Update seconds hand 10 times per second
      timerIDUpdateSeconds = setTimeout("updateClockSeconds()", 100);
    }
    else
    {

      var lSecondOffset = 1000 - lMilliSeconds;
  
      if (lSecondOffset > 0)
      {
        timerIDUpdateSeconds = setTimeout("updateClockSeconds()", lSecondOffset);
      }
      else
      {
        updateClockSeconds();
      }
    }
          
  }
  
}

//------------------------------------------------------------------------------

function visibilityChanged()
{

  if (System.Gadget.visible)
  {
  
    if (!timerIDUpdateClock)
    {
      updateClock();
      updateClockSeconds();
    }
    
  }
  else
  { 
    // Stop clock
    clearTimeout(timerIDUpdateClock);
    timerIDUpdateClock = null;
    clearTimeout(timerIDUpdateSeconds);
    timerIDUpdateSeconds = null;
  }
  
}

//------------------------------------------------------------------------------

function updateDigitalTime(aHours, aMinutes, aSeconds)
{

  var digitalHours   = aHours;
  var digitalMinutes = aMinutes;
  var digitalSeconds = "";
  var AMPM           = "";         

  digitalMinutes = (digitalMinutes < 10 ? "0" : "") + digitalMinutes;    

  if (mySetting.secondsEnabledDigital)
  {
    digitalSeconds = aSeconds;
    digitalSeconds = (digitalSeconds < 10 ? ":0" : ":") + digitalSeconds;
  }

  if (mySetting.show24)
  {
      digitalHours = (digitalHours < 10 ? "0" : "") + digitalHours;
  }
  else
  {
      AMPM = (digitalHours < 12) ? " AM" : " PM";
      digitalHours = (digitalHours > 12) ? digitalHours - 12 : digitalHours;
      digitalHours = (digitalHours == 0) ? 12 : digitalHours;
  }

  digitalTime.innerText = digitalHours + ":" + digitalMinutes + digitalSeconds + AMPM;

}

//------------------------------------------------------------------------------

function animateNewDate(aDate, aTop) {

  animatingDate = true;

  var lNewTop  = aTop;
  var lNewDate = aDate;

  dateDisplay.style.top = aTop + "px";
  setGUIDate(aDate);
 

  if (aDate != uDate) {    
  
    lNewTop++;
   
    // Select new date when old one has gone. Scroll to top.
    if (lNewTop > 10) {
      lNewTop = -10;
      lNewDate = uDate;
    }

    timerIDAnimateDate = setTimeout("animateNewDate(" + lNewDate + "," + lNewTop + ")", 100);
  }
  else {
  
    // Finish scrolling until the date is centered
    if (aTop < 0) {
    
      lNewTop++;    
      
      lTimeout = 300 + 20 * lNewTop;      

      timerIDAnimateDate = setTimeout("animateNewDate(" + aDate + "," + lNewTop + ")", lTimeout);
      
    }
    else {
      animatingDate = false;
    }
    
  }

}

//------------------------------------------------------------------------------

function animateNewDay(aDay, aTop) {

  animatingDay = true;

  var lNewTop = aTop;
  var lNewDay = aDay;

  setGUIDay(aDay);
  
  dayDisplay.style.top = aTop + "px";

  if (aDay != uDay) {    
  
    lNewTop++;
   
    // Select new day when old one has gone. Scroll to top.
    if (lNewTop > 10) {
      lNewTop = -10;
      lNewDay = uDay;
    }

    timerIDAnimateDay = setTimeout("animateNewDay(" + lNewDay + "," + lNewTop + ")", 200);
  }
  else {
  
    // Finish scrolling until the day is centered
    if (aTop < 0) {
    
      lNewTop++;    
      
      lTimeout = 500 + 30 * lNewTop;      
      
      timerIDAnimateDay = setTimeout("animateNewDay(" + aDay + "," + lNewTop + ")", lTimeout);
      
    }
    else {
      animatingDay = false;
    }
    
  }

}

//------------------------------------------------------------------------------

function updateDateAndDay()
{
  
  // Only update date if:
  // - Show date option = on      AND
  // - Not animating              AND
  // - Date has actually changed

  if (mySetting.showDate && 
      !animatingDate     && 
      displayedDate != uDate) 
  {
    animateNewDate(displayedDate, 0);
  }
  
  if (mySetting.showDay && 
      !animatingDay     && 
      displayedDay != uDay) 
  {
    animateNewDay(displayedDay, 0);
  }

  if (mySetting.showWeek && 
      displayedWeek != uWeek) 
  {
    setGUIWeek(uWeek);
  }
    
}

//------------------------------------------------------------------------------

function settingsClosed(event)
{
  if (event.closeAction == event.Action.commit)
  {
      settingsChanged();
  }
}

//------------------------------------------------------------------------------

function settingsChanged()
{

  mySetting.load();
  
  if (mySetting.timeZoneIndex != -1 && zonesCount == 0)
  {
    updateTimeZones();
  }
  
  // Set default values
  var topPX  = 0; // JD Note: These variables should go
  var leftPX = 0;
  
  var clockFont                  = "Segoe UI, Arial Narrow, Tahoma, Sans-Serif";
  var clockFontColor             = "#000000";
  var clockNameFontSize          = 12;   // Font sizes are and need to be in pixels! Otherwise fonts would be scaled up on desktops with high dpi settings.
  var clockDigitalFontSize       = 13;
  var clockDateFontSize          = 11;
  var clockDateFontWeight        = "normal";
  var midClockNameFontHeight     = 8;
  var maxClockNameWidth          = 60;
  var maxDigitalTimeWidth        = 60;
  var posClockNameTop            = 30;
  var posDateDisplayTop          = 54; 
  var posDateDisplayLeft         = 80;
  var posDayDisplayTop           = 54;
  var posDayDisplayLeft          = 27;
  var posDigitalTimeTop          = 76;

  
  var curTheme = clockThemes[getValidThemeID(mySetting.themeID)];
  
  switch (curTheme)
  {
    case "1r4":
      clockFontColor       = "#ffffff";
      topPX               += 3;
      leftPX              += 1;
      maxClockNameWidth    = 72;
      maxDigitalTimeWidth  = 72;
      clockPartsWidth      = 13;
      clockPartsHeight     = 129;
      clockPartsTop        = 0;
      clockPartsLeft       = 58;
      posClockNameTop      = 30;
      posDayDisplayTop     = 74;
      posDayDisplayLeft    = 27;
      posDateDisplayTop    = 74;
      posDateDisplayLeft   = 80;
      posDigitalTimeTop    = 86;
      dateBorderColor      = "red";
      break;
    case "r":
      clockFontColor       = "#d20000";
      topPX               += 3;
      leftPX              += 1;
      maxClockNameWidth    = 72;
      maxDigitalTimeWidth  = 72;
      clockPartsWidth      = 13;
      clockPartsHeight     = 129;
      clockPartsTop        = 0;
      clockPartsLeft       = 58;
      posClockNameTop      = 30;
      posDayDisplayTop     = 72;
      posDayDisplayLeft    = 27;
      posDateDisplayTop    = 72;
      posDateDisplayLeft   = 80;
      posDigitalTimeTop    = 85;
      dateBorderColor      = "red";
      break;
    case "r1":
      clockFontColor       = "#000000";
      topPX               += 3;
      leftPX              += 1;
      maxClockNameWidth    = 72;
      maxDigitalTimeWidth  = 72;
      clockPartsWidth      = 13;
      clockPartsHeight     = 129;
      clockPartsTop        = 0;
      clockPartsLeft       = 58;
      posClockNameTop      = 30;
      posDayDisplayTop     = 72;
      posDayDisplayLeft    = 27;
      posDateDisplayTop    = 72;
      posDateDisplayLeft   = 80;
      posDigitalTimeTop    = 85;
      dateBorderColor      = "black";
      break;
    case "r2":
      clockFontColor       = "#000000";
      topPX               += 3;
      leftPX              += 1;
      maxClockNameWidth    = 72;
      maxDigitalTimeWidth  = 72;
      clockPartsWidth      = 13;
      clockPartsHeight     = 129;
      clockPartsTop        = 0;
      clockPartsLeft       = 58;
      posClockNameTop      = 30;
      posDayDisplayTop     = 74;
      posDayDisplayLeft    = 27;
      posDateDisplayTop    = 74;
      posDateDisplayLeft   = 80;
      posDigitalTimeTop    = 86;
      dateBorderColor      = "black";
      break;
    case "r3":
      clockFontColor       = "#ffffff";
      topPX               += 3;
      leftPX              += 1;
      maxClockNameWidth    = 72;
      maxDigitalTimeWidth  = 72;
      clockPartsWidth      = 13;
      clockPartsHeight     = 129;
      clockPartsTop        = 0;
      clockPartsLeft       = 58;
      posClockNameTop      = 30;
      posDayDisplayTop     = 74;
      posDayDisplayLeft    = 27;
      posDateDisplayTop    = 74;
      posDateDisplayLeft   = 80;
      posDigitalTimeTop    = 86;
      dateBorderColor      = "red";
      break;

  }
  
  dot.style.width  = h.style.width  = m.style.width  = s.style.width  = clockPartsWidth;
  dot.style.height = h.style.height = m.style.height = s.style.height = clockPartsHeight;
  dot.style.top    = h.style.top    = m.style.top    = s.style.top    = clockPartsTop;
  dot.style.left   = h.style.left   = m.style.left   = s.style.left   = clockPartsLeft;
  
  setImages();

  // Clock title settings
  with (clockName.style)
  {
    width      = maxClockNameWidth + "px";
    fontFamily = clockFont;
    fontSize   = clockNameFontSize + "px";            
    color      = clockFontColor;
  }
  
  if (mySetting.showWeek)
  {
    setGUIWeek(uWeek);
  }
  else
  {
    clockName.innerText = mySetting.clockName;
  }

  with (clockNamePosition.style)
  {
    top        = posClockNameTop + topPX + midClockNameFontHeight - Math.floor(clockName.offsetHeight / 2) + "px";
    left       = leftPX + "px";
    
    // Show clock name only if there is  a title
    if ((clockName.innerText.length > 0) ||
	    (mySetting.showWeek))
    {
      visibility = "visible";
    }
    else
    {
      visibility = "hidden";
    }
  }

  with (dayDisplayPosition.style)
  {
    top        = posDayDisplayTop + topPX + "px";
    left       = posDayDisplayLeft + "px";
    border     = "solid " + dateBorderColor + " 1px";
    visibility = "visible";
  }

  with (dayDisplay.style)
  {
    width      = "28px";
    fontFamily = clockFont;
    fontSize   = clockDateFontSize + "px";
    fontWeight = clockDateFontWeight;
    color      = clockFontColor;
  }

  with (dateDisplayPosition.style)
  {
    top        = posDayDisplayTop + topPX + "px";
    left       = posDateDisplayLeft + "px";
    border     = "solid " + dateBorderColor + " 1px";
    visibility = "visible";
  }

  with (dateDisplay.style)
  {
    width      = "15px";
    fontFamily = clockFont;
    fontSize   = clockDateFontSize + "px";
    fontWeight = clockDateFontWeight;
    color      = clockFontColor;
  }

  with (digitalTimePosition.style)
  {
    top        = posDigitalTimeTop + topPX + "px";
    left       = leftPX + "px";
    visibility = "visible";
  }

  with (digitalTime.style)
  {
    width      = maxDigitalTimeWidth + "px";
    fontFamily = clockFont;
    fontWeight = clockDateFontWeight;
    color      = clockFontColor;
    fontSize   = clockDigitalFontSize + "px";
  }

  if (mySetting.secondsEnabled)
  {
    s.style.visibility   = "visible";   
    dot.style.visibility = "hidden"; 
  }
  else
  {
    s.style.visibility   = "hidden";
    dot.style.visibility = "visible";
  }
  
  if (mySetting.showDay)
  {
    dayDisplayPosition.style.visibility = "visible";   
  }
  else
  {
    dayDisplayPosition.style.visibility = "hidden";
  }

  if (mySetting.showDate)
  {
    dateDisplayPosition.style.visibility = "visible";   
  }
  else
  {
    dateDisplayPosition.style.visibility = "hidden";
  }

  if (mySetting.showDigital)
  {
    digitalTimePosition.style.visibility = "visible";   
  }
  else
  {
  digitalTimePosition.style.visibility = "hidden";
  }
  
  startClock();

}

//------------------------------------------------------------------------------

function BounceSecondBack()
{
  s.Rotation = s.Rotation - bounceAngle;
}

//------------------------------------------------------------------------------

function AnimateSeconds(currentAngleSeconds)
{

  animatingSeconds = true;

  // angleSeconds can get smaller than the current angle if the seconds hands crosses
  // 0 degrees while animating. Therefore add 360 and take 360 modulus take make sure
  // deltaSeconds is positive.
  deltaSeconds = (angleSeconds - currentAngleSeconds + 360) % 360;

  if (deltaSeconds > 2)
  {
    newAngleSeconds = currentAngleSeconds + (deltaSeconds / 15);
    
    // Make sure seconds catches up with actual seconds. We maintain a minimum speed
    // of 1 degree / 20ms.
    if (newAngleSeconds - currentAngleSeconds < 1) {

      // Note: this step may not be larger than 2, as we check for (deltaSeconds > 2) above.
      newAngleSeconds = currentAngleSeconds + 1;

    }
    
    newAngleSeconds       = newAngleSeconds % 360;

    s.Rotation            = newAngleSeconds;
    
    timerIDAnimateSeconds = setTimeout("AnimateSeconds(" + newAngleSeconds + ")", 20);
  }
  else
  {
    s.Rotation       = angleSeconds;
    animatingSeconds = false;
  }
}

//--------------------------------------------------------------------------------

function AnimateMinutes(currentAngleMinutes)
{
  // Minutes are animated clockwise and 50% slower than the seconds

  animatingMinutes = true;
  
  var deltaMinutes = angleMinutes - currentAngleMinutes;

  if (currentAngleMinutes < (angleMinutes - 1))
  {
    newPositionMinutes    = currentAngleMinutes + (deltaMinutes / 30);

    m.Rotation            = newPositionMinutes;

    timerIDAnimateMinutes = setTimeout("AnimateMinutes(" + newPositionMinutes + ")", 20);
  }
  else
  {
    m.Rotation       = angleMinutes;
    animatingMinutes = false;
  }
} 

//------------------------------------------------------------------------------

function AnimateHours(currentAngleHours)
{
  // Hours are animated anti-clockwise and half speed

  animatingHours = true;

  if (currentAngleHours > (angleHours + 1))
  {

    var deltaHours = currentAngleHours - angleHours;

    var newPositionHours = currentAngleHours - (deltaHours / 40);

    h.Rotation = newPositionHours;

    timerIDAnimateHours = setTimeout("AnimateHours(" + newPositionHours + ")", 20);
      
  }
  else
  {
    h.Rotation = angleHours;
    animatingHours = false;
  }
}

//------------------------------------------------------------------------------

function AnimateDate(aDate, aTop)
{

  animatingDate = true;

  // The date animation can go proceed infinitely if the date changes from 
  // 31 to 1 during an animation. Chances are very very small, but it can happen.
  if (aDate > uDate) {
    aDate = uDate; 
  }

  var lNewTop    = aTop;
  var lNewDate   = aDate;
  var lDeltaDate = uDate - aDate;

  dateDisplay.style.top = aTop + "px";
  setGUIDate(aDate);


  if (aDate < uDate)
  {
    
    if (lDeltaDate > 5) {
    
      lDelay  = 30;
      
      lNewTop += 3;

    }
    else  {
    
      lDelay = 50 - (uDate - aDate) * 10;
      
      lNewTop++;
    }
    
    // Pretend like mechanics stop every date for a tiny second.
    if (lNewTop == 0) {
      lDelay *= 7;
    }

    if (lNewTop > 10) {
      lNewTop = -10;
      lNewDate++;
    }

    
    timerIDAnimateDate = setTimeout("AnimateDate(" + lNewDate + "," + lNewTop + ")", lDelay);
  }
  else
  {
  
    // Finish scrolling until the date is centered
    if (aTop < 0) {
    
      lNewTop++;    
      
      timerIDAnimateDate = setTimeout("AnimateDate(" + aDate + "," + lNewTop + ")", 70);
      
    }
    else {
      animatingDate = false;
    }
    
  }
} 

//------------------------------------------------------------------------------

function AnimateDay(aDay, aTop)
{

  animatingDay = true;
  
  var lNewTop = aTop;
  var lNewDay = aDay;
  
  dayDisplay.style.top = aTop + "px";
  setGUIDay(aDay);

  if (aDay < uDay)
  {
     
    lNewTop++;
    
    // Select new day when old one has gone and scroll from top.
    if (lNewTop > 10) {
    
      lNewTop = -10;
      
      lNewDay++;
      
    }
    
    // Pretend like mechanics stop every day for a tiny second.
    if (lNewTop == 0) {
      lTimeOut = 400;
    }
    else {
      lTimeOut = 50;
    }
    
    timerIDAnimateDay = setTimeout("AnimateDay(" + lNewDay + "," + lNewTop + ")", lTimeOut);
    
  }
  else
  {

    // Finish scrolling until the day is centered
    if (lNewTop < 0) {
    
      lTimeout = 100 + 5 * lNewTop;

      lNewTop++;    
      
      timerIDAnimateDay = setTimeout("AnimateDay(" + lNewDay + "," + lNewTop + ")", lTimeout);
      
    }
    else {
    
      animatingDay = false;
      
    }

  }
} 

//------------------------------------------------------------------------------

function AnimateDigitalTime(aHours, aMinutes, aSeconds)
{

  // Tried a few digital time animations, but all looked cheesy or over the top.
  // Just set the digital time after the animation startup delay instead.
  
  updateDigitalTime(hours, minutes, seconds);
  
  animatingDigitalTime = false;
  
} 
