(function($){ $.fn.Carrousel = function(params) { var speed = (params.speed !== undefined) ? params.speed : 500; var interval = (params.interval !== undefined) ? params.interval : 5000; var auto = (params.auto !== undefined) ? params.auto : 1; var center = (params.center !== undefined) ? params.center : 0; var addnextprev = (params.addnextprev !== undefined) ? params.addnextprev : 0; var livenextprev = (params.livenextprev !== undefined) ? params.livenextprev : 1; var obj = $(this); var count = $(obj).find('li').size(); var timer = 1; var ready = 1; function setupCarrousel() { var item_width = $(obj).find('li').width(); var item_height = $(obj).find('li').height(); var list_width = 0; $(obj).find('img').each(function() { list_width += $(this).closest('li').outerWidth(); $(obj).find('ul').css('width',list_width); }); $(obj).find('ul').css('height',item_height); } function autoSlide() { clearInterval(timer); timer = setInterval(function(){ if(auto == 1) { slide(); } }, interval); } function slide(direction) { if(typeof(direction)==='undefined') direction = 'right'; var holder_width = $(obj).children('.holder').width(); var list_width = $(obj).children('.holder').children('ul').width(); var step = Math.round(holder_width / 4); var pos_crnt = parseInt($(obj).find('ul').css('left')); var pos_max = -(list_width - holder_width); var pos_next; ready = 0; if(direction == 'left') { pos_next = pos_crnt+step; } if(direction == 'right') { pos_next = pos_crnt-step; } if(pos_next < pos_max || pos_next > 0) { if(Math.abs(pos_crnt) == Math.abs(pos_max)) { pos_next = 0; } else { pos_next = pos_max; } } $(obj).find('ul').delay(100).animate({ left: pos_next },speed,function(){ ready = 1; }); } function centerCarrousel() { $(obj).find('ul').find('span.num').parent().click(function() { var auto = 0; var id = $(this).find('span.num').html(); var item_width = $(obj).find('li').width(); var step = Math.round($(obj).width() / item_width / 2); if(id>step) { if(id>=(count-step)) { pos_next = -(item_width*(count-$(obj).width() / item_width)); } else { pos_next = -(item_width*(id-step)); } } else { pos_next = 0; } $(obj).find('ul').delay(100).animate({ left: pos_next },speed,function(){ ready = 1; }); }); } function liveNextPrev() { $(obj).find('a.nextprev').click(function() { if($(this).hasClass('next') && ready == 1) { slide('right'); } if($(this).hasClass('prev') && ready == 1) { slide('left'); } }); } return this.each(function(){ setupCarrousel(); if(center == 1) { centerCarrousel(); } if(auto == 1) { autoSlide(); } if(addnextprev == 1) { addNextPrev(); liveNextPrev(); } if(addnextprev == 0 && livenextprev == 1) { liveNextPrev(); } }); } })(jQuery);