var rotateId = 0;
var rotateSeconds = 10;
function kickoffRotation() {
	// get the classes in place
	$(".sidebar .learn-item").not("#railroad").addClass("rotatable").eq(0).addClass("active-rotatable");
	rotateInternal();
}
function rotateInternal() {
	rotateId = window.setInterval(rotateBenefit, rotateSeconds*1000);
}
function rotateBenefit() {
	moveToBenefit(1, true);
}
function benefitNext() {
	window.clearInterval(rotateId);
	moveToBenefit(1, false);
	rotateInternal();
}
function benefitBack() {
	window.clearInterval(rotateId);
	moveToBenefit(-1, false);
	rotateInternal();
}
function moveToBenefit(offset, abortIfHover) {
	var benefits = jQuery(".rotatable");
	var numBenefits = benefits.size();
	var activeClass = "active-rotatable";
	var foundActive = false;
	for (var i=0; i<numBenefits; i++) {
		var benefit = benefits.eq(i);
		if (benefit.hasClass(activeClass)) {
			foundActive = true;
			// if the user is currently moused-over, then don't
			// switch.
			//if (abortIfHover && benefit.is(":visible")) {
			//	return;
			//}
			benefit.fadeOut(300, function() {
				jQuery(this).removeClass(activeClass);
				var nextIndex = (i + offset) % numBenefits;
				benefits.eq(nextIndex).fadeIn(300, function() {
					// protect against multiple actives
					benefits.not(this).removeClass(activeClass).hide();
					jQuery(this).addClass(activeClass);
				});
			});
			
			break;
		}
	}
	if (!foundActive) {
		// pick a random one
		var index = Math.floor(Math.random()*numBenefits);
		benefits.eq(index).addClass(activeClass);
	}
}

