$(document).ready(function() {
  $('div.loan-application-form textarea, [id$=amount_requested], form#loan_calculation_estimation_form input#amount_borrowed').click(function() { this.value = ''; });

  $('div.loan-application-form form').submit(function() {
	var amount_field = $(this).find('[id$=amount_requested]');
	
	var stripped_val = amount_field.attr('value').replace(/[^\d\.]/gi, '');
	amount_field.attr('value', stripped_val);
	
	var state = $('select#loan_application_applicant_state').attr('value');
	
	// if (!confirm('Reminder:\n\nThe item value field reflects the object\'s second hand value and not the MSRP.\n\nContinue?')) {
	// 		return false;
	// 	}
	// if(state == 'Colorado') {
	// 		alert("We're sorry!\n\nWe are not currently accepting loan applications from Colorado residents. Please call (877) 755 7296.");
	// 		return false;
	// 	}
  });

  $('form#might-get-estimation-form').submit(function() {
	var calculation_type   = $(this).find('input#calculation_type').attr('value');
	
	var item_value         = $(this).find('input#item_value');	
	var stripped_val       = item_value.attr('value').replace(/[^\d\.]/gi, '');
	item_value.attr('value', stripped_val);
	
	var authenticity_token = $(this).find('[name=authenticity_token]').attr('value');
	
	$.post('/loan_calculation', {calculation_type: calculation_type, authenticity_token: authenticity_token, item_value: stripped_val}, function(data) {
		$('div#might-get-results').slideUp('slow',
			function() {
				$('div#might-get-results').html(data).slideDown('slow');
			}
		);
	});
	
    return false;
  });

  $('form#will-pay-estimation-form').submit(function() {
	var calculation_type   = $(this).find('input#calculation_type').attr('value');
	
	var amount_field = $(this).find('input#amount_borrowed');

	var stripped_val = amount_field.attr('value').replace(/[^\d\.]/gi, '');
	amount_field.attr('value', stripped_val);
	
	var loan_period = $(this).find('select#loan_period').attr('value');
	var authenticity_token = $(this).find('[name=authenticity_token]').attr('value');
	
	//  just have to get events in order...
	
	$.post('/loan_calculation', {calculation_type: calculation_type, amount_borrowed: stripped_val, loan_period: loan_period, authenticity_token: authenticity_token}, function(data) {
		$('div#will-pay-results').slideUp('slow',
			function() {
				$('div#will-pay-results').html(data).slideDown('slow');
			}
		);
	});
	
	return false;
  });

  // hiding "hidden" checkbox field that rails is adding
  $('div.loan-application-form [value=0], [name=authenticity_token]').css({background: 'transparent', display: 'none'});
		
  $(':password, :text').addClass('text');
  
  $(':submit').addClass('submit');
  
  $(':input.required').each(function(n,i) {
    $('label[for="'+i.id+'"]').addClass('required_label');
  });
  
  $('a[rel=external]').click(function() { window.open(this.href); return false; });
  
  $('a.popup').click(function() {
    $('<div />').appendTo('body').dialog({
      title: $(this).attr('title'),
      modal: true,
      resizable: false
    }).load($(this).attr('href') + ' form', function() {
      $form = $(this).find('form')
      $form.find(':text:first').focus();
      $btn = $form.find(':submit');
      var txt = $btn.val();
      $btn.remove();
      var buttons = {};
      $dialog = $(this);
      buttons[txt] = function() { 
        $.ajax({
          type: $form.attr('method'),
          url: $form.attr('action'), 
          data: $form.serialize(),
          dataType: 'script',
          complete: function(xhr, status) {
            $text = xhr.responseText;
            $form.append('<div class="'+status+'">'+$text+'</div>');
            setTimeout(function() { $dialog.dialog('close'); },3000);
            return false;
          }
        });
      };
      $(this).dialog('option','buttons', buttons);
    });
    return false;
  });
  
  $('.date_picker').datepicker({dateFormat: 'mm/dd/yy', gotoCurrent: true});
	
	$('#nav li').click(function() {
	  $current = $('#nav ul li ul:visible');
	  $current.hide();
	  $current.parent('li').removeClass('selected');
	  $(this).find('ul').show();
	  $(this).addClass('selected');
	});
	
	$(document).bind('click', function(e) {
    var $clicked=$(e.target);
    if($clicked.is('#nav ul li') || $clicked.parents().is('#nav ul li')) { 
    } else {
      $current = $('#nav ul li ul:visible');
  	  $current.hide();
  	  $current.parent('li').removeClass('selected');
    }
  });
  
});