/**
 * Detect slideshows in pages
 * and start animating them
 */
function detectSlideshows()
{
    // List of slideshow elements
    var slideshows = $('.js-slideshow');
    
    if (slideshows.length > 0)
        setInterval("slideSwitch()", 5000);
}

function slideSwitch()
{
    var slideshows = $('.js-slideshow');
    slideshows.each(function()
    {
        var top = $(this);
        var active = top.find('img.active');

        if (active.length == 0)
            active = top.find('img:last');

        var next =  active.next().length ? active.next()
            : top.find('img:first');

        active.addClass('last-active');

        next.css({opacity: 0.0})
            .addClass('active')
            .animate({opacity: 1.0}, 1000, function() {
                active.removeClass('active last-active');
            });
    });
}

$(detectSlideshows);

