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


function StylePulse( styles, fadeSpecs )
{
   this.base = Timer;


   this.clearFadeSpecs = function( )
   {
      this.stop( );
      this.fadeSpecs = [ ];
      this.reps = this.styles.length;
   }


   this.addFadeSpec = function( fadeSpec )
   {
      this.stop( );
      this.fadeSpecs.push( fadeSpec );
      this.reps += fadeSpec.reps;
   }


   this.addFadeSpecs = function( fadeSpecs )
   {
      if ( fadeSpecs == undefined ) fadeSpecs = [ ];
      else if ( fadeSpecs.constructor != Array ) fadeSpecs = [ fadeSpecs ];
      for ( var i = 0; i < fadeSpecs.length; i ++ )
         this.addFadeSpec( fadeSpecs[ i ] );
   }


   this.clearStyles = function( )
   {
      this.stop( );
      this.reps -= this.styles.length;
      this.styles = [ ];
   }


   this.addStyle = function( style )
   {
      this.stop( );
      this.styles.push( style );
      this.reps ++;
   }


   this.addStyles = function( styles )
   {
      if ( styles == undefined ) styles = [ ];
      else if ( styles.constructor != Array ) styles = [ styles ];
      for ( var i = 0; i < styles.length; i ++ )
         this.addStyle( styles[ i ] );
   }


   this.base( );
   this.delay = 50;
   this.styles = [ ];
   this.clearStyles( );
   this.clearFadeSpecs( );
   this.addStyles( styles );
   this.addFadeSpecs( fadeSpecs );


   this.begin = function( )
   {
      if ( this.fadeSpecs.length == 0 ) return;

      for ( var i = 0; i < this.styles.length; i ++ )
         this.fadeSpecs[ 0 ].styleStep( this.styles[ i ], 0 );
   }


   this.end = function( )
   {
      if ( this.fadeSpecs.length == 0 || this.rep == this.reps ) return;

      for ( var i = 0; i < this.styles.length; i ++ )
         this.fadeSpecs[ this.fadeSpecs.length - 1 ].styleStep(
            this.styles[ i ], this.reps );
   }


   this.fire = function( rep, reps )
   {
      for ( var i = 0; i < this.styles.length; i ++ )
      {
         var step = rep - i;
         var whichSpec = 0;
         while ( whichSpec < this.fadeSpecs.length )
         {
            if ( step <= this.fadeSpecs[ whichSpec ].reps )
            {
               this.fadeSpecs[ whichSpec ].styleStep( this.styles[ i ], step );
               whichSpec = this.fadeSpecs.length;
            } else {
               step -= this.fadeSpecs[ whichSpec ].reps;
               ++ whichSpec;
            }
         }
      }
   }


   this.setFadeSpecReps = function( which, reps )
   {
      this.stop( );
      if ( which < 0 || which >= this.fadeSpecs.length ) return;

      this.reps -= this.fadeSpecs[ which ].reps;
      this.fadeSpecs[ which ].reps = reps;
      this.reps += reps;
   }
}
StylePulse.prototype = new Timer;
