
// class fadeobject

//const FADEMODE_FADEUP=0;   // fading up
//const FADEMODE_FADEDOWN=1; // fading down

FADEMODE_FADEUP=0;
FADEMODE_FADEDOWN=1;


// members:
// mode - the current FADEMODE_xxx
// objid - the DOM id of the object we control
// current_level - the current transparency level
// current_image_index - the index (0..len-1) of the image we're currently displaying
// imglist - a list of the images we're set up to display


// constructor
// todo - take imglist, mode, etc
function Fadeobject (name,              // the name of the javascript object that stores "this"
                     elementID,         // the DOM element ID of the corresponding image
                     images,            // an array listing the images we'll display          
                     rate               // how fast we fade (20 is a nice median, lower is faster)
                    ) {
  this.myname              = name;
  this.mode                = FADEMODE_FADEDOWN;
  this.imglist             = images;
  this.objID               = document.getElementById(elementID);
  this.current_image_index = 0;
  this.current_level       = 100; // start at max
  this.faderate            = rate;

  // we're done with setup, so start the fading
  this.fadelogic();
}

// timer callback, does fading and waiting operations
Fadeobject.prototype.fadelogic = function() {
  switch(this.mode){
    case FADEMODE_FADEDOWN:
      if (this.current_level >0) {
        this.current_level -=2;
        setTimeout(this.myname+".fadelogic()", this.faderate);
        this.set_opacity();
      }
      else {
        // flip to the next image in the series
        this.current_image_index ++;
        if (this.current_image_index == this.imglist.length) {
          this.current_image_index = 0;
        } 
        this.objID.src =  this.imglist[this.current_image_index];

        // set fadeup mode, and wait for the DOWN period before starting that fade
        this.mode = FADEMODE_FADEUP;
        setTimeout(this.myname+".fadelogic()", 2000); // FIXME: get downtime from param
      }
      break;

    case FADEMODE_FADEUP:
      if (this.current_level <100) {
        this.current_level +=2;
        this.set_opacity();
        setTimeout(this.myname+".fadelogic()", this.faderate);
      }
      else {
        // set fadedown mode, and wat for the UP period before starting to fade
        this.mode = FADEMODE_FADEDOWN;
        setTimeout(this.myname+".fadelogic()", 3000); // FIXME: get uptime from param
      }
      break;

    // default: something's wrong, best to do nothing
  }
}

// set the opacity of this fader's element to its current fade level
Fadeobject.prototype.set_opacity = function(){
  // standard
  this.objID.style.opacity      = (this.current_level / 100);

  // KHTML (konqueror, safari)
  this.objID.style.KhtmlOpacity = (this.current_level / 100);

  // Gecko (firefox, galleon, etc.)
  this.objID.style.MozOpacity   = (this.current_level / 100);

  // old IE
  this.objID.style.filter       = "alpha(opacity=" + this.current_level + ")";
}



// main fading code
function fadestart(){
  fader1 = new Fadeobject("fader1", "p1", new Array('images/a7.jpg', 'images/a3.jpg', 'images/a6.jpg'), 40);
  fader2 = new Fadeobject("fader2", "p2", new Array('images/a8.jpg', 'images/a4.jpg'),                  20);
  fader3 = new Fadeobject("fader3", "p3", new Array('images/a5.jpg', 'images/a1.jpg'),                  60);
  fader4 = new Fadeobject("fader4", "p4", new Array('images/a6.jpg', 'images/a2.jpg'),                  80); 
}

