$(document).ready(function() {
   if('#imageRotator') {
            
      $('#imageRotator ul li').css({
         display: 'none'
      });
      
      $('#imageRotator ul li:first').addClass('show');
         
      $('#imageRotator ul li:first').css({
         display: 'inline'
      });
      
      //don't start rotating images if there aren't at least 2 images to show
      if($('#imageRotator ul li').length >= 2) {
         setInterval('rotate()', 3000);
      }
   }

});

function rotate(){
   
   // get the first image in the rotator
   var currentImage = ($('#imageRotator ul li.show') ? $('#imageRotator ul li.show') : $('#imageRotator ul li:first'));
   
   //get the next image. When it reaches the end go back to the first image
   var nextImage = ((currentImage.next().length) ? ((currentImage.next().hasClass('show')) ? $('#imageRotator ul li:first') : currentImage.next()) : $('#imageRotator ul li:first'));

   if(currentImage.next().length === 1) {      
      nextImage.css({
         display: 'none'
      })
      .addClass('show')
      .fadeIn('slow', function(){
         currentImage.fadeOut('slow')
      .removeClass('show');  
      });
   }
   else {
      nextImage.css({
         display: 'none'
      })
      .addClass('show')
      .fadeIn('slow');
      
      currentImage.fadeOut('slow')
      .removeClass('show');
   }
   
   
}
