﻿var slidecount = 0;
var currentslide = 1;
var slideTimerId = 0;
var timerRunning = false;
function startSlideShow() {
    // hide all the slides
    $$('.productSlide').each(function(img) {
        slidecount++;
        img.setStyle('display', 'none');
    });

    var controlcount = 0;
    $$('.slideshowControl').each(function(img) {
        controlcount++;
        if (controlcount >= 7) {
            // nothing
        } else if (controlcount > slidecount) {
            // hide the controls if there are less than 6 slides
            img.setStyle('display', 'none');
        } else {
            // set up the control
            img.state = "active";
            var src = img.getProperty('src');
            var extension = src.substring(src.lastIndexOf('.'), src.length)
            img.addEvent('mouseenter', function() {
                if (img.state == "active") {
                    img.setProperty('src', src.replace(extension, '_over' + extension));
                }
            });
            img.addEvent('mouseleave', function() {
                if (img.state == "active") {
                    img.setProperty('src', src);
                }
            });
            img.Select = function() {
                img.setProperty('src', src.replace(extension, '_over' + extension));
                img.state = "selected";
            }
            img.Reset = function() {
                img.setProperty('src', src);
                img.state = "active";
            }
        }
    });
    if (slidecount > 0) {
        showSlide(1);
    }
    playSlideshow();
}
function showNextSlide() {
    showSlide(currentslide + 1);
}
function showSlideClick(number) {
    pauseSlideshow();
    showSlide(number)
}
function showSlide(number) {
    if (number > slidecount) {
        number = 1;
    }
    if (slidecount > 0) {
        var count = 0;
        $$('.productSlide').each(function(img) {
            count++;
            if (count == number) {
                img.fade('in');
                img.setStyle('display', 'block');
            } else {
                img.fade('out');
            }
        });
        count = 0;
        $$('.slideshowControl').each(function(img) {
            count++;
            if (count == number) {
                img.Select();
            } else if (count <= slidecount) {
                img.Reset();
            }
        });
        currentslide = number;
    }
}
function playSlideshow() {
    if (slidecount > 0 && timerRunning == false) {
        slideTimerId = setInterval("showNextSlide()", 3500);
        timerRunning = true;
        $('slideShowPause').setStyle('display', 'block');
        $('slideShowPlay').setStyle('display', 'none');
        if (slidecount == 1) {
            // don't bother looping if there's only one slide
            pauseSlideshow();
        }
    }
}
function pauseSlideshow() {
    if (slidecount > 0) {
        clearInterval(slideTimerId);
        timerRunning = false;
        $('slideShowPause').setStyle('display', 'none');
        $('slideShowPlay').setStyle('display', 'block');
    }
}