var image_index = 0;
var play_slideshow = false;
var interval_timer = null;
var images = new Array;

function SetImages(imgs) {
    for (i = 0; i < imgs.length; i++) {
        images[i] = new Image();
        images[i].src = imgs[i];
    }
}

function fade_out_on_end() {
    document.getElementById("main_image_id").src = images[image_index].src;
    dojo.fadeIn({node: "main_image_id", duration: 1000}).play();
}

function LoadImage(index) {
    if (play_slideshow) {
        RestartTimer();
    }

    if (index < 0) {
        index = images.length - 1;
    }
    
    if (index >= images.length) {
        index = 0;
    }

    image_index = index;

    if (image_index == 0) {
        document.getElementById("previous_image_id").src = "images/previous_disabled.gif"
        document.getElementById("next_image_id").src = "images/next.gif"
    } else if (index == (images.length - 1)) {
        document.getElementById("previous_image_id").src = "images/previous.gif"
        document.getElementById("next_image_id").src = "images/next_disabled.gif"
    } else {
        document.getElementById("previous_image_id").src = "images/previous.gif"
        document.getElementById("next_image_id").src = "images/next.gif"
    }

    dojo.fadeOut({node: "main_image_id", duration: 1000,
        onEnd: fade_out_on_end
    }).play();
}

function PreviousImage(index) {
    if (play_slideshow) {
        RestartTimer();
    }

    LoadImage(image_index - 1);
}

function NextImage(index) {
    if (play_slideshow) {
        RestartTimer();
    }

    LoadImage(image_index + 1);
}

function PlaySlideshow() {
    interval_timer = setInterval("NextImage()", 5000);
    document.getElementById("play_slideshow_image_id").src = "images/pause.gif";
}

function PauseSlideshow() {
    document.getElementById("play_slideshow_image_id").src = "images/play.gif";
    clearInterval(interval_timer);
}

function RestartTimer() {
    clearInterval(interval_timer);
    interval_timer = setInterval("NextImage()", 5000);
}

function TogglePlaySlideshow() {
    play_slideshow = !play_slideshow;

    if (play_slideshow) {
        PlaySlideshow();
    } else {
        PauseSlideshow();
    }
}

