﻿<!--  Hide from non-javascript browsers
//
// Public declarations
//

// Pause function to coordinate image roll and text
function TimerSleep(millis) 
{
   var date = new Date();
   var curDate = null;

   do
   {
      curDate = new Date();
   } 
   while((curDate - date) < millis);
} 

//
//  Start of object definition for Interval Timer
//

function IntervalTimer(odata)  
{                                // Constructor for IntervalTimer

   //
   //  Define a instance of function parameter odata
   //
   var CustomData = odata;

   //
   //  Declare 'self' as 'this' so scope doesn't change on us.
   //
   var self = this;

   //
   //  Handle to the timer.
   //
   var timeoutID = null;
      
   //
   // Default is 1 rotation over 5 seconds composed of (10) 0.5 second intervals
   // Function parameter is an object to be use in calling user defined routines
   //
   self.StartDelay = 100;        // Milliseconds before beginning first sequence
   self.IntervalDelay = 100;     // Milliseconds between intervals
   self.TimeSlice = 500;         // Miliseconds to perform a step in sequence
   self.Interval = 5000;         // Milliseonds to complete sequence
   self.Rotations = 1;           // Number of times to repeat sequence, -1 = forever
   
   //
   //  Timer Properties
   //
   //  Running means the timer is actively in motion
   //  Paused means the time is in motion but not executing anthing
   //        It can be resumed from the point is was paused
   //  Stopped means the time sequence was interupted.
   //        It can only be restarted from the beginning
   //        The user defined cleanup routine will be executed.
   //
   //  While Running and Paused are activity flags which have meaning for
   //        an active Timer, Stopped is only set for non-active timers
   //        It is intended as more of a why we stopped indicator than
   //        as a Timer status.
   //
   self.IsRunning = false;           // 0 = Timer not active, 1 = Timer active;
   self.IsPaused = false;			 // 0 = Not paused, 1 = paused and can be Resumed
   self.WasStopped = false;          // 0 = Not Stopped, 1 = Was Stopped 

   //
   //  Default of all methods are null (timer will tick but nothingwill happen)
   //
   self.InitMethod = TimerNullMethod;         // Called before starting first sequence
   self.IntervalMethod =  TimerNullMethod;    // Called upon completing sequence
   self.SliceMethod = TimerNullMethod;        // Called to perform a sequnce step
   self.CleanupMethod = TimerNullMethod;      // Called when all sequences have completed.
   
   //
   // Operational Counters
   //
   self.RotationsCompleted = 0;                // Number of completed rotations.
   self.IntervalTimeUsed = 0;                  // Interval Time consumed
   self.IntervalTimeRemaing = self.Interval;   // Interval Time not yet comsumed
   self.SlicesPerInterval =                    // # Slices required to complete Interval
      Math.ceil(self.Interval / self.TimeSlice);    
   self.SlicesCompleted = 0;                   // Number of completed Slices in current Interval

   //
   //  Start of Private Instance Function Definitions
   //
   
   // Interval Time Handler
   function TimerHandler()
   {
      if (! self.IsRunning)  // Only do this work is we are running
         return;

      if (self.IsPaused)   // Also skip the work if we are running but paused
         return;
   
      self.IntervalTimeUsed += self.TimeSlice;      // Record Milliseconds consumed
      self.IntervalTimeRemaining -= self.TimeSlice;
      self.SliceMethod(self, CustomData);           // Call user defined routine
      self.SlicesCompleted++;                       // Give ourselfs credit for it
                                   
      if (self.SlicesCompleted >= self.SlicesPerInterval)
      {                                             // Interval is over
         self.RotationsCompleted++;                 // Count a completed rotation
 
         if ((self.Rotations == -1) || (self.RotationsCompleted < self.Rotations))
         {                                                            // More Rotations To Do
            self.IntervalMethod(self, CustomData);                    // Call user defined routine
            TimerSleep(self.IntervalDelay);                           // Pause between Intervals
            clearTimeout(timeoutID);
            timeoutID = setTimeout(TimerHandler, self.TimeSlice);

         }
         else
         {  
            self.StopTimer();                       // All Rotations Completed        
            TimerFlagReset();                       // Reset the flags
            self.CleanupMethod(self, CustomData);   // Run user defined cleanup routine
            self.RotationsCompleted = 0;
         }
      }
      else
      {                                                         //  More Slices left in Interval
         clearTimeout(timeoutID);
         timeoutID = setTimeout(TimerHandler, self.TimeSlice);  // Start the next Slice
      }
   }  // end TimerHandler()

   function TimerNullMethod(TimerObj, DataObj)
   {                          // A do nothing routine
      return; 
   }
   
   function TimerFlagReset()
   {
      // Reset flags    
      self.IsRunning = false; 
      self.IsPaused = false;
      self.WasStopped = false;  
   }
   
   function TimerReset()
   {                                                // Reset Consumable Timer parameters
      self.IntervalTimeUsed = 0;                    // Start with no Intervals Used;
      self.IntervalTimeRemaining = self.Interval;   // Start with all Intervals remaining
      self.SlicesCompleted = 0;                     // Start with no Slices completed
      self.SlicesPerInterval =                      // # Slices required to complete Interval
         Math.ceil(self.Interval / self.TimeSlice);    
      TimerFlagReset();                             // Reset Flags
   }
   
   self.StopTimer = function ()
   {
      if (self.IsRunning)
      {
         self.IsRunning = false;
         self.IsPaused = true;
         self.WasStopped = true;
         if (timeoutID != null)
            clearTimeout(timeoutID);
      }
   }
   
   self.PauseTimer = function ()
   {
       if (! self.IsPaused)
       {
          self.IsPaused = true;
          self.WasStopped = true;
          if (timeoutID != null)
             clearTimeout(timeoutID);
       }
   }
   
   self.RestartTimer = function ()
   {
      if (self.IsPaused)
      {
         self.IsPaused = false;
         self.WasStopped = false;
         timeoutID = setTimeout(TimerHandler, self.TimeSlice); 
      }
   }

   self.StartTimer = function ()
   {
      if (! self.IsRunning)
      {
         // do initial data loads
         TimerReset()                         // Reset the timer
         self.IsRunning = true;               // Tell the world we are running the timer
         self.InitMethod(self, CustomData);   // Run initialization routine

         if (timeoutID != null)
            clearTimeout(timeoutID);
         timeoutID = setTimeout(TimerHandler, self.StartDelay);  // Pause for start delay
      }
   }  // end StartTimer()

}  // end object IntervalTimer

// -->
