(function($) {
	$.fn.ClearTypeFadeTo = function(options) {
		if (options)
			$(this)
				.show()
				.each(function() {
					if (jQuery.browser.msie) {
						// Save the original background color
						$(this).attr('oBgColor', $(this).css('background-color'));
						// Set the bgColor so that bold text renders correctly (bug with IE/ClearType/bold text)
						$(this).css({ 'background-color': (options.bgColor ? options.bgColor : '#fff') })
					}
				})
				.fadeTo(options.speed, options.opacity, function() {
					if (jQuery.browser.msie) {
						// ClearType can only be turned back on if this is a full fade in or
						// fade out. Partial opacity will still have the problem because the
						// filter style must remain. So, in the latter case, we will leave the
						// background color and 'filter' style in place.
						if (options.opacity == 0 || options.opacity == 1) {
							// Reset the background color if we saved it previously
							$(this).css({ 'background-color': $(this).attr('oBgColor') }).removeAttr('oBgColor');
							// Remove the 'filter' style to restore ClearType functionality.
							$(this).get(0).style.removeAttribute('filter');
						}
					}
					if (options.callback != undefined) options.callback();
				});
	};

	$.fn.ClearTypeFadeIn = function(options) {
		if (options)
			$(this)
				.css({ opacity: 0 })
				.ClearTypeFadeTo({ speed: options.speed, opacity: 1, callback: options.callback });
	};

	$.fn.ClearTypeFadeOut = function(options) {
		if (options)
			$(this)
				.css({ opacity: 1 })
				.ClearTypeFadeTo({ speed: options.speed, opacity: 0, callback: options.callback });
	};
})(jQuery);

function copy_shipping() 
{
  if($("#billing_same").attr('checked')) {
	  $("input[name='billing_address_line1']").val($("input[name='address_line1']").val());
	  $("input[name='billing_address_line2']").val($("input[name='address_line2']").val());
	  $("input[name='billing_city']").val($("input[name='city']").val());
	  $("#billing_state").val($("#state").val());
	  $("input[name='billing_zip']").val($("input[name='zip']").val());
	  $("#billing_country").val($("#country").val());
  }
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;	
}

function updateDonationAmountHelp(helpObj,valObj) {
	if(valObj.value > 0) {
		$("#donation_amt_other").val('');
	}
	
	var amt;
	if(valObj.value.length == 0) {
		amt = 0;
	}
	else {
		amt = valObj.value;
	}
		
	var perChild = Math.max(0,parseFloat(amt)) / $("input[name='num_members']").val();
	$(helpObj).html('$' + addCommas(perChild.toFixed(2)) + ' per member' );
}

function updateDonationOtherHelp(helpObj,valObj) {
	$("input[name='donation_amt']:nth(6)").attr("checked","checked");
	
	// remove non-digits
	valObj.value = valObj.value.replace(/[^\d.]/g,'');
	var amt;
	if(valObj.value.length == 0) {
		amt = 0;
	}
	else {
		amt = valObj.value;
	}	
	amt = Math.max(0,parseFloat(amt));
	var perChild = amt / $("input[name='num_members']").val();
	
	$(helpObj).html('$' + addCommas(perChild.toFixed(2)) + ' per member' );
}

function submitGiftForm() {
	$("button[name='donate']").attr('disabled',true);
	$("button[name='donate']").addClass('processing');
	return true;
}

function updateBillingFields() {
	if($("#billing_id").val() != 0) {
		$('#ccInfo').hide('normal');
		$('#billingInfo').hide('normal');
	}
	else {
		$('#ccInfo').show('normal');
		$('#billingInfo').show('normal');	
	}
}

function toggleAnonymous() {
	if($("#anonymous").is(":checked")) {
		$('#sponsor_name').val("Anonymous");
	}
	else {
		$('#sponsor_name').val("");
	}
} 

function updateGoal(o) {
	var current_amt = $(o).find("input[name='current_amt']").val();
	var goal = $(o).find("input[name='goal']").val();
	var id = $(o).find("input[name='id']").val();
	$(o).parent().hide();
	
	var span = $(o).parent().parent().find("span");
	var goalBarFull = $(o).parent().parent().find(".goalBarFull");
	
	span.ClearTypeFadeOut('fast');
	
	$.ajax({
		type: "GET",
		cache: false,
		url: '/give/fundraisers/update-goal/id/' + id + '/goal/' + goal,
		success:function(html) {
			span.html(html);
			var pattern = new RegExp("[,$]","g");
			var newGoal = parseFloat(html.replace(pattern,''));
			var newPercent = Math.min(current_amt / newGoal * 100.0, 100.0);
			goalBarFull.animate({
				width: newPercent + "%" 
			}, 500);
			span.ClearTypeFadeIn('fast');
		}
	});
		
	return false;
}

function ecardSubmitted() {
	$('#shareFundraiserEcardForm').submit();
}

$(document).ready(function(){
	$("#GiveOnline input[name='donation_amt']").click(function() {
		updateDonationAmountHelp('#donationHelp1',this);
	});
	$("#GiveOnline #donation_amt_other").keyup(function(e) {
		updateDonationOtherHelp('#donationHelp1',this)
	});
	$("#anonymous").click(function() {
		toggleAnonymous();
	});
	$("#billing_id").attr("onChange","updateBillingFields()");
	$(".goal a").click(function() {
		$(this).siblings('.goalForm').show();
	});
	$("#tribute_mail").click(function() {
		if($(this).attr('checked')) {
			$("#tributeAddress").show();
		}
		else {
			$("#tributeAddress").hide();
		}
	});
	$("input[name='in_honor_of']").click(function() {
		if($(this).attr('checked')) {
			$("input[name='in_honor_of_name']").parent().parent().show();
		}
		else {
			$("input[name='in_honor_of_name']").parent().parent().hide();
		}
	});	
});
