// GLOBAL CONSTANTS.  Change these to control the slideshow behavior

var ROTATION_INTERVAL = 4.0; // seconds. How long to display each image
var FADE_DURATION = 0.7;     // seconds. How long the actual fade takes, once it starts
var STRETCH_IMAGES = true;  // true or false. Resize images to divSlideShow.style.width and .height



/******************************************************************************************
* AUTHOR
*
*   Written by Arin Friedlander (with the exception of the opacity functions at bottom)
*   July 2006
*
*******************************************************************************************
* COMPATIBILITY
*
*   Theoretically, this should work in IE 4.0+, Firefox 1.0+, Opera 9.0+, Safari 1.2+, and
*   Netscape 6.0+. It might also work with earlier versions of Safari, Mozilla, Phoenix,
*   Firefox (pre 1.0), FireBird, and Konqueror.
*
*   Has been actually tested to work in IE6, Firefox 1.5, Opera 9, and Netscape 8 on Win XP
*   (It's a little jumpy in Netscape 8 when using the "Display Like Firefox" render engine)
*
*******************************************************************************************
* USING THIS SCRIPT
*
*   To use, you need to do three things:
*   1. In the page header, include a reference to this script (must use proper path).
*   2. Call the slideShow() function with valid paths to at least two pictures.  Example:
*   3. Then, place an empty div element with an id of "divSlideShow" in your document where
*      you want the slide show to appear.  It is highly recommended to set the div's style's
*      width and height, usually set to the size of the largest image. Example:
*
*   Here's a barebones example:
<html>
<head>
<script language="JavaScript" src="slideShow.js"></script>
</head>
<body onload="slideShow('images/Pic1.jpg', 'http://www.hp.com/pic.gif', 'me.png');">
<div id="divSlideShow" style="width: 200px; height: 150px;"></div>
</body>
</html>
*
*******************************************************************************************/



// GLOBAL VARIABLES

var pics = new Array();  //String array, holds paths to images
var curPicNum = -1;  //Index of current picture. Using -1 starts at first picture.



// FUNCTIONS


/*** slideShow() ***************************************************************************
*
* Takes:   Any number of string arguments, specifying valid paths (absolute or relative) to
*          any image file the browser can display.
*
* Returns: nothing
*
********************************************************************************************
*
* Call this function from the web page body's "onload" event
*
*******************************************************************************************/
function slideShow() {

    if (arguments.length >= 2)  //Only perform slideshow if >= 2 images
    {
        // This script doesn't work well if the fades overlap, so this makes sure one
        // fade is done before the next starts.
        if (FADE_DURATION > ROTATION_INTERVAL)
        FADE_DURATION = ROTATION_INTERVAL;

        // Read in as many image path arguments as user specified; store them in
        // array "pics"
        for (var i = 0; i < arguments.length; i++) {
            pics[pics.length] = arguments[i];
        }

        //Add the page elements necessary to create the slide show
        createPageElements();

        //Load the images into memory so they appear smoothly when it's their turn
        preCacheImages();

        //Start the slideshow
        var timeout = setTimeout('showNextPic("imgSlideShow1", "imgSlideShow2")',
        ROTATION_INTERVAL*1000);
    }
}


/*** createPageElements() ******************************************************************
*
* Takes:   nothing
*
* Returns: nothing
*
********************************************************************************************
*
* Creates the three page elements necessary for the slideshow:
*  Two <img> elements, one to hold current picture, one to hold next picture.
*  One <div> element, positioned off-screen, which'll hold the precache images.
*
*******************************************************************************************/
function createPageElements() {

    //Get a reference to the object that'll hold our images
    var divSlideShow = document.getElementById('divSlideShow');
    //divSlideShow.style.position = 'relative';

    //Create two image elements
    var img1 = document.createElement('img');
    var img2 = document.createElement('img');

    img1.setAttribute('id', 'imgSlideShow1');
    img2.setAttribute('id', 'imgSlideShow2');

    img1.style.position = 'absolute';
    img2.style.position = 'absolute';
    img1.style.zIndex = 100;
    //img1.style.visibility = "hidden";
    img2.style.zIndex = 200;



    img2.src = getNextPic();  //Queue up the first image
    img1.src = getNextPic();
    curPicNum --;

    //Resize the first image, if enabled
    if (STRETCH_IMAGES) {
        img2.style.width = divSlideShow.style.width;
        img2.style.height = divSlideShow.style.height;
    }

    divSlideShow.appendChild(img1);
    divSlideShow.appendChild(img2);

    //setOpacity('100','imgSlideShow2');

    //Create a div to hold all the images for pre-caching
    var divPreCache = document.createElement('div');
    divPreCache.setAttribute('id', 'divPreCache');
    divSlideShow.appendChild(divPreCache);

    divPreCache.style.position = 'absolute';
    divPreCache.style.top = '-9999px';  // off the screen
    divPreCache.style.visibility = 'hidden';
}


