var pausePosition = 5000;
var currentPosition = 0;
var slideWidth = 960;
var numberOfSlides;
var slideTimer;

$(document).ready(function() {
    $('#slideshow').hide();

    // Get current collection of slides
    var slides = $('.slide');
    numberOfSlides = slides.length;

    // Add "wrapper" slides on either end
    //$('.slide:first').prepend(slides[numberOfSlides - 1]);
    $('.slide:last').after($('.slide:first').clone());
    $('.slide:first').before($('.slide:last').prev().clone());
    // Update collection of slides
    slides = $('.slide');
    numberOfSlides = slides.length;

    // Remove scrollbar for container
    $('#slidesContainer').css('overflow', 'hidden');

    // Put slides into a "slidestrip"
    slides.wrapAll('<div id="slideInner"></div>')
    // Put slides side by side
	    .css({
	        'float': 'left',
	        'width': slideWidth
	    });

    // Make sure slide strip is wide enough to accomdate all slides
    $('#slideInner').css('width', slideWidth * numberOfSlides);

    // Add arrows to slide show
    $('#slideshow')
        .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
        .append('<span class="control" id="rightControl">Clicking moves right</span>');

    // Set starting position of slide show
    currentPosition = 1;
    $('#slideInner').css({ 'marginLeft': slideWidth * (-currentPosition) });

    // Event for arrow controls
    $('.control')
            .bind('click',
                function() {
                    // Stop any automatic slide show
                    clearTimeout(slideTimer);
                    // If right arrow clicked move forward one slide, otherwise move back
                    if ($(this).attr('id') == 'rightControl') {
                        moveSlide(1);
                    }
                    else {
                        moveSlide(-1);
                    }
                    slideTimer = setTimeout(timerFunction, pausePosition * 4);
                }
            );

    var timerFunction = function() {
        moveSlide(1);
        slideTimer = setTimeout(timerFunction, pausePosition);
    }
    slideTimer = setTimeout(timerFunction, pausePosition);
    $('#slideshow').show();

});

function moveSlide(positionChange) {
    var startMove = slideWidth * (-currentPosition);
    // If right arrow clicked move forward one slide, otherwise move back
    currentPosition = currentPosition + positionChange;
    // If at the end of the slide show, wrap back to the start slide
    if (currentPosition == numberOfSlides - 1) {
        currentPosition = 1;
        startMove = 0;
    }
    // If we've moved backwards to the start, wrap back to the last slide
    if (currentPosition == 0) {
        startMove = slideWidth * (-(numberOfSlides - 1));
        currentPosition = numberOfSlides - 2;
    }
    // Move slide strip from start to end animation position
    $('#slideInner').css({ 'marginLeft': startMove });
    $('#slideInner').animate({
        'marginLeft': slideWidth * (-currentPosition)
    }, 'fast');
}

