$(document).ready(function() {

//  window.parent.$("body").animate({scrollTop:0}, 'slow');

// Overlay is displayed if we have content in it
  if ($('#overlay_message').html()) { $('#overlay').show(); $('#overlay_message').show(); }
  $('.close_overlay').click(function(e) { $('#overlay').hide(); $('#overlay_message').hide(); e.preventDefault(); });

  if ($('#notification_message').html()) { $('#notification').show(); }
  $('.close_notification').click(function(e) { $('#notification').slideUp('slow'); e.preventDefault(); });


  if ($('#load_shifts').val() == '1') {
// Loading...
  $('#shifts').html('Loading shifts...<br /><br /><br />');
// Go get the shift data for this event (global variable is set)
    getAllEventShifts($('#id').val());
// Now show them
    displayAllEventShifts();
  }

// Set the graphic
  if ($('#sorted_by').val() == 'date') $('#date_image').attr('src', $('#date_image').attr('src').replace('1', '2'));
  if ($('#sorted_by').val() == 'name') $('#name_image').attr('src', $('#name_image').attr('src').replace('1', '2'));


// Set the width of .shift_detail_width relative to the page; it breaks if the description is long
  var primary_form_width = parseFloat($('#primary_form').css('width'));
  $('.shift_detail_width').css('width', primary_form_width - 142);
  $(window).resize(function(){
      var primary_form_width = parseFloat($('#primary_form').css('width'));
      var w = $(window).width();
      $(".shift_detail_width").css('width', primary_form_width - 142);
  });


// Custom sort
  $('.sort').click(function() {
// Loading...
    $('#shifts').html('Loading shifts...<br /><br /><br />');

    $('#name_image').attr('src', $('#name_image').attr('src').replace('2', '1'));
    $('#date_image').attr('src', $('#date_image').attr('src').replace('2', '1'));
    $(this).children('#' + $(this).attr('id') + '_image').attr('src', $('#' + $(this).attr('id') + '_image').attr('src').replace('1', '2'));

    displayAllEventShifts($(this).attr('id'));
    $('.shift_detail_width').css('width', primary_form_width - 142);
  });

// Overlay shows job description
  $('.job_description').click(function() {
    $('#overlay').show();
    $('#overlay_message').html('<div class="close_overlay">[x] Close</div>' + $(this).children('.job_description_content').html());
    $('#overlay_message').show();
    initCloseOverlay();
  });
  


// Hide email_div if they don't have email
  $('#no_email').click(function() {
    $('#email').val('');
    if ($(this).is(':checked')) $('#email_div').slideUp('fast');
    else $('#email_div').slideDown('fast');
  });

// Show additional_volunteers div (total number)
  $('#additional_volunteers_toggle').click(function() {
    if ($(this).is(':checked')) $('#additional_volunteers_data').slideDown('fast');
    else $('#additional_volunteers_data').hide();
    $('#total_volunteers').val('');
    $('#additional_volunteers_names').hide();
  });
// Show additional_volunteers_names div (names)
  $('#total_volunteers').blur(function() {
    if ($('#total_volunteers').val() > 1) $('#additional_volunteers_names').show();
    else $('#additional_volunteers_names').hide();
  });

  $('#subscribe_vl_explain_link').click(function() {
    if ($('#subscribe_vl_explain_content').css('display') == 'none') $('#subscribe_vl_explain_content').slideDown('slow');
    else $('#subscribe_vl_explain_content').slideUp('slow');

  });

});

//********************************
// Auto-Confirm scripts
if ($('form#auto_confirm_form').length) {
  initAutoConfirm();
}

//********************************
// Self-check scripts
if ($('form#self_check_email_form').length) {
  initSelfCheck();
}
