// Slideshow time between slides (in ms) var nSlideshowTimer = 5000; // Slideshow fade speed (in ms) var nSlideshowFadeSpeed = 1000; // On document ready (When the page is loaded) $(document).ready( function() { // Trigger the slideshow setTimeout( function () { // Handle the slideshow HandleSlideshow(); }, nSlideshowTimer); }); /* HandleSlideshow() ================= Handles the slideshow, is called repetativly by itself */ function HandleSlideshow() { // Store the slides var aSlides = $(".slide"); // Store the current slide index var nCurrentSlideIndex = -1; // Store the next slide index var nNextSlideIndex = -1; // If there is only one slide if( aSlides.length == 1 ) { // Exit the function return; } // Loop through all the slides for( var nIndex = 0; nIndex < aSlides.length; nIndex++ ) { // Turn the item into a jquery item aSlides[nIndex] = $( aSlides[nIndex] ); // If this slide is active if( aSlides[nIndex].hasClass("slide-active") ) { // Store the current slide image nCurrentSlideIndex = nIndex; // Store the next slide image nNextSlideIndex = nIndex + 1; // If the next slide image index is out of range if( nNextSlideIndex == aSlides.length ) { // Set the next slide to the first slide nNextSlideIndex = 0; } // Turn the next slide into a jquery item aSlides[nNextSlideIndex] = $( aSlides[nNextSlideIndex] ); // Exit the loop break; } } // If the next slide doesn't have the required class if( !aSlides[nNextSlideIndex].hasClass("slide-next") ) { // Add the required class (next slide class) aSlides[nNextSlideIndex].addClass("slide-next"); } // Fade the current slide out aSlides[nCurrentSlideIndex].fadeOut( nSlideshowFadeSpeed, function() { // Remove the next slide class aSlides[nNextSlideIndex].removeClass("slide-next"); // Add the current slide class aSlides[nNextSlideIndex].addClass("slide-active"); // Remove the current slide class aSlides[nCurrentSlideIndex].removeClass("slide-active"); // Trigger the slideshow again setTimeout( function () { // Handle the slideshow HandleSlideshow(); }, nSlideshowTimer); }); }