// Initialize.
function init_rotator() {

	// Does element exist?
	if (!$('#rotator').length) {

		// If not, exit.
		return;
	}

	// Rotate speed.
	var speed = 5000;

	// 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 li.current').next('li').length ? $('#rotator_controls li.current').next('li') : $('#rotator_controls li:first');

		// Animate.
		$('#rotator_controls li.current').removeClass('current');
		$next_a.addClass('current');

		// Continue.
		function doIt() {
			rotate($next_li);
		}

		// Fade out <li>.
		$(element).fadeOut(1000);

		// Show next <li>.
		$($next_li).fadeIn(1000, function() {

			// Slight delay.
			setTimeout(doIt, speed);
		});
	}

	// Add click listeners for controls.
	$('#rotator_controls li').click(function() {

		// Show target, hide other <li>.
		$($(this).find('a').attr('href')).show().siblings('li').hide();

		// Add class="current" and remove from all others.
		$(this).addClass('current').siblings('li').removeClass('current');;

		// Pause animation.
		pause = true;

		// Nofollow.
		this.blur();
		return false;
	});

	// Hide all but first <li>.
	$('#rotator li:first').show();

	// Wait for page load.
	$(window).load(function() {

		// Begin rotation.
		function start(){
			rotate($('#rotator li:visible:first'));	
		}
		setTimeout(start, 5000);
	});
}

// Kick things off.
$(document).ready(function() {
	init_rotator();
	
});
