(function($) {

	$.fn.infiniteNews = function(options){
	  
		// default configuration properties
		var defaults = {
			width			: 500,
			time			: 5000,
			changeSpeed		: 2000,
			readSpeed		: 9000,
			next			: '',
			prev			: ''
		}; 
		
		var options = $.extend(defaults, options);
				
		this.each(function() {
			var parent 	= $(this),
			list 		= $('ul', parent),
			current		= 0,
			next		= 1,
			total 		= list.children().size() + 1,
			minus		= 0,
			working 	= false,
			direction	= 'left',
			timer;
			
			if(total <= 2) return false;
			
			// set minimum width
			list.children().each(function(){
				if($(this).width() < defaults.width) $(this).width(defaults.width);
			});
			
			// build infinit effect
			var lastChild = list.children().first().clone();
			list.append(lastChild);
			
			// functions
			check_fn = function(){
				
				if(list.children().eq(current).width() > defaults.width){
					timer = setTimeout("inner_fn();", (defaults.time/2));
				}
				else {
					timer = setTimeout("next_fn();", defaults.time);
				}
				
			}
			
			next_fn = function(){
				
				if(!working) {
					working = true;
				
					parent.scrollTo($('li:eq('+next+')', list), defaults.changeSpeed, {easing:'swing', onAfter:function(){
						current++;
						next++;
						if(next >= total)
						{
							current = 0;
							next = 1;
							parent.scrollTo($('li:eq(0)', list),0);
						}
						check_fn();
						working = false;
					}});
					
				}
				
			}
			
			inner_fn = function(){
				
				minus = list.children().eq(current).outerWidth(true) - defaults.width;
				parent.scrollTo('+='+minus, defaults.readSpeed, {easing:'linear', onAfter:function(){
					timer = setTimeout("next_fn();", (defaults.time/2));
				}});
				
			}
			
			// buttons
			next_btn = function(){
				
				if(!working) {
					working = true;
					
					if((next + 1) < total) {
						parent.stop();
						clearTimeout(timer);
						
						parent.scrollTo($('li:eq('+next+')', list), defaults.changeSpeed, {easing:'swing', onAfter:function(){
							current = next;
							next++;
							direction = 'left';
							working = false;
							timer = setTimeout("scroll_inside();", (defaults.time/2));
						}});
					} else {
						working = false;
					}
				}
				
			}
			
			prev_btn = function(){
				
				if(!working) {
					working = true;
					
					prev = current-1;
					if(prev >= 0) {
						parent.stop();
						clearTimeout(timer);
						
						parent.scrollTo($('li:eq('+prev+')', list), defaults.changeSpeed, {easing:'swing', onAfter:function(){
							next = current;
							current--;
							direction = 'left';
							working = false;
							timer = setTimeout("scroll_inside();", (defaults.time/2));
						}});
					} else {
						working = false;
					}
				}
				
			}
			
			scroll_inside = function(){
				
				if(list.children().eq(current).width() > defaults.width){
					
					minus = list.children().eq(current).outerWidth(true) - defaults.width;
					parent.scrollTo((direction=='left'?'+':'-')+'='+minus, defaults.readSpeed, {easing:'linear', onAfter:function(){
						direction = (direction == 'left' ? 'right' : 'left');
						timer = setTimeout("scroll_inside();", (defaults.time/2));
					}});
					
				}
				
			}
			
			if(defaults.next != '') $(defaults.next).unbind('click').click(function(){ next_btn(); return false; });
			if(defaults.prev != '') $(defaults.prev).unbind('click').click(function(){ prev_btn(); return false; });
			
			// slide :)
			check_fn();
			
		});
	  
	};

})(jQuery);
