function Slider( elem, params ){ this.element = jQuery(elem); this.params = params || {}; this.autoPlay = !!this.params.autoPlay || false; this.interval = this.params.interval || 5000; this.duration = this.params.duration || 500; this.classActiveDirect = this.params.classActiveDirect || 'JS-Slider-Direct-active'; this.__construct(); }; Slider.prototype.__construct = function(){ this._isAnim = false; this._timeout = false; this._index = 0; this.viewport = this.element.find('.JS-Slider-Viewport'); this.list = this.element.find('.JS-Slider-List'); this.items = this.list.find('.JS-Slider-Item'); this.toolbar = this.element.find('.JS-Slider-Toolbar'); this.directs = this.toolbar.find('.JS-Slider-Direct').remove(); if( this.items.length > 1 ){ this._init(); }else{ this.toolbar.remove(); } }; Slider.prototype._init = function(){ var i, itemWidth, context = this, len = this.items.length - 1; this.toolbar.html('').append(this.directs); for( i=0; i < len; i++ ){ this.toolbar.append(this.directs.clone(false).removeClass('JS-Slider-Direct-active').removeClass(this.classActiveDirect)); } this.directs = this.toolbar.find('.JS-Slider-Direct'); do{ len++; itemWidth = 100 / len; }while( itemWidth != parseInt(itemWidth) ) this._itemWidth = itemWidth; this.list.css({'position' : 'relative', 'width' : 100*len + '%', 'left' : '0%'}); this.items.css({'width' : this._itemWidth + '%', 'display' : 'block'}); this.directs.bind('click', function(){ return context.do_direct.apply(context, [this]) }); this.startPlay(); }; Slider.prototype.do_direct = function( elem ){ var direct = jQuery(elem); if( !direct.hasClass('JS-Slider-Direct-active') ){ this.stopPlay(); this.show(this.directs.index(direct)); } return false; }; Slider.prototype.show = function( index, dir ){ var duration = parseInt(this.duration); shift = this.items.eq(index).width(); duration = isNaN(duration) ? shift : duration; this._isAnim = true; if( index == this._index ){ this._isAnim = false; }else if( (index > this._index && dir != 'prev') || dir == 'next' ){ this._next(index, duration); }else if( (index < this._index && dir != 'next') || dir == 'prev' ){ this._prev(index, duration); } }; Slider.prototype._prev = function( index, duration ){ var context = this; this.items.eq(index) .css({'margin-left' : -this._itemWidth + '%'}) .prependTo(context.list) .animate({'margin-left' : '0%'}, duration, function(){context.cb_show.apply(context, [index])}); }; Slider.prototype._next = function( index, duration ){ var context = this; this.items.eq(this._index) .after(this.items.eq(index)) .animate({'margin-left' : -this._itemWidth + '%'}, duration, function(){context.cb_show.apply(context, [index])}); }; Slider.prototype.cb_show = function( index ){ this._isAnim = false; this.directs.removeClass('JS-Slider-Direct-active').removeClass(this.classActiveDirect) .eq(index).addClass('JS-Slider-Direct-active').addClass(this.classActiveDirect); this.items.eq(this._index).remove().css({'margin-left' : '0%'}).appendTo(this.list); this._index = index; this.startPlay(); }; Slider.prototype.startPlay = function(){ var context = this; if( this.autoPlay && this._timeout === false ){ this._timeout = setInterval(function(){ context.play.call(context) }, this.interval); }; }; Slider.prototype.stopPlay = function(){ clearInterval(this._timeout); this._timeout = false; this._isAnim = false; }; Slider.prototype.play = function(){ if( this._timeout !== false && !this._isAnim ){ var index = this._index + 1; this.show(index >= this.items.length ? 0 : index, 'next'); } }; /*--/Slider--*/