/*
 * imgStripDisolve.js
 * (c) 2009 Bear Peak Software LLC
 */
  
var imgStrip = null;

// TODO: imagefilesBasename = "imageStrip"; imageFilesCount = 4;
// design a way to pass this into init from the html page

registerEvent(window, "load", initImgStrip);

function initImgStrip() {
  //log("initImgStrip");
  imgStrip = new ImgStrip("imgStrip", 6);
  setTimeout(imgStrip.rotate, imgStrip.pauseIntvs[0]);  
}

function ImgStrip(imgId, nImages) {
  this.state = 0;
  this.n = nImages;
  this.i = 1;
  this.img = document.getElementById(imgId);
  this.img.useMap = "#map1";
  this.img.style.opacity = 1.0;
  this.intv = 50; // milliseconds, 66 is good, 50 ms (20 fps) is very smooth
  this.pauseIntvs = [
    4500, 6000 , 6000 , 6000 , 6000 , 6000
  ];
}

ImgStrip.prototype.setNextImg = function() {
  this.i = (this.i % this.n) + 1;
  this.img.src = "images/imgStrip" + this.i + ".jpg";
  this.img.useMap = "#map" + this.i;
}

ImgStrip.prototype.rotate = function() {
  //log("rotate: state=" + imgStrip.state);

  var nextIntv = imgStrip.intv;

  switch (imgStrip.state) {
  
  case 0:
    if (fadeout(imgStrip.img)) {
      imgStrip.setNextImg();
      imgStrip.state = 1;
    }
    break;
    
  case 1:
    if (fadein(imgStrip.img)) {
      imgStrip.state = 0;
      nextIntv = imgStrip.pauseIntvs[imgStrip.i-1];
    }
    break;
  }
  
  setTimeout(imgStrip.rotate, nextIntv);
}

function fadeout(img) {
  //log("fadeout");
  var opc = parseFloat(img.style.opacity);
  //log("opc=" + opc);
  opc -= 0.05;
  setOpacity(img, opc);
  return (opc <= 0.0);
}

function fadein(img) {
  //log("fadein");
  var opc = parseFloat(img.style.opacity);
  //log("opc=" + opc);
  opc += 0.05; // also try 0.075
  setOpacity(img, opc);
  return (opc >= 1.0);
}
