jQuery(document).ready(function($){
		   
  // Open external links in new tab/window
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });
    
  // Return inputs to default value on blur.
  $('input[alt]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });

  window.check_total = function(){
    var rewards_form = $('#new_claim');
    $.post('/claims/check_total', rewards_form.serialize(), function(r){
      $('#total').text(r);
    });
  };
  $('input[type=text]', $('#new_claim')).live('change', function(){
    check_total();
  });

  $('a.inc, a.dec').live('click', function(){
    var input = $(this).siblings('input');
    var diff = $(this).attr('class') == 'inc' ? 1 : -1;
    input.val(Math.max((parseInt(input.val()) || 0)+diff, 0)).change();
  });

  $('#claim_account_credit').live('change', function(){
    $('#mask').css({"opacity":0.8,"display":($(this).get(0).checked ? 'block':'none')});
    $('#gifts input').attr('disabled', $(this).get(0).checked);
    check_total();
  }).change();

  $('#admin_content #new_claim').live('submit', function(){
    var tr = $(this).parents('tr');
    var id = $('#claim_customer_id').val();
    var form = $(this); 
    $.post('/admin/customers/'+id, $(this).serialize(), function(r){
      if(r == 'success'){
        $.get('/admin/customers/row/'+id+'?_method=get', function(r){
          tr.prev().replaceWith(r);
          tr.remove();
        });
      }else{
        form.replaceWith(r);
      }
    });
    return false;
  });

});

