// Initialize.
function init_rotator() {
    // Does element exist?
    if (!$('#rotator').length) {
        // If not, exit.
        return;
    }

    // Rotate speed.
    var speed = 600;
    var change = 3000;
    var currentone = 1;
    // 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('div').length ?
            $(element).next('div') :
            $('#rotator div:first');
    
        // Either next / first control link.
        var $next_a = $('#secondary_navigation a.current').parent('li').next('li').length ?
            $('#secondary_navigation a.current').parent('li').next('li').find('a') :
            $('#secondary_navigation a:first');

        // Continue.
        function doIt() {
            rotate($next_li);
        }
		
		// Change background image
		$('#page').removeClass('panel_' + currentone);
		currentone++;
		if(currentone > 9)
		{
			currentone = 1;
		}
		$('#page').addClass('panel_' + currentone);

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

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

        // Show next <li>.
        $($next_li).fadeIn(speed, function() {
            // Slight delay.
            setTimeout(doIt, change);
        });
    }
	
    // Add click listeners for controls.
    $('#secondary_navigation a').click(function() {
        
        // Show target, hide other <li>.
        $($(this).attr('href')).show().siblings('div').hide();
        // Add class="current" and remove from all others.
        $(this).addClass('current').parent('li').siblings('li')
        .find('a').removeClass('current');
		
		// Change background image
		$('#page').removeClass();
		$('#page').addClass($(this).attr('href').substr(1));

        // Pause animation.
        pause = true;
            
        // Nofollow.
        this.blur();
        return false;
    });
    
    // Hide all but first <li>.
    $('#rotator div:first').siblings('div').hide();
    // Wait for page load.
    $(window).load(function() {
        // Begin rotation.
        if(currentone == 1)
        {
            $(this).oneTime(4000, "hide", function() {
                rotate($('#rotator div:visible:first')); 
            });
        }
        else rotate($('#rotator div:visible:first'));
        
    });
}
