$(document).ready(function(){
  // Drop down menus
  $('li.toplink').hover(
    function() { $(this).addClass('active'); $('ul.subnav', this).slideDown(200); },
    function() { $(this).removeClass('active'); $('ul.subnav', this).slideUp(100); }
  );


  // Fancybox
  $('a.zoom').fancybox({
    'hideOnContentClick': true,
    'overlayShow': false,
    'zoomOpacity': true,
    'zoomSpeedIn': 300,
    'zoomSpeedOut': 300
  });


  // Auto Rotate our home page slideshow
  var slideShowInterval = 7000;
  var slideIntervalId = setInterval("slideSwitch()", slideShowInterval);
  slideFadeAllThumbnails(0); // Fade all of our slideshow thumbnails
  slideUnfadeThumbnail('photo1', 0);  // And unfade the first thumbnail

  // Hovering over the slideshow should stop the slideshow
  $('#slideshow').hover(
    function() { clearInterval(slideIntervalId); },
    function() { slideIntervalId = setInterval("slideSwitch()", slideShowInterval); }
  );

  // Hovering over a slideshow thumbnail should show the slide for that thumbnail
  $('#thumbnails .thumbnail').hover(
    function() {
      clearInterval(slideIntervalId); // Stop the slideshow
      var photo_id = $(this).attr('id').replace('-thumbnail', '');
      // Switch to the thumbnails slide, unless that slide is already active
      if (!$("#" + photo_id).hasClass("active")) {
        slideSwitch(photo_id);
      }
    },
    function() {
      slideIntervalId = setInterval("slideSwitch()", slideShowInterval); // Restart the slideshow
    }
  );

  // Add even/odd class to gallery links
  $("div.gallery-link-container:odd").addClass("odd");
  $("div.gallery-link-container:even").addClass("even");
});

function slideSwitch(next_id) {
  // Show all slideshow images (we hide all of them except for the initial slide on page load, so you dont see a bunch of images loading at once)
  $('#slideshow a').show();

  var $active = $('#slideshow A.active');
  if ( $active.length == 0 ) $active = $('#slideshow A:last');
  if (next_id) {
    var $next = $("#" + next_id);
    var speed = 0;
  } else {
    var $next =  $active.next().length ? $active.next() : $('#slideshow A:first');
    var speed = 800;
  }
  $active.addClass('last-active');
  $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, speed * 2, function() {
    $active.removeClass('active last-active');
  });

  // Fade all thumbnails, and then unfade the next thumbnail
  slideFadeAllThumbnails(speed);
  slideUnfadeThumbnail($next.attr('id'), speed);
}

//function slideSwitch() {
//  var $active = $('#slideshow A.active');
//  if ( $active.length == 0 ) $active = $('#slideshow A:last');
//  var $next =  $active.next().length ? $active.next() : $('#slideshow A:first');
//  $active.addClass('last-active');
//  $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
//    $active.removeClass('active last-active');
//  });
//
//  // Fade all thumbnails, and then unfade the next thumbnail
//  slideFadeAllThumbnails(400);
//  slideUnfadeThumbnail($next.attr('id'), 400);
//}

function slideFadeAllThumbnails(speed) {
  $('#thumbnails img').fadeTo(speed, 0.60);
  //$('#thumbnails .caption').fadeOut(speed);
}

function slideUnfadeThumbnail(id, speed) {
  $('#thumbnails #' + id +'-thumbnail img').fadeTo(speed, 1.0);
  //$('#thumbnails #' + id +'-thumbnail .caption').fadeIn(speed);
}

