jQuery.fn.extend({
 slidebar: function(am_options) {

  var _o_self = this;

  var $_o_list, $_o_parts;
  var _i_currentPart, _i_totalParts, _i_partWidth;
  var _b_initialized = false;
  var _b_autorollPause;
  var _h_autoroll;

  var _am_defaultOptions = {
   callbackOnInit: null,
   speed: 'slow',
   autoroll: true,
   autorollSpeed: 8000,
   callbackOnAutoroll: null
  };

  am_options = $.extend({}, _am_defaultOptions, am_options);

  // Inicjuje slidebar:
  function _init() {
   if (_b_initialized) return false;

   $(_o_self).addClass('jquery-slidebar');
   $_o_list = $('> ul:first', _o_self);
   $_o_parts = $_o_list.children('li');
   _i_currentPart = 0;
   _i_totalParts = $_o_parts.length;
   _i_partWidth = parseInt($($_o_parts.get(0)).css('width'));
   _b_autorollPause = false;

   // Określenie szerokości warstwy ukrytej:
   $_o_list.css({width: _i_partWidth*_i_totalParts + 'px'});

   if (am_options.autoroll) {
    _h_autoroll = setInterval(_autoroll, am_options.autorollSpeed);
	$_o_list
	 .bind('mouseenter', function() { _b_autorollPause = true; return false; })
	 .bind('mouseleave', function() { _b_autorollPause = false; return false; });
   }

   _b_initialized = true;

   if (am_options.callbackOnInit && $.isFunction(am_options.callbackOnInit)) am_options.callbackOnInit(_o_self);
  };

  // Automatyczne przewijanie:
  function _autoroll() {
   if (_b_autorollPause) return false;
   _o_self.goTo((_i_currentPart+1)%_i_totalParts);

   if (am_options.callbackOnAutoroll && $.isFunction(am_options.callbackOnAutoroll)) am_options.callbackOnAutoroll(_o_self);
  }

  // Przewija do wybranej pozycji:
  this.goTo = function(i_index) {
   if (i_index < 0 || i_index == _i_currentPart || i_index > _i_totalParts) return false;
   _i_currentPart = i_index;
   
   $_o_list.animate({marginLeft: -Math.abs(_i_currentPart*_i_partWidth) + 'px'}, 'slow');
  };

  // Zwraca bieżącą pozycję:
  this.getCurrentIndex = function() {
   return _i_currentPart;
  };

  // Pauzuje automatyczne przewijanie jeśli zostało włączone:
  this.pause = function() {
   _b_autorollPause = true;
  };

  // Wznawia automatyczne przewijanie jeśli zostało włączone:
  this.unPause = function() {
   _b_autorollPause = false;
  };

  // Inicjacja:
  _init();

  return this;
 }
});
