text: (function () {
  
      angular.module('plan', ['ui.bootstrap', 'ngRoute']);
  
      function config ($routeProvider, $locationProvider) {
          $routeProvider
              .when('/', {
                  templateUrl: '/home/home.view.html',
                  controller: 'homeCtrl'
              })
              .when('/register', {
                  templateUrl: '/authentication/register/register.view.html',
                  controller: 'registerCtrl'
              })
              .when('/login', {
                  templateUrl: '/authentication/login/login.view.html',
                  controller: 'loginCtrl'
              })
              .when('/profile', {
                  templateUrl: '/profile/profile/profile.view.html',
                  controller: 'profileCtrl'
              })
              .otherwise({redirectTo: '/'});
  
          // use the HTML5 History API
          $locationProvider.html5Mode(true);
      }
  
      //TODO: update this to handle other things non logged in people shouldn't see
      function run($rootScope, $location, authentication) {
          $rootScope.$on('$routeChangeStart', function(event, nextRoute, currentRoute) {
              if ($location.path() === '/profile' && !authentication.isLoggedIn()) {
                  $location.path('/');
              }
          });
      }
  
      angular
          .module('plan')
          .config(['$routeProvider', '$locationProvider', config])
          .run(['$rootScope', '$location', 'authentication', run]);
  
  })();
  (function () {
  
      angular
          .module('plan')
          .controller('homeCtrl', homeCtrl);
  
      homeCtrl.$inject = [];
      function homeCtrl() {
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .controller('loginCtrl', loginCtrl);
  
      loginCtrl.$inject = ['$location', '$scope', 'authentication'];
      function loginCtrl($location, $scope, authentication) {
  
          $scope.credentials = {
              username : "",
              password : ""
          };
  
          $scope.onSubmit = function () {
              authentication
                  .login($scope.credentials)
                  .error(function(err){
                      alert(err.message);
                  })
                  .then(function(){
                      $location.path('/profile');
                  });
          };
  
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .controller('registerCtrl', registerCtrl);
  
      registerCtrl.$inject = ['$location', '$scope', 'authentication'];
      function registerCtrl($location, $scope, authentication) {
  
          $scope.errorPresent = false;
          $scope.errorMsg = [];
  
          $scope.credentials = {
              username: "",
              password : "",
              confirmPass : ""
          };
          
          $scope.onSubmit = function () {
              console.log('Checking registration...');
  
              authentication
                  .register($scope.credentials)
                  .error(function(err){
                      alert(err.message);
                  })
                  .then(function () {
                      $location.path('profile');
                      console.log('Submitting registration');
                  });
          };
  
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .service('authentication', authentication);
  
      authentication.$inject = ['$http', '$window'];
      function authentication ($http, $window) {
  
          var saveToken = function (token) {
              $window.localStorage['token'] = token;
          };
  
          var getToken = function () {
              return $window.localStorage['token'];
          };
  
          var isLoggedIn = function() {
              var token = getToken();
              var payload;
  
              if(token){
                  payload = token.split('.')[1];
                  payload = $window.atob(payload);
                  payload = JSON.parse(payload);
  
                  return payload.exp > Date.now() / 1000;
              } else {
                  return false;
              }
          };
  
          var currentUser = function() {
              if(isLoggedIn()){
                  var token = getToken();
                  var payload = token.split('.')[1];
                  payload = $window.atob(payload);
                  payload = JSON.parse(payload);
                  return {
                      name : payload.username
                  };
              }
          };
  
          var register = function(user) {
              return $http.post('/api/register', user).success(function(data){
                  saveToken(data.token);
                  $window.localStorage['id'] = currentId().id;
              });
          };
  
          var login = function(user) {
              return $http.post('/api/login', user).success(function(data) {
                  saveToken(data.token);
                  $window.localStorage['id'] = currentId().id;
              });
          };
  
          var currentId = function () {
              if(isLoggedIn()){
                  var token = getToken();
                  var payload = token.split('.')[1];
                  payload = $window.atob(payload);
                  payload = JSON.parse(payload);
                  return {
                      id : payload._id
                  };
              }
          };
  
          var logout = function() {
              $window.localStorage.removeItem('token');
          };
  
          return {
              currentUser : currentUser,
              saveToken : saveToken,
              getToken : getToken,
              isLoggedIn : isLoggedIn,
              register : register,
              login : login,
              logout : logout
          };
      }
  
  })();
  (function() {
  
      angular
          .module('plan')
          .service('planData', planData);
  
      planData.$inject = ['$http', 'authentication'];
      function planData ($http, authentication) {
  
          var getProfile = function (username) {
              if (username) {
                  return $http.get('/api/profile?user=' + username, {
                      headers: {
                          Authorization: 'Bearer ' + authentication.getToken()
                      }
                  });
              } else {
                  return $http.get('/api/profile', {
                      headers: {
                          Authorization: 'Bearer ' + authentication.getToken()
                      }
                  });
              }
          };
  
          var getOnlineUsers = function () {
              return $http.get('/api/profile/online', {
                  headers: {
                      Authorization: 'Bearer '+ authentication.getToken()
                  }
              });
          };
  
          var getOnlineFriends = function () {
              return $http.get('/api/friends/online', {
                  headers: {
                      Authorization: 'Bearer '+ authentication.getToken()
                  }
              });
          };
  
          return {
              getProfile : getProfile,
              getOnlineUsers : getOnlineUsers,
              getOnlineFriends: getOnlineFriends
          };
      }
  
  })();
  (function () {
      angular
          .module('plan')
          .factory('socketFact', socketFact);
  
      socketFact.$inject = ['$rootScope', '$window', 'authentication'];
      function socketFact($rootScope, $window, authentication) {
  
          var on = function (eName, callback) {
              $window.socket.on(eName, function () {
                  var argus = arguments;
                  $rootScope.$apply(function () {
                      callback.apply( $window.socket, argus);
                  });
              });
          };
  
          var emit = function (eName, data, callback) {
              $window.socket.emit(eName, data, function () {
                  var argus = arguments;
                  $rootScope.$apply(function () {
                      if (callback) {
                          callback.apply( $window.socket, argus);
                      }
                  });
              })
          };
  
          var destroy = function () {
              $window.socket.removeAllListeners();
          };
  
          $window.socket = io('http://localhost:3000');
          emit('socket-register', $window.localStorage['id']);
  
          return {
              on: on,
              emit: emit,
              destroy: destroy
          };
      }
  })();
  (function() {
  
      angular
          .module('plan')
          .controller('profileCtrl', profileCtrl);
  
      profileCtrl.$inject = ['$scope', '$location', '$routeParams', 'planData', 'authentication'];
      function profileCtrl($scope, $location, $routeParams, planData, authentication) {
  
          var username = $routeParams.view;
          $scope.myProfile = true;
  
          if (username) {
              $scope.myProfile = false;
          }
  
          $scope.user = {};
  
          $scope.logout = function () {
              authentication.logout();
              $location.path('/');
          };
  
          planData.getProfile()
              .success(function (data) {
                  $scope.user = data;
              })
              .error(function (err) {
                  console.log(err);
              });
  
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .controller('navbarCtrl', navbarCtrl);
  
      navbarCtrl.$inject = ['$rootScope', 'authentication'];
      function navbarCtrl($rootScope, authentication) {
          $rootScope.isLoggedIn = authentication.isLoggedIn;
      }
  
  })();
  
  (function () {
  
      angular
          .module('plan')
          .directive('navbar', navbar);
  
      function navbar () {
          return {
              restrict: 'EA',
              templateUrl: '/common/directives/navbar/navbar.template.html',
              controller: 'navbarCtrl'
          };
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .controller('sidebarCtrl', sidebarCtrl);
  
      sidebarCtrl.$inject = ['$rootScope', 'authentication', 'planData', 'socketFact'];
      function sidebarCtrl($rootScope, authentication, planData, socketFact) {
          $rootScope.isLoggedIn = authentication.isLoggedIn;
          $rootScope.onlineUsers = [];
          planData.getOnlineFriends()
              .success(function (data) {
                  $rootScope.onlineUsers = data;
              })
              .error(function (err) {
                  console.log(err)
              });
          socketFact.on('update_list', function (username) {
              console.log("socket recieved update_list");
              console.log(username);
              $rootScope.onlineUsers.push(username);
          });
          socketFact.on('remove_list', function (username) {
              console.log("socket recieved remove_list");
              console.log(username);
              var index = $rootScope.onlineUsers.indexOf(username);
              if (index >= 0) {
                  $rootScope.onlineUsers.splice(index, 1);
              }
          });
          $rootScope.$on('$destroy', function (event) {
              socketFact.destroy();
          });
      }
  
  })();
  (function () {
  
      angular
          .module('plan')
          .directive('sidebar', sidebar);
  
      function sidebar () {
          return {
              restrict: 'EA',
              templateUrl: '/common/directives/sidebar/sidebar.template.html',
              controller: 'sidebarCtrl'
          };
      }
  
  })();