window.slideshow = { /** * Number of slideshow items */ ITEMCOUNT : 19, /** * Delay between images in milliseconds */ DELAY : 3000, /** * Image width in pixels */ IMAGE_WIDTH : 480, /** * Current slide index */ currentIndex : 0, /** * Timer */ timer : null, /** * Init */ init : function() { var i, ilen, images = [], img, slides = document.getElementById('slides'); // Randomize images: for (i = 0, ilen = this.ITEMCOUNT; i < ilen; ++i) { images[images.length] = i + 1; } images = this.shuffle(images); // Add images to DOM: for (i = 0, ilen = this.ITEMCOUNT; i < ilen; ++i) { img = document.createElement('img'); img.src = 'images/slide' + images[i] + '.jpg'; slides.appendChild(img); } // Add end slide: img = document.createElement('img'); img.src = 'images/slide_end.jpg'; slides.appendChild(img); // Start the timer: this.resetTimer(); return this; }, /** * Previous slide */ prev : function() { this.resetTimer(); if (this.currentIndex > 0) { --this.currentIndex; }else{ this.currentIndex = this.ITEMCOUNT; } this.showSlide(this.currentIndex); return this; }, /** * Next slide */ next : function() { this.resetTimer(); if (this.currentIndex < this.ITEMCOUNT) { ++this.currentIndex; }else{ this.currentIndex = 0; } this.showSlide(this.currentIndex); return this; }, /** * Show specific slide */ showSlide : function(index) { var slides = document.getElementById('slides'), position = (0 - index * this.IMAGE_WIDTH) + 'px'; slides.setAttribute('style', '-webkit-transform: translateY(' + position + '); -moz-transform: translateY(' + position + '); -ms-transform: translateY(' + position + '); -o-transform: translateY(' + position + '); transform: translateY(' + position + ');'); return this; }, /** * Reset timer */ resetTimer : function() { if (this.timer !== null) { window.clearInterval(this.timer); } this.timer = window.setInterval(function() { window.slideshow.next(); }, this.DELAY); return this; }, /** * Shuffle array */ shuffle : function(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } }; window.onload = window.slideshow.init();