jQuery(document).ready(function($){
	
	//Enable external links
	$('a[rel~=external]').attr('target','_blank');
	
	//Setup form field with default value
	function setFieldValue(id,value){
		$('#'+id).val(value).focus(function(){
			if( $(this).val() == value)
				$(this).val('');
		}).blur(function(){
			if( $(this).val() == '')
				$(this).val(value);
		});
	}
	
	//Homepage rotation
	if( $('#slideshow').size() ){
		$('#slideshow').cycle({
			fx : 'fade',
			timeout : 5000
		});
	}
	
	var inlineForm = {
		'action' : '/form2/receive.json',
		'callback' : function(data,status){
			if(data.errors_count > 0){
				var errors = '<strong>'+inlineForm.error_message+'</strong><ul>';
				for( var key in data.errors ) errors += '<li>'+data.errors[key]+'</li>';
				errors += '</ul>';
				$('#form-2 .generated-inline-form-message').addClass('error').removeClass('success').html(errors);
			} else {
				$('#form-2 .generated-inline-form-message').addClass('success').removeClass('error').html(inlineForm.success_message);
				$('#form-2')[0].reset();
			}
		},
		'success_message' : '',
		'error_message' : ''
	};	
	$('#form-2').submit(function(){
		$.post(inlineForm.action,$(this).serialize(),inlineForm.callback,"json");
		alert('Thank you for your support!');
		return false;
	});
	
	$('#branding form input[type=text]').focus(function(){
		if( $(this).val() == 'Email Address' || $(this).val() == 'Zip Code')
			$(this).val('');
	});
	
	$('#nav li:nth-child(1)').css('background','none');

  // donate form
  if ($('form#donate'.length)) {
    $('#Other').click(function() { $('#amountCustom').attr('checked','checked'); });
    $('input[name=amount]:radio').not('#amountCustom').click(function() {
      $('#Other').val('');
    });
    $('#period').change(function() {
      if ($(this).val()) {
        $('#recurring_term').removeAttr('disabled').val('5');
        $('.recurrentOpts').slideDown();
      } else {
        $('#recurring_term').attr('disabled','disabled').val('');
        $('.recurrentOpts').slideUp();
      }
    });
    $('.recurrentOpts').hide();
    
    $('#period,#recurring_term,input[name=amount]').change(function() {
      var total = $("input[name=amount]:checked").val();
      if (total == 'Other') { total = $('#Other').val(); }
      $('#totalAmount strong').text( '$' + total * $('#recurring_term').val() );
    });
    $('#donate').validate({
      submitHandler: function(form) {
        $('#SubmDonation').val('Sending...').attr('disabled','disabled');
        form.submit();
      }
    });
  }
  
});