var nrImagesPath;
var rotAttr, newsList, newsNum, newsIdx;
var nrTimerID, nrPlaying = false, currentSlot = 0;
var nrButtonFGColor = "#FFFFFF";
var nrButtonHLColor = "#333333";
function newsrotatorInit (pathToXMLData)
{
	// Add the wrapper (for positioning)
	$("#newsrotator").html("<div id=\"nrWrapper\"></div>");
	// Add the news snippet and the slide viewer
	$("#nrWrapper").append("<div id=\"nrStory\"></div>");
	$("#nrWrapper").append("<div id=\"nrSlideViewer\"></div>");
	// Add the Story Elements (two stories to enable cross-fade)
	//    Story 1
	$("#nrStory").append("<div id=\"nrStory1\"></div>");
	$("#nrStory1").append("<h3></h3>");
	$("#nrStory1").append("<h4></h4>");
	$("#nrStory1").append("<p class=\"nrSnippet\"></p>");
	$("#nrStory1").append("<p class=\"nrReadmore\"></p>");
	$("#nrStory1").addClass("nrToggleDisplay");
	//    Story 2
	$("#nrStory").append("<div id=\"nrStory2\"></div>");
	$("#nrStory2").append("<h3></h3>");
	$("#nrStory2").append("<h4></h4>");
	$("#nrStory2").append("<p class=\"nrSnippet\"></p>");
	$("#nrStory2").append("<p class=\"nrReadmore\"></p>");
	$("#nrStory2").addClass("nrToggleDisplay");
	$("#nrStory2").hide();
	// Add the SlideViewer Elements
	$("#nrSlideViewer").append("<div id=\"nrSlideViewerBorder\"></div>");
	$("#nrSlideViewer").append("<div id=\"nrSlideSelector\"></div>");
	// Load Data
	$.get(pathToXMLData, "", dataLoaded);
}
function dataLoaded(xmlData, textStatus, jqXHR) {
	//alert($(xmlData).find("newsitem").slice(0, 1).text());
	rotAttr = $(xmlData).find("newsrotator");
	// Display the title
	$("#nrStory1 h3").html(rotAttr.attr("title"));
	$("#nrStory2 h3").html(rotAttr.attr("title"));
	// Customize Color
	//    Background Color
	if(rotAttr.attr("bgcolor") != undefined) {
		$("#newsrotator").css("background-color", "#"+rotAttr.attr("bgcolor"));
	}
	//    Frame Color
	if(rotAttr.attr("framecolor") != undefined) {
		$("#nrSlideViewerBorder").css("border-color", "#"+rotAttr.attr("framecolor"));
		$("#nrSlideSelector").css("background-color", "#"+rotAttr.attr("framecolor"));
	}
	//    Story Titles
	if(rotAttr.attr("itemtitlecolor") != undefined) {
		$("#newsrotator h4").css("color", "#"+rotAttr.attr("itemtitlecolor"));
	}
	//    Links
	if(rotAttr.attr("itemreadmorecolor") != undefined) {
		$("#newsrotator a").css("color", "#"+rotAttr.attr("itemreadmorecolor"));
	}
	// Load the blank images
	nrImagesPath = rotAttr.attr("images");
	$("#nrSlideViewer").append("<div id=\"nrSlide1\" class=\"nrSlide\"><img src=\""+nrImagesPath+"blank.gif\"></div>");
	$("#nrSlideViewer").append("<div id=\"nrSlide2\" class=\"nrSlide\"><img src=\""+nrImagesPath+"blank.gif\"></div>");
	$("#nrSlide1").addClass("nrSlide");
	$("#nrSlide2").addClass("nrSlide");
	$("#nrSlide1").addClass("nrToggleDisplay");
	$("#nrSlide2").addClass("nrToggleDisplay");
	$("#nrSlide2").hide();
	// Get the list of News Items
	newsList = $(xmlData).find("newsitem");
	var newsListTest = $(xmlData).find("newsitem");
	var dateString = ""
	var beginDate, endDate, currentDate;
	//     Check the start/end dates of each item and remove the ones that should not be displayed
	newsNum = newsListTest.length;
	for (var i=0;i<newsNum;i++) {
		// Get begin date
		dateString = $(newsListTest[i]).attr("begin");
		if (dateString.length > 0) {
			beginDate = new Date(
				Number(dateString.substr(0, 4)),	// Year
				Number(dateString.substr(5, 2))-1,	// Month (zero based)
				Number(dateString.substr(8, 2)),	// Date
				Number(dateString.substr(11, 2)),	// Hour (24 hour)
				Number(dateString.substr(14, 2)));	// Minute
		} else {
			beginDate = new Date(2000, 0, 1);  // Create a date object for the past
		}
		// Get end date
		dateString = $(newsListTest[i]).attr("end");
		if (dateString.length > 0) {
			endDate = new Date(
				Number(dateString.substr(0, 4)),	// Year
				Number(dateString.substr(5, 2))-1,	// Month (zero based)
				Number(dateString.substr(8, 2)),	// Date
				Number(dateString.substr(11, 2)),	// Hour (24 hour)
				Number(dateString.substr(14, 2)));	// Minute
		} else {
			endDate = new Date(2100, 0, 1); // Create a date object for the future
		}
		// Get current date
		currentDate = new Date();
		if ( (beginDate > currentDate) || (endDate < currentDate) ) {
			newsList.splice(i-(newsListTest.length-newsList.length),1);
		}
	}
	newsNum = newsList.length;
	if (newsNum > 9) newsNum = 9;  // Limit the number of stories to 9
	// Display the story buttons
	for (var i=0;i<newsNum;i++) {
		$("#nrSlideSelector").append("<div id=\"nrSelect" + (i+1) + "\" class=\"nrSelect\"><span>" + (i+1) + "</span></div>");
		$("#nrSelect"+(i+1)).css("left", i*28+4);
		$("#nrSelect"+(i+1)).hover(
			function () {
				$(this).addClass("nrSelectHover");
			},
			function () {
				$(this).removeClass("nrSelectHover");
			} );
		$("#nrSelect"+(i+1)).click(nrSelectClick);
	};
	// Display the Play/Pause button
	$("#nrSlideSelector").append("<div id=\"nrPlayPause\" class=\"nrSelect\"><img src=\""+nrImagesPath+"nrPause.png\"></div>");
	$("#nrPlayPause").css("left", 270);
	$("#nrPlayPause").click(nrPlayPauseClick);
	$("#nrPlayPause").hover(
		function () {
			$(this).addClass("nrSelectHover");
		},
		function () {
			$(this).removeClass("nrSelectHover");
		} );
	// Button Colors
	if(rotAttr.attr("buttonbgcolor") != undefined) {
		$(".nrSelect").css("background-color", "#"+rotAttr.attr("buttonbgcolor"));
	}
	if(rotAttr.attr("buttonfgcolor") != undefined) {
		nrButtonFGColor = "#"+rotAttr.attr("buttonfgcolor");
		$(".nrSelect").css("color", nrButtonFGColor);
	}
	if(rotAttr.attr("buttonhlcolor") != undefined) {
		nrButtonHLColor = "#"+rotAttr.attr("buttonhlcolor");
	}
	// Display First Story
	newsIdx = 0;
	nrPlaying = true;
	displayStory();
	//// Start the timer
	//nrTimerID = setInterval("timerComplete()", 5000);
	//nrPlaying = true;
}
function nrSelectClick() {
	clearInterval(nrTimerID);
	nrPlaying = false;
	$("#nrSelect"+(newsIdx+1)).css("color",nrButtonFGColor);
	newsIdx = Number($("span", this).html())-1;
	displayStory();
	nrPlayPauseDisplay();
}
function nrPlayPauseClick() {
	if (nrPlaying) {
		clearInterval(nrTimerID);
		nrPlaying = false;
	} else {
		nrTimerID = setInterval("timerComplete()", 5000);
		nrPlaying = true;
	}
	nrPlayPauseDisplay();
}
function nrPlayPauseDisplay () {
	if (nrPlaying) {
		$("#nrPlayPause").html("<img src=\""+nrImagesPath+"nrPause.png\">");
	} else {
		$("#nrPlayPause").html("<img src=\""+nrImagesPath+"nrPlay.png\">");
	}
}
function displayStory() {
	var target = "";
	currentSlot = (currentSlot + 1) % 2;
	// Load story elements
	$("#nrStory"+(currentSlot+1)+" h4").html($(newsList[newsIdx]).attr("title"));
	$("#nrStory"+(currentSlot+1)+" p.nrSnippet").html($(newsList[newsIdx]).text());
	if ($(newsList[newsIdx]).attr("newwin")=="true") target = " target=\"_blank\"";
	$("#nrStory"+(currentSlot+1)+" p.nrReadmore").html("<a href=\""+$(newsList[newsIdx]).attr("readmore")+"\""+target+">"+$(newsList[newsIdx]).attr("readmoretext")+"</a>");
	// color links
	if(rotAttr.attr("itemreadmorecolor") != undefined) {
		$("#newsrotator a").css("color", "#"+rotAttr.attr("itemreadmorecolor"));
	}
	// Load image
	$("div#nrSlide"+(currentSlot+1)+" img").attr("src", nrImagesPath + $(newsList[newsIdx]).attr("image"));
	// Cross Fade
	$("div.nrToggleDisplay").fadeToggle(500);
	$("div.nrToggleDisplay").promise().done(fadeToggleComplete);
	$("#nrSelect"+(newsIdx+1)).css("color", nrButtonHLColor);
}
function fadeToggleComplete() {
	if (nrPlaying) {
		nrTimerID = setInterval("timerComplete()", 5000);
	}
}
function timerComplete() {
	clearInterval(nrTimerID);
	// Load the next story
	$("#nrSelect"+(newsIdx+1)).css("color",nrButtonFGColor);
	newsIdx = (newsIdx + 1) % newsNum;
	displayStory();
}

