function quotes_setup_login(target, reason, postcall) {
	var login = $('#login_dialog');
	if(!login.data('jqm_setup')) {
		login.jqm({
			ajax: url_prefix + '/modules/login',
			initialFocus: quotes_login_focus,
			onLoad: function() {
				$(this).keypress(function(ev) {
					if(ev.keyCode == 27)
						$(this).jqmHide();
				});
				quotes_sync_login_target(this);
				quotes_sync_registration_reason(this);
			}
		});
		login.data('jqm_setup', true);
	}
	if(target != null)
		login.data('target', target);
	if(reason != null)
		login.data('reason', reason);
	if(postcall != null)
		login.data('postcall', postcall);
	return login;
}

function quotes_start_login(ev, target, reason, postcall) {
	quotes_setup_login(target == null ? quotes_target : target, reason, postcall).jqmShow();
	return false;
}

function quotes_login_process(data) {
	if(data.login) {
		if(data.username) {
			$('#current_user_display').html(data.username + ' logged in');
			$('#logout_link').attr('title', 'Log out of Discordian Quotes as ' + data.username);
		}
		$('.loginshow').show();
		$('.loginhide').hide();
		var postcall = $('#login_dialog').data('postcall');
		if(postcall)
			postcall();
		quotes_login = data.email;
		$('#login_dialog').jqmHide();
	} else {
		if(data.message)
			$('#login_feedback').html(data.message_text).show();
		else
			$('#login_feedback').hide();
		$('#email').val(data.email);
		$('#password').val(data.password);
		$('#remember').attr('checked', data.remember);
		quotes_login_focus();
	}
}

function quotes_sync_login_target(where) {
	var target = $('#login_dialog').data('target');
	if(target)
		$('.login_link, .registration_link', $(where)).each(function() {
			var href = $(this).attr('href');
			if(href.match(/[?&]target=/))
				href = href.replace(/([?&]target=)[^?&]+/, '$1' + encodeURIComponent(target));
			else
				if(href.match(/\?/))
					href += '&target=' + encodeURIComponent(target);
				else
					href += '?target=' + encodeURIComponent(target);
			$(this).attr('href', href);
		});
	else
		$('.login_link, .registration_link', $(where)).each(function() {
			var href = $(this).attr('href');
			if(href.match(/[?&]target=/)) {
				href = href.replace(/&target=[^?&]+/, '').replace(/\?target=[^?&]+&/, '&').replace(/\?target=[^?&]+$/, '');
				$(this).attr('href', href);
			}
		});
}

function quotes_sync_registration_reason(where) {
	var reason = $('#login_dialog').data('reason');
	if(reason)
		$('.login_link, .registration_link', $(where)).each(function() {
			var href = $(this).attr('href');
			if(href.match(/[?&]reason=/))
				href = href.replace(/([?&]reason=)[^?&]+/, '$1' + encodeURIComponent(reason));
			else
				if(href.match(/\?/))
					href += '&reason=' + encodeURIComponent(reason);
				else
					href += '?reason=' + encodeURIComponent(reason);
			$(this).attr('href', href);
		});
	else
		$('.login_link, .registration_link', $(where)).each(function() {
			var href = $(this).attr('href');
			if(href.match(/[?&]reason=/)) {
				href = href.replace(/&reason=[^?&]+/, '').replace(/\?reason=[^?&]+&/, '&').replace(/\?reason=[^?&]+$/, '');
				$(this).attr('href', href);
			}
		});
}

function quotes_login_focus() {
	if($('#email').val())
		$('#password').focus();
	else
		$('#email').focus();
}

function quotes_login_process(data, status) {
	if(data.login) {
		if(data.username) {
			$('#current_user_display').html(data.username + ' logged in');
			$('#logout_link').attr('title', 'Log out of Discordian Quotes as ' + data.username);
		}
		$('.loginshow').show();
		$('.loginhide').hide();
		quotes_login = data.email;
		$('#login_dialog').jqmHide();
		var postcall = $('#login_dialog').data('postcall');
		if(postcall)
			postcall();
		quotes_refresh_reactions();
	} else {
		if(data.message)
			$('#login_feedback').html(data.message_text).show();
		else
			$('#login_feedback').hide();
		$('#email').val(data.email);
		$('#password').val(data.password);
		$('#remember').attr('checked', data.remember);
		quotes_login_focus();
	}
}

function quotes_start_logout(ev) {
	$.getJSON(url_prefix + '/connectors/logout', quotes_logout_process);
	return false;
}

function quotes_logout_process(data, status) {
	if(!data.logout)
		return;
	$('#current_user_display').html('');
	$('#logout_link').attr('title', 'Log out of Discordian Quotes');
	$('.loginshow').hide();
	$('.loginhide').show();
	quotes_login = null;
	$('.attributions .cancel').click();
	quotes_refresh_reactions();
}

function quotes_refresh_reactions() {
	if(quote_id)
		$.getJSON(url_prefix + '/connectors/quote_reactions', { id: quote_id }, quote_process_reactions);
	$('.tag').each(function() {
		var quote_tag_id = $(this).attr('quote_tag_id');
		$.getJSON(url_prefix + '/connectors/quote_tag_reactions', { id: quote_tag_id }, quote_tag_process);
	});
	$('.attribution').each(function() {
		var quote_attribution_id = $(this).attr('quote_attribution_id');
		$.getJSON(url_prefix + '/connectors/quote_attribution_reactions', { id: quote_attribution_id }, quote_attribution_process);
	});
}
