// By p.schalm
// v 0.2.0
var _scroller = null;

function _scroller_moveNext() {
  var next = _scroller.scroller.wrapper.find( _scroller.scroller.item_selector + '.active' ).next();
  var url = next.attr('rel');
  next.click();
  _scroller.scroller.moveRight( null, 1 );
  return url;
}

jQuery.fn.scroller = function(s, lbut, rbut, item_selector, options) {
  if ( s ) {
    $(this).each(function(){
      this.options = {
        wrapAround: true,
        perPage: 4,
        speed: 300
      }
      
      jQuery.extend( this.options, options );
      
      this.scroller = {
        item_selector: item_selector,
        wrapper: $(this).find('.wrapper:first')
      }
      var self = _scroller = this;
      var scroller = $(this).find(s);
      if (self.scroller.wrapper.children(item_selector).size() == 0) { return; }

      var base_width = self.scroller.wrapper.children(item_selector).eq(0).innerWidth();
      
      this.scroller.moveRight = function(e, n) {
        if (e) { e.preventDefault(); }
        
        var perPage = n || self.options.perPage;
        
        var node = self.scroller.wrapper.children(item_selector + ':first');
        var i = 0;
        while ( node.size() && node.css('margin-left') != "0px" && node.css('margin-left') != "0" && node.css('margin-left') != "auto" ) {
          i++;
          node = node.next();
        }
        
        (function( nodeset ) {
          node = nodeset.eq( 0 );
          if ( 
            ( self.scroller.wrapper.children( item_selector ).size() - i ) * node.outerWidth( true ) < self.scroller.wrapper.parents('.scroller').width()
            ) {
              return;
            }
            
          var callee = arguments.callee;
          node.animate({
            "marginLeft": -( base_width + 100 ) + "px",
            "marginRight": 100 + "px"
          }, self.options.speed, 'linear', ( self.options.wrapAround ? 
            (function() {
              $(this).appendTo(self.scroller.wrapper).attr('style','').css({
                'marginLeft': 0,
                'marginRight': '10px'
              });
              callee( nodeset.slice( 1 ) );
            }) : 
            (function() { 
              callee( nodeset.slice( 1 ) );
            })  
          ));
        })( $( node.add( node.nextAll().splice( 0, perPage - 1 ) ) ) )
      };
        
      this.scroller.moveLeft = function(e, n) {
        if (e) { e.preventDefault(); }
      
        var perPage = n || self.options.perPage;
        
        var node = self.scroller.wrapper.children(item_selector + ':first');
        while ( node.size() 
          && node.next().css('margin-left') != "0px" 
          && node.next().css('margin-left') != '0' 
          && node.next().css('margin-left') != 'auto' 
        ) {
          node = node.next();
        }
        
        (function( nodeset ) {
          node = nodeset.eq( 0 );
          if ( node.prev().size() == 0 && node.css('margin-left') == '0px' ) { return; }
          node.css({
              'marginLeft': -( base_width + 100 ) + "px",
              'marginRight': 100 + 'px'
            });
          if ( self.options.wrapAround ) {
            node.prependTo( self.scroller.wrapper );
          }
          
          var callee = arguments.callee;
          node.animate({
            "marginLeft": '0px',
            'marginRight': '10px'
          }, self.options.speed, 'linear', 
            function() { callee( nodeset.slice( 1 ) ); }
          );
        })( $( node.add( node.prevAll().splice( 0, perPage - 1 ) ) ) );
      };

      $(this).find(lbut).click(this.scroller.moveLeft);
      $(this).find(rbut).click(this.scroller.moveRight);
    });
  } else {
    var ref = $(this);
    return { 
      moveTo: function(id) {
        ref.each(function(){
          var i = -1;
          $(this).find(this.scroller.item_selector).each(function(n){
            if ( this.id == id ) {
              i = n;
            }
          });
          if ( i > 0 ) {
            this.scroller.moveRight( null, i );
          }
        })
      }
    }
  }
}