// Sets up the home page rotator using the jQuery Cycle plugin.
//
(function ($) {

  // Initializes the rotator. The rotator-nav div is added here to prevent it from being visible
  // in browsers that don't support JavaScript. Note that the easeinout easing option requires
  // the jQuery Easing plugin to be included on the page.
  //
  function initialize() {
    var isPaused = false;
    var container = $('#rotator > ul');

    // Add a mask for the stroke around the slides and nav
    var mask = $('<a id="rotator-mask" />');
    container.before(mask);

    // Pause the rotator when the cursor is over the mask
    mask.hover(
      function () { container.cycle('pause'); },
      function () { if (!isPaused) container.cycle('resume'); }
    );

    // Create the nav element and add it before the UL
    container.before($('<nav />'));

    // Pauses the slideshow (called when slide is changed manually)
    var pause = function () {
      isPaused = true;
      container.cycle('pause');
    };

    // Updates the mask's href to match the current rotator
    var setMaskHref = function () {
      var link = $(this).find('a:first');
      mask.attr('href', link.attr('href'));
    };

    // Enable the rotator via the jQuery Cycle plugin
    container.cycle({
      before: setMaskHref,
      easing: 'easeinout',
      fx: 'fade',
      pager: '#rotator nav',
      pagerClick: pause,
      pause: true,
      pauseOnPageHover: true,
      speed: 500,
      timeout: 6000
    });
  }

  // Initialize the rotator when the DOM is ready
  $(document).ready(initialize);

})(jQuery);

