(function($){
	$.fn.initRoller = function(options) {
		
		var Roller = function(roller, options) {
			this.roller = roller;
			this.options = options;
			
			this._init();
		};
		
		Roller.prototype._init = function() {
			this._initBlocks();
			this._prepareRoller();
			this._initHandlers();
		};
		
		Roller.prototype._initBlocks = function() {
			var optionsToConvert = {containerClass: '', leftControlClass: '', rightControlClass: ''};
			
			for(option in this.options)
			{
				if ((option in optionsToConvert) && !(this.options[option] instanceof Array))
				{
					this.options[option] = new Array(this.options[option]);
				}
			}
			
			// blocks
			this.container = $('#' + this.options.parentId + ' .' + this.options.containerClass.join('.'));
			this.thumbs = $('li', this.container);
			this.thumbsLength = this.thumbs.length;
			
			this.controls = {};
			this.controls.left = $('#' + this.options.parentId + ' .' + this.options.leftControlClass.join('.'));
			this.controls.right = $('#' + this.options.parentId + ' .' + this.options.rightControlClass.join('.'));
			
			// params
			this.containerWidth = this.container.width();
			this.thumbnailWidth = this._calculateThumbnailWidth();
			
			this.animatingCounter = 0;
		};
		
		Roller.prototype._calculateThumbnailWidth = function() {
			return $('li:first', this.container).width() + this.options.thumbHorizontalPadding;
		};
		
		Roller.prototype._prepareRoller = function() {
			var thisObj = this;
			this.thumbs.each(function(i, e) {
				$(e).css({
					position: 'absolute',
					top: 0,
					left: i < thisObj.options.viewPortLength ? (i * thisObj.thumbnailWidth) + 'px' : thisObj.containerWidth + 'px'
				});
			});
		};

		Roller.prototype._initHandlers = function() {
			var thisObj = this;
			
			this.controls.left.click(function(){
				thisObj._roll('+');
			});
			
			this.controls.right.click(function(){
				thisObj._roll('-');
			});
		};
		
		Roller.prototype._roll = function(signOfDirection)
		{
			if (0 == this.animatingCounter)
			{
				var thisObj = this;
			
				if ('+' == signOfDirection)
				{
					$('li:last', thisObj.container).prependTo($('ul', thisObj.container)).css('left', '-' + thisObj.thumbnailWidth + 'px');
				}
				
				var thumbsToAnimate = $('li:lt(' + (this.options.viewPortLength + 1) + ')', this.container);
			
				this.animatingCounter = thumbsToAnimate.length;
			
				thumbsToAnimate.each(function(i, e){
					$(e).animate({left: signOfDirection + '=' + thisObj.thumbnailWidth + 'px'}, {duration: thisObj.options.delay, complete: function(){
						if(0 == --thisObj.animatingCounter) {
							if ('-' == signOfDirection)	{
								$('li:first', thisObj.container).appendTo($('ul', thisObj.container)).css('left', thisObj.containerWidth);
							}
						}
					}});
				});
			}
		};
		
		return new Roller(this, $.extend($.fn.initRoller.defaults, options));
    };
    
    $.fn.initRoller.defaults = {
    	parentId: 'roller-thumbs',
    	containerClass: 'roller-container',
    	leftControlClass: ['roller-control', 'left'],
    	rightControlClass: ['roller-control', 'right'],
    	delay: 600,
    	thumbHorizontalPadding: 0,
    	viewPortLength: 3
    };
    
    $(window).load(function(){
    	$('.roller').initRoller({
    		thumbHorizontalPadding: 87,
    		delay: 700
    	});
    });
})(jQuery);
