
/*	The Classic News Ticker
	Requires jQuery to work.
	Last updated: August 22, 2008
*/

//	This'll need to be referenced across several functions, so a global variable is necessary here.
//	If the user clicks on the navigation, this is the switch that flips.
var newsAndEventsFrozen = false;

//	Now we can get started. This runs the following functions on page load.
$(function() {	
	initNewsAndEvents();
	window.setInterval("intervalHandler();", 6000);
});

function intervalHandler() {
	if (newsAndEventsFrozen == false) {
		cycleNewsItems();
	}
}

function initNewsAndEvents() {
	//	We want to build this navigation if there is a news section on the page, and if it has more than one piece.
	//	Because having a menu with only one option would be silly.
	if ($("#news") && $("#news li").length > 1) {
		//	Now we create an ordered list to hold the little selector menu on the right.
		var selector = $("<ol></ol>")
			.attr("id", "news-selector")
			.appendTo("#news");
		
		//	To be used in the following function...
		var newSelectorItemLink;

		//	Now we have to create a new list item in the selector for each item in the news-items list.
		$("#news-events li").each(function(i) {

			//	Assign a unique id to each news item so that we can identify them from the selector.
			$(this).attr("id", "news-item-" + i);
			
			//	Construct the link to be added to the new selector item
			newSelectorItemLink = $('<a id="link-to-news-item-' + i + '" href="#" onclick="newsEventLinkHandler(this.id); return false;">&nbsp;</a>');
			
			//	Since we're initializing, the first news item will be selected automatically.
			//	So we just assign the "selected" class to the first selector list item.
			if (i == 0) {
				$(newSelectorItemLink).addClass("selected");
			}
			
			//	Lastly we create a new, matching list element for the menu at the right...
			$("<li></li>")
				//	...attach the newly created link...
				.append(newSelectorItemLink)
				//	...and append this to the menu's list element.
				.appendTo(selector);
		});
		
		//	This adds the divs for the transparent fade images at the top and bottom
		//	of the news section. The if statement screens out IE6, since these are transparent PNGs.
		if (window.XMLHttpRequest) {
			$('<div id="news-fade-top"></div>').appendTo("#news");
			$('<div id="news-fade-bottom"></div>').appendTo("#news");
		}
	}
}

function newsEventLinkHandler(linkToNewsItemId) {
	//	First, now that one of the buttons has been clicked, we want to turn off the automatic cycle.
	newsAndEventsFrozen = true;
	
	//	Then we manage which selector item is being highlighted.
	//	To do this we first remove the class from whatever list item currently has it.
	$("#news-selector li a.selected").removeClass("selected");
	//	Then we add the class to the link that was just clicked.
	$("#" + linkToNewsItemId).addClass("selected");
	
	//	Then we extract the news item we want to navigate to from the link's id.
	var newsItemId = linkToNewsItemId.substr(8);
	var newsItemIdNumber = linkToNewsItemId.substr(18);
	
	//	In order to calculate where to scroll to, we'll need the height of a news item.
	//	To get this we take the height of the news events list and divide by how many items are in it.
	var itemHeight = $("#news-events").height() / $("#news-events li").length;
	
	//	Then we get the pixel position to scroll to by multiplying that height by the number of the news item.
	var scrollAmount = itemHeight * newsItemIdNumber;
	
	//	Finally we scroll to that location.
	scrollIt(newsItemIdNumber);
}

//	This function, to be called on a set interval, scrolls the news section to the next item in order
function cycleNewsItems() {
	//	First we find which news item is currently selected.
	var fullLinkId = $("#news-selector a.selected").attr("id");
	//	Then we extract the number from its id.
	var numberWeNeed = fullLinkId.substr(18);
	
	//	Remove the "selected" class from whatever currently has it.
	$("#news-selector a.selected").removeClass("selected");
	
	//	Now we determine whether we are currently on the last list item.
	var newsItems = $("#news-selector li").length - 1;
	
	if (numberWeNeed == newsItems) {
		//	If so, cycle back to the first item.
		numberWeNeed = 0;
	} else {
		//	If not, increment to the next item.
		numberWeNeed++;
	}
	
	//	Then we construct the id of the next link so we can take that and add the "selected" class
	var newId = "link-to-news-item-" + numberWeNeed;
	
	//	Then we add the class to that link
	$("#" + newId).addClass("selected");
	
	//	Lastly we scroll to the next one.
	scrollIt(numberWeNeed);
}

/*	Modifed from the original code entitled Animated Scrolling with jQuery 1.2 by Karl Swedberg
	http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
*/
function scrollIt(newsItemIdNumber) {
	var divOffset = $('#news-events').offset().top;
	var liOffset = $('#news-events li:eq(' + newsItemIdNumber + ')').offset().top;
	var liScroll = liOffset - divOffset;
	$('#news-events').animate({scrollTop: '+=' + liScroll + 'px'}, 1000);
}

