/*
 * This file contains all the variables for a user's session.
 * It should contain 
 * These variables can modified by the client.
 *
 */

var usergrid = usergrid || {};

usergrid.session = (function() {

  /* This code block *WILL* load before the document is complete */

  function loggedIn() {
    return self.loggedInUser && self.accessToken;
  }

  function readIt() {
    self.currentOrganization = localStorage.getObject('currentOrganization');
    self.currentApplicationId = localStorage.getItem('currentApplicationId');
    self.loggedInUser = localStorage.getObject('usergridUser');
    self.accessToken = localStorage.getItem('accessToken');
  }

  function clearIt() {
    self.loggedInUser = null;
    self.accessToken = null;
    self.currentOrganizationName = null;
    localStorage.removeItem('usergridUser');
    localStorage.removeItem('accessToken');
    if (usergrid.client.useSSO()){
      sendToSSOLogoutPage();
    }
  }

  function saveIt() {
    localStorage.setObject('currentOrganization', self.currentOrganization);
    localStorage.setItem('currentApplicationId', self.currentApplicationId);
    localStorage.setObject('usergridUser', self.loggedInUser);
    localStorage.setItem('accessToken', self.accessToken);
  }

  var self = {
    loggedInUser: {},
    accessToken: "",
    currentOrganization: {},
    currentApplicationId: "",

    loggedIn: loggedIn,
    readIt: readIt,
    clearIt: clearIt,
    saveIt: saveIt
  }

  return self

})();

@@ -722,26 +722,156 @@
 elf.
-accessToken = null
+currentOrganizationName = null;%0A    localStorage.removeItem('usergridUser');%0A    self.accessToken = null;%0A    localStorage.removeItem('accessToken')
 ;%0A  
@@ -888,36 +888,32 @@
 rentOrganization
-Name
  = null;%0A    loc
@@ -930,38 +930,83 @@
 removeItem('
-usergridUser')
+currentOrganization');%0A    self.currentApplicationId = null
 ;%0A    localS
@@ -1020,39 +1020,49 @@
 removeItem('
-accessToken
+currentApplicationId
 ');%0A
+%0A
     if (user

complete localStorage clearing upon logout