$.fn.kNewsticker = function(options) {
	options = $.extend({ 
		duration:     10000,
		autoStart:    true,
		rotate:       true,
		fadeDuration: 200,
		random:       true,
		change:       undefined
	}, options);
	
	var $next, $prev;
	var $news = $(this);
	var nextIndex = -1;
	var prevIndex = -1;
	var timer = null;
	
	$news.children().hide();
	
	function showNews() {
		prevIndex = nextIndex;
		
		if (options.random) {
			do { // avoid identical index
				nextIndex = Math.max(Math.round(Math.random() * $news.children().length - 1), 0);
			} while (nextIndex == prevIndex);
		}
		else {
			nextIndex++;
			if (nextIndex > $news.children().length - 1) {
				nextIndex = 0;
			}
		}
		
		$next = $news.children().eq(nextIndex);
		
		if (prevIndex != null) {
			$prev = $news.children().eq(prevIndex);
			$prev.fadeOut(options.fadeDuration)
				.queue(function(){
					$next.fadeIn(options.fadeDuration, options.change);
					$(this).dequeue();
				});
		}
		else $next.fadeIn(options.fadeDuration);
	}
	
	function stop() {
		if (timer == null) return;
		clearInterval(timer);
		timer = null;
	}
	
	function start() {
		if (timer != null) return;
		showNews();
		if (!options.rotate) return;
		timer = setInterval(showNews, options.duration);
	}
	
	if (options.autoStart) start();
};

