// Initialize.
function init_rotator() {

	// Does element exist?
	if (!$('#rotator').length) {

		// If not, exit.
		return;
	}

	// Rotate speed.
	var speed = 2000;

	// Pause setting.
	var pause = false;

	// Rotator function.
	function rotate(element) {

		// Stop, if user has interacted.
		if (pause) {
			return;
		}

		// Either the next /first <li>.
		var $next_li = $(element).next('li').length ? $(element).next('li') : $('#rotator li:first');

		// Either next / first control link.
		var $next_a = $('#rotator_controls a.current').parent('li').next('li').length ? $('#rotator_controls a.current').parent('li').next('li').find('a') : $('#rotator_controls a:first');

		// Animate.
		$('#rotator_controls a.current').removeClass('current');
		$next_a.addClass('current');

		// Continue.
		function doIt() {
			rotate($next_li);
		}

		// Fade out <li>.
		$(element).fadeOut(speed);

		// Show next <li>.
		$($next_li).fadeIn(speed, function() {

			// Slight delay.
			setTimeout(doIt, speed);
		});
	}

	// Hide all but first <li>.
	$('#rotator li:first').show();

	// Wait for page load.
	$(window).load(function() {

		// Begin rotation.
		rotate($('#rotator li:visible:first'));
	});
}

// Kick things off.
$(document).ready(function() {
	init_rotator();
});
