(function($, win) {
    
    $(win).load(function() {
        var 
        heroshot    =   $('#slideshow'),
        images      =   heroshot.find('img'),
        show        =   (heroshot.data('show') || 5) * 1000,
        transition  =   (heroshot.data('transition') || 1) * 1000,
        slider      =   $('<div class="hero-slider" />'),
        maxOffset   =   0;
        images.each(function(index) {
            var image   =   $(this);
            if(index == images.length - 1) {
                maxOffset   =   slider.width();
            }
            image.data('offset', slider.width());
            slider.width(slider.width() + image.width());
            if(image.parent('a').length) {
                slider.append(image.parent('a'));
            } else {
                slider.append(image);
            }
        });
        slider.width(slider.width() + 1);
        images.eq(0).addClass('active');
        heroshot.append(slider);
        function rotateImage() {
            var 
            active  =   images.filter('.active'),
            next;
            if(active.data('offset') == maxOffset) {
                next    =   images.eq(0);
            } else {
                next    =   images.eq(images.index(active) + 1);
            }
            active.removeClass('active');
            next.addClass('active');
            slider.animate({
                "left": (0 - next.data('offset')) + "px"
            }, transition);
            setTimeout(rotateImage, transition + show);
        }
        
        if(images.length > 1) {
            setTimeout(rotateImage, show + transition);
        }
    });
    
})(jQuery, window);
