// Initialize.
var allowDoIt = true;
var manualRotate = false;
var manualSpeed;
var pause = false;
var startRotate;

function init_rotator() {
    
	// Does element exist?
	if (!$('#rotator').length) {

		// If not, exit.
		return;
    }

	// Rotate speed.
    var speed = 2000;
    manualSpeed = speed;

	// Pause setting.
	//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');

		// Continue.
		function doIt() {
		    if (allowDoIt) {
		        rotate($next_li);
		    }
		}

		// Fade out <li>.
		var d = new Date();
		var time = d.getTime();
		$(element).fadeOut(speed);

		// 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');

		// Layla's solution, find current control link
		var count = $next_li.attr("id");
		count = count.substring(6);
		count = "#photoThumbnail_" + count;
		count = $(count);

		// Animate.
		d = new Date();
		var time2 = d.getTime();
		var diff = time2 - time;

		if (pause == false && count.hasClass('current') == false) {
		    $('#rotator_controls a.current').removeClass('current');
		    //$next_a.addClass('current');
		    count.addClass('current');
		}

		// Show next <li>.
		$($next_li).fadeIn(speed, function () {
		    d = new Date();
		    time2 = d.getTime();
		    diff = time2 - time;
		    if (diff < (speed / 2)) {
		        setTimeout(doIt, speed / 10);
		    }
		    else {
		        // Slight delay.
		        if (pause == false && count.hasClass('current') == false) {
		            $('#rotator_controls a.current').removeClass('current');
		            //$next_a.addClass('current');
		            count.addClass('current');
		        }
		        setTimeout(doIt, speed);
		    }
		});
    }

    function start() {
        rotate($('#rotator li:visible:first'));
    }
    startRotate = start;
    
	// Add click listeners for controls.
	$('#rotator_controls a').click(function () {

	    // Change button text.
	    if ($('#rotator_play_pause').hasClass('pauseButton')) {
	        $('#rotator_play_pause').removeClass('pauseButton');
	        $('#rotator_play_pause').addClass('playButton');
	    }


	    // Show target, hide other <li>.
	    $($(this).attr('href')).show().siblings('li').hide();

	    // Add class="current" and remove from all others.
	    $(this).addClass('current').parent('li').siblings('li').find('a').removeClass('current'); ;

	    // Pause animation.
	    pause = true;

	    // Nofollow.
	    this.blur();
	    return false;
	});

	// Pause / Play the animation.
	$('#rotator_play_pause').click(function () {

	    // What does the button say?
	    if ($(this).hasClass('pauseButton')) {

	        // Stop rotation.
	        pause = true;

	        // Change the text.
	        $('#rotator_play_pause').removeClass('pauseButton');
	        $('#rotator_play_pause').addClass('playButton');

	    } else {

	        // Remove class="pause".

	        // Start the rotation.
	        pause = false;
	        rotate('#rotator li:visible:first');

	        // Change the text.
	        $('#rotator_play_pause').removeClass('playButton');
	        $('#rotator_play_pause').addClass('pauseButton');
	    }



	    this.blur();
	    return false;
	});


	// Hide all but first <li>.
	$('#rotator li:first').show();


	// Begin rotation.
	//rotate($('#rotator li:visible:first'));

	// Wait for page load.
	$(window).load(function () {

	    // Begin rotation.
	    if (!manualRotate) {
	        rotate($('#rotator li:visible:first'));
	    }	    
	});
}

// Kick things off.
$(document).ready(function () {
    init_rotator();
});
