// Pictures var photos = [ { "image" : "/sites/all/themes/longwaydown/images/titles/title_5.png" }, { "image" : "/sites/all/themes/longwaydown/images/titles/title_1.png" }, { "image" : "/sites/all/themes/longwaydown/images/titles/title_2.png" }, { "image" : "/sites/all/themes/longwaydown/images/titles/title_3.png" }, { "image" : "/sites/all/themes/longwaydown/images/titles/title_4.png" } ]; // Preload all images (function($) { var cache = []; // Arguments are image paths relative to the current page. $.preLoadImages = function() { var args_len = photos.length; for (var i = args_len; i--;) { var cacheImage = document.createElement('img'); cacheImage.src = photos[i].image; cache.push(cacheImage); } } })(jQuery) jQuery.preLoadImages(); $(document).ready(function() { var interval; var activeContainer = 1; var currentImg = 0; var animating = false; var navigate = function(direction) { // Select next image currentImg++; if(currentImg == photos.length + 1) { currentImg = 1; } // Check which container we need to use var currentContainer = activeContainer; if(activeContainer == 1) { activeContainer = 2; } else { activeContainer = 1; } showImage(photos[currentImg - 1], currentContainer, activeContainer); }; var currentZindex = -1; var showImage = function(photoObject, currentContainer, activeContainer) { animating = true; // Make sure the new container is always on the background currentZindex--; // Set the background image of the new active container $("#headerimg" + activeContainer).css({ "background-image" : "url(" + photoObject.image + ")", "display" : "block", "z-index" : currentZindex }); // Fade out the current container $("#headerimg" + currentContainer).fadeOut(1000, function() { setTimeout(function() { animating = false; }, 500); }); }; // We should statically set the first image navigate("next"); // Start playing the animation interval = setInterval(function() { navigate("next"); }, 6000); });