$.fn.infiniteCarousel = function () {

  function repeat(str, num) {
    return new Array( num + 1 ).join( str );
  }

  return this.each(function () {
    var $wrapper = $('.wrapper', this),
        $ui_wrapper = $('.ui-wrapper', this),
        $slider = $wrapper.find('> ul.slider'),
        $nav = $ui_wrapper.find('> ul.nav'),
        $items = $slider.find('> li'),
        $single = $items.filter(':first'),
        singleWidth = $single.outerWidth(true),
        //visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
        visible = 1,
        currentPage = 1,
        pages = Math.ceil($items.length / visible)
        faded_opacity = 0.15,
        animate_duration = 511;

    // pad so that 'visible' number will always be seen, otherwise create empty items
    if (($items.length % visible) != 0) {
      $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
      $items = $slider.find('> li');
    }

    // top and tail the list with 'visible' number of items, top has the last section, and tail has the first
    $items.filter(':first').before($items.slice(-(visible+1)).clone().addClass('cloned'));
    $items.filter(':last').after($items.slice(0, visible+1).clone().addClass('cloned'));
    $items = $slider.find('> li'); // reselect

    // set the left position to the first 'real' item, and set overflow hidden property
    $wrapper.css('overflow', 'hidden')
    $items.not('.s'+currentPage).css({opacity: faded_opacity});

    function reset() {
      var w = $(window).width(),
      w2 = w-920,
      pad = parseInt($wrapper.css('padding-left'));
      $wrapper
        .scrollLeft(singleWidth * (visible+currentPage) - (w2/2)+pad)
        .width(w-104);
    }
    reset();
    $(window).resize(reset);

    // paging function
    function gotoPage(page) {
      if($wrapper.is(':animated') || page == currentPage)
        return false;
      
      var dir = page < currentPage ? -1 : 1,
          n = Math.abs(currentPage - page),
          left = singleWidth * dir * visible * n;

      if (page == 0) {
        page = pages;
      } else if (page > pages) {
        page = 1; // reset back to start position
      }

      // move current class to the correct nav item
      $('.n'+currentPage, $nav).removeClass('current');
      $('.n'+page, $nav).addClass('current');

      // fade out current page and fade in new page
      $('.s'+currentPage, $slider).animate({opacity: faded_opacity}, animate_duration);
      $('.s'+page, $slider).css({opacity: 1});

      $wrapper.animate({
        scrollLeft : '+=' + left
      }, animate_duration, 'easeOutBack', function () {
        currentPage = page;
        reset();
      });

      return false;
    }

    $ui_wrapper.append('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');

    var return_false = function() {return false};

    $('a', $nav).mousedown(function() {
     return gotoPage($(this).parent().index()+1);
    }).click(return_false);

    // bind to the forward and back buttons
    $('a.back', this).mousedown(function () {
      return gotoPage(currentPage - 1);
    }).click(return_false);

    $('a.forward', this).mousedown(function () {
      return gotoPage(currentPage + 1);
    }).click(return_false);

    // keep track of mouse x coordinate
    var mouse_x = null;
    $(window).bind('mousemove', function(e) {
      mouse_x = e.pageX;
    });
    
    
  });
};

$(document).ready(function () {
  $('#infiniteCarousel').infiniteCarousel();
});
