(function(jQuery){
  jQuery.fn.carousel = function(options) {
    // build main options
    var $this = jQuery(this);
    var interval = null;
    var index = 0;
    var tabCount = 0;
    var opts = jQuery.extend({}, jQuery.fn.carousel.defaults, options);
    return this.each(function(){
      initialize();
    });
  
    function initialize() {
      var tabs = jQuery('.'+opts.tabClass);
      tabCount = tabs.length;
      tabs.each(function() {
        var tab = jQuery(this);
        tab.hide();
        tab.css({
          position: 'absolute'
        });
      });
      jQuery(tabs[0]).fadeIn(opts.fadeSpeed);
      interval = setInterval(function() {
          flip();
        },
        opts.speed
      );
    };
    
    function flip() {
      var tabs = jQuery('.'+opts.tabClass);
      jQuery(tabs[index]).fadeOut(opts.fadeSpeed);
      index = index + 1 == tabCount ? 0: index + 1;
      jQuery(tabs[index]).fadeIn(opts.fadeSpeed);
    };
  };
  
  jQuery.fn.carousel.defaults = {
      speed: 3000,
      fadeSpeed: 750,
      tabClass: null,
      reverse: false,
      useLinks: false
  };
})(jQuery);
