(function( $ ){
 $.fn.transitionHeroBlock = function() {
   var the_link_id = this.attr('id');
   var the_block_id = the_link_id.replace('link', 'block'); // ''
   var is_this_block_active = ($('.hero_image_container:visible').first().attr('id') == the_block_id) ;
   if (!is_this_block_active && $('ul.in_transition').length == 0) { // don't transition if a.) the linked-to block is already active b.) transition already underway 
     $('#home_hero_links ul').addClass('in_transition'); // mark the ul so that we can't do more than one transition at a time
     var arrow_src = $('.active_hero_arrow').attr('src');
     $('.active_hero_arrow').remove();
     this.parent().siblings('.active').removeClass('active'); // de-"active"-ate the previously active li
     this.parent().addClass('active'); // 'active'-ate the newly active li
     var newarrow = "<img class='active_hero_arrow' src='" + arrow_src +  "' />";
     this.before(newarrow);
     $('.hero_image_container:visible').first().fadeOut('slow', function() { // fade out the old
       $('#'+the_block_id).fadeIn('slow', function() { // fade in the new
         $('ul.in_transition').removeClass('in_transition'); // everything is finished -Êremove the 'in_transition' mark
       });
     });
   }
   
 };
 
 $.fn.vAlign = function() {
   return this.each(function(i){
     var h = $(this).height();
     var oh = $(this).outerHeight();
     var mt = (h + (oh - h)) / 2;
     $(this).css("margin-top", "-" + (mt+1) + "px");
     $(this).css("top", "50%");
     $(this).css("position", "absolute");
   });
 };

})(jQuery);

$(function() {
  
  $('#secondarynav-wrapper h2 a').vAlign();
  
  $('#home_hero_links a').click(function(e) {
    e.preventDefault();
    $(this).transitionHeroBlock();
  }).vAlign();
  
  $('.nojssubmit').hide();
  $('#date_select').change(function() {
    $(this).closest('form').submit();
  });
  
});
