var msiofApp = angular.module('msiofApp', ['ui.bootstrap', 'angularMoment']).config(function($interpolateProvider){
		  $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});
msiofApp.directive('selectOnClick', function () {
		  return {
					 restrict: 'A',
					 link: function (scope, element, attrs) {
								element.on('click', function () {
										  this.select();
								});
					 }
		  };
});

msiofApp.controller('HomeCtrl', function ($scope, $http, $interval) {
		  $scope.servers = {};
		  $scope.filter = '';
		  $scope.loaded = false;
		  $scope.showInstallInstructions = false;
		  $scope.sortBy = '+name';
		  $scope.expandAll = false;
		  $scope.deletedServerKeys = [];

		  $scope.sortOptions = [
					 { 
								'display': 'Name',
								'value': '+name'
					 },
					 { 
								'display': 'Issues',
								'value': '-hasIssues'
					 },
					 { 
								'display': 'Memory Usage',
								'value': '-mem.percentage.usage'
					 },
					 { 
								'display': 'CPU Usage',
								'value': '-cpu.percentage.usage'
					 },
					 { 
								'display': 'Disk Usage',
								'value': '-disk.percentage.usage'
					 },
					 { 
								'display': 'Network Usage',
								'value': '-network.total.totalkbps'
					 },
					 { 
								'display': 'Load Average',
								'value': '-system.loadavg'
					 },
		  ];

		  $scope.deleteServer = function(serverKey) {
					 $scope.deletedServerKeys.push(serverKey);
					 $http({
								method: 'DELETE',
								url: '/server/' + serverKey,
					 }).then(function(response) {
								if(response.success == false) {
										  alert('Failed to remove');
										  // @TODO: Better alert
								} else {
										  $scope.updateServers();
								}
					 });
		  }

		  $scope.setSort = function(sortValue) {
					 $scope.sortBy = sortValue;
		  }

		  $scope.updateServers = function() {
					 $http({
								method: 'GET',
								url: '/servers/' + $scope.apiKey,
					 }).then(function(response){
								var data = response.data;
								$scope.servers = data;
								$scope.loaded = true;
					 });
		  };

		  $scope.apiKeyWatcher = $scope.$watch("apiKey", function(){
					 $scope.updateServers();
					 $interval(function() {
								$scope.updateServers();
					 }, 30000);
					 $scope.apiKeyWatcher();
		  });
});

@@ -502,16 +502,40 @@
 s = %7B%7D;%0A
+%09%09  $scope.alerts = %7B%7D;%0A
 %09%09  $sco
@@ -712,24 +712,279 @@
 Keys = %5B%5D;%0A%0A
+%09%09  $scope.addAlert = function(msg, type) %7B%0A%09%09%09%09%09 type = typeof type !== 'undefined' ? type : 'success';%0A%09%09%09%09%09 $scope.alerts.push(%7B'msg': msg, 'type': type%7D);%0A%09%09  %7D;%0A%0A%09%09  $scope.closeAlert = function(index) %7B%0A%09%09%09%09%09 $scope.alerts.splice(index, 1);%0A%09%09  %7D;%0A%0A
 %09%09  $scope.s
@@ -1877,17 +1877,27 @@
 %09%09%09%09%09%09  
-a
+$scope.addA
 lert('Fa
@@ -1914,46 +1914,28 @@
 move
-');%0A%09%09%09%09%09%09%09%09%09%09  // @TODO: Better alert
+ server', 'danger');
 %0A%09%09%09

add alerts on server removal error