(function($){

  var defaults = {
    
    item      : '.item',
    container : '',
    speed     : 600,
    easing    : 'swing',
    wrap      : false,
    initCallback : function (){},
    nextBtnTemplate : '<a href="#" class="next">Next</a>',
    prevBtnTemplate : '<a href="#" class="prev">Previous</a>'
        
  };
  
  var moveTo = function ( p, conf ) {
    
    conf.container
      .stop()
      .animate({ 'scrollLeft': p }, 
      conf.speed, 
      conf.easing,
      function () { 
        updateIndicator( conf );
      }
    );
  };
  
  var buildControls = function ( conf ) {
    var ctrl = $('<div class="controls"></div>');
    // don't make back/fwd buttons for single screens
    if (conf.numScreens > 1) {
      ctrl
        .append(
          $( conf.prevBtnTemplate )
            .click(function(){
              var p = conf.container.scrollLeft() - conf.windowSize;
              p = Math.floor( p / conf.windowSize ) * conf.windowSize;
              p = ( conf.wrap && p < 0 ) ? conf.maxScroll : Math.max( 0, p );
              moveTo( p, conf );
              return false;
            })
          )
        .append(
          $( conf.nextBtnTemplate )
            .click(function(){
              var p = conf.container.scrollLeft() + conf.windowSize;
              p = Math.ceil( p / conf.windowSize ) * conf.windowSize;
              p = ( conf.wrap && p > conf.maxScroll ) ? 0 : Math.min( conf.maxScroll, p ) ;+
              moveTo( p, conf );
              return false;
            })
          )
    }
    ctrl.append('<div class="direct"></div>');
    return ctrl;
  };

  var updateIndicator = function ( carousel ) {

    var dir = $( '.direct', carousel.controls );

    // deactivate old current
    $( '.current', dir ).removeClass( 'current' );

    // current screen?
    var cs = (carousel.container.scrollLeft() / carousel.windowSize);
    $( 'span.i', carousel.controls ).eq( cs ).addClass( 'current' );
    
  };

  $.fn.extend({

  	simpleCarousel : function( config ) {
      
      this.each(function(){
        
        var conf = $.extend( {}, defaults, config );
  
        conf.handle = $( this );
        conf.handle.addClass( 'carousel-active' );
        
        conf.container = conf.handle.wrap( '<div class="container"></div>' ).parent();
        conf.container.scrollLeft( 0 );
        conf.windowSize = conf.container.eq(0).width();
        
        conf.items = $( conf.item, conf.handle );
        
        for (var i = conf.itemsWidth = 0; i<conf.items.length; i++)  {
          conf.itemsWidth += conf.items.eq(i).outerWidth();
        }
        
        conf.itemsPadded = Math.ceil( conf.itemsWidth / conf.windowSize ) * conf.windowSize;
        
        conf.maxScroll   = conf.itemsPadded - conf.windowSize;
        conf.numScreens  = conf.itemsPadded / conf.windowSize;
        conf.handle.width( conf.itemsPadded );
  
        // build controls and add them
        conf.controls = buildControls( conf );
        var indicators = '';
        for (var i=0; i<conf.numScreens; i++) {
          indicators += '<span class="i"><i>' + i + '</i></span>';
        }
        var dir = $('.direct', conf.controls).append( indicators );
        conf.container.after( conf.controls );
        updateIndicator( conf );
  
        conf.initCallback( conf );
      })

    }

  });


})(jQuery);