(function(){
    'use strict';
    
    angular.module('sisatosApp')
    
     .config(['$routeProvider', '$controllerProvider',
                function($routeProvider, $controllerProvider){       
                    angular.module('sisatosApp').registerCtrl = $controllerProvider.register;
                    
                    $routeProvider
                    .when('/', {
                        templateUrl : basepathview('inicio.html'),
                        controller  : 'inicioCtrl'
                     })
                     .when('/Modulo/mudarModuloAtual/:moduloId', {
                        templateUrl : basepathview('inicio.html'),
                        controller: 'mudarModuloCtrl'
                     })
                     .when('/Pessoas/index', {
                        templateUrl : basepathview('Pessoas/index.html'),
                        controller: 'PessoasCtrl',
                        resolve: loader('Pessoas',['PessoasCtrl'])
                     })
                     .when('/Pessoas/add', {
                        templateUrl : basepathview('Pessoas/add.html'),
                        controller: 'InserirPessoaCtrl',
                        resolve: loader('Pessoas',['InserirPessoaCtrl'])
                     })
                     .when('/Pessoas/editar/:idPessoa', {
                        templateUrl : basepathview('Pessoas/editar.html'),
                        controller: 'EditarPessoaCtrl',
                        resolve: loader('Pessoas',['EditarPessoaCtrl'])
                     })
                     .otherwise ({ redirectTo: '/' });
                     
                     function basepathview(uri) {
                        return 'app/view/pages/'+uri;
                     }
                     
                     
                     function loadScript(model, file) {
                        var result = $.Deferred(),
                        script = document.createElement("script");
                        script.async = "async";
                        script.type = "text/javascript";
                        script.src = 'app/view/js/ng/'+model+'/'+file+'.js';
                        script.onload = script.onreadystatechange = function (_, isAbort) {
                            if (!script.readyState || /loaded|complete/.test(script.readyState)) {
                               if (isAbort)
                                   result.reject();
                               else
                                  result.resolve();
                          }
                        };
                        
                        script.onerror = function () { result.reject(); };
                        document.querySelector("head").appendChild(script);
                        return result.promise();
                      }
                      
                      function loader(model, arrayName){

                        return {
                          load: function($q){
                                    var deferred = $q.defer(),
                                    map = arrayName.map(function(name) {
                                        return loadScript(model, name);
                                    });

                                    $q.all(map).then(function(r){
                                        deferred.resolve();
                                    });

                                    return deferred.promise;
                            }
                        };
                    }
                     
                     
                     
                }]);
            
})();