$(document).ready(function() {
  // Set up slideshow
  var ss = $(".slideshow");
  var slides = $(".slideshow li");
  var transitionTime = 8000;
  
  if (slides.length <= 1) {
    // Do nothing. One image = static "slide show"

  } else if (slides.length == 2) {
    // function crossFade() {
    // alert(slides.get(0).css(class))

  } else {
    $(".hero").css("overflow-x", "hidden");
    ss.jCarouselLite({
      // btnNext: ".next",
      // bthPrev: ".prev",
      easing: "easeInOutCubic",
      start: 0,
      auto: transitionTime,
      speed: 2500
    });
  };
  
  // Expand Search box on focus
  $("#search").focusin(function() {

    $(this).animate({
      width: "64%"
      }, 500);

    $("#quicklinks").animate({
      width: "30%"
    }, 500);
          
  }).focusout(function() {
    $(this).animate({
      width: "47%"
    }, 1500);
    
    $("#quicklinks").animate({
      width: "47%"
    }, 1500);
  });
  
  // Main navigation fade in effects
  $("nav.beta ul li").hover(
    function() { // handlerIn
      $("ul", this).hide().fadeIn(275)
  },function() { // handlerOut
      $("ul", this).fadeOut(500)
  });
  
  // Sub page nav helpers - fade out items underneath
  $("nav.beta").hover(
    function() { // handlerIn
      $(".banner .hero-img, nav.gamma").stop().fadeTo(1500, 0.35)
  },function() { // handlerOut
      $(".banner .hero-img, nav.gamma").stop().fadeTo(1000, 1)
  });
  
  // Lock Main Navigation to top of screen on scroll
  var the_window = $(window);
  var main_nav = $("nav.beta");
  var initial_nav_offset = main_nav.offset().top // save this to reset later
  
  the_window.scroll(function () {
    if (the_window.scrollTop() >= initial_nav_offset) {
      main_nav.addClass("locked");
    } else if (the_window.scrollTop() < initial_nav_offset) {
      main_nav.removeClass("locked");
    };
  });
  
  // Current page links
  $('nav a[href="' + window.location.pathname + '"]').parent().addClass('current');
  $('nav.beta a[href="' + window.location.pathname + '"]').parents('li li').addClass('current');

  // Forms submission / errors
  $('.success').fadeIn(5000);
  $('.errors').fadeIn(3000);

  // jQuery Date Picker
  if ( $('.jquery-datepicker').length )
      $('.jquery-datepicker').datepicker({
          dateFormat: 'yy-mm-dd'
      });
  
  // Same as above but with a pickable "Year".
  if ( $('.jquery-datepicker-year').length )
      $('.jquery-datepicker-year').datepicker({
          dateFormat: 'yy-mm-dd',
          changeYear: true
      });

  // Same as above but with a pickable "Year", and default date of 25yrs ago
  dateObjToday = new Date();
  
  var dateObj1980 = new Date();
  dateObj1980.setFullYear(1980,0,1);
  
  var dateObj1900 = new Date();
  dateObj1900.setFullYear(1900,0,1);
  
  if ( $('.jquery-datepicker-dob').length )
      $('.jquery-datepicker-dob').datepicker({
          dateFormat: 'yy-mm-dd',
          changeYear: true,
          yearRange: '-100:+100',
          minDate: dateObj1900,
          maxDate: dateObjToday,
          defaultDate: dateObj1980
      });

  // Expandable Textareas (ala facebook)
  $('textarea').elastic();
  
  // Modular, cross fading pages
  $('body.modular-xfade .module').hide(); // Set the page up
  if (window.location.hash) {
    $(window.location.hash).show()
  } else {
    $('body.modular-xfade .module.default').show()
  };
  
  $('body.modular-xfade #toc a').click(function () {
    var section_id = $(this).attr("href").substring($(this).attr("href").indexOf("#")); // get #localLink of anchor's href
    $('.module').fadeOut(500);
    $(section_id).fadeIn(1500);
//    window.location.hash=section_id; // Set address bar URL for linking and module display on page load
    return false; // Return false to stop page scrolling to anchor since we're removing and adding content
  });

  // Scroll smoothly to anchors
  // http://flesler.blogspot.com/2007/10/jqueryscrollto.html
  // http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html
  var optionsLocalScroll = {
    hash: true,
    easing:'easeInQuart',
    offset:{top:-40},
    onAfter: function (){
      var origBackgroundColor = $(location.hash).css('backgroundColor'); // grab orig BG colour incase it isn't 'transparent'
      $(location.hash).animate({backgroundColor: '#fff9cc'}, 200) // fade yellow in
        .effect('shake', {distance: 2}, 125, function(){ // perform shake animation
          $(this).animate({backgroundColor: origBackgroundColor}, 2000) //fade yellow out
          })
      }
    };
    
  $('.faq-questions, .nav').localScroll(optionsLocalScroll) // Run localScroll for FAQs, regardless of page
  
  if (!$('body').hasClass('no-localscroll')) { // Only run local scroll on pages without body.no-localscroll
    $.localScroll.hash();
    $.localScroll(optionsLocalScroll);
  };

});
