/*
 Author: 		Kevin Thompson
 Last Updated: 02/28/2007 05:40:10 PM
 */

  var imageFader = Class.create();
  imageFader.prototype = {
    initialize: function(id,wait){
      this.id = id;
      this.el = $(id);
      this.wait = wait;
      this.running = true;
      this.fading = false;
      this.fadeState = 0;
      //get first child
      this.activeImage = this.el.down();
      //error check
      if(!this.activeImage){
        alert('Cannot find any images.');
        this.running = false;
        return;
      }else{
        this.activeImage.setStyle({
           position: 'absolute',
           left: 0,
           top: 0
        });
      }
      // hide all other images
      var img = this.activeImage;

      while(img = img.next()){
        img.setStyle({
           opacity: null,
           position: 'absolute',
           left: 0,
           top: 0
        });

      }

      this.pe = new PeriodicalExecuter(this._executer.bind(this),this.wait);
    },
    stop: function (){
      this.running = false;
      this.fading = false;
      this.pe.stop();
      this._stopFade();
    },
    _executer: function(){
      if(this.running == false){
        this.stop();
        return;
      }else if(!this.activeImage){
        alert('Error in imageFader: No active image was available.');
        this.stop();
      }
      this._startFade();
    },
    _fade: function (pe){
      if(this.fading==false){
        this._stopFade();
      }else if(this.fadeState>=1){
        this.fadeIn.setStyle({opacity:1});
        this.fadeOut.setStyle({opacity:null});
        this._stopFade();
      } else {
        this.fadeState += 0.01667;
        this.fadeOut.setStyle({opacity: (1 - this.fadeState)});
        this.fadeIn.setStyle({opacity: this.fadeState});
      }
    },
    _startFade: function(){
		if(this.fading) return;
      this.fadeOut = this.activeImage;
      this.fadeIn = this.fadeOut.next();
      if(!this.fadeIn){
        this.fadeIn = this.el.down();
        if(!this.fadeIn){
          alert('Cannot find any images.');
          this.stop();
        }
      }
      
      this.fading = true;
      this.fadeState = 0;
      this.fader = new PeriodicalExecuter(this._fade.bind(this),0.01);
    },
    _stopFade: function (){
      if(this.fader) this.fader.stop();
      this.fadeState = 0;
      this.fading = false;
      this.activeImage = this.fadeIn;
      this.fader = this.fadeIn = this.fadeOut = null;
    }
  }
