/*USAGE**************
HTML code
<ul id="listScroller_sample">
	<li>item</li>
	<li>item</li>
	<li>item</li>
</ul>

Javascript code
$('#listScroller_sample').listScroller({options});

--- Options ---

direction: -1: items are going UP, 1: items are going DOWN
duration: items staying time (ms)
speed: moving speed (ms)
textAlpha: (0 - 1)
display: number of effected list
easing: (string) jquery.easing.1.3.js plugin required

*************************/

(function($) {
	$.fn.listRotatorVertical = function(userArgs) {
		if(!this.length) return false;
		if(!this.is('ul') && !this.is('ol')) return this;
		
		var args = {
			direction:1,
			duration:3000,
			speed:500,
			textAlpha:0.7,
			display:5,
			easing:''
		}
		$.extend(true, args, userArgs);
		
		if(args.display%2 == 0) args.display++;
		
		var frame = this.css({'position':'relative','overflow':'hidden'});
		var total = frame.find('li').length;
		
		if(total < args.display) return this;
		
		if(args.direction <= 0) var counter = 0; else counter = -1;
		var totalLi = 0;
		var lis = new Array();
		var timer = null;
		
		frame.find('li').each( function(index) {
			var thisHeight = $(this).outerHeight(true);
			lis.push({obj:$(this),height:thisHeight,flag:false});
			totalLi += thisHeight;
			$(this).css({'position':'absolute','left':0,'top':frame.height()+10+'px','opacity':args.textAlpha});
			thisHeight = null;
		});
		
		//ALLOCATION
		var positions = posiCalcu(lis, 0, args.display) 
		//console.log(positions);
	
		frame.find('li').eq(0).css('opacity',1);
		
		if(totalLi > frame.height()) {
			
			var counter = 0;
			timer = window.setInterval(function() {
				counter++;
				counter = indexAdjust(counter);
				var test = posiCalcu(lis, counter, args.display);
			}, args.duration);
		}
		
		function posiCalcu(lis, center, display) {
			var k = Math.floor(display/2);
			var activeLis = new Array();
			
			for(var i=0; i<display; i++) {
				if(args.direction >= 0)
					activeLis.push({index:indexAdjust(i-k+center),posi:0, alpha:args.textAlpha});
				else if(args.direction < 0)
					activeLis.push({index:indexAdjust(k+center-i),posi:0, alpha:args.textAlpha});
			}
			var cenPosi = (frame.height()-lis[center].height)*0.5;
			activeLis[k].posi = cenPosi;
			activeLis[k].alpha = 1;
			
			var tempHigh = cenPosi;
			for(i=1; i<=k; i++) {
				tempHigh -= lis[activeLis[k+i].index].height;
				activeLis[k+i].posi = tempHigh;
			}
			tempHigh = cenPosi;
			for(i=1; i<=k; i++) {
				tempHigh += lis[activeLis[k-i+1].index].height;
				activeLis[k-i].posi = tempHigh;	
			}
			
			if(args.direction >= 0) {
				for(i=0; i<display-1; i++) {
					lis[activeLis[i].index].obj.stop(true,true).animate({'top':activeLis[i].posi+'px', 'opacity':activeLis[i].alpha}, args.speed, args.easing);	
				}
				lis[activeLis[display-1].index].obj.css({'top':activeLis[display-1].posi, 'opacity':activeLis[display-1].alpha});
			}else if(args.direction < 0) {
				for(i=1; i<display; i++) {
					lis[activeLis[i].index].obj.stop(true,true).animate({'top':activeLis[i].posi+'px', 'opacity':activeLis[i].alpha}, args.speed, args.easing);	
				}
				lis[activeLis[0].index].obj.css({'top':activeLis[0].posi, 'opacity':activeLis[0].alpha});
			}
			return false;
		}
		
		function indexAdjust(index) {
			if(index < 0) index = total+index;
			else if(index >= total) index = index-total;
			return index;	
		}
		
		this.stopTimer = function() {
			window.clearInterval(timer);
		}
		
		this.startTimer = function() {
			timer = window.setInterval(function() {
				counter++;
				counter = indexAdjust(counter);
				//var test = posiCalcu(lis, counter, args.display);
			}, args.duration);
		}
		
		return this;
	}
})(jQuery);