/*** preCacheImages() **********************************************************************
*
* Takes:   nothing
*
* Returns: nothing
*
********************************************************************************************
*
* Creates a new <img> element for each image, which starts loading them with the page,
* instead of when it's their turn to be displayed.
*
*******************************************************************************************/
function preCacheImages() {
    var newImg;

    var divContainer = document.getElementById('divPreCache');

    for (var i = 1; i < pics.length; i++)
    {
        newImg = document.createElement('img');
        newImg.setAttribute('width', '1');
        newImg.setAttribute('height', '1');
        newImg.setAttribute('src', pics[i]);
        divContainer.appendChild(newImg);
    }
}


/*** showNextPic() *************************************************************************
*
* Takes:   imgTopID (string), a DHTML identifier for an <img> element on the page.
*          imgBottomID (string), a DHTML identifier for an <img> element on the page.
*
* Returns: nothing
*
********************************************************************************************
*
* Fades the current picture into the next one
*
*******************************************************************************************/
function showNextPic(imgTopID, imgBottomID) {

    if ((curPicNum + 1) < pics.length) {
        //NC.Log(curPicNum + '< ' + pics.length);
        //Load them backwards -- first thing we'll do is swap 'em.
        var imgTop = document.getElementById(imgBottomID);
        var imgBottom = document.getElementById(imgTopID);

        //Swap 'em; put top image on top
        imgTop.style.zIndex = 300;
        imgBottom.style.zIndex = 100;

        //Load next picture
        imgBottom.src = getNextPic();

        //Stretch image, if this option is enabled
        if (STRETCH_IMAGES) {
            var divSlideShow = document.getElementById('divSlideShow');
            imgBottom.style.width = divSlideShow.style.width;
            imgBottom.style.height = divSlideShow.style.height;
        }

        //Reset next picture to be visible
        //	setOpacity(100, imgBottom.id);
        clearOpacity(imgBottom.id);

        //Fade out top image
        fadeImg(imgTop.id, 99, 0, FADE_DURATION*1000);


        var timeout = setTimeout("showNextPic('" + imgTop.id + "', '" + imgBottom.id + "')",
        ROTATION_INTERVAL*1000);
    }
}


/*** showNextPic() *************************************************************************
*
* Takes:   nothing.  Reads global integer curPicNum
*
* Returns: (string) path to the next picture
*
********************************************************************************************
*
* Increment the number of the current picture. After the last picture, reset to the first.
*
*******************************************************************************************/
function getNextPic() {
    if (++curPicNum >= pics.length) {
        //curPicNum = 0;
    }

    return(pics[curPicNum]);
}




////////////////////////////////////////////////////////////////////////////////////////////
//
// Opacity functions.  These appear on several websites and forums, so unfortunately I
// don't know the original author.  I changed some function and variable names for clarity.
//
////////////////////////////////////////////////////////////////////////////////////////////


/*** fadeImg() *****************************************************************************
*
* Takes:   id (string), the DHTML ID of the target <img> element to fade
*          opacStart (integer), the image's starting opacity,  100 = opaque, 0 = transparent
*          opacEnd (integer), the image's ending opacity,  100 = opaque, 0 = transparent
*          duration (integer), the total number of milliseconds the fade should take.
*
* Returns: nothing
*
********************************************************************************************
*
* Starts the fade-in or fade-out of the specified image
*
*******************************************************************************************/
function fadeImg(id, opacStart, opacEnd, duration) {
    //speed for each frame
    var speed = Math.round(duration / 100);
    var timer = 0;

    //Decides whether to fade in or fade out
    //If opacStart and opacEnd are the same, nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("setOpacity(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
    else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++) {
            setTimeout("setOpacity(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}


/*** setOpacity() **************************************************************************
*
* Takes:   opacity (integer), how opaque to make image.  100 = opaque, 0 = transparent
*          id (string), the DHTML ID of the target <img> element whose opacity is changing
*
* Returns: nothing
*
********************************************************************************************
*
* Change opacity of the specified image
*
*******************************************************************************************/
function setOpacity(opacity, id) {
    var imgStyle = document.getElementById(id).style;

    //CSS3 method; works in Firefox 1.0+, IE 7.0+, Safari 1.2+, Opera 9.0+
    imgStyle.opacity = (opacity / 100);

    imgStyle.MozOpacity = (opacity / 100);  //Netscape 6-7, older Mozilla/Firefox/Phoenix/FireBird
    imgStyle.KhtmlOpacity = (opacity / 100);  //Konqueror 3.0-3.2, Safari 1.0-1.1
    imgStyle.filter = "alpha(opacity=" + opacity + ")";  //IE 4.0-6.0
}

function clearOpacity(id) {
    var imgStyle = document.getElementById(id).style;

    //CSS3 method; works in Firefox 1.0+, IE 7.0+, Safari 1.2+, Opera 9.0+
    imgStyle.opacity = "";

    imgStyle.MozOpacity = "";  //Netscape 6-7, older Mozilla/Firefox/Phoenix/FireBird
    imgStyle.KhtmlOpacity = "";  //Konqueror 3.0-3.2, Safari 1.0-1.1
    imgStyle.filter = "";  //IE 4.0-6.0
}