(function() {
  'use strict';
  angular.module('Monitour.theme')
    .run(stateChangeStart);


  /** @ngInject */
  function stateChangeStart($rootScope, $state, $localStorage, $http, $location) {
   
     // keep user logged in after page refresh
        if ($localStorage.currentUser) {
			console.log( $localStorage.currentUser);
		    $http.defaults.headers.common.Authorization = $localStorage.currentUser.token;
			
			 //you probably don't need this line.  This lets me connect to my server on a different domain
        }
 	   $http.defaults.headers.common['Authorization'] = $localStorage.currentUser;
	 //$http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; 
        // redirect to login page if not logged in and trying to access a restricted page
        $rootScope.$on('$locationChangeStart', function (event, next, current) {
			
                 // redirect to login page if not logged in and trying to access a restricted page
            var restrictedPage = $.inArray($location.path(), ['/login', '/register' , '/password' , '/pin']) === -1;
		    var loggedIn = $localStorage.currentUser;
			
            if (restrictedPage && !loggedIn) {
                $location.path('/login');
            }
          
        });
		
	

	
	
  }

})();