﻿// (C) 2000 www.CodeLifter.com
/************************************** 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()
 *          checkLoaded( imgSrc )
 *          showSlideshowImage( i )
 *          showGalleryView()
 *          showSlideshowView()
 *          runSlidehow()
 *          stopSlideshow()
 *          slideshowNext()
 *          slideshowPrev()
 *          changeImage()
 *
 **************************************************************************************/

/********************************** Global Variables *********************************/
// Speedn in milliseconds
var slideShowSpeed = 15000;

// Speed in milliseconds
var fadeSpeed = 500;

var preLoad = new Array();
var Pic = new Array();
var t;
var j = 1;
var p = null;

/********************************** Public Functions *********************************/

/****
 * Preloads the images in the array so that they're there to use when needed.
 * Image data in imageDataDiv as innerHTML 
 * (<image src>:<image alt>|<image src>:<image alt>)
 ****/
function preLoadImages()
{
    p = Pic.length;
    
    for (i=0; i<p; i++)
    {
        preLoad[i] = new Image();
        preLoad[i].src = Pic[i].src;
        preLoad[i].alt = Pic[i].alt;
	preLoad[i].title = Pic[i].title;
    }
    
    if( p > 1 ){
        t = setTimeout('runSlideShow()', slideShowSpeed);
    }else{
        try{
            $('#slideshowNav').hide();
            $('#slideshowButtons').hide();
        }catch(e){}
    }
}

/****
 * Checks to see whether the image set has already been loaded
 ****/
function checkLoaded( imgSrc )
{
    for( var i=0; i<preLoad.length; i++)
        if( preLoad[i].src.indexOf(imgSrc) > -1 )
            return true;

    return false;
}

/****
 * Takes the position of the image in the image array and displays it in the slideshow mode
 ****/
function showSlideshowImg( i )
{
    j = i;
    
    // Try to pre-load slideshow images, if not already loaded...
    if(preLoad.length == 0)
	    preLoadImages();
	    
    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);
}
/****
 * Clears the timeout that runs the slide show
 ****/
function stopSlideshow()
{
    clearTimeout(t);
    try{
        var node = document.getElementById('slideshowLink');
        node.href = 'Javascript:runSlideShow()';
        node.className = 'viewSlideShow';
    }catch(e){}
}

/****
 * Changes to the next image in the imaes array
 ****/
function slideshowPrev()
{
    j = parseInt(j-1);
    if( j < 0 )
        j=parseInt((preLoad.length-1));

   $('#slideshowImage').animate({
        opacity: 'hide'
        }, fadeSpeed, 'backinout', changeImage);

    if( t )
        stopSlideshow();

}

/****
 * Changes to the previous image in the images array
 ****/
function slideshowNext()
{
    j = parseInt(j+1);
    if( j > parseInt(preLoad.length-1) )
        j=0;

   $('#slideshowImage').animate({
        opacity: 'hide'
        }, fadeSpeed, 'backinout', changeImage);

    if( t )
        stopSlideshow();
}

/****
 * Function to change the image over
 ****/
function changeImage()
{
    try
    {
        document.getElementById('slideshowImage').src = Pic[j].src;
        document.getElementById('slideshowImage').alt = Pic[j].alt;
        document.getElementById('slideshowLink').href = Pic[j].href;
	document.getElementById('slideshowLink').title = Pic[j].title;
        $('#slideshowImage').animate({
            opacity: 'show'
            }, fadeSpeed, 'backinout');
    } catch(e)
    {
        if( t )
            stopSlideshow();
    }
}


