/**
 * Simple News Ticker for Brilliance
 * @author gabe@brilliance.com
 */

(function($) {
	
	function run(settings, el) {
		var first = el.children('li:first');
		var last = el.children('li:last');
		var to = last.prev();
		to.css('display', 'block');
		
		// cater for groups
		var subli = to.find('li');
		if (subli.length) {
			var curr = to.find('li[class=active]');
			var next = curr.next();
			if (!next.length) {
				next = to.find('li:first')
			}
			next.addClass('active');
			curr.removeClass('active');
		}
		
		el.scrollTo(to, settings.transition_time*1000, {
			onAfter: function() {
				el.prepend(last.clone());
				last.remove();
				el.scrollTop(1000);
				el.data('scroll-timer', setTimeout(function() {
					run(settings, el);
				}, settings.display_time*1000));
			}
		});
	}
	
	$.fn.extend({
		newsTicker: function(settings) {
			settings = jQuery.extend({
		 	  	id: "#news",
		   		display_time: 10,
		   		transition: 'linear',
		    	transition_time: 0.2,
		    	startDelay: 2
			}, settings);
			
			var el = $(settings.id);
			
			el.scrollTop(1000);
			
			el.hover(function() {
				clearTimeout(el.data('scroll-timer'));
			}, function() {
				el.data('scroll-timer', setTimeout(function(){run(settings, el); }, settings.startDelay*1000));
			});
			
			el.data('scroll-timer', setTimeout(function(){run(settings, el); }, settings.startDelay*1000));
		}
	
	});
	

})(jQuery);

