$(document).ready(function() {

    var imageWidth = $(".window").width();
    var imageSum = $(".image_reel span").size();
    var imageReelWidth = imageWidth * imageSum;

    $(".image_reel").css({'width' : imageReelWidth});

    rotate = function() {
        var triggerID = $active.attr("rel") - 1;
        var image_reelPosition = triggerID * imageWidth - triggerID;
        $(".paging_helper a").removeClass('active');
        $active.addClass('active');
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 500 );
    };

    rotateSwitch = function(){
        play = setInterval(function() {
            $active = $('.paging_helper a.active').next();
            if ( $active.length === 0) {
                $active = $('.paging_helper a:first');
            }
            rotate();
        }, 8000);
    };

    rotateSwitch();

    $(".image_reel a").hover(function() {
        clearInterval(play); 
    }, function() {
        rotateSwitch();
    });

    $(".paging a").click(function() {
        $active = $(this);
        clearInterval(play);
        rotate();
        rotateSwitch();
        return false;
    });

});
