
// Slide Show script --
//         Copyright 1999 B. Randle Unix Consulting
//                 All Rights Reserved
//   Permission is granted to use this script, or parts of it,
//   with or without modification, for no fee provided the
//   above copyright notice is included.
//   If you have useful ideas or suggestions, feel free to
//   send them to me at billr@coinet.com.
//       Bill Randle    24 April 1999
//                      26 May 1999 - convert to standalone
//			15 August 1999 - strip way down

// Note 1: this script requires the slide.js (Slide Object) javascript.
// Note 2: this script originally had Zope DTML directives embedded in it. The
//         Zope database contains the Zope/Python Slide object, which
//         contains information about the images. The button bitmaps
//         and a list of slides to include in this slide show are also
//         in the database. If you want to use this scipt standalone,
//         without Zope, it should be pretty apparent what changes need
//         to be made. For more info on Zope (a great product), see
//         http://www.zope.org.
// Note 3: converted to use static pathnames and slide ids. If you'd like
//         the Zope version, drop me an email.

// initially built with ideas from Thau's JavaScript Tutorial
//    http://www.hotwired.com/webmonkey/javascript/
// but very highly modified and reworked since then.

// There's probably a way to do this directly with Zope/Python,
// but I don't know what it would be.

// With a lot of images or a slow link, it could take
// quite awhile to preload them all. Because of that,
// we do some tricky things using the onLoad/onload event
// handlers. All preload() does is to create all the JavaScript
// Slide objects - with no image loading. After the objects are
// created, the first slide image data is loaded into its Slide.
//
// When the image loading is complete, the onload() event handler
// (which points to loadNextImage()). loadNextImage() copies the
// newly loaded slide image data to the Image on the web page.
// When the web page image is finished updating, its onLoad()
// event handler fires off (which calls readyForNext()). The
// readyForNext() function starts loading the next slide with
// its image data, starting the cycle over again.

// All of the above is well and good, but IE4 does really strange
// things as far as sequencing goes. So, in this striped down version,
// the image loading is all done in preload() which happens when
// the javascript code is loaded. People on slow links will just have
// to wait. Again, if you'd like a full featured version, send me email.


// -- local customization
// the server and pathname to the the slide images
var sserverPath = "images/";

// list of identifiers for the slides
var slideIds = new Array( "one", "two", "three", "four", "five", "six","seven" ,"eight","nine","ten","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twentyone","twentyoneb","twentytwo");

// list of slide titles
//var slideTitles = new Array( "Entertainment", "Business", "Portraits",
//  "Sports", "Still Life", "Scenic","Xtra" );

// list of filenames
var slideImages = new Array( "one.jpg", "two.jpg",
   "three.jpg", "four.jpg", "five.jpg", "six.jpg" ,"seven.jpg", "eight.jpg", "nine.jpg","ten.jpg","twelve.jpg","thirteen.jpg","fourteen.jpg","fifteen.jpg","sixteen.jpg","seventeen.jpg","eighteen.jpg","nineteen.jpg","twenty.jpg","twentyone.jpg","twentyoneb.jpg","twentytwo.jpg");

// list of descriptions about each slide (optional)
//var slideInfos = new Array( "", "", "", "", "", "" );

// time delay in seconds between slides
var delayTime = 4;

// -- vars used internally by the script
var ssOK = 0;  // flag to indicate we can run the slide show
var numSlides;  // toal number of slides in this show
var lastSlide;  // index of the last slide (i.e. numSlides - 1)
var currentSlide = -1;  // index of slide currently displayed
var theTimer = 0;  // timer for auto advance
var stopped = 1;  // are we stopped or playing
var pslides = new Array();  // array of JavaScript slides (see slide.js)
var restarted = 0;

if (document.images)
{
  preload();
}
else
{
  // image swapping not supported on this browser
  alert("Image Swaping not supported. Upgrade your browser!");
}

function preload()
{
  ssOK = 0;  // flag to indicate we can run the slide show
  numSlides = 0;
  lastSlide = -2;
  currentSlide = -1;
  theTimer = 0;
  stopped = 1;

  // create a new slide and populate it with info from the
  // the corresponding arrays
  // [This may seem kludgey and a duplicate of the info in the
  // global arrays, but in the original implementation all of
  // tyhe slide info was stored in the Zope database.
  for (numSlides=0; numSlides<slideIds.length; numSlides++)
  {
    pslides[numSlides] = new Slide(
	slideIds[numSlides],
        "", // slideTitles[numSlides]
        "", // slideInfos[numSlides]
        1, // force loading now
        slideImages[numSlides],
        "",
        "");
  }

  // allow the slide show to start
  currentSlide = 0;
  lastSlide = numSlides - 1;
  ssOK = 1;
}

function forward(step)
{
  var newSlide;

  if (currentSlide >= lastSlide)
  {
    // we're at the end - start over at the beginning
    if (restarted)
      newSlide = 0;
  }
  else
  {
    if (step < 0)
      newSlide = lastSlide;  // go to the end
    else
      newSlide = currentSlide + step;
  }

  if (ssOK)
  {
    currentSlide = newSlide;
    if (pslides[currentSlide].image.src)
    {
//alert("copying slide #" + currentSlide + " to display (fwd)\nstopped is " + stopped);
      document.images.theSlide.src = pslides[currentSlide].image.src;
    }
    else
      alert("image number " + currentSlide + " is empty!");
    if (stopped)
      startTimer();
  }
}

function startTimer()
{
  if (ssOK)
  {
      stopped = 0;
      theTimer = setTimeout("runTimer();", delayTime*1000);
  }
}

function runTimer()
{
  if (ssOK && !stopped)
  {
    forward(1);
    theTimer = setTimeout("runTimer();", delayTime*1000);
  }
  else if (!ssOK)
    alert("No images to run.");
}

function stopTimer()
{
  if (!stopped)
  {
    stopped = 1;
    if (theTimer)
      clearTimeout(theTimer);
  }
}

function startShow()
{
  // called from the html page onLoad event
  restarted++;
  forward(1);
}
