// ****************************************************************************
// zone_slideshow.js
//
// Uses Adobe Spry.Effect.Fade to cross fade an array of elements
// ****************************************************************************

var arrElems = new Array();

var nCurrElem = 0;
var nNextElem = 1;

var bSkankyIE = false;
// detect IE
if(/MSIE/.test(navigator.userAgent)){
  bSkankyIE = true;
}

var fade_effect;
var obs = new Object;

// ****************************************************************************
// startSlides
// ****************************************************************************
function startSlides() {
	timer = setTimeout("nextSlide()",3*1000);
}

// ****************************************************************************
// nextSlide
// ****************************************************************************
function nextSlide() {
  fade_effect = new Spry.Effect.Fade(arrElems[nNextElem], {duration: 3000, from: 0, to: 100, toggle: true});   
  // Attach the observer to the Fade effect
  fade_effect.addObserver(obs);    
  fade_effect.start();
  
  // as we have trouble detecting opacity in IE we instead use a second fade to fade out previous element
  // this is less efficient than using the onStep method.
  if (bSkankyIE) {
    var fadeout_effect = new Spry.Effect.Fade(arrElems[nCurrElem], {duration: 3000, from: 100, to: 0, toggle: true});   
    fadeout_effect.start();
  } 
}

// ****************************************************************************
// onPostEffect
//
// called by fade observer when fade complete
// ****************************************************************************
obs.onPostEffect = function(ef){
  nCurrElem = nNextElem;
  if (nNextElem == arrElems.length-1) {
    nNextElem = 0;
  }
  else {
    nNextElem += 1;
  }
  // start timer for next slide
	timer = setTimeout("nextSlide()",3*1000);  
}

// ****************************************************************************
// onStep
//
// called by fade observer for each step of fade
// NOTE: IE does not seem to give us the opacity in a friendly manner so we
// do not use this for IE
// ****************************************************************************
obs.onStep = function(ef){
  if (!bSkankyIE) {
    otherEl = document.getElementById(arrElems[nCurrElem]);
  
    var opacity = Spry.Effect.getStyleProp(ef.element, 'opacity');
    otherEl.style.opacity = (1 - opacity);
  }
}