/************
 * This Jquery Plugin creates a horizontal sliding Text with infinit repetition
 * If the last list Object is shown, he append the first list Object to the list.
 * After the first Object is fully overflowed, it will be removed from list. 
 * 
 * 
 * @name infinitSlider
 * @author Thomas Joußen, Björn Heyser
 * @company Databay AG
 * @copyright Databay AG 2011 - now()
 ********/

(function($){	
	var methods = {
		init: function(settings){
			
			var options = {
				speed: 15000,
				spacingWidth: 30,
				spacingString: '',
				scrolling: 'linear',
				endingString: '',
				endingWidth: 40,
				pauseOnHover: true
			};
			
			return this.each(function(){
		
				options = $.extend(
					options, 
					settings
				);	
					
				var that = $(this);
					
				var position = 0;
				var elemMax = $(this).find('li').length;
				var elemCounter = 0;
					
				var list = [];
				var longest = 0;
				$(this).find('li').each(
					function()
					{
						if(options.spacingString.length != 0 && (elemCounter < elemMax || options.endingString.length == 0) ){
							var spacer = $('<li>'+options.spacingString+'</li>');
							spacer.css('width', options.spacingWidth)
								  .css('text-align', 'center');
							list[position++] = spacer;
						}	
						
						if($(this).width() > longest){
							longest = $(this).width();
						}
						list[position++] = $(this).remove();
						elemCounter++;
							
						/*if(options.spacingString.length != 0 && (elemCounter < elemMax || options.endingString.length == 0) ){
							var spacer = $('<li>'+options.spacingString+'</li>');
							spacer.css('width', options.spacingWidth)
								  .css('text-align', 'center');
							list[position++] = spacer;
						}	*/	
					}
				);
				if(list.length == 0){
					return;
				}	
				if(options.endingString.length != 0){
					var ending = $('<li>'+options.endingString+'</li>');
					ending.css('width', options.endingWidth)
					      .css('text-align', 'center');
					list[position] = ending;
				}
						
				position = 0;
					
				var scroll = function()
				{
					var doAppend = false;
						
					if( that.find('li').length == 0 )
					{
						doAppend = true;
					}
					else
					{
						var lastEntry = that.find('li:last');
						var posLastEntryRight = lastEntry.offset().left 
											  + lastEntry.width() 
											  + ((options.spacingString.length == 0)? options.spacingWidth : 0);
						var posListRight = that.offset().left + that.width();
							
					
						if( posLastEntryRight < posListRight ){
							doAppend = true;
						}
					}
					
					if( doAppend )
					{
						var li = $(list[position]).clone();
						li.css('left', that.width());	
						that.append(li);
							
						var newLeft = that.width() + longest;
		
						li.animate(
						{
							left: '-='+newLeft
						},
						options.speed,
						options.scrolling,
						function()
						{
							$(this).remove();
						}
						);
						if(options.pauseOnHover){
							that.hover(function(){
								$(this).find('li').each(function(){
									$(this).stop();
								});
							},
							function(){
								that.find('li').each(function(){
									$(this).animate(
									{
										left: '-='+newLeft	
									},
									options.speed,
									options.scrolling,
									function()
									{
										$(this).remove();
									}
									)
								});
							});	
						}
							
						(position < list.length - 1)? position++ : position = 0;
					}
						
					window.setTimeout(scroll, 1);
				};					
				scroll();
			});
		}
	};
	
	$.fn.infinitSlider = function( method ) {          
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.ilcategoryselection' );
		}
	};      
})(jQuery);

