var Auth = {};
Auth.display = function() {
	Auth.render();
};

Auth.render = function(html) {
	if (html) {
		$('#anytimeLogin').html(html);
	}
	$('#anytimeLogin').show();
	cancelButton('#anytimeLogin');
}

Auth.submit = function(form) {
	var data = $(form).serialize();
	ajax('/auth/new', data, function(json) {
		if (json.success) {
			Auth.success(json);
			$('#anytimeLogin').hide().empty();
		}
		else {
			Auth.fail(json);
		}
	});
};

Auth.fail = function(json) {
	Auth.render(json.html);
	$('body').removeClass('loggedIn');
};

Auth.success = function(json) {
	$('body').addClass('loggedIn');

	if (typeof(destinations) != 'undefined') {
		if (json.userDestinations) {
			destinations.clearList('pending');
			$('#userDestinations').replaceWith(json.userDestinations);
			destinations.setClassToDestinations('pending');
			destinations.setClassToDestinations('completed');
		}
		if (json.address) {
			$('#addressContainer').replaceWith(json.address);
		}
	}
	$('#userProfile').replaceWith(json.profile);
	
	// continue to trip builder if conditions are met
	if (typeof(ViewManager) != 'undefined' && ViewManager.prompted) {
		ViewManager.continueToTripBuilder();
	}
};

Auth.Forget = {};
Auth.Forget.display = function() {
	ajaxHTML('/auth/forget', null, function(html) {
		Auth.Forget.render(html);
	});
};

Auth.Forget.render = function(html) {
	if (html) {
		$('#anytimeForgotPassword').html(html);
	}
	$('#anytimeForgotPassword').show();
	cancelButton('#anytimeForgotPassword');
}

Auth.Forget.submit = function(form) {
	var data = $(form).serialize();
	ajax('/auth/forget', data, function(json) {
		Auth.Forget.render(json.html);
	});
};