(function($) {                                          // Compliant with jquery.noConflict()
$.fn.carousel = function(o) {
    o = $.extend({
      speed: 200,
      visible: 6,
      scroll: 3
    }, o || {});

    return this.each(function(i) {

      var running = false;
      var div = $(this), ul = $('ul', div), v = o.visible;
      var divSize = div.width()-50;
      
      var pr = $('.carousel-prev', div), nt = $('.carousel-next', div);
      
      var li = $('li', ul), itemLength = li.size(), curr = 0;

      console.log($(this).width());
      
      var liSize = Math.ceil(divSize/v);
      var ulSize = liSize * itemLength + 50;

      li.css('width', liSize+'px');
      ul.css('width', ulSize+'px');
      
      pr.addClass('disabled');
      
      if (v >= itemLength) {
        nt.addClass('disabled');
      }

      pr.click(function() {
        return go(curr-o.scroll, $(this));
      });

      nt.click(function() {
        return go(curr+o.scroll, $(this));
      });

      function go(to, btn) {
        if(!running) {

          if(to < 0 || to > itemLength-v) {
            return;
          } else {
            curr = to;
          }

          running = true;

          ul.animate({marginLeft: -(curr*liSize)}, o.speed, function() {running = false;});

          pr.removeClass('disabled');
          nt.removeClass('disabled');
          
          if (curr-o.scroll < 0 || curr+o.scroll > itemLength-v) {
            $(btn).addClass('disabled');
          }
        }
        return false;
      };
  });
};

})(jQuery);
