﻿$(document).ready(function () {
    setupSlideShow();
});

// Go to a specific slide in the start page slide show
var currentIndex = 0;
function goToSlide(index) {

    var slides = $("#slideshow .slide");
    var count = slides.length;
    var slideWidth = 700;

    if (count == 0) {
        return;
    }

    if (index < 0) {
        index = count - 1;
    }

    if (index > count - 1) {
        index = 0;
    }

    var delta = index - currentIndex;

    var newPosition = delta * slideWidth;

    $("#slideshowcontainer").animate({ marginLeft: '-=' + newPosition }, "slow", "swing");

    currentIndex = index;

    $("#position a").removeClass("active");
    $($("#position a")[currentIndex]).addClass('active');
}

// Setup the slideshow on the start page
function setupSlideShow() {

    if ($("#slideshow").length == 0) {
        return;
    }

    // Add bullets for showing current position
    for (var i = 0; i < $("#slideshow .slide").length; i++) {
        if (i == 0) {
            $("#position").append("<a href='#" + i + "' class='active'></a>");
        }
        else {
            $("#position").append("<a href='#" + i + "'></a>");
        }
    }

    // Set up navigation by clicking the position bullets
    $("#position a").live('click', function () {

        var newIndex = parseInt(this.href.substring(this.href.length - 1, this.href.length));

        goToSlide(newIndex);
    });

    // Set up "go to next" button
    $("#slideshow #next").click(function (e) {

        goToSlide(currentIndex + 1);

        e.preventDefault();
    });

    // Set up "go to previous" button
    $("#slideshow #previous").click(function (e) {
        goToSlide(currentIndex - 1);

        e.preventDefault();
    });
}
