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


function SlideShow( width, height, pics )
{
   this.base = Obj;
   this.base( );


   this.setDimensions = function( width, height )
   {
      if ( width == undefined ) width = 640;
      if ( height == undefined ) height = 480;

      this.wipe.fadeSpecs[ 0 ].fades[ 0 ].start =
         this.unwipe.fadeSpecs[ 0 ].fades[ 0 ].finish = width;
      this.wipe.fadeSpecs[ 0 ].fades[ 1 ].start =
         this.unwipe.fadeSpecs[ 0 ].fades[ 1 ].finish = height;
   }


   this.clearPics = function( )
   {
      this.current = -1;
      this.pics = [ ];
   }


   this.addPic = function( pic )
   {
      var newIndex = this.pics.length;
      pic.style.zIndex = newIndex + 1;
      this.pics.push( pic );
      this.current = newIndex;
   }


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


   this._createWipes = function( )
   {
      this.wipe = new StylePulse( [ ], new FadeSpec( 10, [
         { prop: "width", unit: "px", start: 0, finish: 0 },
         { prop: "height", unit: "px", start: 0, finish: 0 } ] ) );
      this.unwipe = new StylePulse( [ ], new FadeSpec( 10, [
         { prop: "width", unit: "px", start: 0, finish: 0 },
         { prop: "height", unit: "px", start: 0, finish: 0 } ] ) );
   }


   this._autoplay_cb = function( )
   {
      ( this.atEnd( ) ) ? this.reset( ) : this.next( );
   }


   this._setupAutoPlay = function( )
   {
      this.autoplay = new Timer( 0, 5000, this.cb( "_autoplay_cb" ) );
   }


   this._createWipes( );
   this.setDimensions( width, height );
   this.clearPics( );
   this.addPics( pics );
   this._setupAutoPlay( );


   this.next = function( )
   {
      if ( this.atEnd( ) ) return;

      this.unwipe.stop( );
      this.wipe.clearStyles( );
      this.wipe.addStyle( this.pics[ this.current -- ].style );
      this.wipe.go( );
   }


   this.prev = function( )
   {
      if ( this.atStart( ) ) return;

      this.wipe.stop( );
      this.unwipe.clearStyles( );
      this.unwipe.addStyle( this.pics[ ++ this.current ].style );
      this.unwipe.go( );
   }


   this.play = function( )
   {
      if ( ! this.isPaused( ) ) return;
      this.autoplay.go( );
   }


   this.pause = function( )
   {
      if ( this.isPaused( ) ) return;
      this.autoplay.stop( );
   }


   this.isPaused = function( )
   {
      return this.autoplay.isStopped( );
   }


   this.togglePause = function( )
   {
      ( this.isPaused( ) ) ? this.play( ) : this.pause( );
   }


   this.reset = function( )
   {
      this.wipe.stop( );
      this.unwipe.clearStyles( );
      for ( var i = 0; i < this.pics.length; i ++ )
         this.unwipe.addStyle( this.pics[ i ].style );
      this.unwipe.end( );
      this.unwipe.clearStyles( );
      this.current = this.pics.length - 1;
   }


   this.antireset = function( )
   {
      this.unwipe.stop( );
      this.wipe.clearStyles( );
      for ( var i = 0; i < this.pics.length; i ++ )
         this.wipe.addStyle( this.pics[ i ].style );
      this.wipe.end( );
      this.wipe.clearStyles( );
      this.current = -1;
   }


   this.toStart = function( )
   {
      if ( this.atStart( ) ) return;
      this.reset( );
   }


   this.toEnd = function( )
   {
      if ( this.atEnd( ) ) return;
      this.antireset( );
   }


   this.atEnd = function( )
   {
      return ( this.current < 0 );
   }


   this.atStart = function( )
   {
      return ( this.current >= this.pics.length - 1 );
   }


   this.setWipeReps = function( reps )
   {
      this.wipe.setFadeSpecReps( 0, reps );
   }


   this.setUnwipeReps = function( reps )
   {
      this.unwipe.setFadeSpecReps( 0, reps );
   }
}
SlideShow.prototype = new Obj;
