
(function($){  
 $.fn.extend({   
	 //plugin name  
	 newsticker: function(options) { 
		
		// Settings list and the default values  
		// ADD CLASSTYLES, TOGGLESPEED, etc to make it as dynamic as possible

		var defaults = {  
			currentItem: 0, // to manually set a different starting item
			speedInterval: 3500,
			speedAnimation: 2000
		 };  
		   
		 var options = $.extend(defaults, options);  
	  	 
		
		 return this.each(function() {
			
			var obj = $(this); // assign current element to variable 
			var param =options;
			var mouseoutactive = false;
			
			var rotateInterval;
			var totalItems = $("div.newsticker_item",obj).size();
			var previousItem = param.currentItem;
			var heightContainer = $(this).height() + 10; // for example container is 200 then animate the item to top -205 to make sure it will not be visible after animations      
			
										 
			// set default location of the items
			$("div.newsticker_item",obj).css({
					"top" : heightContainer,
					"position" : "absolute"}
					); // active item location
			
			$("div.newsticker_item:eq("+param.currentItem+")",obj).css('top','0px');
			
			rotateInterval = setInterval(headline_rotate,param.speedInterval); //time in milliseconds
			


			$(this).hover(function() {
				
				clearTimeout($(this).data('timeout'));// timeout need to avoid hoverissues
				clearInterval(rotateInterval);
	
			}, function() {
				
				// timeout need to avoid hoverissues
				// there is also a hover plugin : http://cherne.net/brian/resources/jquery.hoverIntent.html
				var t = setTimeout(function() {
					
					rotateInterval = setInterval(headline_rotate,param.speedInterval); //time in milliseconds
					headline_rotate();

				}, param.speedAnimation);
				$(this).data('timeout', t);
			
			});
			

			function headline_rotate() {	 
				
				param.currentItem = (previousItem + 1) % totalItems; 
 
				$("div.newsticker_item:eq(" + previousItem + ")",obj).animate({top: -heightContainer},param.speedAnimation, function() {
					$(this).css('top',heightContainer);
				});
				
							
				$("div.newsticker_item:eq(" + param.currentItem + ")",obj).show().animate({top: 0},param.speedAnimation);  
				previousItem = param.currentItem;
				
			}
			
		
				
		 });  
	 }  
 });  
})(jQuery); 

