/*********** USAGE ************
HTML CODE
<ul id="unique_id">
	<li><img src=""/></li>
	<li><img ....
	......
</ul>

JAVASCRIP CODE
$('#unique_id').listFlow(options);

OPTIONS (object)
	fps: frame rate (int)
	speed: move speed (int / px)
	direction: move direction (int / 1 or -1)
	display: if list items are less than this number, images don't move (int)
	
*****************************/
(function($) {
	$.fn.listFlow = function(userArgs) {
		if(!this.length) return false;
		if(!this.is('ul') && !this.is('ol')) return this;
		
		var args = {
			fps:10,
			speed:1,
			direction:-1,
			display:2
		};
		
		$.extend(true, args, userArgs);
		
		var frame = this;
		var timer = null;
		var timer_run = false;
		var script_run = false;
		var FPS = Math.ceil(1000/args.fps);
		var moving = new Array();
		var currentIndex = 0;
		
		this.css({'overflow':'hidden','position':'relative','padding':0});
		var lists = this.find('li');
		var totalHeight = 0;

		if(lists.length > args.display) {
			script_run = true;
			for(var i=0;i<args.display+1;i++) {
				moving.push({id:i, move:true});
				if(i == args.display) moving[i].move = false; 	
			}
		
			$(window).load( function() {
				
				lists.each( function(index) {
					if(args.direction < 0) {
						if(totalHeight > frame.height()) var topPosi = frame.height();
						else topPosi = totalHeight;
						totalHeight += $(this).height();
					}else{
						totalHeight -= $(this).height();
						if(frame.height()+totalHeight < $(this).height()*-1) topPosi = $(this).height()*-1;
						else topPosi = frame.height()+totalHeight;
					}
					$(this).css({'position':'absolute','top':topPosi+'px','left':(frame.width()-$(this).find('img').width())*0.5+'px'});
				});
				
				frame.mouseenter( function(e) {
					e.preventDefault();
					clearTimer();
				}).mouseleave( function(e) {
					e.preventDefault();
					setTimer();
				});
				
				setTimer();
			});
		}
		
		function setTimer() {
			if(!timer_run) {
				timer = window.setInterval(function(){
					flowing();
				}, FPS);
				timer_run = true;
			}
			return false;
		}
		
		function clearTimer() {
			window.clearInterval(timer);
			timer_run = false;
			return false;
		}
		
		function flowing() {
			for(var i=0;i<moving.length;i++) {
				if(moving[i].move) {
					var curObj = lists.eq(moving[i].id);
					var topPosi = curObj.position().top+args.speed*args.direction;
					curObj.css({'top':topPosi+'px'});
					if(args.direction < 0) {
						if(topPosi <= curObj.height()*-1) {
							curObj.css('top',frame.height()+'px');
							moving.shift();
							moving.push({id:adjust(moving[moving.length-1].id+1),move:false});
						}
					}else{
						if(topPosi >= frame.height()) {
							curObj.css('top',curObj.height()*-1+'px');
							moving.shift();
							moving.push({id:adjust(moving[moving.length-1].id+1),move:false});
						}
					}
					
				}else{
					var checkObj = lists.eq(adjust(moving[i].id-1));
					if(args.direction < 0) {
						if(checkObj.position().top <= frame.height()-checkObj.height()) {
							moving[i].move = true;
						}
					}else{
						if(checkObj.position().top > 0) {
							moving[i].move = true;
						}
					}
				}
			}
			return false;
		}
		
		function adjust(id) {
			if(id >= lists.length) return 0;
			if(id < 0) return lists.length-1;
			return id;
		}
		
		this.stopTimer = function() {
			if(script_run) clearTimer();
		}
		
		this.startTimer = function() {
			if(script_run) setTimer();
		}
		
		return this;
	}
})(jQuery);
