/*********************
Author: Timothy Grindall
Copyright: Timothy Grindall, 2008

Liscense: GNU Public Liscense. Note: if you don't know what the GNU public liscense is, you can probably find out by searching in Wikipedia at "http://www.wikipedia.org". A GNU Public Liscense basically means that you can use this code in any way you like as long as you don't sell the whole or any part of it in the original javascript or in a translated and/or compiled form.

Credits: This script is based on the slideshow script from the book "Javascript for the World Wide Web, Fifth Edition, Visual Quickstart Guide" by Tom Negrino and Dori Smith (ISBN 0-321-19439-X), but with magor enhancements allowing the image count to pass from page to page.
*********************/

thumbs = new Array("image1", "image2", "image3", "image4", "image5")  // each of theses id names in this array correspond to an id definition in the css file that is the same as the others, differing only in the background picture used
imgCt = thumbs.length   // number of images
this_image = 0; // index of the current image

//variable used to pre-load the next image
next = new Image(183,121);
next.src = "graphics/side-gallery/image" + (this_image+1) + ".jpg";

//get the image count retrieved by the getArgs function
if (img) {
    this_image = parseInt(img);
}
/* if(!img) alert("missing img!"); */

function cycle() {
    if (document.images) {
        
        if (this_image == imgCt) {
            this_image = 0
        }

        var picture = document.getElementsByName("gallery_pic")[0]; // get a handle to the picture element
        
        if (thumbs[this_image]) picture.id = thumbs[this_image];    // If the picture exists, change the id to the next picture
        else {      // If the picture does not exist, reset to zero and display the first picture
            this_image = 0;
            picture.id = thumbs[this_image];
        }
        this_image++    // increment our image count
        
        // preload the image we will show next time so its ready
        nextimage = this_image + 1; // find the index of the next image
        if (nextimage == 5) {
            /* alert("nextimage == " + nextimage); */
            nextimage = 1;
        }
        next.src = "graphics/side-gallery/image" + (nextimage) + ".jpg"; // store the image in the cache
            /* if (next.src) alert("next.src: " + next.src);
            else alert("image load failure"); */
            
        // set the side links to pass the image count to the next page...
        
        var links = document.getElementsByName("side_links");    // get an array of the side links
        
         // go through the array and append the image count onto the url of each link
        for (i = 0; i < links.length; i++) {
            links[i].href = links[i].href.split("?")[0] + "?img=" + this_image;   // set the link
        }
        
        // if existant, set the email link too
        if (document.getElementsByName("email")[0]) {
            document.getElementsByName("email")[0].href = "email.php?img=" + this_image;
        }
        
        // set the slideshow link too
        if (document.getElementsByName("slideshow")[0]) {
            document.getElementsByName("slideshow")[0].href  = "slideshow.htm?img=" + this_image;
            document.getElementsByName("slideshow")[1].href  = "slideshow.htm?img=" + this_image;
            /* if (document.getElementsByName("slideshow")[1].href == "slideshow.htm") alert("assignment failure..."); */
        }
        
        
        setTimeout("cycle()", 5 * 1000) // every five second(s)
    }
    /* else alert('!document.images'); */
}

function cyclestart() {
    alert("cyclestart()");
}
