//*********************************************************************
//
// Slide Show
//
// Copyright (c) 2008, MFM Communication Software, Inc.
//
//*********************************************************************
//
// slideshow.js
// $Id: slideshow.js,v 1.4 2008/07/01 15:09:46 mdame Exp $
//
// Functions to implement a JavaScript slide show
//
//*********************************************************************
//
// Tell jsjam.pl to not change these identifier and function names
//
// jsjam-keep:FadeSlide
// jsjam-keep:InitSlideShow
// jsjam-keep:NextPrevSlide
// jsjam-keep:StartStopSlideShow
// jsjam-keep:getElementById
// jsjam-keep:clearInterval
// jsjam-keep:setInterval
// jsjam-keep:filters
// jsjam-keep:innerHTML
// jsjam-keep:opacity
// jsjam-keep:style
// jsjam-keep:zIndex
// jsjam-keep:MozOpacity
// jsjam-keep:KhtmlOpacity
//
//*********************************************************************

var imageList = null;
var imagePtr = 0;
var imageCnt = 0;
var imageDur = 0;
var fadeDur = 0;
var imgHeight = 0;
var imgWidth = 0;
var extraImgParams = '';

var playing = 0;
var fading = 0;
var opacVal = 0;

var canvasBase = "slideshow_canvas";
var curCanvas = '';
var actCanvas;


//*********************************************************************
//
// InitSlideShow(delay, images, height, width, imgParams)
//
// delay - integer
// The delay between slides in seconds.  If 0, don't automatically play
// the slide show.
//
// images - array
// Array of images to include in the slide show
//
// height - integer
// Height of the slideshow
//
// width - integer
// Width of the slideshow
//
// imgParams
// Extra parameters to add to each slide's <img> tag
//
// Initialize the slide show
//
//*********************************************************************
function InitSlideShow(delay, images, height, width, imgParams)
{
  if (images && (images.length > 0))
  {
    imageList = new Array();
    for (i = 0; i < images.length; i++)
    {
      imageList[i] = new Image();
      imageList[i].src = images[i];
    }

    imageCnt = imageList.length;

    if (delay > 0)
      imageDur = delay * 1000;

    // Give a default duration
    if (imageDur <= 0)
      imageDur = 5000;

    // Give a default fade duration
    if (fadeDur <= 0)
      fadeDur = 50;

    imgHeight = height;
    imgWidth = width;
    extraImgParams = imgParams;
  }
  else
  {
    imageList = null;
    imageDur = 0;
  }
}


//*********************************************************************
//
// StartStopSlideShow(delay)
//
// delay - integer
// The time between slides in seconds
//
// Start or stop playing the slide show
//
//*********************************************************************
function StartStopSlideShow(delay)
{
  if (!imageList || (imageList.length <= 0))
    return;

  if (playing)
  {
    clearInterval(playing);
    playing = 0;
  }
  else
  {
    if (delay > 0)
      imageDur = delay * 1000;

    // Give a default duration
    if (imageDur <= 0)
      imageDur = 5000;  

    opacVal = 10;
    imagePtr = 1;
    curCanvas = canvasBase + "_0";

    var obj = document.getElementById(curCanvas);
    InitSlide(obj, 0);

    NextPrevSlide(1);

    playing = setInterval("NextPrevSlide(1)", imageDur);
  }
}


//*********************************************************************
//
// InitSlide(canvas, imageIdx)
//
// canvas - div object
// the layer where we will draw the slide
//
// imageIdx - integer
// The index of the image to show
//
// Display the given slide on the given canvas
//
//*********************************************************************
function InitSlide(canvas, imageIdx)
{
  var html = '<img src="' + imageList[imageIdx].src + '"';
  html += 'height="' + imgHeight + '" width="' + imgWidth + '"';

  if (extraImgParams != '')
    html += extraImgParams;

  html += ">";

  canvas.innerHTML = html;  
}


//*********************************************************************
//
// NextPrevSlide(direction)
//
// direction - integer
// If positive, go to the next slide.  If negative, go to the previous
// slide.  A zero value is a noop
//
// Show the next or the previous slide
//
//*********************************************************************
function NextPrevSlide(direction)
{
  ResetFade();

  actCanvas = document.getElementById(curCanvas);
  actCanvas.style.zIndex++;

  imagePtr = imagePtr + direction;

  if (imagePtr > imageCnt)
    imagePtr = 1;
  else if (imagePtr < 1)
    imagePtr = imageCnt;

  // Make sure we aren't in the middle of another one
  if (fading)
    clearInterval(fading);
  
  fading = setInterval("FadeSlide()", fadeDur);

  curCanvas = (curCanvas == canvasBase + "_0")
       ? canvasBase + "_1" : canvasBase + "_0";
}


//*********************************************************************
//
// FadeSlide()
//
// Fade in the current slide
//
//*********************************************************************
function FadeSlide()
{
  if (opacVal < 100)
  {
    opacVal += 10;
    
    if (actCanvas.filters && actCanvas.filters[0])
      actCanvas.filters[0].opacity = opacVal;
    else if (actCanvas.style.MozOpacity)
      actCanvas.style.MozOpacity = opacVal/101;
    else if (actCanvas.style.KhtmlOpacity)
      actCanvas.style.KhtmlOpacity = opacVal/100;
    else if (actCanvas.style.opacity && !actCanvas.filters)
      actCanvas.style.opacity = opacVal/101;
  }
  else
  {
    clearInterval(fading);
    fading = null;

    actCanvas = document.getElementById(curCanvas);

    InitSlide(actCanvas, imagePtr - 1);
  }
}


//*********************************************************************
//
// ResetFade()
//
// Reset the fade status
//
//*********************************************************************
function ResetFade()
{
  opacVal = 10;

  var obj = document.getElementById(curCanvas);

  if (obj.filters && obj.filters[0])
    obj.filters[0].opacity = opacVal;
  else if (obj.style.MozOpacity)
    obj.style.MozOpacity = opacVal/101;
  else if (obj.style.KhtmlOpacity)
    obj.style.KhtmlOpacity = opacVal/100;
  else if (obj.style.opacity && !obj.filters)
    obj.style.opacity = opacVal/101;
}


//*********************************************************************
//
// CHANGE HISTORY
//
// $Log: slideshow.js,v $
// Revision 1.4  2008/07/01 15:09:46  mdame
// Added keywords to jsjam list
//
// Revision 1.3  2008/07/01 15:00:15  mdame
// Fix syntax error in NextPrevSlide()
//
// Revision 1.2  2008/06/27 16:58:38  mdame
// Added handling for fade transitions
//
// Revision 1.1  2008/06/12 18:03:24  mdame
// Initial revision
//
//*********************************************************************
