/**
 *
 * jQuery.offerRotatorThumbs
 * @author Profitroom
 *
 */

(function( $ ){

	var settings = {
		  item: '.offerItem'
		, container: '#allItems'
		, buttonLeft: '#leftButton'
		, buttonRight: '#rightButton'
		, interval: 5000
		, shrunkVisibleElements: 'img'
		, shrunkOpacity: 0.5
		, expandedCount: 3
		, shrunkWidth: 100
	};
	
	var methods = {
		init : function (options) {
			return $(this).each(function(){
				var $this = $(this)
					, data = $this.data('offerrotator')
				;
				
				if ( data )
					return;
				
				if ( options ) {
					$.extend( settings, options );
				}

				var items = $(settings.item)
					, itemWidth = items.first().outerWidth()
					, containerWidth = itemWidth * items.length
					, data = {
						  target : $this
						, buttonLeft: $(settings.buttonLeft)
						, buttonRight: $(settings.buttonRight)
						, container: $(settings.container)
						, items: items
						
						, itemWidth: itemWidth
						, containerWidth: containerWidth
						
						, position: 0
						, direction: 1
					}
				;
				
				data.container
					.width( data.containerWidth )
					.css({
						  position: 'absolute'
						, top: 0
					})
					.animate({ left: ($this.innerWidth() - data.itemWidth * Math.min(settings.expandedCount, data.items.length))/2 })
				;
				
				if ( settings.expandedCount >= data.items.length ) {
					return;
				}
				
				$this.add(data.buttonLeft).add(data.buttonRight)
					.hover(
						  function (ev) { methods.stop.apply( $this ); }
						, function (ev) { methods.start.apply( $this ); }
					);
				
				$this.data('offerrotator', data);
				
				
				
				methods.start.apply($this);
				
					data.items.css({
						  overflow: 'hidden'
						, width: data.itemWidth
						, height: data.items.first().height()
					});
				
				methods.shrinkUpdate.apply( $this );
				
				data.buttonLeft.click( function (ev) {
					ev.preventDefault();
					methods.scrollLeft.apply($this);
				});

				data.buttonRight.click( function (ev) {
					ev.preventDefault();
					methods.scrollRight.apply($this);
				});
				
				data.items.click( function (ev) {
					if ( !$(this).hasClass('shrunk') || $this.is(':animated') )
						return true;
					
					ev.preventDefault();
					
					methods.scroll.apply($this, [null, data.items.index(this)]);
				});
			});
		}
		
		, start: function () {
			return $(this).each(function(){
				var $this = $(this)
					, data = $this.data('offerrotator')
				;
				
				if ( !data || data.intervalId )
					return;

				data.intervalId = settings.interval && setInterval( function () {
						methods.scroll.apply($this);
				}, settings.interval);
				
				$this.data('offerrotator', data);
			});
		}
		
		, stop: function () {
			return $(this).each(function(){
				var $this = $(this)
					, data = $this.data('offerrotator')
				;
				
				if ( !data )
					return;
				
				clearInterval(data.intervalId);
				
				data.intervalId = null;
				$this.data('offerrotator', data);
			});
		}
		
		, shrinkUpdate: function () {
			return $(this).each(function(){
				var $this = $(this)
					, data = $this.data('offerrotator')
				;
				
				if ( !data )
					return;
				
				data.shrunkLeft = data.items.slice(0, data.position);
				data.shrunkRight = data.items.slice( data.position + settings.expandedCount, data.items.length );
				data.shrunk = data.shrunkLeft.add( data.shrunkRight);
				
				data.expanded = data.items.not('.shrunk');
				
				var toShrink = data.shrunk.not('.shrunk')
					toExpand = data.items.slice(data.position, data.position + settings.expandedCount)
				;
				
				toShrink.each( function () {
					$(this)
						.removeClass('expanded')
						.animate(
							  {
								  width: settings.shrunkWidth
								, opacity: settings.shrunkOpacity
							  }
							, function () {
								$(this).addClass('shrunk');
							}
						)
					;
					
					$(this).contents().not(settings.shrunkVisibleElements).fadeOut();
				});
				
				toExpand.each( function () {
					$(this)
						.removeClass('shrunk')
						.animate(
							  {
								  width: data.itemWidth
								, opacity: 1
							  }
							, function () {
								$(this).addClass('expanded');
							}
						)
					;
					
					$(this).contents().not(settings.shrunkVisibleElements).fadeIn();
				});
				
				$this.data('offerrotator', data);
			});
		}
		
		, scroll: function ( argDirection, argPosition ) {
			return $(this).each(function(){
				var $this = $(this)
					, data = $this.data('offerrotator')
				;
				
				if ( !data || data.items.filter(':animated').length )
					return;
				
				if ( typeof argPosition == 'undefined' ) {
					if ( !argDirection ) {
						var direction = data.direction;
					}
					else if ( Math.abs(argDirection) != 1 ) {
						return;
					}
					else {
						var direction = argDirection;
					}
					
					var posExceeded = ( ( ( data.position + direction ) + settings.expandedCount ) > data.items.length )
										|| ( data.position + direction < 0 )
					;
					
					if ( argDirection && posExceeded ) {
						return;
					}
					else if (posExceeded) {
						direction = -data.direction;
						data.direction = direction;
					}
					
					data.position += direction;
				}
				else if ( data.items[argPosition] ) {
					data.position = Math.min(argPosition, data.items.length - settings.expandedCount);
				}
				else {
					return;
				}
				
				methods.shrinkUpdate.apply( $this );
				
				data.container.animate({
					left: ($this.innerWidth() - data.itemWidth * settings.expandedCount)/2
							- data.shrunkLeft.length * settings.shrunkWidth
				});

				
				$this.data('offerrotator', data);
			});
		}
		
		, scrollLeft: function () {
			methods.scroll.apply(this, [-1]);
		}

		, scrollRight: function () {
			methods.scroll.apply(this, [1]);
		}
};

	$.fn.offerRotatorThumbs = function( options ) {
			if ( methods[options] ) {
				return methods[options].apply( this, Array.prototype.slice.call( arguments, 1 ));
			} else if ( typeof options === 'object' || !options ) {
				return methods.init.apply( this, arguments );
			} else {
				
			}
};

})( jQuery );
