// Constantes 

var TOUT_CAMBIO = 5000 ;
var TOUT_FUNDIDO = 30 ;
var RATIO_OPACIDAD = 0.01 ;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * clase ebs_FundidoImagenes_Frame
 */
function ebs_FundidoImagenes_Frame( name, img_id, images )
{
   this.name = name ;
   this.img_id = img_id ;
   this.images = images ;

   this.img_1 ;
   this.img_2 ;
   this.cache ;

   this.actual = 0 ;
   this.tout = TOUT_FUNDIDO ;
   this.turno = 0 ;
   this.op_x = RATIO_OPACIDAD ;

   this.op_1;
   this.op_2;
   this.sec ;

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.cambio_turno
    */
   this.cambio_turno = function()
   {
      this.turno ++ ;

      if ( this.turno >= this.images.length )
      {
         this.turno = 0 ;
      }
   }
   /* end metodo ebs_FundidoImagenes_Frame.cambio_turno
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.swap
    */
   this.swap = function()
   {
      var tmp = this.img_1 ;
      this.img_1 = this.img_2 ;
      this.img_2 = tmp ;
   }
   /* end metodo ebs_FundidoImagenes_Frame.swap
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.cambio
    */
   this.cambio = function( sec )
   {
      this.sec = sec ;

      this.cambio_turno() ;

      this.cache = new Image() ;
      this.cache.src = this.images[ this.turno ] ;

      this.wait_img() ;
   }
   /* end metodo ebs_FundidoImagenes_Frame.cambio
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.wait_img
    */
   this.wait_img = function()
   {
      if ( this.cache.complete )
      {
         this.swap() ;

         this.img_1.src = this.cache.src ;

         this.op_1 = 0 ;
         this.op_2 = 1 ;

         this.run() ;
      }
      else
      {
         setTimeout( name + '.wait_img()', 100 ) ;
      }
   }
   /* end metodo ebs_FundidoImagenes_Frame.wait_img
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.run
    */
   this.run = function()
   {
      setTimeout( name + '.fundir()', this.tout ) ;
   }
   /* end metodo ebs_FundidoImagenes_Frame.run
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.fundir
    */
   this.fundir = function()
   {
      this.fundir_img( this.img_1, this.op_1 ) ;
      this.fundir_img( this.img_2, this.op_2 ) ;

      this.op_1 += this.op_x ;
      this.op_2 -= this.op_x ;

      if ( this.op_1 > 1 )
      {
         this.op_2 = 0 ;

         this.fundir_img( this.img_2, this.op_2 ) ;

         this.sec.run() ;
         return ;
      }

      this.run() ;
   }
   /* end metodo ebs_FundidoImagenes_Frame.fundir
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_Frame.fundir_img
    */
   this.fundir_img = function( img, op )
   {
      img.style.opacity = op ;
	
      if ( img.filters )
      {
         img.filters.alpha.opacity = op * 100 ;
      }
   }
/* end metodo ebs_FundidoImagenes_Frame.fundir_img
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

}
/* end ebs_FundidoImagenes_Frame
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * clase ebs_FundidoImagenes_FadeLoop
 */
function ebs_FundidoImagenes_FadeLoop( name, frames )
{
   this.name	= name ;
   this.frames	= frames ;
   this.tout	= TOUT_CAMBIO ;
   this.tout_f	= TOUT_FUNDIDO ;
   this.turno	= -1 ;

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_FadeLoop.iniciar
    */
   this.iniciar = function( tout_cambio )
   {
      if ( tout_cambio != null )
      {
         this.tout = tout_cambio ;
      }

      for ( i= 0; i < this.frames.length; i++ )
      {
         this.frames[i].img_1 =
         document.getElementById( this.frames[i].img_id + '_1' ) ;

         this.frames[i].img_2 =
         document.getElementById( this.frames[i].img_id + '_2' ) ;
      }

      this.run() ;
   }
   /* end metodo ebs_FundidoImagenes_FadeLoop.iniciar
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_FadeLoop.run
    */
   this.run = function()
   {
      setTimeout( this.name + '.cambio()', this.tout ) ;
   }
   /* end metodo ebs_FundidoImagenes_FadeLoop.run
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes_FadeLoop.cambio
    */
   this.cambio = function()
   {
      this.turno ++ ;

      if ( this.turno >= this.frames.length )
      {
         this.turno = 0 ;
      }

      this.frames[ this.turno ].cambio( this ) ;
   }
/* end metodo ebs_FundidoImagenes_FadeLoop.cambio
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

}
/* end ebs_FundidoImagenes_FadeLoop
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * clase ebs_FundidoImagenes
 */
function ebs_FundidoImagenes( name )
{
   this.name = name ;
   this.frames = new Array() ;
   this.loop = null ;

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes.add
    * 
    *  Inserta un nuevo objeto ebs_FundidoImagenes en el array que realizar la
    *  secuencia ( loop ) de transiciones ...
    */
   this.add = function( fade_id, fade_img )
   {
      var last_id = this.frames.length ;

      this.frames[ last_id ] = 
         new ebs_FundidoImagenes_Frame(
               this.name + '.frames[' + last_id + ']',
               'fade_a',
               fade_img
            ) ;
   }
   /* end ebs_FundidoImagenes.add
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    * metodo ebs_FundidoImagenes.run
    */
   this.run = function( tout )
   {
      this.loop = new ebs_FundidoImagenes_FadeLoop(
                        this.name + '.loop',
                        this.frames
                     ) ;
   
      this.loop.iniciar( tout * 1000 ) ;
   }
/* end ebs_FundidoImagenes.run
    * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

}
/* end ebs_FundidoImagenes
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
