jQuery(document).ready( function($){
  var rotator = setInterval('rotateImages()', 6000);

  $('div.button > img.off').first().css('visibility', 'hidden');

  $('.button').click(function() {
    var id = ($(this).attr('id'));
    $(this).children().last().css('visibility', 'hidden');
    $(this).siblings().each(function(index) {
      $(this).children().last().css('visibility', 'visible')
    });

    $('.featured_content').each(function(index) {
    if($(this).attr('id') == id) {
      $(this).addClass('current');
      $(this).removeClass('hidden');
    } else {
      $(this).removeClass('current');
      $(this).addClass('hidden');
    }
    });
    clearInterval(rotator);
  });

});

function rotateImages() {
  var oCurPhoto = $('#featured div.current');
  var oNxtPhoto = oCurPhoto.next();
  
  if (oNxtPhoto.length == 0)
      oNxtPhoto = $('#featured div:first');

  oCurPhoto.removeClass('current').addClass('previous');
  oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 2000,
      function() {
          oCurPhoto.removeClass('previous');
      });
  $('.button').each(function(index) {
    if ($(this).attr('id') == (oNxtPhoto.attr('id'))) {
      $(this).children().last().css('visibility', 'hidden');
      $(this).siblings().each(function(index) {
        $(this).children().last().css('visibility', 'visible')
      });
    }
  });
}

