﻿

/* Module level constants */
var IMAGE_THUMBNAIL_PATH    = 0
var IMAGE_MEDIUM_PATH       = 1
var IMAGE_HIRES_PATH        = 2
var IMAGE_TITLE             = 3
var IMAGE_TEXT              = 4

/* Module level variables */
var iCurrentImageIndex = 0
var iMaxImages = 0
var aryImageData = new Array()
var bStopSlideshow = false;
var iTimeouthandle = '';

/* Clear down the array */
function clearArray() {
    aryImageData.length = 0;
    iCurrentImageIndex = 0;
    iMaxImages = 0;
}

/* Add an image to the array */
function addImageToArray(Title, Description, ThumbnailPath, MediumPath, HiResPath) {
    var arySingleImage = new Array();

    arySingleImage[IMAGE_TITLE] = Title;
    arySingleImage[IMAGE_TEXT] = Description;
    arySingleImage[IMAGE_THUMBNAIL_PATH] = ThumbnailPath;
    arySingleImage[IMAGE_MEDIUM_PATH] = MediumPath;
    arySingleImage[IMAGE_HIRES_PATH] = HiResPath;

    aryImageData[iMaxImages] = arySingleImage;
    iMaxImages++;
}

/* Display an image from the array to the on-screen positions (thumbnail image click) */
function displayImage(ImageIndex) {

    if (aryImageData[ImageIndex]) {

        var arySingleImage = new Array();
        arySingleImage = aryImageData[ImageIndex];

        // Put the display code in here once we have the document created //
        if (document.getElementById("imgImageViewer")) {
            document.getElementById("imgImageViewer").src = arySingleImage[IMAGE_MEDIUM_PATH];
        }
        if (document.getElementById("spnImageDescription")) {
            document.getElementById("spnImageDescription").innerHTML = arySingleImage[IMAGE_TEXT];
        }
        if (document.getElementById("hlHiRes")) {

            if (arySingleImage[IMAGE_HIRES_PATH] != '') {
                document.getElementById("hlHiRes").style.display = 'block';
                document.getElementById("hlHiRes").href = arySingleImage[IMAGE_HIRES_PATH];
            }
            else {
                document.getElementById("hlHiRes").style.display = 'none';
            }

        }
        
        iCurrentImageIndex = ImageIndex; 
    }

}

/* Start or stop the slideshow */
function toggleSlideshowState() {
    bStopSlideshow = !bStopSlideshow;
    if (bStopSlideshow) {
        
        window.clearTimeout(iTimeouthandle);
        if (document.getElementById("hlSlideshow")) { document.getElementById("hlSlideshow").innerHTML = 'Start slideshow'; }
    } 
    else { imageSlideshow(); } 
}

/* Main slideshow routine */
function imageSlideshow() {

    if (!bStopSlideshow) {

        if (document.getElementById("hlSlideshow")) { document.getElementById("hlSlideshow").innerHTML = 'Stop slideshow'; }
        displayImage(iCurrentImageIndex);

        iCurrentImageIndex++;
        if (iCurrentImageIndex > iMaxImages) { iCurrentImageIndex = 0; }

        if (!bStopSlideshow) { iTimeouthandle = window.setTimeout('imageSlideshow()', 3000); }
    }
    else {
        
        if (document.getElementById("hlSlideshow")) { document.getElementById("hlSlideshow").innerHTML = 'Start slideshow'; }

    }
}

/* Show next image in the sequence */
function showNextImage() {
    iCurrentImageIndex++;
    if (iCurrentImageIndex > iMaxImages) { iCurrentImageIndex = 0; }
    displayImage(iCurrentImageIndex); 
}

/* Show previous image in the sequence */
function showPreviousImage() {
    iCurrentImageIndex--;
    if (iCurrentImageIndex < 0) { iCurrentImageIndex = iMaxImages; }
    displayImage(iCurrentImageIndex); 
}