
/*
 *  IS USER LOGGED IN
 * 	
 *	builds flux ctx user object and returns status of user : loggedIn, loggedInNotValid, & loggedOut
 *	when status is verified it will fire call back function with this status passed to it.
 *	
 *	userStatus:
 *		loggedIn = logged in and has agreed to the terms
 *		loggedInNotValid = logged in but hasn't agreed to the terms
 *		loggedOut = user is not logged in (dur)
 *		fluxIsDown = flux community is not responding
 */

// TODO - this is not how flux context is supposed to be used
// I have hacked this to be correct, but this function should be
// removed or migrated into the 

function isUserLoggedIn(callback) {
	var userStatus = '';
	var userID = '';
	var fluxContext;
	if(typeof(Flux) !== 'undefined' && Flux != null ){ /*is the Flux object available aka is flux up and running*/
		Flux.loadContext(function(ctx){
			fluxContext = ctx;
			if (fluxContext.user != null){
				/* USER IS LOGGED IN */
				if ( fluxContext.user.acceptedCommunityTerms && fluxContext.user.acceptedFluxTerms){
					userStatus = 'loggedIn';
					userID = fluxContext.user.ucid;
					callback(userStatus, userID, fluxContext);
				} else {
					userStatus = 'loggedInNotValid';
					callback(userStatus, userID, fluxContext);
				}
			} else {
				/* USER IS LOGGED OUT */
				userStatus = 'loggedOut';
				callback(userStatus, userID, fluxContext);
			}
		});
	} else {
		userStatus = "fluxIsDown";
		callback(userStatus, userID, fluxContext);
	}
}

