// NEWS TICKER
showTicker = setTimeout ( "loadTicker()", 5000);
showHeadlines = setInterval ( "loadHeadline()", 6000);

function loadTicker() {
	$("div#ticker_wrapper").slideDown("slow");
	$("div#ticker").css({'display':'block'});
}

function closeTicker() {
	$("div#ticker_wrapper").slideUp("slow", function() {
		$("div#ticker").css({'display':'none'});
		clearTimeout(showTicker);
		clearInterval(showHeadlines);
	});
}

function loadHeadline() {
	var count = countHeadlines();
	var i = 1;
	var display = "";
	var visible = 0;
	
	//retrieve active headline, if any
	while (i<=count) {
		display = $("div#headline" + i).css("display");
		if (display == "block") {
			visible = i;
		}
		i++;
	}

	//set next headline to load	
	var nextHeadline = getNextHeadline(visible,count);
	
	//unload current headline
	if (visible>0) {
		$("div#headline" + visible).fadeOut('slow', function() {
			$("div#headline" + nextHeadline).fadeIn('slow');
		});
	}
	else {
		$("div#headline" + nextHeadline).fadeIn('slow');
		$("div#ticker_close").fadeIn('slow');		
	}


}

function countHeadlines() {
	var parent = document.getElementById('ticker');
	var childCount = parent.getElementsByTagName('div').length;
	return childCount-1;
}

function getNextHeadline(current, limit) {
	var nextHeadline = current + 1;
	if (nextHeadline > limit) {
		nextHeadline = nextHeadline - limit;
	}
	return nextHeadline;
}
