var intervalID;
var runSlideShow = true;
function slideSwitch(num) {

    if (runSlideShow != true) return;

    //get the active slide       
    var $active = $("#slider div.active");
    var activeNav = $($active).attr('id');

    //if user clicks on any of the other slides we pass a num value to take the user to that slide
    if (num != undefined) {

        //get the child divs of the slider
        //var selector = "#slider div:nth-child(" + (num) + ")";
        
        //the $next var is now equal to the one user selected
        var $next = $($("#slider div")[num-1]);
        //stop the time
        clearInterval(intervalID);
    }
    else {
        //set the $next to the next slide
        var $next = $active.next();
    }

    if ($next.length > 0) {

        var currentIndex = $("#slider > div").index($(".active"));

        var changeSlide = currentIndex != (num - 1);

        //if there is a next slide then animate
        if (changeSlide) {
            //reset the navigation buttons
            $("div#slider-buttons img").attr("src", "/images/home/slide-inactive-nav.gif");

            $active.removeClass("active");
            $active.addClass("inactive");

            $next.css("opacity", "0")
                 .removeClass("inactive")
                 .addClass("active")
                 .animate({ opacity: 1.0 }, 1000, function() {
                     $active.css("opacity", "0");
                 });
            activeNav = $($next).attr('id');
            //set navigation
            var prevNav = "div#slider-buttons img[id=" + (activeNav - 1) + "]";
            var nextNav = "div#slider-buttons img[id=" + activeNav + "]";
            $(nextNav).attr("src", "/images/home/slide-active-nav.gif");
            $(prevNav).attr("src", "/images/home/slide-inactive-nav.gif");
        }
    }
    else
    {
        // if no more slides, revert to the first slide
        var currentIndex = $("#slider > div").index($(".active"));

        //reset the navigation buttons
        $("div#slider-buttons img").attr("src", "/images/home/slide-inactive-nav.gif");

        $active.removeClass("active");
        $active.addClass("inactive");

        //$next = $("#slider div:nth-child(1)");
        $next = $($("#slider div")[0]);

        $next.css("opacity", "0")
                 .removeClass("inactive")
                 .addClass("active")
                 .animate({ opacity: 1.0 }, 1000, function() {
                     $active.css("opacity", "0");
                 });

        //set navigation
        var prevNav = "div#slider-buttons img[id=" + (activeNav) + "]";
        activeNav = $($next).attr('id');
        var nextNav = "div#slider-buttons img[id=" + activeNav + "]";
        $(nextNav).attr("src", "/images/home/slide-active-nav.gif");
        $(prevNav).attr("src", "/images/home/slide-inactive-nav.gif");
    }
}        

$(document).ready(function(){
    //set the timer on page load
    intervalID = setInterval("slideSwitch()", 5000);
});

function showSlide(nr)
{  
   clearInterval(intervalID);
   slideSwitch(nr);
}
