$(function () {
	$('#unsubscribe a').click(function () {
		if ($('#unsubscribe #unsubAdd').length) {
			$('#unsubAdd').slideUp('fast', function () { $('#unsubAdd').remove(); });
			$(this).html('<strong>Save a Tree</strong> &ndash; Unsubscribe from the paper version of Inside Story</a>');
		} else {
			$.ajax({
				type: "GET",
				url: this.href,
				dataType: 'html',
				error: function(msg) {
					window.location.href = '/unsubscribe/';
				},
				success: function(data) {
					var d = $('<div id="unsubAdd" style="display: none">').hide().prependTo('#unsubscribe');
					//d.hide();
					d.html(data);
					d.slideDown('medium', function () {
						d.find('#confirmButton').click(function() {
							$.ajax({
								type: "GET",
								url: this.href,
								dataType: 'json',
								error: function (msg) {
									window.location.href = '/unsubscribe/confirm/';
								},
								success: function (data) {
									doUnsubSuccess();
								}
							});
							return false;
						});
					});
				}
			});
			$(this).html('Close');
		}
		return false;
	});
});

function doUnsubSuccess() {
	$("#unsubAdd").slideUp('medium',function () {
		$('#unsubAdd').html('<p class="success">You\'ve saved a tree! Thanks!</p>');
		$("#unsubAdd").slideDown('fast');
		window.setTimeout('$("#unsubscribe").slideUp()', 4000);
	});
}