/*** 
    Simple jQuery slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
    
    Extended by: Media Motion AG
***/

function slideSwitch() {
    
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    
    // Aktives Bild in Box zeigen
    if ($('#boxes').find('span:last').hasClass('activeThumb')) {
        $('#boxes').find('span:last')
            .removeClass('activeThumb');
        $('#boxes').find('span:first').addClass('activeThumb');
    } else {
        $('#boxes').find('span.activeThumb')
            .removeClass('activeThumb')
            .next().addClass('activeThumb');
    };
}

$(function() {
    


    if( $('#slideshow').find('img').length != 1 ) {
        $('#slideshow').find('img:first').addClass('active');
        setInterval( "slideSwitch()", 5000 );
        
        // Boxes generieren
        var anzahlBilder = $('#slideshow').find('img').length;
        var i = 0;
        while ( i < anzahlBilder ){
            $('#boxes').append('<span></span>');
            i++;
        }
        
        // Erstes Thumb auf aktiv setzen
        $('#boxes').find('span:first').addClass('activeThumb');
        
        // Event-Handler auf Boxen setzen
        $('#boxes').find('span').each(function(i) {
            
            // Klick befehl
            $(this).click(function(){
                $('#slideshow').find('img').removeClass('active last-active');
                $('#slideshow').find('img').eq(i).addClass('active');
                $('#boxes').find('span').removeClass('activeThumb');
                $(this).addClass('activeThumb');
            });
            
        });
        
    } else {
        $('#slideshow').find('img').css('opacity', 1);
    };
    
    
});
