$(document).ready(function(){
  jQuery.extend( jQuery.easing,
  {
    easeinout: function (x, t, b, c, d) {
      if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
      return c/2*((t-=2)*t*t*t*t + 2) + b;
    }
  });

  var $carousel = $('.carousel'),
      $carouselHolder = $('.carousel-holder', $carousel),
      $items = $('.item', $carouselHolder),
      $switches = $('.switcher li', $carousel),
      current = Math.round(Math.random() * ($switches.length - 1)),
      showNextTimeout = 5000,
      showNextTimer,
      animationDuration = 1200,
      animationEasing = 'easeinout',
      setupShowNextAnimation = function(){
        clearInterval(showNextTimer);
        showNextTimer = setInterval(showNext, showNextTimeout);
      },
      showNext = function(){
        $switches.eq(current).removeClass('active');
        current++;
        $carouselHolder.clearQueue().animate({left: - $items[current].offsetLeft}, animationDuration, animationEasing, function(){
          $carouselHolder.css({left: - $items[current].offsetLeft});
        });
        if (current >= $switches.length ) current = 0;
        $switches.eq(current).addClass('active');
      };

  $switches.eq(current).addClass('active');
  $carouselHolder.css({left: - $items[current].offsetLeft});


  $items = $items.add($items.eq(0).clone().appendTo($carouselHolder));
  $switches.each(function(index){
    $(this).click(function(){
      setupShowNextAnimation();
      $switches.eq(current).removeClass('active');
      current = index;
      $switches.eq(current).addClass('active');
      $carouselHolder.clearQueue().animate({left: - $items[current].offsetLeft}, animationDuration, animationEasing);
      return false;
    });
  });

  setupShowNextAnimation();
});
