//*******************************************************************************
//* RotaShow -- a slideshow implemented as unobtrustive JavbaScript
//*
//* Target: JavaScript with Prototype (v1.6+) and Scriptaculous (v1.8+)
//* Authors: Brad Garner
//*          Mike Arace (based on an example at http://mikeomatic.net/?p=78)
//* Version: 0.1
//*
//* Description:
//*  Unobtrusive JavaScript implementation of an auto-rotating, fading slideshow 
//* with arbirtrary content in the pane.
//*
//* HTML should be of the form:
//*   <div id="rotashow">
//*    <div>pane 1 content</div>
//*    <div style="display:none">pane 2 content</div>
//*    <div style="display:none">pane 3 content</div>
//*    ...
//*   </div>
//*
//* Related Files:
//*  rotashow.css
//*
//* Limitations:
//*  Delay between slides is hard-coded at 5 seconds.
//*  Only one rotashow slideshow per page.
//*
//'*******************************************************************************

// Globally-scoped globals
var currentPhotoNum;
var minPhotoNum;
var maxPhotoNum;

// Performs a fade
function rotashowSwapFade() {
  if (maxPhotoNum <= minPhotoNum) return; // Nothing to do if only zero/one photo.

  Effect.Fade('rotashow-' + currentPhotoNum, { duration:1, from:1.0, to:0.0 });

  // Move to next photo, or wrap around
  currentPhotoNum++;
  if (currentPhotoNum > maxPhotoNum) {
    currentPhotoNum = minPhotoNum;
  }

  Effect.Appear('rotashow-' + currentPhotoNum, { duration:1, from:0.0, to:1.0 });
}

// Event handler that inits the show and starts the fading.
function rotashowInit() {
  // Init globals
  minPhotoNum = 1;
  maxPhotoNum = 0;
  currentPhotoNum = 1;
    
  // Add unique id tags to each <div> within the rotashow
  var photos =  $('rotashow').select('div');
  for (var j=0; j<photos.length; j++) {
    maxPhotoNum++;
    photos[j].id = "rotashow-" + maxPhotoNum;
  }

  // Set up delayed callback
  var waitTimeMs = 10000; // wait, in milliseconds, for each photo
  setInterval('rotashowSwapFade()', waitTimeMs);
}

// On-page-load event registration
Event.observe(window, 'load', rotashowInit);

