﻿var current_img = 0;
var img_list = null;
var slideshow_on = false;
var slideshow_stopped = false;
var timer;
var interval = 5;

$(function() {
    img_list = $(".header_wrapper > .images > img");

    // position images
    $(img_list).css("top", $(".header_wrapper > .images").offset().top).css("left", $(".header_wrapper > .images").offset().left);

    // set up labels/links
    var control_labels = "";

    for (i = 0; i < img_list.length; i++) {
        control_labels += "<a href=\"javascript:ShowById(" + i + ");\" id=\"label_" + i + "\">" + $(img_list[i]).attr("title") + "</a> | ";

        $(img_list[i]).bind("click", function() { OpenURL($(this).attr("href")); });
        $(img_list[i]).attr("title", "");
    }

    $(".header_wrapper > .controls > .labels").html(control_labels.substring(0, control_labels.length - 3));

    // set up start/stop on mouse over
    $(".header_wrapper").bind("mouseover", function() { PauseSlideshow(); });
    $(".header_wrapper").bind("mouseout", function() { StartSlideshow(); });


    // show first image
    Show(current_img);
    StartSlideshow();
});

function StartSlideshow() {
    if (!slideshow_stopped) {
        slideshow_on = true;
        timer = setTimeout(ShowNext, interval * 1000);
    }
}

function PauseSlideshow() {
    slideshow_on = false;
    clearTimeout(timer);
}

function StopSlideshow(control) {
    if (slideshow_stopped) {
        slideshow_on = true;
        slideshow_stopped = false;
        ShowNext();
        $(control).attr("src", "/images/homepage_headers/button_pause.png");
    }
    else {
        PauseSlideshow();
        slideshow_stopped = true;
        $(control).attr("src", "/images/homepage_headers/button_play.png");
    }
}

function Show(id) {
    $(img_list[current_img]).fadeOut("slow");
    $(".header_wrapper > .controls > .labels > #label_" + current_img).removeClass("current").addClass("normal");

    $(img_list[id]).fadeIn("slow");
    $(".header_wrapper > .controls > .labels > #label_" + id).removeClass("normal").addClass("current");
}

function ShowById(id) {
    clearTimeout(timer);
    Show(id);
    current_img = id;
}

function ShowNext() {
    if (current_img + 1 >= img_list.length) {
        Show(0);
        current_img = 0;
    }
    else {
        Show(current_img + 1);
        current_img++;
    }

    if (slideshow_on && !slideshow_stopped) {
        timer = setTimeout(ShowNext, interval * 1000);
    }
}

function ShowPrev() {
    if (current_img <= 0) {
        Show(img_list.length - 1);
        current_img = img_list.length - 1;
    }
    else {
        Show(current_img - 1);
        current_img--;
    }

    if (slideshow_on && !slideshow_stopped) {
        timer = setTimeout(ShowNext, interval * 1000);
    }
}

function OpenURL(url) {
    if (url.substring(0, 4) == "http") {
        window.open(url);
    }
    else {
        location.href = url;
    }
}
