﻿/**
  * CSI Houston script file for photo gallery
*/

var index = 0;

//  Timeout value in milliseconds.
var timeout = 5000;

var timer = null;

var playingSlides = false;

//  List of images for each event
var galleryImages = [
    'convention1.jpg',
    'convention2.jpg',
    'convention3.jpg',
    'convention4.jpg',
    'convention5.jpg',
    'golf_2006_1.jpg',
    'golf_2006_2.jpg',
    'golf_2006_3.jpg',
    'golf_2006_4.jpg',
    'golf_2006_5.jpg'
];
/*
var galleryImages = [
    '1105-a.jpg',
    '1105-b.jpg',
    '1105-c.jpg',
    '1105-d.jpg',
    'golf-a.jpg',
    'golf-b.jpg',
    'golf-c.jpg',
    'golf-d.jpg',
    '1005-a.jpg',
    '1005-b.jpg',
    '1005-c.jpg',
    '1005-d.jpg',
    '0805-a.jpg',
    '0805-b.jpg',
    '0805-c.jpg',
    '0805-d.jpg',
    'scrc-a.jpg',
    'scrc-b.jpg',
    'scrc-c.jpg',
    'scrc-d.jpg',
    '0705-a.jpg',
    '0705-b.jpg',
    '0705-c.jpg',
    '0705-d.jpg',
    'gala-1.jpg',
    'gala-2.jpg',
    'tour-1.jpg',
    'tour-2.jpg',
    'xmas1.jpg',
    'xmas2.jpg',
    'xmas3.jpg',
    'xmas4.jpg'
];
*/

function doChangePhoto(idx){
    index = idx;
    displayPhoto();
}

function displayPhoto(){
    
    toggleLoading(true);
    
    var newSrc = galleryImages[index];
    var img = document.getElementById('displayImage');
    var menu = document.getElementById('eventList');
    
    if (img){
        img.src = 'gallery/images/' + newSrc;
        img.onload = function(){
            toggleLoading(false);
            
            //  Update the menu
            if (menu){
                if (index >=0 && index < menu.options.length){
                    menu.selectedIndex = index;
                }
            }
            
            if (playingSlides){
                continueSlideShow();
            }
        }
    }
    
}

function toggleLoading(loading){
    if (loading){
        document.getElementById('loadingContainer').style.display = 'block';
    }
    else{
        document.getElementById('loadingContainer').style.display = 'none';
    }
}

function startSlideShow(){
    if (index+1 >= galleryImages.length){
        index = 0;
    }
    timer = setTimeout("doSlideShow();", timeout);
    playingSlides = true;
}

function stopSlideShow(){
    playingSlides = false;
    clearTimeout(timer);
}

function doSlideShow(){
    
    if (!playingSlides){
        return;
    }
    
    //  Make sure we have a valid index before continuing
    if (index+1 < galleryImages.length){
        //timer = setTimeout("doSlideShow();", timeout);
        index++;
        displayPhoto();
    }
    else{
        //  Start over if we have reached the end.
        clearTimeout(timer);
        playingSlides = false;
        doFirst();
    }
    
}

function continueSlideShow(){
    timer = setTimeout("doSlideShow();", timeout);
}

function doNext(){
    if (index+1 < galleryImages.length && !playingSlides){
        index++;
        displayPhoto();
    }
}

function doPrevious(){
    if (index-1 >= 0 && !playingSlides){
        index--;
        displayPhoto();
    }
}

function doFirst(){
    if (!playingSlides){
        index = 0;
        displayPhoto();
    }
}

function doLast(){
    if (!playingSlides){
        index = galleryImages.length-1;
        displayPhoto();
    }
}

function changeDelay(menu){
    
    timeout = parseInt(menu.options[menu.selectedIndex].value);
    
    if (playingSlides){
        clearTimeout(timer);
        continueSlideShow();
    }
    
}