﻿//
// Programmer   : Johan Machielse
// Company      : Machielse Software
// Date         : 05-01-2008
// Version      : 1.00
//
// A timer object fires an event after an interval has been expired. This event can be handled
// by more than one handlers.
//
// Example
//   This example creates a Timer object which fires an event every 500 ms. An event handler
//   is added which is called every 1000 ms after the timer has been started.
// 
//   var timer = new Timer(500);
//  
//   timer.AddHandler( 1000, function(){ alert('Event fired!'); } );
//   timer.Start();
//
function Timer( interval )
{  
    //fields   
    this.interval = interval;
    this.handlers = new Array();
    this.idTimer = 0;
    
    //
    // Starts the timer. The onload event of the body element is a perfect location
    // for starting the timer.
    //
    this.Start = function()
    {
        this.idTimer = setTimeout( "timer.Handle()", this.interval );
    }
    
    //
    // Stops the timer.
    //
    this.Stop = function()
    {
        if( this.idTimer ) 
        {
            clearTimeout( this.idTimer );
            this.idTimer  = 0;
            
            for ( handler = 0 ; handler < this.handlers.length ; handler++ )
            {
                this.handlers[handler].Reset();
            }
        }
    }
    
    //
    // Handles all handlers.
    //
    this.Handle = function()
    {
        for ( handler = 0 ; handler < this.handlers.length ; handler++ )
        {
            this.handlers[handler].Handle(this.interval);
        }
            
        this.Start();
    }
    
    //
    // Add a handlers that is called after a particular time out interval.
    //
    // Parameters
    //      interval    : Time-out interval [ms]
    //      handler     : Handler function
    this.AddHandler = function( interval, handler )
    {
        indexHandler = this.handlers.length;
        
        this.handlers[indexHandler] = new TimerHandler( interval, handler );
    }
    
    //
    // Set the timer interval.
    //
    // Parameters
    //      interval    : Time-out interval [ms]
    //      reset       : Reset the handlers (boolean).
    //
    this.SetInterval = function( interval, reset )
    {
        this.interval = interval;
        
        if ( reset )
        {
            for ( handler = 0 ; handler < this.handlers.length ; handler++ )
            {
                this.handlers[handler].Reset();
            }
        }
    }
}

function TimerHandler( interval, handler )
{
    //fields   
    this.expired = 0;
    this.interval = interval;        
    this.handler = handler;
    
    //
    // Calls the handler.
    //
    // Parameters
    //      interval    : The interval of time that has been expired [ms].
    //
    this.Handle = function( interval )
    {
        this.expired = this.expired + interval;
                        
        if ( this.expired >= this.interval )
        {
            this.expired = 0;
            this.handler();
        }
    }
    
    //
    // Resets the expiration vale.
    //
    this.Reset = function()
    {
        this.expiration = 0;
    }
}
