/*
 * imgStripRotate.js
 * (c) 2010 Bear Peak Software, LLC
 */

/*
		Global Variables
		
		Implementation Note: This code adds two variables to the global name space.
		Global variables start with "biff" and are intentionally long to avoid
		possible conflicts with other JavaScript code or libraries. 
		
		This code relies on a configuration object named biffImageStripConfiguration.
		This object can be defined here or in an HTML file that uses this script.
 */

/* the image strip state object */
var biffImageStrip = null;

registerEvent(window, "load", initBiffImageStrip);

function initBiffImageStrip() {
  //log("initBiffImageStrip: number of images = " + biffImageStripConfiguration.length);
  biffImageStrip = new BiffImageStrip("imageStrip", biffImageStripConfiguration);
  setTimeout(biffImageStrip.rotate, biffImageStrip.duration());  
	
	// prefetch remaining images
	var w = 790;
	var h = 220;
	var firstImg = document.getElementById("imageStrip");
	if (firstImg) {
		w = firstImg.width;
		h = firstImg.height;
	}
	for (var i = 1; i < biffImageStripConfiguration.length; i++) {
		var img = new Image(w, h);
		img.src = biffImageStripConfiguration[i].filename;
	}
}

function BiffImageStrip(imgId, configuration) {
  this.state = 0;
  this.i = 1;
  this.intv = 50; // milliseconds, 66 is good, 50 ms (20 fps) is very smooth
  this.img = document.getElementById(imgId);
  this.img.style.opacity = 1.0;
	this.config = configuration;
	this.n = configuration.length;
}

BiffImageStrip.prototype.setNextImg = function() {
  this.i = (this.i + 1) % this.n;
  this.img.src = this.config[this.i].filename;
  this.img.useMap = this.config[this.i].mapName;
}

BiffImageStrip.prototype.rotate = function() {
  var nextIntv = biffImageStrip.intv;

  switch (biffImageStrip.state) {
  
  case 0:
    if (fadeoutElement(biffImageStrip.img, 0.05)) {
      biffImageStrip.setNextImg();
      biffImageStrip.state = 1;
    }
    break;
		
  case 1:
    if (fadeinElement(biffImageStrip.img, 0.05)) {
      biffImageStrip.state = 0;
      nextIntv = biffImageStrip.duration();
    }
    break;
  }
  
  setTimeout(biffImageStrip.rotate, nextIntv);
}

BiffImageStrip.prototype.duration = function() {
	var milliSeconds = this.config[this.i].durationSeconds * 1000;
	return milliSeconds;
}

