// GLOBAL VARIABLES

// Constant times for slideshow
// work in terms of milliseconds.

var TIME_BETWEEN_PHOTOS = 8000; // Time to pause/wait between transitions to new photos.
var STANDARD_TRANSITION_SPEED = 1000; // The length of time a transition between a photo should take.
var QUICK_TRANSITION_SPEED = 150; // The length of time a quick transition between a photo should take.

// An array of CSS strings pointing
// to image locations.

var gPictures;

// An array of strings containing html content.

var gContent;

// These variables manager whether
// or not a transition is active,
// what the current speed is specified at,
// and when the transition started.

var gTransitionIsActive;
var gCurrentTransitionLength;
var gTransitionStartTime;

// These manage the current opacity
// of a picture, and the interval
// of change for opacity during transitions.

var gOpacityInterval;
var gCurrentOpacity;

// Manages which is the current
// photo being shown.

var gCurrentPhoto;

// Manages which container is currently
// on top and bottom.

var gTopContainer = "container1";
var gBottomContainer = "container2";

// Interval and timeout IDs used to
// run both the slideshow pauses
// and the fade/transitions between photos.

var gFadeIntervalID;
var gRotationIntervalID;

function crossFade()
{
   // Static variable used to track
   // how many times we've called this function
   // during a single transition.  It is 
   // reset to 0 here, and also reset each
   // time we finish a photo transition.

   if( typeof crossFade.stepsMade == 'undefined' )
   {
      crossFade.stepsMade = 0;
   }// end if

   // The goal here is to continually reduce
   // the opacity of the image in front until it is
   // invisible, showing the image in the back.
   // Then swap the back image as the new
   // front image.

   if( gCurrentOpacity <= 0 )
   {
      // If we're done reducing the opacity of the
      // image in front, then swap the back image
      // into the front image, and make the front
      // image opaque once more.  This is transparent
      // to the user, but it frees up the back image
      // to be changed to something new later on.

      document.getElementById( gTopContainer ).style.opacity = 0;
      document.getElementById( gTopContainer ).style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=0)';

      document.getElementById( gTopContainer ).style.zIndex = 2;
      document.getElementById( gBottomContainer ).style.zIndex = 3;

      temp = gTopContainer;
      gTopContainer = gBottomContainer;
      gBottomContainer = temp;

      document.getElementById( gBottomContainer ).style.opacity = 1;
      document.getElementById( gBottomContainer ).style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=100)';

      // Clear the timer interval that kept calling
      // this function, report that the
      // transaction is no longer active,
      // and reset our step counter.
      // We have completed a full photo transition,
      // and may quit for now.

      window.clearInterval( gFadeIntervalID );
      gTransitionIsActive = false;
      crossFade.stepsMade = 0;
      return;
   }// end if

   // If we're here, the images have not been fully
   // transitioned yet.  Continue to reduce the
   // opacity of the image in front.

   gCurrentOpacity = gCurrentOpacity - gOpacityInterval;
   document.getElementById( gTopContainer ).style.opacity = gCurrentOpacity;
   document.getElementById( gTopContainer ).style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=' + (gCurrentOpacity*100) + ')';

   // Keep count of how many steps we've made
   // to aid in speed recalculation.

   ++crossFade.stepsMade;

   // Different speeds of computers make photo
   // transitions difficult to perform.  We
   // must perform a number of calculations
   // based on time and current opacity status
   // to readjust our transition rate.

   // What time in milliseconds is it right now?

   var currentTransitionTime = new Date().getTime();

   // Find out how much time has passed since
   // we started the transition, and also how much
   // time is left according to our current transition
   // speed.

   var timePassed = currentTransitionTime - gTransitionStartTime;
   var timeLeft = gCurrentTransitionLength - timePassed;

   // Sometimes our time left won't be a positive number.
   // In this case, we avoid division by zero errors
   // by giving us a tiny bit more time.  This won't affect
   // our transition since it stops when our opacity
   // reaches zero, not when our time runs out.

   if( timeLeft <= 0 )
   {
      timeLeft = 1;
   }// end if

   // Regardless of the time intervals we set the browser
   // to follow, it may be too slow.  Instead of trying to guess
   // a nice interval time for all computers, we initially
   // set the interval to 1 MS.  This is too fast for
   // most computers, so we get the average amount of time
   // the computer has been capable of so far.  This will
   // be our new time interval that we set later.

   var avgTimeBetweenSteps = timePassed / crossFade.stepsMade;
   var newTimeInterval = avgTimeBetweenSteps;

   // With a new time interval and only a certain
   // amount of time and opacity left, we must set the opacity interval
   // so it transitions smoothly in this amount of time.

   var projectedStepsLeft = timeLeft / newTimeInterval;
   var newOpacityInterval = gCurrentOpacity / projectedStepsLeft;

   gOpacityInterval = newOpacityInterval;

   // Clear the old interval, and set a new one with
   // our newly calculated time interval.

   window.clearInterval( gFadeIntervalID );
   gFadeIntervalID = self.setInterval("crossFade()", newTimeInterval);

}// end crossFade

