$(document).ready(function() {
    slideTime = 1000;
    pauseTime = 3000;
    autoSlide = false;
    var sliderWidth = $('#enclosing').width();
    var sliderHeight = $('#enclosing').height();
    var position = 0;
    var itemCount = $('#slider').children().length;
    var $slides = $('#slider').children();
    $slides.css({ left: 0, top: 0 })
                    .width(sliderWidth)
                    .height(sliderHeight);

    function slideTo(i) {
    // this code allows us to provide the illusion of a circular slide show
        if (position == 0)
            $('#slider').css({ left: 0, top: 0 });

        if (position == itemCount - 1 && i == 0) {
            $('#slider').animate({ left: -1 * itemCount * sliderWidth, top: 0 }, 
                slideTime);
        } else {
            $('#slider').animate({ left: -1 * i * sliderWidth, top: 0 }, 
                slideTime);
        }
        position = i;
    }

    // rotates the target elements in increasing numerical order
    function rotate() {
        slideTo((position + 1) % itemCount);
    }

    var intervalID = false; // track the auto interval so we can clear it

    function autoOff() {
        clearInterval(intervalID);
        autoSlide = false;
    }

    function autoOn() {
        autoSlide = true;
        intervalID = setInterval(rotate, pauseTime + slideTime);
    }

    function toggleAuto() {
        if (!autoSlide) {
            autoOn();
        } else {
            autoOff();
        }
    }

    // set up the auto toggle
    $('#auto').click(toggleAuto);

    // append a slide to circularize the show
    $('#slider').append($($slides[0]).clone());

    // move the children to the right places
    var i= 0;
    $('#slider').children().each(function() {
        $(this).css({left: i * sliderWidth});
        ++i;
    });

    // set up the controls
    i = 0;
    $('#controls .slideTo').each(function() {
        this.position = i;
        ++i;
    });

       $('#controls .slideTo').click(function() {
       autoOff();
       slideTo(this.position);
   });

//   autoOn();   auto scroll on load

});