var numSlides = 0; var currentSlide = 0; $(document).ready(function() { $(document).keyup(handleKeys); numSlides = $("div.slide").css("display", "none").click(nextSlide).each(function (i) { $(this).attr("id", "slide_" + i); }).size(); showSlide(); }); function handleKeys(e) { var keyID = (window.event) ? event.keyCode : e.keyCode; switch(keyID) { case 32: nextSlide(); break; case 37: prevSlide(); break; case 38: setSlide(0); break; case 39: nextSlide(); break; case 40: lastSlide(); break; } } function showSlide() { $("div.slide").css("display", "none"); $("#slide_" + currentSlide).css("display", "inline"); } function setSlide(number) { currentSlide = number; showSlide(); } function nextSlide() { currentSlide++; if (currentSlide >= numSlides) { currentSlide = 0; } showSlide(); } function prevSlide() { currentSlide--; if (currentSlide < 0) { currentSlide = 0; } showSlide(); } function lastSlide() { currentSlide = (numSlides - 1); showSlide(); }