(function($) {

  $.fn.clearForm = function() {
    return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
    });
  };

  $.fn.donations = function(){

    var self = $(this);

    $.stagehand.add('amount_selection', {

      up: function(){
        $('#mailer_response').fadeIn();
        $('#gift_amount .decision')
          .css('opacity', 1)
          .find('input')
            .attr('disabled', false);
      },

      down: function(){
        $('#mailer_response').fadeOut();
        $('#gift_amount .decision')
          .css('opacity', 0.6)
          .find('input')
            .attr('checked', false)
            .attr('disabled', true);
      }

    });

    $.stagehand.add('in_honor', {

      up: function(){
        $('#honor_memory').fadeIn();
      },

      down: function(){
        $('#honor_memory')
          .fadeOut()
          .find('input')
            .attr('checked', false);
      }

    });

    $.stagehand.add('gift_recipient', {

      up: function(){
        $('#gift_recipient')
          .fadeIn()
          .find('.decision')
            .css('opacity', 0.6)
              .find('input')
                .attr('checked', false)
                .attr('disabled', true);
      },

      down: function(){
        $('#gift_recipient')
          .fadeOut()
          .find('input[name="donation[honoree]"]')
            .val('');
      }

    });

    $.stagehand.add('gift_recipient_decision', {

      up: function(){
        $('#gift_recipient .decision')
          .css('opacity', 1)
          .find('input')
            .attr('disabled', false);
      },

      down: function(){
        $('#gift_recipient .decision')
          .css('opacity', 0.6)
          .find('input')
            .attr('checked', false)
            .attr('disabled', true);
      }

    });

    $.stagehand.add('mailer_message', {

      up: function(){
        $('#mailer-message').fadeIn();
      },

      down: function(){
        $('#mailer-message').fadeOut();
      }
    });

    function process_amounts(){

      if($(this).is('input:text')) {
        $('.donation_custom').attr('checked', true);
        if($(this).val() == 0) {
          teardown('amount_selection');
          return;
        }
      }

      if($(this).is('.donation_custom')) {
        if($('#gift_amount_selection input:text').val().length == 0) {
          teardown('amount_selection');
          return;
        }
      }

      setup('amount_selection');
    }

    function init(){
      $('#donation_form').clearForm();

      $('#gift_amount_selection input').bind('click keydown keyup keypress', process_amounts);

      $('#gift_amount #honor_decision input').click(function(){
        (!$(this).is(':checked')) ? setup('amount_selection') : setup('in_honor');
      });

      $('#honor_memory input').click(function(){
        $(this).is(':checked') ? setup('gift_recipient') : setup('in_honor');
      });

      $('#gift_recipient input[name="donation[honoree]"]').bind('keydown keyup keypress', function(){
        ($(this).val().length > 0) ? setup('gift_recipient_decision') : teardown('gift_recipient_decision');
      });

      $('#gift_recipient input[name="needs_mailing"]').click(function(){
        $(this).is(':checked') ? setup('mailer_message') : teardown('mailer_message');
      });

      $.stagehand.start();

    }

    function setup(name){
      $.stagehand.setup(name);
    }

    function teardown(name){
      $.stagehand.teardown(name);
    }

    init();

  };

})(jQuery);

$(function(){

  $('.delete_item').click(function(){
    $(this).is(':checked') ? $(this).val('1') : $(this).val('0');
  });

  // Recipient fields need to be validated if the "needs_mailing" checkbox is
  // activated

  var fields = ['name', 'street_1', 'city', 'state', 'zip'];
  var rules = { rules: {} };
  $.each(fields, function(i, field) {
    rules.rules['donation[recipient_' + field + ']'] = {
      required: function(e) {
        return ($('input[name="needs_mailing"]:checked').length > 0);
      }
    };
  });

  // Add a rule that Honoree name is required if a gift type is set
  rules.rules['donation[honoree]'] = {
    required: function(e) {
      return ($('input[name="donation[gift_type]"]:checked').length > 0);
    }
  };

  $('#donation_form').validate(rules);

});
