$.fn.scroll = function(params) {
  running = false;
  options = {
    interval:              50,
    maxSpeed:              5,
    imageIncrease:         '0px',
    current:               -1,
    minImagesNumberToAnim: 10
  };
  jQuery.extend(options, params);
  images = {
    oryginalWidth: 0,
    oryginalHeight: 0,
    computedWidth: 0,
    computedHeight: 0
  };

  function generateId(length) {
    var c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var r = '';
    for (var i=0; i < length ; i++) {
      r += c[Math.floor(Math.random() * c.length)];
    }
    return r;
  }

  return this.each(function() {
    if ($(this).attr('run') != undefined) {
      return;
    } else {
      $(this).attr('run', 'true')
    }

    $(this).attr('id')
    if ($(this).attr('id') == '') {
      $(this).attr('id', generateId(8));
    }

    totalWidth = $(this).find('.content').width();
    firstImageOffset = $(this).find('li').offset();
    images.count          = $(this).find('img').size();
    images.oryginalWidth  = $(this).find('img').width();
    images.oryginalHeight = $(this).find('img').height();
    if (options.imageIncrease.indexOf('%') > 0) {
      images.computedWidth  = Math.round((images.oryginalWidth  * (100 + parseFloat(options.imageIncrease))) / 100);
      images.computedHeight = Math.round((images.oryginalHeight * (100 + parseFloat(options.imageIncrease))) / 100);
    } else {
      images.computedWidth  = images.oryginalWidth  + parseInt(options.imageIncrease);
      images.computedHeight = images.oryginalHeight + parseInt(options.imageIncrease);
    }
    images.marginLeft     = -Math.ceil((images.computedWidth - images.oryginalWidth) / 2);
    images.marginRight    = -Math.floor((images.computedWidth - images.oryginalWidth) / 2);
    images.marginTop      = -Math.ceil((images.computedHeight - images.oryginalHeight) / 2);
    images.marginBottom   = -Math.floor((images.computedHeight - images.oryginalHeight) / 2);

    images.outerWidth     = $(this).find('li').width();
    if (images.count > options.minImagesNumberToAnim) {
      var multiplier = Math.ceil((totalWidth + images.outerWidth) / (images.count * images.outerWidth));
      for (var i = 0 ; i < multiplier - 1 ; i++)
      {
        $(this).find('li').each(function(index) {
          if (index < images.count)
          {
            $(this).clone().appendTo($(this).parents('ul'));
          }
        });
      }
      if (multiplier > 1)
      {
        images.count *= multiplier;
      }
    }

    $(this).find('img').each(function (){
      $(this).mouseover(function() {
        $(this).css('margin', images.marginTop + 'px ' + images.marginRight  + 'px ' + images.marginBottom + 'px ' + images.marginLeft + 'px');
        $(this).css('width',  images.computedWidth + 'px');
        $(this).css('height', images.computedHeight + 'px');
      });
      $(this).mouseout(function() {
        $(this).css('margin', '0');
        $(this).css('width',  images.oryginalWidth + 'px');
        $(this).css('height', images.oryginalHeight + 'px');
      });
    });


    if (images.count > options.minImagesNumberToAnim) {
      $(this).find('li').each(function(index) {
        var thisOffset = $(this).offset();
        $(this).css('margin-left', (firstImageOffset.left - thisOffset.left) + 'px');
        $(this).css('margin-top', (firstImageOffset.top - thisOffset.top) + 'px');
        $(this).attr('index', index);
        var thisLeft = index * $(this).width();
        if (options.current != -1)
        {
          thisLeft = index * $(this).width() + ((totalWidth + images.oryginalWidth)/2 - options.current * $(this).width()) - images.oryginalWidth/2;
        }
        if (thisLeft < -$(this).width()) {
          thisLeft += images.count * $(this).width();
        } else {
          if (thisLeft >= (images.count - 1) * images.outerWidth) {
            thisLeft -= images.count * images.outerWidth;
          }
        }
        $(this).css('left', thisLeft);
        $(this).attr('left', thisLeft);
      });

      $(this).find('.prev').mouseover(function(){
        $(this).everyTime(options.interval, $(this).parents('div').attr('id'), function(){
          $(this).next('div').find('li').each(function(index) {
            var thisLeft = parseFloat($(this).attr('left'));
            thisLeft += parseFloat(options.maxSpeed);
            if (thisLeft >= (images.count - 1) * images.outerWidth) {
              thisLeft -= images.count * images.outerWidth;
            }
            $(this).attr('left', thisLeft);
            $(this).css('left', thisLeft + 'px');
            $(this).css('margin-bottom', '1px');
            $(this).css('margin-bottom', '0px');
          });
        });
      })

      $(this).find('.prev').mouseout(function(){
        $(this).stopTime($(this).parents('div').attr('id'));
      });

      $(this).find('.next').mouseover(function(){
        $(this).everyTime(options.interval, $(this).parents('div').attr('id'), function(){
          $(this).prev('div').find('li').each(function(index) {
            var thisLeft = parseFloat($(this).attr('left'));
            thisLeft -= parseFloat(options.maxSpeed);
            if (thisLeft < -$(this).width()) {
              thisLeft += images.count * $(this).width();
            }
            $(this).attr('left', thisLeft);
            $(this).css('left', thisLeft + 'px');
            $(this).css('margin-bottom', '1px');
            $(this).css('margin-bottom', '0px');
          });
        });
      });

      $(this).find('.next').mouseout(function(){
        $(this).stopTime($(this).parents('div').attr('id'));
      });
    }
  });
}