function changeImage( imageNumber, speed )
{
   // If a transition is already active,
   // then we will not start another one until
   // the current one is finished.

   if( gTransitionIsActive == true )
   {
      return;
   }// end if

   if( imageNumber < 0 || imageNumber >= gPictures.length )
   {
      return;
   }// end if

   idToChange = "navBox" + imageNumber;

   for( x=0; x<gPictures.length; ++x )
   {
      currentID = "navBox" + x;
      if( x == imageNumber )
      {
         document.getElementById( currentID ).className = "navBoxActive";
      }// end if
      else
      {
         document.getElementById( currentID ).className = "navBox";
      }// end else
   }// end for

   // Set the foreground image,
   // which will be seen by the user.
   // In fact, it should already been seen by the user,
   // but we're making sure here.

   if( gCurrentPhoto >=0 && gCurrentPhoto < gPictures.length )
   {
       document.getElementById(gTopContainer).style.backgroundImage = gPictures[gCurrentPhoto];
       document.getElementById(gTopContainer).innerHTML = gContent[gCurrentPhoto];
   }// end if

   // Set the background image, which 
   // we will transition to.

   document.getElementById(gBottomContainer).style.backgroundImage = gPictures[imageNumber];
   document.getElementById(gBottomContainer).innerHTML = gContent[imageNumber];

   // Reset some global variables before
   // we begin a fade/transition interval series.

   // The current opacity of the image in front is 1
   // (completely visible).  We will reduce this during
   // the transition.

   gCurrentOpacity = 1;

   // We are about to begin a transition.  Set
   // this to true to keep people from rotating
   // images while we're already in the middle
   // of a rotation.

   gTransitionIsActive = true;

   // Mark the time we started the transition.
   // We'll use this later to adjust for computer speed.

   gTransitionStartTime = new Date().getTime();

   // Set the transition speed to be what the calling
   // function specified.

   gCurrentTransitionLength = speed;

   // The opacity interval needs to
   // be = 1 / (Transition Time / Time Interval).
   // This would mean a smooth opacity transition
   // spread perfectly over time.

   // However since we recalculate our timer interval/frame rate
   // dynamically during this, we will start with a timer
   // interval of 1 millisecond (probably too fast and will
   // be adjusted later).

   // Simplifying math...

   // Opacity Interval = 1 / (Transition Time / Time Interval )
   // Opacity Interval = 1 / (Transition Time / 1 )
   // Opacity Interval = 1 / Transition Time

   gOpacityInterval = 1 / gCurrentTransitionLength;

   // Set the timer interval to begin the transition!

   gFadeIntervalID = self.setInterval("crossFade()", 1);

   // Update the global current photo.

   gCurrentPhoto = imageNumber;

}// end changeImage

function rotateImage( direction, speed )
{
   // If a transition is already active,
   // then we will not start another one until
   // the current one is finished.

   if( gTransitionIsActive == true )
   {
      return;
   }// end if

   // Based on the direction specified,
   // we move our index to the previous or next
   // photo in the array.

   if( direction == "forward" )
   {
      imageNumber = gCurrentPhoto + 1;
      if ( imageNumber > ( gPictures.length - 1 ) )
      {
         imageNumber = 0;
      }// end if
   }// end if
   else
   {
      imageNumber = imageNumber - 1;
      if ( imageNumber < 0 )
      {
         imageNumber = (gPictures.length - 1);
      }// end if
   }// end else

   changeImage( imageNumber, speed );

}// end rotateImage

function initRotator()
{
   // Specify the image files used
   // in the rotator.

   gPictures = new Array();

   gPictures[0] = "url(/global/rotator/rotatorimages/05.jpg)";
   gPictures[1] = "url(/global/rotator/rotatorimages/01.jpg)";
   gPictures[2] = "url(/global/rotator/rotatorimages/02.jpg)";
   gPictures[3] = "url(/global/rotator/rotatorimages/03.jpg)";
   gPictures[4] = "url(/global/rotator/rotatorimages/04.jpg)";
   

   gContent = new Array();

   gContent[0] = document.getElementById("frame4").innerHTML;
   gContent[1] = document.getElementById("frame0").innerHTML;
   gContent[2] = document.getElementById("frame1").innerHTML;
   gContent[3] = document.getElementById("frame2").innerHTML;
   gContent[4] = document.getElementById("frame3").innerHTML;
   
   gTransitionIsActive = false;

   /*
   Load with a random image.
   imageNumber = Math.floor(Math.random() * gPictures.length);
   */
   imageNumber = 0;
   changeImage( imageNumber, QUICK_TRANSITION_SPEED );

   startRotator();

} // end initRotator

function startRotator()
{
   // Just in case a timer interval is
   // already going, let's clear it.

   window.clearInterval( gRotationIntervalID );

   // Start auto-rotating!

   gRotationIntervalID = self.setInterval( "rotateImage( 'forward', STANDARD_TRANSITION_SPEED )", TIME_BETWEEN_PHOTOS );
}// end startRotator

function stopRotator()
{
   window.clearInterval( gRotationIntervalID );
}// end stopRotator

