﻿var arySlideshowImageList = new Array();
var arySlideshowImageCaptions = new Array();
var sSlideshowDivName = '';
var sCaptionTitleDivName = '';
var sCaptionInfoDivName = ''; 
var iSlideshowImageCounter = 0;
var iCurrentSlideshowItem = 0;
var bSlideshowRunning = false;
var iSlideshowInterval = 5000;
var iSlideshowIntervalStep = 500;
var iSlideshowIntervalMin = 500;
var iSlideshowIntervalMax = 10000;
var sCounterDisplayDivName = '';
var bShowCounter = false;
var bShowCaptions = true; 
var sImageCSSText = ''; 

function setSlideshowState(newState) {
    bSlideshowRunning = newState; 
}

function setImageCSSText(textName) {
    sImageCSSText = textName; 
}

function startStopSlideshow() {
    bSlideshowRunning = !bSlideshowRunning; 
}

function addToSlideshowArray(fileName) {
    arySlideshowImageList[iSlideshowImageCounter] = fileName;
    iSlideshowImageCounter++;
}

function addToCurrentImageCaption(itemTitle, mainCaption) {

    // This function MUST be called before addToSlideShowArray()
    var arySingleCaptionItem = new Array();
    arySingleCaptionItem[0] = itemTitle;
    arySingleCaptionItem[1] = mainCaption;
    arySlideshowImageCaptions[iSlideshowImageCounter] = arySingleCaptionItem; 

}

function setSlideshowDivName(divName) {
    sSlideshowDivName = divName; 
}

function setCaptionTitleDivName(divName) {
    sCaptionTitleDivName = divName;
}

function setCaptionTextDivName(divName) {
    sCaptionInfoDivName = divName;
}

function setCounterDisplayDivName(divName) {
    sCounterDisplayDivName = divName;
}

function setDisplayCounter(value) {
    bShowCounter = value; 
}

function setSlideshowInterval(newInterval) {
    iSlideshowInterval = newInterval; 
}

function setSlideshowIntervalStep(newStep) {
    iSlideshowIntervalStep = newStep; 
}

function setCaptionDisplay(newState) {
    bShowCaptions = newState;
}

function showCurrentSlideshowImage() {

    // Fade in the new image to show (based on iCurrentSlideshowImage)
    if (arySlideshowImageList[iCurrentSlideshowItem]) {

        var sContainerDIVName = '#' + sSlideshowDivName; 

        if (document.getElementById(sSlideshowDivName).children.length > 0) {

            $(sContainerDIVName + ' > img').remove();
            $(sContainerDIVName).addClass('LoadingImage')
        }

        var img = new Image();
        $(img).load(function () {

            $(this).hide();

            if (sImageCSSText != '') {
                $(this).addClass(sImageCSSText);
            }

            $(sContainerDIVName).removeClass('LoadingImage').append(this);

            $(this).fadeIn();

            if (bShowCaptions) { showImageCaption();  };

        })
            .attr('src', arySlideshowImageList[iCurrentSlideshowItem]);

    }

    // Show the counter if that is what we need to do
    showImageCounter(); 

    // If we are currently in the slideshow then set up the next loop
    if (bSlideshowRunning == true) { setTimeout("showNextSlideshowImage()", iSlideshowInterval); }

}

function showImageCaption() {

    // Find the caption and the header from the main aray
    if (arySlideshowImageCaptions[iCurrentSlideshowItem]) {

        var arySingleCaptionInfo = arySlideshowImageCaptions[iCurrentSlideshowItem];
        if (arySingleCaptionInfo[0] && (sCaptionTitleDivName != '')) {

            $('#' + sCaptionTitleDivName).html(arySingleCaptionInfo[0]); 

        }
        if (arySingleCaptionInfo[1] && (sCaptionInfoDivName != '')) {

            $('#' + sCaptionInfoDivName).html(arySingleCaptionInfo[1]); 
        }
    }

}

function showImageCounter() {

    if ((bShowCounter == true) && (sCounterDisplayDivName != '')) {

        $('#' + sCounterDisplayDivName).text((iCurrentSlideshowItem + 1) + ' of ' + iSlideshowImageCounter);        

    }

}

function showNextSlideshowImage() {

    if ((iCurrentSlideshowItem + 1) < iSlideshowImageCounter) { iCurrentSlideshowItem++; } else { iCurrentSlideshowItem = 0; }
    showCurrentSlideshowImage(); 
}

function showPreviousSldeshowImage() {
    if (iCurrentSlideshowItem > 0) { iCurrentSlideshowItem--; } else { iCurrentSlideshowItem = iSlideshowImageCounter - 1; }
    showCurrentSlideshowImage();
}

function speedupSlideshow() {
    if (iSlideshowInterval - iSlideshowIntervalStep >= iSlideshowIntervalMin) { iSlideshowInterval -= iSlideshowIntervalStep; }
}

function slowdownSlideshow() {
    if (iSlideshowInterval + iSlideshowIntervalStep <= iSlideshowIntervalMax) { iSlideshowInterval += iSlideshowIntervalStep; } 
}
