// app.js

//function resolveController(path) {
//    var deferred = $q.defer();
//    var script = document.createElement('script');
//    script.src = path;
//    script.onload = function () {
//        $scope.$apply(deferred.resolve());
//    };
//    document.body.appendChild(script);
//    return deferred.promise;
//};



angular.module('myApp', [
  'ejangular',
  'xeditable',
  'ngAnimate',
  'ngFileUpload',
  'LocalStorageModule',
  'mgcrea.ngStrap',
  'ngSanitize',
  'ngDialog',
  'angularMoment',
  'ngRoute'
])


.config([
  '$routeProvider', '$httpProvider', '$compileProvider',
  function($routeProvider, $httpProvider, $compileProvider) {
    $compileProvider.debugInfoEnabled(false);
    
    var forceRefresh = '?' + moment().format();
    $routeProvider
    //Test route to view (cshtml) with autoload private script
      .when('/test', {
      templateUrl: '/Home/About'
    })

    .when('/', {
        templateUrl: '/App/Main/views/login/login.html' + forceRefresh
          //templateUrl: '/App/Main/views/home/home.html'
          //,controller: 'home',
          //controllerAs: 's'
      })
      .when('/Reports', {
        templateUrl: '/App/Main/views/Reports/Reports.html' + forceRefresh
      })

    .when('/:section', {
        templateUrl: function(rp) {
            //if (rp.section === "default") {
            //    rp.section = "content";
            //}

            //$.getScript('/App/Main/views/' + rp.section + '/' + rp.section + '.js');

            //var script = document.createElement("script");
            //script.setAttribute("type", "text/javascript");
            //script.setAttribute("src", '/App/Main/views/' + rp.section + '/' + rp.section + '.js');
            //document.getElementsByTagName("head")[0].appendChild(script);


            return '/App/Main/views/' + rp.section + '/' + rp.section + '.html' + forceRefresh;
          }
          //resolve: resolveController('/App/Main/views/contact/contact.js')
      })
      .when('/:section/:tree', {
        templateUrl: function(rp) {
          if (!rp.section || !rp.tree) {
            return '/dashboard';
          } else {
            //resolve: resolveController('/App/Main/views/' + rp.section + '/' + rp.tree + '/' + rp.tree + '.js')

            //var script = document.createElement("script");
            //script.setAttribute("type", "text/javascript");
            //script.setAttribute("src", '/App/Main/views/' + rp.section + '/' + rp.tree + '/' + rp.tree + '.js');
            //document.getElementsByTagName("script")[0].appendChild(script);


            return '/App/Main/views/' + rp.section + '/' + rp.tree + '/' + rp.tree + '.html' + forceRefresh;
          }
        }
      })
      .when('/:section/:tree/:method', {
        templateUrl: function(rp) {
          if (!rp.section || !rp.tree) {
            return '/dashboard';
          } else {

            return '/App/Main/views/' + rp.section + '/' + rp.tree + '/' + rp.tree + '.html' + forceRefresh;
          }
        }
      })
      .when('/:section/:tree/:method/:id', {
        templateUrl: function(rp) {
          if (!rp.section || !rp.tree) {
            return '/dashboard';
          } else {
            return '/App/Main/views/' + rp.section + '/' + rp.tree + '/' + rp.tree + '.html' + forceRefresh;
          }
        }
      })

    .otherwise({
      redirectTo: '/'
    });

    //		$httpProvider.interceptors.push('AuthHttpResponseInterceptor');

    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    //delete $httpProvider.defaults.headers.common["X-Requested-With"];
  }


])

.directive('onFinishRender', function($timeout) {
  return {
    restrict: 'A',
    link: function(scope, element, attr) {
      if (scope.$last === true) {
        $timeout(function() {
          scope.$emit('ngRepeatFinished');
        });
      }
    }
  }
});
