﻿//  ==============================================================================
//  Adapted from a script by Edward O'Connor (http://edward.oconnor.cx/)
//  http://edward.oconnor.cx/2009/08/marking-up-a-slideshow-in-html5
//  Licensed under CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/
//  ==============================================================================
    
    
    var Presentation = function() {
    var slides = null;
    var current_slide = 0;
    var num_slides = 0;
    var ShowStarted = false;
    var int = null;

    function set_current_slide(index) {
        current_slide = index;

        var slide = $(slides.get(index));

        slides.not(slide).hide();
        slide.show();

        // Update hash if this slide has an @id
        var id = slide.attr('id');
        if (id) {
            location.hash = id;
        }
    }

    function set_slide(offset) {
        var index = current_slide + offset;
        if (index < 0) index = (num_slides - 1);
        if (index > (num_slides - 1)) index = (0);
        set_current_slide(index);
    }

    function previous_slide() {
        set_slide(-1);
        return false;
    }

    function next_slide() {
        set_slide(1);
        return false;
    }

    // Run through the slides automatically, switching every 15 seconds.
    function ignite() {

        if (ShowStarted === false) {
            ShowStarted = true;
            int = setInterval(next_slide, 5 * 1000);
            return false;
        }
    }


    return function() {
        slides = $('div.deck > div');
        num_slides = slides.size();

        // If there's a hash, start there instead of the first slide
        if (location.hash != "") {
            set_current_slide(slides.index($(location.hash).get(0)));
        } else {
            set_current_slide(0);
        }

        $('#previous-slide').click(previous_slide);
        $('#next-slide').click(next_slide);
        $('.deck').hover(
        function() {
            int = clearInterval(int);
        },
         function() {
             int = setInterval(next_slide, 5 * 1000);
         }
         );
        $('body').mouseenter(ignite);
    };
} ();

$(document).ready(Presentation);
