var ver = Math.floor((Math.random() * 10000) + 1);



(function () {
    'use strict';

    angular
        .module('app', ['ngRoute', 'ngSanitize', 'ngCookies'])
        .config(config)
        .run(run)
        .filter('nl2br', ['$sanitize', function($sanitize) {
			var tag = (/xhtml/i).test(document.doctype) ? '<br />' : '<br>';
			return function(msg) {
				// ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
				msg = (msg + '').replace(/(\r\n|\n\r|\r|\n|&#10;&#13;|&#13;&#10;|&#10;|&#13;)/g, tag + '$1');
				return $sanitize(msg);
			};
		}]).filter('decode2html', ['$sanitize', function($sanitize) {
			return function(msg) {
				// ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
				msg = msg.replace(/@\[([\s\w]+):[0-9a-z-]+\]/gi, '<span>$1</span>');
				return $sanitize(msg);
			};
		}]);    

    config.$inject = ['$routeProvider', '$locationProvider'];
    function config($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                controller: 'HomeController',
                templateUrl: 'home/home.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Virtual Employee Video Dashboard',
            })
			/* New */
            .when('/general', {
                controller: 'veController',
                templateUrl: 'general/index.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'General Folder: Video Database 2.0',
			    db:'ve',
            })
			/* New */
			/* New2 */
            .when('/report', {
                controller: 'veController',
                templateUrl: 'report/index.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Report Folder: Video Database 2.0',
			    db:'ve',
            })
			/* New2 */
			.when('/cam', {
                controller: 'veController',
                templateUrl: 'cam/index.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Cam Folder: Video Database 2.0',
			    db:'ve',
            })
            .when('/virtualemployee', {
                controller: 'veController',
                templateUrl: 'virtualemployee/index.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Video List: Virtual Employee Database',
			    db:'ve',
            })
            .when('/paradox', {
                controller: 'veController',
                templateUrl: 'paradox/index.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Paradox Marketer Database',
			    db:'pd',
            })
            .when('/login', {
                controller: 'LoginController',
                templateUrl: 'login/login.view.html?v=' + ver,
                controllerAs: 'vm',
			    title:'Login',
            })
            .otherwise({ redirectTo: '/' });
    }

    run.$inject = ['$rootScope', '$location', '$cookies', '$http', '$route'];
    function run($rootScope, $location, $cookies, $http, $route) { 
        // keep user logged in after page refresh
        $rootScope.globals = $cookies.getObject('globals') || {};
        if ($rootScope.globals.currentUser) {
            $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata;
        }

        $rootScope.$on('$locationChangeStart', function (event, next, current) {
			// Update Page Title
			//document.title = $route.current.$$route.title;
            // redirect to login page if not logged in and trying to access a restricted page
            var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
            var loggedIn = $rootScope.globals.currentUser; 
            if (restrictedPage && !loggedIn) {
				localStorage.setItem("redirect", $location.path());
				// $location.path('/login?red=' + abc);
                $location.path('/login');
                
            }
        });
    }

})();
