(function($) { $.fn.carousel = function(settings) { var timmer; var numObjects; var objectWidth var currentPost = 1; var numberOfPost = 0; var refObject = this; settings = jQuery.extend({ transitionlength: '6000', fadeOut: 'slow', objectsToRotate: '.carosel_obj', prevButtonObject: '.carosel_prev_button', nextButtonObject: '.carosel_next_button' },settings); //Start It Up! _initialize(); function _initialize(){ var moveNextFN = function() { _move_carousel('next'); }; numObjects = _get_number_of_objects_to_rotate(); if(numObjects > 1) refObject.timmer = window.setInterval(moveNextFN, settings.transitionlength); //console.log('numObjects: ' + numObjects); objectWidth = _get_objects_width(); //console.log('objectWidth: ' + objectWidth); $(settings.prevButtonObject).click(function (){ window.clearInterval(refObject.timmer); _move_carousel('prev'); return false; }); $(settings.nextButtonObject).click(function (){ window.clearInterval(refObject.timmer); _move_carousel('next'); return false; }); $(this).children('.carousel_slider').click(function (){ window.clearInterval(refObject.timmer); return false; }); $(this).children('a').click(function (){ window.location = $(this).attr('href'); }); } function _get_number_of_objects_to_rotate(){ //console.log(refObject.attr('class') + ' count:' + refObject.find(settings.objectsToRotate).length); return refObject.find(settings.objectsToRotate).length; } function _get_objects_width(){ return refObject.find(settings.objectsToRotate).width(); } function _move_carousel(direction){ //console.log(refObject.attr('class') + ' move carosel to' ); refObject.children('.carousel_slider').fadeOut('slow', function() { // Move Images to New Location if(direction == 'prev'){ var newID = _get_prev(); } else { var newID = _get_next(); } var newPosition = _get_position(newID); refObject.children('.carousel_slider').css('marginLeft', '-' + newPosition + 'px'); refObject.children('.carousel_slider').fadeIn('slow', function() { currentPost = newID; }); }); } function _get_prev(){ var prevID; if(currentPost == 1){ prevID = numObjects; } else { prevID = parseInt(currentPost) -1; } //console.log('Move Prev: ' + prevID); return prevID; } function _get_next(){ //console.log('numObjects: ' + numObjects) var nextID; if(currentPost == numObjects){ nextID = 1; } else { nextID = parseInt(currentPost) +1; } //console.log('Move Next: ' + nextID); return nextID; } function _get_position (ID){ return (ID - 1) * objectWidth; } //return this.unbind('click').click(_initialize); }; })(jQuery);