(function($){
	$.fn.initContinuousRoller = 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.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.listWidth = this._calculateListWidth();
		};
		
		Roller.prototype._calculateListWidth = function() {
			var thisObj = this;
			
			var width = 0;
			
			$('li', this.container).each(function(i, e){
				width += $(e).width() + thisObj.options.thumbHorizontalPadding;
			});
			
			return width;
		};
		
		Roller.prototype._prepareRoller = function() {
			var currentRibbonLength = this.listWidth;
			var thisObj = this;
			
			this.listCount = 1;
			
			do {
				this.container.append($('ul:last', this.container).clone());
			
				this.listCount++;
				
				currentRibbonLength += this.listWidth;
			} while(currentRibbonLength < this.containerWidth + this.listWidth);
			
			$('ul', this.container).each(function(i, e){
				$(e).css({
					width: thisObj.listWidth + 'px',
					position: 'absolute',
					top: 0,
					left: (i * thisObj.listWidth) + 'px'
				});
			});
			
			this.leftOffset = 0;
		};

		Roller.prototype._initHandlers = function() {
			var thisObj = this;
			
			this.controls.left.mouseenter(function(){
				thisObj._startRolling('left');
			});
			this.controls.left.mouseout(function() {
				thisObj._stopRolling();
			});
			
			this.controls.right.mouseenter(function(){
				thisObj._startRolling('right');
			});
			this.controls.right.mouseout(function() {
				thisObj._stopRolling();
			});
		};
		
		Roller.prototype._startRolling = function(direction) {
			switch(direction) {
			case 'left': 
				this.timer = setInterval((function(thisObj) {
					return function() {
						thisObj._rollLeft();
					};
				})(this), this.options.delay);
				break;
				
			case 'right': 
				this.timer = setInterval((function(thisObj) {
					return function() {
						thisObj._rollRight();
					};
				})(this), this.options.delay);
				break;
			}
		};
		
		Roller.prototype._stopRolling = function(direction) {
			clearInterval(this.timer);
		};
		
		Roller.prototype._rollLeft = function() {
			var thisObj = this;
			
			if (this.leftOffset == 0 || this.leftOffset < this.options.offset)
			{
				$('ul:last', this.container).prependTo(this.container).css('left', -(this.listWidth + this.leftOffset) + 'px');
				
				this.leftOffset = this.listWidth + this.leftOffset;
			}
			
			$('ul', this.container).each(function(i, e){
				var ul = $(e);
				
				ul.css('left', (parseInt(ul.css('left')) + thisObj.options.offset) + 'px');
			});
			
			this.leftOffset -= this.options.offset;
		};
		
		Roller.prototype._rollRight = function() {
			var thisObj = this;
			
			if (this.leftOffset == this.listWidth || this.leftOffset + this.options.offset > this.listWidth)
			{
				$('ul:first', this.container).appendTo(this.container).css('left', (this.listWidth * (this.listCount - 2) + this.leftOffset) + 'px');
				
			    //this.leftOffset = this.options.offset;
				this.leftOffset = 0;
			}
			
			$('ul', this.container).each(function(i, e){
				var ul = $(e);
				
				ul.css('left', (parseInt(ul.css('left')) - thisObj.options.offset) + 'px');
			});
			
			this.leftOffset += this.options.offset;
		};
		
		return new Roller(this, $.extend($.fn.initContinuousRoller.defaults, options));
    };
    
    $.fn.initContinuousRoller.defaults = {
    	parentId: 'roller-logos',
    	containerClass: 'roller-container',
    	leftControlClass: ['roller-control', 'left'],
    	rightControlClass: ['roller-control', 'right'],
    	delay: 25,
    	offset: 4,
    	thumbHorizontalPadding: 0
    };
    
    $(window).load(function(){
    	$('.roller').initContinuousRoller({
    		thumbHorizontalPadding: 35
    	});
    });
})(jQuery);
