if (window.disqus === undefined) {
	var disqus = {};
}

(function () {
	disqus.initialize = function () {
		// Password changing
		$('.changePasswordField > a').click(function (event) {
			$(this).hide();
			$('#change-password-fields').show();
			return false;
		});

		// set up delete user button.
		$('button.delete-user').click(function (event) {
			getDeleteAccount();
		});
	
		// Notifications - Set toggling
		$('.receive-notifications input').click(function (event) {
			// if($(this).attr('value') == 1)
			if($(this).attr('value') == '1') {
				$('.notification-options').show('medium');
			} else {
				$('.notification-options').hide('medium');
			}
		});

		// Help form.
		$('.contact-help form button.button').click(function (event) {

			if ($('input[name=contact]').length && !$('input[name=contact]').val().length) {
				$('div.help-validation').show();

				return false;
			}
			$('.contact-help form').submit();
		});
	};

	// private json methods.
	var urls = {
		'delete': '/auth/json/user_delete/'
	};

	var getDeleteAccount = function () {
		$.get(urls['delete'], {
		}, function (data) {
			if (data.succeeded) {
				$.facebox(data.message);

				var delete_form = $('#user-delete');
				// set up cancel button.
				delete_form.find('button.cancel').click(function (event) {
					$.facebox.close();

					return false;
				});

				// set up delete button.
				delete_form.find('button.delete').click(function (event) {
					if (!delete_form.find(':checked').length) {
						$('#user-delete-validation').show();

						return false;
					}

					postDeleteAccount(delete_form.find(':checked').val(), $('textarea[name=delete_other]').val());

					return false;
				});
			}
		}, 'json');
	};

	var postDeleteAccount = function (reason, other) {
		$.post(urls['delete'], {
			'delete_reason': reason,
			'delete_other': other
		}, function (data) {
			if (data.succeeded) {
				$.facebox.close();
				$('button.delete-user').replaceWith(data.message);
			}
		}, 'json');
	};

})();

$(document).ready(function () {
	disqus.initialize();
});
