/********************************************************************************************************************************************************************************************
                                                                                  TIMER.JS

Copyright © 2006 Illusionator, Inc.

A module that provides functions for timing events.

********************************************************************************************************************************************************************************************/


// IlsTimer()
//		bRunning:	If true, the timer is created and immediately starts running.  Otherwise, the returned timer is stopped and has not yet run at all.
//	returns:		A new timer.
//
// This is the constructor method for timers.  It should be invoked as "new IlsTimer()".  Creates a new timer.

function IlsTimer(bRunning)
{
	// Internally, a timer is an object with three properties:
	//		dateStart:	The Date object representing the date/time the timer was created.
	//		dateStop:	The Date object representing the date/time the timer was most recently stopped.  If the timer is running, this is null.
	//		iMsStopped:	The total number of milliseconds that the timer has been stopped so far.  Is updated whenever the timer is started again.

	// If this is the first time this function is being invoked, add the methods to the prototype
	if (!IlsTimer.start) {
		IlsTimer.prototype.start = IlsTimerStart;
		IlsTimer.prototype.stop = IlsTimerStop;
		IlsTimer.prototype.elapsed = IlsTimerElapsed;
	}

	this.dateStart = new Date();
	this.dateStop = (bRunning ? null : this.dateStart);
	this.iMsStopped = 0;
} // IlsTimer


// IlsTimerStart()
//	returns:	Nothing.
//
// Starts the timer running.

function IlsTimerStart()
{
	if (this.dateStop) {
		// Compute the number of milliseconds that the timer was stopped this time, and add that into the total number of milliseconds that the timer has been stopped
		this.iMsStopped += ((new Date()).getTime() - this.dateStop.getTime());
		this.dateStop = null;
	}
} // IlsTimerStart


// IlsTimerStop()
//	returns:	Nothing.
//
// Stops the timer.

function IlsTimerStop()
{
	if (!this.dateStop) {
		this.dateStop = new Date();
	}
} // IlsTimerStop


// IlsTimerElapsed()
//	returns:	Number of milliseconds that the timer has been running.
//
// Returns the number of millseconds that the timer has been running.  Does not include any time during which the timer was stopped.  Can be invoked when the timer is running or not.

function IlsTimerElapsed()
{
	var iMsCurrent = (new Date()).getTime();
	var iMsStopped = this.iMsStopped;
	if (this.dateStop) {
		// If the timer is currently stopped, we have to also add in the number of milliseconds that it has been stopped this time
		iMsStopped += (iMsCurrent - this.dateStop.getTime());
	}

	// Total elapsed time the timer has been running is the current time (in milliseconds), minus the number of seconds the timer was stopped added to the time (in milliseconds) that
	// the timer was started
	return (iMsCurrent - (this.dateStart.getTime() + iMsStopped));
} // IlsTimerElapsed
