(function () {

var standalone = !!window.personaExitURL;
var apiPath = standalone ? '' : '../';

var $button = $('<a></a>', {
	href: '#',
	id: 'login-button',
	'class': 'persona-button dark',
	css: {'margin-top': '0.5em'},
});
var $caption = $('<span>Loading...</span>').appendTo($button);
$button.appendTo(standalone ? 'body' : 'fieldset');

function inform(msg, color) {
	$caption.text(msg);
	$button.toggleClass('orange', color == 'orange');
	$button.toggleClass('dark', color == 'dark');
}

function setup_button() {
	if (!window.loggedInUser) {
		inform('Invoke your Persona', 'orange');
		$button.click(function (event) {
			if (navigator.id)
				navigator.id.request();
			else
				inform('Persona system not loaded.', 'dark');
			event.preventDefault();
		});
	}
	else {
		inform('Logout', 'blue');
		$button.click(function (event) {
			if (navigator.id)
				navigator.id.logout();
			else
				inform('Persona system not loaded.', 'dark');
			event.preventDefault();
		});
	}
	$button.focus();
}

function on_login(assertion) {
	inform('Invoking...', 'dark');
	$.ajax({
		type: 'POST',
		url: apiPath+'login',
		data: {assertion: assertion},
		dataType: 'json',
		success: function (res) {
			if (res && res.status == 'okay') {
				inform('Success!', 'blue');
				setTimeout(return_to_site, 500);
			}
			else
				inform(res.message||'Unknown error.', 'dark');
		},
		error: function (res) {
			inform('Network error.', 'dark');
			console.error(res);
		},
	});
}

function on_logout() {
	inform('Logging out...', 'dark');
	$.ajax({
		type: 'POST',
		url: apiPath+'logout',
		data: {csrf: window.x_csrf},
		dataType: 'json',
		success: function (res) {
			if (res && res.status == 'okay') {
				inform('Logged out.', 'orange');
				setTimeout(return_to_site, 1000);
			}
			else
				inform(res.message||'Unknown error.', 'dark');
		},
		error: function (res) {
			inform('Network error.', 'dark');
			console.error(res);
		},
	});
}

function return_to_site() {
	if (standalone)
		window.location.href = window.personaExitURL;
	else
		window.location.reload();
}

yepnope({
	load: 'https://login.persona.org/include.js',
	complete: function () {
		setup_button();
		navigator.id.watch({
			loggedInUser: window.loggedInUser || null,
			onlogin: on_login,
			onlogout: on_logout,
		});
	},
});

$('<link></link>', {
	rel: 'stylesheet',
	href: mediaURL + 'css/persona-buttons.css',
}).appendTo('head');

})();

@@ -622,38 +622,16 @@
 vent) %7B%0A
-%09%09%09if (navigator.id)%0A%09
 %09%09%09navig
@@ -653,66 +653,8 @@
 ();%0A
-%09%09%09else%0A%09%09%09%09inform('Persona system not loaded.', 'dark');%0A
 %09%09%09e
@@ -760,30 +760,8 @@
 ) %7B%0A
-%09%09%09if (navigator.id)%0A%09
 %09%09%09n
@@ -786,66 +786,8 @@
 ();%0A
-%09%09%09else%0A%09%09%09%09inform('Persona system not loaded.', 'dark');%0A
 %09%09%09e

Trim old login code