/*
 * Timer.js
 * Copyright (c) 2003 Paul Sturm, all rights reserved.  For authorized use only.
 * sturm@branewave.com
 *
 * Dependencies: Obj.js
 *
 * $Header: /home/sturm/js/RCS/Timer.js,v 1.4 2003/05/07 21:02:05 sturm Exp $
 *
 */


function Timer( reps, delay, callback )
{
   this.base = Obj;
   this.base( );

   this.intervalID = null;
   this.rep = 0;

   this.reps = ( reps == undefined ) ? 1 : reps;
   this.delay = ( delay == undefined ) ? 1 : delay;
   this.fire = callback || function( ) { }
   this.begin = function( ) { }
   this.end = function( ) { }
   this.links = new Array( );


   this.isStopped = function( )
   {
      return ( this.intervalID == null );
   }


   this.stop = function( )
   {
      if ( this.isStopped( ) ) return;

      clearInterval( this.intervalID );
      this.intervalID = null;
      this.rep = 0;

      this._end( );
   }


   this._callback = function( )
   {
      if ( ++ this.rep <= this.reps || this.reps == 0 )
         this.fire( this.rep, this.reps );
      if ( this.rep == this.reps ) this.stop( );
   }


   this.go = function( )
   {
      this.stop( );
      this.begin( );
      if ( this.reps < 0 ) return this._end( );

      this.intervalID = setInterval( this.cb( "_callback" ), this.delay );
   }


   this._end = function( )
   {
      this.end( );
      for ( var i = 0; i < this.links.length; i ++ )
         this.links[ i ].go( );
   }


   this.link = function( linkTo )
   {
      this.links.push( linkTo );
   }


   this.clearLinks = function( )
   {
      this.links = new Array( );
   }
}
Timer.prototype = new Obj;
