/**
 *   CareNewsToday.js
 *   
 *   (c) 2012 Pete Bradford
 *   
 */

var headlines;
var headlineIndex;
var headlineChar;
var bannerTimeout;
var bannerIndex = 0;
var cursorVisible = true;
var cursorTimeout;

$(document).ready(function() { 
	setupTicker();
	setupTabs();
	setupBanners();
});

function setupTicker()
{
	// index.php?node=CareNewsHeadlines
	$.getJSON('index.php?node=CareNewsHeadlineProvider', function(data) {
		headlines = data;
		headlineIndex = 0;
		headlineChar = 0;
		window.setTimeout(function() { updateTicker(); }, 200);
	});
	$('#news-ticker').click(function() { location.href = headlines[headlineIndex].link; });
	cursorTimeout = window.setTimeout(function() { showHideCursor(); }, 150);
	$('#comment-form-toggle').click( function() { showCommentsForm(); } );
}

function showCommentsForm()
{
	$('#comment-form-toggle').slideUp('fast');
	$('#comment-form').slideDown('slow');
}

function setupTabs()
{
	$('.cat-tab').click(function() {
		$('.cat-tab-content').fadeOut('fast');
		var pane = $(this).attr('id') + '-content';
		$('#' + pane).fadeIn('fast');
		$('.cat-tab').removeClass('cat-tab-active');
		$(this).addClass('cat-tab-active');
	});
}

function setupBanners()
{
	bannerTimeout = window.setTimeout(function() { bannerSwap(); }, 7000);
	$('#rightbar-banners').mouseenter(function() {
		window.clearTimeout(bannerTimeout);
	}).mouseleave(function() {
		bannerTimeout = window.setTimeout(function() { bannerSwap(); }, 3000);
	});
}

function bannerSwap()
{
	$('#rightbar-banners-' + bannerIndex).fadeOut('slow', function() {
		bannerIndex++;
		var x = $('.rightbar-banner-panel').size();
		if (bannerIndex > (x - 1))
			bannerIndex = 0;
		$('#rightbar-banners-' + bannerIndex).fadeIn('slow', function() {
  		bannerTimeout = window.setTimeout(function() { bannerSwap(); }, 7000);
  	});
	});
}

function updateTicker()
{
	var headline = headlines[headlineIndex].headline;
	if (headline == null)
	{
		headlineIndex++;
		if (headlineIndex >= headlines.length)
			headlineIndex = 0;
		headline = headlines[headlineIndex].headline;
	}
	if (headlineChar > headline.length)
	{		
		window.setTimeout(function() {
			$('#news-ticker-cursor').hide();
			$('#news-ticker').fadeOut('slow', function() {
				$('#news-ticker').css( { left: 112 } );
				$('#news-ticker-cursor').show();
				$('#news-ticker').html('');
				$('#news-ticker').show();
				headlineIndex++;
				if (headlineIndex >= headlines.length)
					headlineIndex = 0;
				headlineChar = 0;				
				window.setTimeout(function() { updateTicker(); }, 800); // pause before starting
			});
		}, 4000); // hold delay
	}
	else
	{
		$('#news-ticker').html(headline.substr(0,headlineChar));
		if ($('#news-ticker').width() > 830 ) 
		{
			$('#news-ticker').css( { left: 112 - $('#news-ticker').width() + 830 } );
			$('#news-ticker-cursor').css( { left: 943 } );
		}
		else
			$('#news-ticker-cursor').css( { left: (112 + $('#news-ticker').width()) } );
		headlineChar++;
		window.setTimeout(function() { updateTicker(); }, 100); // pause between letters
	}
}

function showHideCursor()
{
	cursorVisible = !cursorVisible;
	if (cursorVisible)
		$('#news-ticker-cursor').html('_');
	else
		$('#news-ticker-cursor').html('');
	cursorTimeout = window.setTimeout(function() { showHideCursor(); }, 150);
}

