$(document).ready(function() {

  // News and Events toggles.

  $('div#news-and-events h2').click(function() {
    if (!$(this).parents('div.section').hasClass('visible')) {

      var sectionToHide = $('div#news-and-events div.section.visible');
      var sectionToShow = $(this).parents('div.section');

      sectionToHide.removeClass('visible');
      sectionToShow.addClass('visible');


      sectionToHide.find('h2').addClass('shadow');
      sectionToShow.find('h2').removeClass('shadow');

      sectionToHide.find('div.section-content').fadeOut(100, function() {
        sectionToShow.find('div.section-content').fadeIn(100);
      });

    }
  });

  $('div#news-and-events div.styled-selector select').change(function() {
    $.ajax({
      url: '/news-and-events-for-region/',
      cache: false,
      dataType: 'json',
      data: {
        region: $(this).val()
      },
      success: function(content) {
        $('div#news div.section-content').html(content.news);
        $('div#events div.section-content').html(content.events);
      }
    });
  });

  // Gallery thumbnail click.

  /*
  $('p#gallery-main-image img').bind('load', function() {
    if ($(this).is(':hidden')) {
      $(this).fadeIn('fast');
    }
  });

  $('div#gallery-scroller div.thumbnails ul li a').click(function() {

    var largeImageSrc = $(this).attr('href');
    var largeImageAlt = $(this).attr('title');

    $('p#gallery-main-image img').fadeOut('fast', function() {
      $('p#gallery-main-image img')
        .attr('src', largeImageSrc)
        .attr('title', largeImageAlt)
        .attr('alt', largeImageAlt);
    });

    return false;

  });
  */

  // Gallery scroller.

  /*
  if ($('div#gallery-scroller div.thumbnails ul').width() > 288) {
    $('div#gallery-scroller div.thumbnails ul').jcarousel({
      scroll: 1,
      auto: 1.5,
      initCallback: initGalleryThumbnailSelector,
      buttonNextHTML: null,
      buttonPrevHTML: null,
      wrap: 'circular',
      animation: 1000
    });
  }
  */

  // Toggle Hover for join button
  $('#join-nawic-button').mouseenter(function() {
    $(this).attr('src', '/img/join-nawic-button-hover.gif');
  })
  $('#join-nawic-button').mouseleave(function() {
    $(this).attr('src', '/img/homepage/join-nawic-button.png');
  })

});

// Gallery callback.

function initGalleryThumbnailSelector(carousel) {

  // Handle left/right buttons (disable auto-scroll and move forward/back)

  $('div#gallery-scroller p.move-left img').bind('click', function() {
    carousel.startAuto(0);
    carousel.prev();
    return false;
  });

  $('div#gallery-scroller p.move-right img').bind('click', function() {
    carousel.startAuto(0);
    carousel.next();
    return false;
  });

  // Handle hovering over the items (pause auto-scroll).

  carousel.clip.hover(function() {
    carousel.stopAuto()
  }, function() {
    carousel.startAuto();
  });

}
