// Initially based on http://www.levelfield.com/ticker.html

var notifications;
var currentNotice = 0;
var iPause = 0;
var notifySelector = '#notifications ol li';
var timer = 4000;

$(function() {
	notifications = $(notifySelector).hide().hover(function() {
		$(this).addClass('hovered');
		iPause = 1;
	}, function() {
		$(this).removeClass('hovered');
		iPause = 0;
	}).filter(':eq(0)').show().add(notifySelector).size() - 1;
	setTimeout(notify, notifyTimer());
});

function notify() {
	if (iPause == 0) {
		if (currentNotice > 0 || (currentNotice == 0 && notifications > 1)) {
			$(notifySelector + ':eq(' + currentNotice + ')').fadeOut('slow', function() { $(this).hide(); });
			currentNotice = ++currentNotice % notifications;
			currentNotice = currentNotice >= $(notifySelector).size() ? 0 : currentNotice;
			$(notifySelector + ':eq(' + currentNotice + ')').fadeIn('slow');
		}
	}
	setTimeout(notify, notifyTimer());
}

function notifyTimer() {
	// Set rel="seconds" on each li for how long you want to see that message
	timer = $(notifySelector + ':eq(' + currentNotice + ')').attr('rel') ? $(notifySelector + ':eq(' + currentNotice + ')').attr('rel') * 1000 : 4000;
	return timer;
}