// Initialize.
function init_rotator() {

// Does element exist?
if (!$('#rotator').length) {

// If not, exit.
return;
}

// Rotate speed.
var speed = 1000;
var delay = 8000;

// 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');
	 var $next_li2 = $(element).next('li').length ? $(element).next('li') : $('#rotator li:first');

    // Continue.
    function doIt() {
      rotate($next_li);
    }

	$('#nav_billboard a.active').removeClass('active');

      // Either next / first control link.
    $('#nav_billboard a[href="#' + $($next_li2).attr('id') + '"]').addClass('active');
	
    // Fade out <li>.
    $(element).fadeOut(speed);

    // Show next <li>.
    $($next_li).fadeIn(speed, function() {
      // Clear numbered buttons.
   //   $('#nav_billboard a.active').removeClass('active');

      // Either next / first control link.
     // $('#nav_billboard a[href="#' + $(this).attr('id') + '"]').addClass('active');

	  
      // Is it paused?
      if (!pause) {

        // Slight delay.
        setTimeout(doIt, delay);
      }
    });
  }

   // Add click listeners for controls.
  $('#nav_billboard a').click(function() {

    // Pause animation.
    pause = true;

    // Stop in-progress animations.
    $('#rotator li:animated').stop();

    // Change button text.
    $('#rotator_play_pause').html('PLAY');
	$('#rotator_play_pause').show();

    // Show target, hide other <li>.
    $($(this).attr('href')).show().css({
      opacity: 1
    }).siblings('li').hide().css({
      opacity: 1
    });

    // Add class="current" and remove from all others.
    $('#nav_billboard a.active').removeClass('active');
    $(this).addClass('active');

    // Nofollow.
    this.blur();
    return false;
  });

  
    // Pause / Play the animation.
  $('#rotator_play_pause').click(function() {

    // Is it paused?
    if (pause) {

      // Remove class="pause".
      pause = false;

      // Start the rotation.
      rotate('#rotator li:visible:first');

      // Change the text.
      $(this).html('PAUSE');
	  $(this).hide();
    }
    else {

      // Stop rotation.
      pause = true;

      // Change the text.
	 
      $(this).html('PLAY');
	   $(this).show();
    }

    // Nofollow.
    this.blur();
    return false;
  });
  
  


// Hide all but first <li>.
$('#rotator li:first').show();
    $('#rotator_play_pause').hide();

// Wait for page load.
$(window).load(function() {
// Begin rotation.

function doIt2() {
rotate($('#rotator li:visible:first'));

}
// tried to add delay before first rotation but wasn't working in chrome...       
setTimeout(doIt2, 5000); 

});

}


function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
 
    });
}



// Kick things off.
$(document).ready(function() {



init_rotator();  
});
