/*
 * The MIT License
 * Copyright (c) 2014-2015 Nick Guletskii
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
require("font-awesome-webpack");

var angular = require("angular");
require("angular-ui-router");
require("angular-ui-bootstrap");
require("angular-ui-bootstrap-tpls");
require("angular-translate");
require("angular-translate-loader-url");
require("angular-ui-codemirror");
require("angular-recaptcha");
require("angular-animate");
require("angular-form-for");
require("angular-form-for-bootstrap");
require("ng-file-upload");

require("app");

angular.module("ool")
	.config( /*@ngInject*/ function($translateProvider) {
		$translateProvider.useSanitizeValueStrategy("escape");
		$translateProvider.useUrlLoader("/translation");
		$translateProvider.preferredLanguage("ru");
	})
	.config( /*@ngInject*/ function($httpProvider, $locationProvider) {

		$locationProvider.html5Mode(true);

		$httpProvider.interceptors.push( /*@ngInject*/ function($q, $rootScope, $location) {
			return {
				"responseError": function(rejection) {
					var status = rejection.status;
					var config = rejection.config;
					if (!config) {
						return $q.reject(rejection);
					}
					var method = config.method;
					var url = config.url;

					if (status === 401) {
						$location.path("/login");
					} else {
						throw new Error(method + " on " + url + " failed with status " + status);
					}

					return $q.reject(rejection);
				}
			};
		});

		$httpProvider.interceptors.push( /*@ngInject*/ function($q, $rootScope, $location) {
			return {
				"request": function(config) {
					if (angular.isDefined($rootScope.authToken)) {
						config.headers["X-Auth-Token"] = $rootScope.authToken;
					}
					return config || $q.when(config);
				},
				"response": function(response) {
					if (response.status === 401) {
						$location.path("/login");
						return $q.reject(response);
					}

					return response;
				},
				"responseError": function(response) {
					if (response.status === 500) {
						$rootScope.showErrorModal = true;
						return $q.reject(response);
					}

					if (response.status === 403) {
						$location.path("/forbidden");
						return $q.reject(response);
					}

					return $q.reject(response);
				}
			};
		});

	}).factory("$exceptionHandler", /*@ngInject*/ function($injector) {
		return function(exception) {
			var $rootScope = $injector.get("$rootScope");
			var $timeout = $injector.get("$timeout");
			$timeout(function() {
				$rootScope.showErrorModal = true;
			}, 1);
			console.error(exception);
			throw exception;
		};
	})
	.run( /*@ngInject*/ function($rootScope, AuthenticationProvider) {
		$rootScope.security = AuthenticationProvider;

		$rootScope.$on("securityInfoChanged", function() {
			$rootScope.security = AuthenticationProvider;
		});
	});
require("ui-setup");

@@ -1928,559 +1928,8 @@
 );%0A%0A
-%09%09$httpProvider.interceptors.push( /*@ngInject*/ function($q, $rootScope, $location) %7B%0A%09%09%09return %7B%0A%09%09%09%09%22responseError%22: function(rejection) %7B%0A%09%09%09%09%09var status = rejection.status;%0A%09%09%09%09%09var config = rejection.config;%0A%09%09%09%09%09if (!config) %7B%0A%09%09%09%09%09%09return $q.reject(rejection);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09var method = config.method;%0A%09%09%09%09%09var url = config.url;%0A%0A%09%09%09%09%09if (status === 401) %7B%0A%09%09%09%09%09%09$location.path(%22/login%22);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09throw new Error(method + %22 on %22 + url + %22 failed with status %22 + status);%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09return $q.reject(rejection);%0A%09%09%09%09%7D%0A%09%09%09%7D;%0A%09%09%7D);%0A%0A
 %09%09$h
@@ -2562,112 +2562,402 @@
 %09%09%7D%0A
-%0A%09%09%09%09%09if (response.status === 403) %7B%0A%09%09%09%09%09%09$location.path(%22/forbidden%22);%0A%09%09%09%09%09%09return $q.reject(response
+%09%09%09%09%09var status = response.status;%0A%09%09%09%09%09var config = response.config;%0A%09%09%09%09%09var method = config.method;%0A%09%09%09%09%09var url = config.url;%0A%0A%09%09%09%09%09if (status === 403) %7B%0A%09%09%09%09%09%09$location.path(%22/forbidden%22);%0A%09%09%09%09%09%09return $q.reject(response);%0A%09%09%09%09%09%7D else if (response.status === 401) %7B%0A%09%09%09%09%09%09$location.path(%22/login%22);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09throw new Error(method + %22 on %22 + url + %22 failed with status %22 + status
 );%0A%09

Remove dependency on execution order in error handling