﻿document.observe('dom:loaded', function(ev) {

    // only do anything if there are several slides
    guides = $$('div.focusOn > div.guide');

    numSlides = guides.length;

    if (numSlides > 1) {

        var lastIndex = numSlides - 1;

        // show all the next/prev links
        // add the prev/next actions
        $$('a.previousGuide', 'a.nextGuide').each(function(el) {

            el.setStyle({
                display: 'inline'
            });

            el.observe('click', function(ev) {
                ev.stop();
                el = ev.element(ev);
                if (el.tagName != 'A') {
                    el = el.up('A');
                }
                                
                currentGuide = el.up('.guide');

                if (el.hasClassName('previousGuide')) {

                    previousSiblings = currentGuide.previousSiblings();

                    // if there are no previous slides, the next slide is the last slide in the set                    

                    if (previousSiblings.length == 0) {

                        nextGuide = $$('.focusOn .guide')[lastIndex];

                    } else {
                        nextGuide = currentGuide.previousSiblings()[0];
                    }

                } else {
                    nextSiblings = currentGuide.nextSiblings();
                    // if there are no next slides, the next slide is the first slide in the set
                    if (nextSiblings.length == 0) {
                        nextGuide = $$('.focusOn .guide')[0];
                    } else {
                        nextGuide = currentGuide.nextSiblings()[0];
                    }
                }

                currentGuide.hide();

                if (nextGuide != null) {
                    nextGuide.show();
                }
            });
        });

    }

});