/** * */ ;(function($, window, undefined) { $.slider = function(element, options) { // default plugins options var defaults = { speed: 8000, effect: 'fade', effectSpeed: 2000 }; var s = this, $wrapper = $(element); s.opts = {}; s.vars = {}; /** * */ s.init = function() { $.extend(s.opts, defaults, options); s.images = $wrapper.children('img'); s.images.css('display', 'none'); // se the current image s.vars.currentIndex = 0; s.vars.maxIndex = s.images.length - 1; s.vars.currentImage = $(s.images[s.vars.currentIndex]); s.vars.currentImage.show(); s.start(); bindEvents(); } var bindEvents = function() { if(s.opts.pauseOnHover) { } } /** * */ s.start = function() { s.vars.timer = setTimeout(function() { s.stepTo(); }, s.opts.speed); } /** * */ s.stop = function() { clearTimeout(s.vars.timer); } /** * */ s.stepTo = function(index) { // avoid effect conflicts if(index == s.vars.currentIndex) return; // default forward if(!index) { index = s.vars.currentIndex == s.vars.maxIndex ? 0 : s.vars.currentIndex + 1; } var current = $(s.images[s.vars.currentIndex]), next = $(s.images[index]); s.vars.currentIndex = index; s.images.css('z-index', 1); current.css({ display: 'block', zIndex: 2 }); switch(s.opts.effect) { case 'fade': default: current.fadeOut(s.opts.effectSpeed); next.css({ display: 'none', zIndex: 3 }).fadeIn(s.opts.effectSpeed, s.start); break; } // reset the loop //s.vars.timer = setTimeout(s.stepTo, s.opts.speed); } /** * */ s.backward = function() { s.forward(s.vars.currentIndex == 0 ? s.vars.maxIndex : s.vars.currentIndex - 1); } // init the plugin s.init(); } $.fn.slider = function(options) { return $(this).each(function() { if (undefined == $(this).data('slider')) { var slider = new $.slider(this, options); $(this).data('slider', slider); } }); } })(jQuery, window);