app.ng.controller("App.Controller.DateView", ["$scope", "$interval", function ($scope, $interval) {
   
	if (typeof $scope.date == "string") {
		$scope.date = moment.utc($scope.date).toDate();
	};

	if (!$scope.datestyle) {
		$scope.datestyle = "center";
	}

	$scope.$watch('date', function (newValue, oldValue) {
		if (newValue !== oldValue) {
			$scope.updateDates();
		}
	});

	$scope.updateDates = function () {
		if ($scope.date) {

			if (typeof $scope.date == "string") {
				$scope.date = moment.utc($scope.date).toDate();
			};

			$scope.rawDate = $scope.date.toLocaleString();
			$scope.mainDate = moment($scope.date).fromNow();
		}
		else {
			$scope.rawDate = "";
			$scope.mainDate = "Not set";
		}
	}

	$scope.updateDates();

	$interval($scope.updateDates, 60000);
    
}]);
app.ng.controller("App.Controller.FileUpload", ["$scope", "$timeout", "$http", "$upload", function ($scope, $timeout, $http, $upload) {

    $scope.onManualFileSelect = function ($files) {
        $scope.waitingFiles = $files;
    }

    $scope.upload = function () {
        $scope.onFileSelected({ files: $scope.waitingFiles });
        $scope.waitingFiles = null;
    }

	$scope.onFileSelect = function ($files) {
		$scope.onFileSelected({ files: $files });
	}
}]);
app.ng.controller("App.Controller.GuidanceEditor", ["$scope", "$rootScope", "$http", "entityController", "$uibModalInstance", "$sce","options", function ($scope, $rootScope, $http, entityController, $uibModalInstance, $sce, options) {

    $scope.options = options;

    angular.extend($scope, entityController);
    $scope.entity = entityController.load(options.indicatorId, 'Indicator').then(function (entity) { $scope.entity = entity; });

    $scope.sce = $sce;

    $scope.saveGuidance = function () {
        $uibModalInstance.close($scope.entity);
    }
}]);
app.ng.controller("App.Controller.PageSizer", ["$scope", "dataContext", "$rootScope", function ($scope, dataContext, $rootScope) {

	$scope.pageSize = 10;

	$scope.listener = $scope.$watch('pageSize', function (newValue, oldValue) {
		if (newValue !== oldValue) {
			if ($scope.onPageSizeChanged) {
				$scope.onPageSizeChanged({ pageSize: $scope.pageSize });
			}
		}
	}, true);

}]);
