var counter = 0;				// tracks headline index

var headlineInt;				// rotation interval
var fadeInt;					// fading interval
var blankInt;					// transition interval

var headlines;					// holds headlines from XML file
var hrefs;					// holds links from XML file
var targets;					// holds target from XML file

var headline;					// the headline reference
var headlineLink;				// the link reference
var headlineTarget;        // the target reference
var headlineHex = 77;				// headline color value
var hexTargetFadeIn = 77;			// desired headline color for fade in

var hexTargetFadeOut = 255;			// desired headline color for fade out

var fadeDuration = 30;				// time for text to fade in or out
var headlineDuration = 3500;			// time of headline cycle
var shortHeadlineDuration = 1500;		// time of shortened (mouseout) cycle
var transitionDuration = 500;			// time between headlines


function rotateHeadlines() {
	var http = getHTTPObject();
	http.open("GET", "/ui/content/headlines.xml", true);
	http.onreadystatechange =function() { 
		if (http.readyState == 4) {
			var xmlDocument = http.responseXML;
			headline = document.getElementById("headlineText");					// get headline text
			headlineLink = document.getElementById("headlineLink");					// get headline link
			headlines = xmlDocument.getElementsByTagName("title");					// get headlines from XML file
			hrefs = xmlDocument.getElementsByTagName("link");					// get links from XML file	
			targets = xmlDocument.getElementsByTagName("target");					// get links from XML file	
			headlineInt = setTimeout("doHeadline()", headlineDuration-1000);			// first headline has shorter duration to compensate for no fade-in
		}
	}; 
	http.send(null);
}
function doHeadline() {
		fadeOut();										// fade out old headline
		blankInt = setTimeout("changeHeadline()",transitionDuration);				// wait before fading in new headline	
			
}
function changeHeadline(){
	counter++;
	if(counter >= headlines.length){counter = 0;}						
	
	var newHeadline = document.createTextNode(headlines[counter].childNodes[0].nodeValue);		// create new headline text node
	var newHeadlineLink = hrefs[counter].childNodes[0].nodeValue;					// create new link location
	var newHeadlineTarget = targets[counter].childNodes[0].nodeValue;			// create new target
	headline.replaceChild(newHeadline, headline.childNodes[0]);					// replace old text node with new text node
	headlineLink.href = newHeadlineLink;															// replace old link with new link 
	headlineLink.target = newHeadlineTarget;  												// replace old target with new target
	fadeIn();											// fade in text
	headlineInt = setTimeout("doHeadline()", headlineDuration);					// repeat!
	
}
function pauseHeadlines(){
	clearTimeout(headlineInt);
	clearTimeout(fadeInt);
	clearTimeout(blankInt);
	headline.style.color = "#cc5252";
}
function resumeHeadlines(){
	headline.style.color = "#4d4d4d";
	headlineHex = hexTargetFadeIn;
	headlineInt = setTimeout("doHeadline()",shortHeadlineDuration);
}
function fadeIn(){ 
	if(headlineHex>hexTargetFadeIn) { 									// if color is not black yet
		headlineHex -= 15; 										// increase color darkness
		headline.style.color="rgb("+headlineHex+","+headlineHex+","+headlineHex+")";
		fadeInt = setTimeout("fadeIn()",fadeDuration); 
	}
}
function fadeOut(){ 
	if(headlineHex<hexTargetFadeOut) { 											// if color is not white yet
		headlineHex += 15; 										// decrease color darkness
		headline.style.color="rgb("+headlineHex+","+headlineHex+","+headlineHex+")";
		fadeInt = setTimeout("fadeOut()",fadeDuration); 
	}
}
