var headlines;
var currentHeadline;

var paused;
var fading;

function initHeadlines()
{
	headlines = document.getElementById( "headlinesBox" ).getElementsByTagName( "div" );
	currentHeadline = 0;
	
	for( i = 0; i < headlines.length; i++ )
	{
		fixLength( headlines[ i ] );
	}
		
	setTimeout( "changeHeadline()", 2000);
}

function fixLength( headline )
{
	var aElement = headline.getElementsByTagName( "a" )[ 0 ];
	
	if( aElement != null )
	{
		var text = aElement.innerHTML;
		
		if( text.length > 68 )
			aElement.innerHTML = text.substring( 0, 68 ) + "&hellip;";
	}				
}

function changeHeadline()
{	
	if( paused )
	{
		setTimeout( "changeHeadline()", 2000);
		return;
	}
		
	var nextHeadline = ( currentHeadline + 1 ) % headlines.length;

	var toHide = headlines[ currentHeadline ];				
	var toShow = headlines[ nextHeadline ];
	
	setOpacity( toHide, 1 );
	setOpacity( toShow, 0 );
	
	toHide.style.display = "block";
	toShow.style.display = "block";
	
	currentHeadline = nextHeadline;
	
	fadeInOut( toHide, toShow, 0 );
}

function setOpacity( element, percent )
{
	element.style.opacity = percent;
	element.style.filter = "alpha(opacity=" + ( percent * 100 )  + ")";
}
			
function fadeInOut( toHide, toShow, pct )
{
	if( pct > 1.0 )
	{
		toHide.style.display = "none";
		toShow.style.filter = "";
		setTimeout( "changeHeadline()", 3000);
		fading = false;
		return;
	}
	
	fading = true;
	
	setOpacity( toHide, 1.0 - pct  );
	setOpacity( toShow, pct );
	
	pct += 0.2;
	
	setTimeout( function(){ fadeInOut( toHide, toShow, pct ) }, 100);
}

function headlinesHold()
{
	paused = true;
}

function headlinesResume()
{
	if( paused )
	{
		paused = false;
	}
}