(function($) {

	var op 			= new Array; //option
	var list		= new Array;
	var positions	= new Array;
	var playInterval = new Array;
	var onAnimate	= false;
	
	$.fn.themesguyTicker= $.fn.themesguyTicker = function(options){
		
		init = function(tt){
			
			op[tt.id] = $.extend({}, $.fn.themesguyTicker.defaults, options);
			list[tt.id]		= new Array();
			positions[tt.id]	= 0;
				

					$.each($('#'+tt.id+' li'), function(i,item){
						list[tt.id][i] 	= $(item);
					});
					
					if($('#'+tt.id+' li').length > 1) { //only do the animation if length more than 1.
						$.buildarrows(tt);
						$.transition(tt);
					}
		};
		
		// transitions
		$.transition = function(tt){
			//clearInterval(playInterval[tt.id]);
			playInterval[tt.id] = setInterval(function() { $.gt(tt) }, op[tt.id].delay);
			
		};
		
		$.gt = function(tt, direc){
		
				if(typeof(direc) == "undefined")
					positions[tt.id]++;
				else
					positions[tt.id] = direc;

				if(positions[tt.id] == list[tt.id].length ) {
					positions[tt.id] = 0;
				}	

				if(positions[tt.id] == -1){
					positions[tt.id] = list[tt.id].length-1;
				}
				
				onAnimate = true;
				$('#'+tt.id+' > ul').animate({ top : op[tt.id].listsHeight * - positions[tt.id] + 'px'}, op[tt.id].speed, function() { onAnimate = false; } );
				
			
		};
		
		$.buildarrows = function(tt){
			$('#'+tt.id).parent().append('<a class="bt-top" href="#"></a><a class="bt-bottom" href="#"></a>');
			
			$('.bt-top').click(function(e){
				e.preventDefault();
				
				if( onAnimate == false ){
					nextPos = positions[tt.id] + 1;
					$.gt(tt, nextPos);
					
					clearInterval(playInterval[tt.id]);
					$.transition(tt);
				}
			
			});
			
			$('.bt-bottom').click(function(e){
				e.preventDefault();
				
				if( onAnimate == false ){
					nextPos = positions[tt.id] - 1;
					$.gt(tt, nextPos);
					
					clearInterval(playInterval[tt.id]);
					$.transition(tt);
				}
			
			});
			
		}

		this.each (
			function(){ init(this); }
		);


	};
	
	// default values
	$.fn.themesguyTicker.defaults = {	
		delay : 5000, // delay between news
		speed : 500, //animation speed
		listsHeight : 60 //height of lists
	};	
	
})(jQuery);
