$(document).ready(function() {
  
  $('#carts-show form').submit(function(e) {
    var $this = $(this);
    $.ajax({
      url: $this.attr('action'),
      type: 'POST',
      dataType: 'script',
      data: $this.serializeArray(),
      beforeSend: function() {
        $.fancybox.showActivity();
        $this.find('#checkout_btn').css('visibility','hidden');
      },
      complete: function(xhr, textStatus) {
        //called when complete
        $.fancybox.hideActivity();
        $this.find('#checkout_btn').css('visibility','visible');
      },
      success: function(data, textStatus, xhr) {
        //called when successful
      },
      error: function(xhr, textStatus, errorThrown) {
        //called when there is an error
        alert(errorThrown);
      }
    });
    
    e.preventDefault();
  });
  
  if ($('html.catalog').length) {
    var $amount = $('#cart_contribution_amount'),
        $placeholder = $('#contribution_placeholder'),
        handlePlaceholder = function() {
          if($amount.val().length > 0) {
            $placeholder.hide();
          } else {
            $placeholder.show();
          }
        };

    // For back button support in Chrome
    handlePlaceholder();

    $amount.focus(function(event) {
      $placeholder.hide();
    }).blur(handlePlaceholder);

    $('html.catalog input#submit').click(function() {
      if(isNaN($amount.val()) || $amount.val() < 0 || $amount.val() > 2000) {
        alert('You must choose a donation amount that is between $0 and $2000.');
        return false;
      }
    });
  } // if ($('html.general').length)

  $('.flash .close_btn').click(function(event) {
    event.preventDefault();

    var $flash = $(this).parent();

    $flash.animate({
      'height': 0,
      'padding': 0,
    }, { 'duration': 400, 'queue': false });

    $flash.animate({
      'opacity': 0,
      'margin-bottom': 0
    }, { 'duration': 500, 'queue': false });

  }); // $('.flash .close_btn').click()

}); // document.ready

