I have a given list of images, presented as thumbnails:
&
You may easily calculate the next image within the switch function. This way you won't mess up your var space ;)
(function switchToImage(img) {
$(img).click()
var images = $('#thumbs img');
var nextIndex = ($.inArray(images, img) + 1) % images.length;
setTimeout(function() {
switchToImage(images[nextIndex])
}, 5000);
})($('#thumbs img')[0]);