/************************************** Splendid *************************************
 * Creation Date:	3rd July 2007 2007
 * Edited ----------------------------------------------------------------------------
 *		By:	    On:
 * Description -----------------------------------------------------------------------
 *		This file contains the functions for creating the image slideshow/forward/back
 *
 *      Called by html page
 *          preLoadImages()
 *          runSlidehow()
 *          stopSlideshow()
 *          slideshowNext()
 *          slideshowPrev()
 *
 **************************************************************************************/

/********************************** Global Variables *********************************/
// Speedn in milliseconds
var slideShowSpeed = 5000;

// Speed in milliseconds
var fadeSpeed = 1200;

// Taken out and included in HTML for easy updating
// Specify the image files in an array
//var Pic = new Array();
//Pic[0] = {src: 'img/429x303/historyStorySlideshow.jpg', alt: '' };
//Pic[1] = {src: 'img/429x303/historyStorySlideshow2.jpg', alt: '' };

var preLoad = new Array();

var t;
var j = 0;
var p = Pic.length;

/********************************** Public Functions *********************************/

/****
 * Preloads the images in the array so that they're there to use when needed.
 ****/
function setPicSize(picSize){
	p = picSize;
}

function preLoadImages()
{
    for (i=0; i<p; i++)
    {
        if(Pic[i] != null){
		preLoad[i] = new Image();
            preLoad[i].src = Pic[i].src;
            preLoad[i].alt = Pic[i].alt;
	  }	
	  
    }
    changeImage();
}

/****
 * Cycles through the images as a slideshow
 ****/
function runSlideShow()
{
    j = j+1;
    if (j > (p-1)) 
        j=0;

    $('#slideshowImage').animate({
        opacity: 'hide'
        }, fadeSpeed, 'backinout', changeImage);

    t = setTimeout('runSlideShow()', slideShowSpeed);
    document.getElementById('slideshowLink').href = 'Javascript:stopSlideshow()';
}

function stopSlideshow()
{
    clearTimeout(t);
    document.getElementById('slideshowLink').href = 'Javascript:runSlideShow()';
}

/****
 * Changes to the next image in the imaes array
 ****/
function slideshowPrev()
{
    j = parseInt(j-1);
    if( j < 0 )
        j=parseInt((p-1));

   $('#slideshowImage').animate({
        opacity: 'hide'
        }, fadeSpeed, 'backinout', changeImage);

}

function changeImage()
{
   if(document.getElementById('slideshowImage') != null && preLoad[j] != null){
		document.getElementById('slideshowImage').src = preLoad[j].src;
   		document.getElementById('slideshowImage').alt = preLoad[j].alt;
   		$('#slideshowImage').animate({
        		opacity: 'show'
        		}, fadeSpeed, 'backinout');

   }
}

/****
 * Changes to the previous image in the images array
 ****/
function slideshowNext()
{
    j = parseInt(j+1);
    if( j > parseInt(p-1) )
        j=0;

   $('#slideshowImage').animate({
        opacity: 'hide'
        }, fadeSpeed, 'backinout', changeImage);
}