Index: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js
===================================================================
--- branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js	(revision 13754)
+++ branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js	(revision 13758)
@@ -1,6 +1,6 @@
 ﻿
-angular.module('wjm', ['ui.bootstrap', 'ui.calendar', 'angularTreeview']).
-
-           controller('resourceCtrl', function ($scope, uiCalendarConfig, $compile, $timeout) {
+angular.module('wjm', ['ui.bootstrap', 'ui.calendar', 'angularTreeview', 'ngDialog']).
+
+           controller('resourceCtrl', function ($scope, uiCalendarConfig, $compile, ngDialog , $timeout) {
                var vm = $scope;
 
@@ -33,18 +33,28 @@
                    hubber.client.processDowntime = function (id, down) {
                        var json = JSON.parse(down);
-                       var downtype = ["Unavailable", "Shutdown command"];
                        var arrdown = [];
-                       console.log(json);
+                       var str = "";
+                       var col = "";
                        for (var i = 0; i < json.length; i++) {
+                           if (json[i].DowntimeType === 0) {
+                               str = "Unavailable";
+                               col = "#006080";
+                           }
+                           else {
+                               str = "Shutdown";
+                               col = "#993300";
+                           }
                            arrdown.push({
                                id: json[i].Id,
-                               title: 'offline',
-                               start: json[i].StartDate,
-                               end: json[i].EndDate,
-                               allDay: json[i].AllDayEvent
+                               title: str,
+                               start: new Date(json[i].StartDate),
+                               end: new Date(json[i].EndDate),
+                               allDay: json[i].AllDayEvent,
+                               color: col
                            });
                        }
                        var dat = { 'id': id, 'down': [arrdown] };
                        $scope.treeview.currentNode.calendar = dat;
+
                        $scope.currentcal = $scope.treeview.currentNode.calendar;
                        $scope.$apply();
@@ -53,20 +63,68 @@
                    $scope.$watch("treeview.currentNode", function (newValue, oldValue) {
                        $scope.currentcal = [];
+                       $(".selected.ng-binding").addClass('loaded');
                        $scope.selectedEventId = -1;
                        if ($scope.treeview.currentNode != null) {
                            if ($scope.treeview.currentNode.calendar.length === 0) {
                                hubber.server.requestDownTime(vm.treeview.currentNode.Id);
+
                            }
                            else {
                                $timeout(function () {
                                    $scope.currentcal = $scope.treeview.currentNode.calendar;
-                                   $scope.$apply();
+
                                }, 0);
-                               
-                           }
-                       }
-
-                   });
-               }
+
+                           }
+                       }
+
+                   });
+               }
+               $scope.saveCurrentCalendar = function () {
+                   console.log("Save Current cal");
+               }
+               $scope.saveAllCalendars = function () {
+                   console.log("Save all");
+               }
+               $scope.clearCurrentCalendar = function () {
+                   
+                   $scope.selectedEventId = -1;
+                   $scope.treeview.currentNode.calendar = null;
+                   $scope.currentcal = [];
+                   hubber.server.requestDownTime(vm.treeview.currentNode.Id);
+                   $(".selected.ng-binding").removeClass('changed');
+               }
+               $scope.clearAllCalendars = function () {
+                   ngDialog.openConfirm({
+                       template:
+                        '<p>Are you sure you want to delete all calendar changes?</p>' +
+                        '<div>' +
+                          '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
+                          '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
+                        '</button></div>',
+                       plain: true
+                   }).then(function (success) {
+                       $(".ng-binding.changed").removeClass('changed');
+                       $(".ng-binding.loaded").removeClass('loaded');
+                       $scope.selectedEventId = -1;
+                       $scope.treeview.currentNode.calendar = null;
+                       $scope.currentcal = [];
+                       for (var i = 0; i < $scope.tree.length; i++)
+                           removeCalendarsRecurse($scope.tree[i]);
+                       hubber.server.requestDownTime(vm.treeview.currentNode.Id);
+                       $(".selected.ng-binding").addClass('loaded');
+                   });
+                  
+               }
+               function removeCalendarsRecurse(node) {
+
+                   node.calendar = [];
+                   node.todelete = [];
+
+                   for (var i = 0; i < node.children.length; i++) {
+                       removeCalendarsRecurse(node.children[i]);
+                   }
+               }
+
                $scope.buildTree = function () {
                    vm.tree = [];
@@ -84,4 +142,5 @@
                        }
                    }
+
                };
 
@@ -89,4 +148,5 @@
                    current.calendar = [];
                    current.children = [];
+                   current.todelete = [];
                    current.collapsed = true;
                    for (var t = 0; t < vm.temptree.length;) {
@@ -112,43 +172,81 @@
                    return current;
                }
-
-               /* alert on eventClick */
-               $scope.alertOnEventClick = function (date, jsEvent, view) {
-                   vm.selectedEventId = date._id;
-               };
-               /* alert on Drop */
-               $scope.alertOnDrop = function (event, delta, revertFunc, jsEvent, ui, view) {
-                   vm.treeview.currentNode.calendar.down[0][event._id - 1].start = event.start;
-                   if (vm.treeview.currentNode.calendar.down[0][event._id - 1].end != null)
-                       vm.treeview.currentNode.calendar.down[0][event._id - 1].end = event.end;
-               };
-               /* alert on Resize */
-               $scope.alertOnResize = function (event, delta, revertFunc, jsEvent, ui, view) {
-                   vm.treeview.currentNode.calendar.down[0][event._id - 1].start = event.start;
-                   vm.treeview.currentNode.calendar.down[0][event._id - 1].end = event.end;
-               };
-
-               /* add custom event*/
-               $scope.addEvent = function () {
-                   
-               };
-               $scope.calendarClick = function(date, jsEvent, view) {
-                   console.log(date);
+               function checkId(id) {
+                   for (var i = 0; i < vm.treeview.currentNode.calendar.down[0].length ; i++) {
+                       if (vm.treeview.currentNode.calendar.down[0][i]._id === id)
+                           return i;
+                   }
+                   return -1;
+               }
+               $scope.setChanged = function () {
+                   if (vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].title === "Unavailable")
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].color = '#0099cc';
+                   else
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].color = '#ff5500';
+                   vm.treeview.currentNode.changes = true;
+                   $(".selected.ng-binding").addClass('changed');
+               }
+               //*Add event by click
+               $scope.calendarClick = function (date, jsEvent, view) {
+                   var newid = 0;
+                   if (vm.treeview.currentNode.calendar.down[0].length != 0)
+                       newid = vm.treeview.currentNode.calendar.down[0][(vm.treeview.currentNode.calendar.down[0].length - 1)]._id + 1;
                    vm.treeview.currentNode.calendar.down[0].push({
                        id: '00000000-0000-0000-0000-000000000000',
-                       title: 'offline',
+                       title: 'Unavailable',
                        start: date.toDate(),
                        end: null,
                        allDay: date._ambigTime,
-                       color: '#378006',
-                       _id: (vm.treeview.currentNode.calendar.down[0].length +1)
-                   });
-                   $('#resourcecalendar').fullCalendar('renderEvent',
-                       vm.treeview.currentNode.calendar.down[0][vm.treeview.currentNode.calendar.down[0].length -1], true);
-
-               }
+                       _id: (newid)
+                   });
+
+                   vm.selectedEventId = vm.treeview.currentNode.calendar.down[0].length - 1;
+                   vm.setChanged();
+
+               }
+               /* alert on eventClick */
+               $scope.alertOnEventClick = function (date, jsEvent, view) {
+                   vm.selectedEventId = checkId(date._id);
+               };
+               /* alert on Drop */
+               $scope.alertOnDrop = function (event, delta, revertFunc, jsEvent, ui, view) {
+                   vm.selectedEventId = checkId(event._id);
+                   vm.setChanged();
+                   if (vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay && event.start._ambigTime == false) {
+                       event.allDay = false;
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay = false;
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = null;
+                   }
+                   else if (!vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay && event.start._ambigTime == true) {
+                       event.allDay = true;
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay = true;
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = null;
+                   }
+                   vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].start = new Date(event.start);
+                   if (vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end != null)
+                       vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = new Date(event.end);
+               };
+               /* alert on Resize */
+               $scope.alertOnResize = function (event, delta, revertFunc, jsEvent, ui, view) {
+                   vm.selectedEventId = checkId(event._id);
+                   vm.setChanged();
+                   vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].start = new Date(event.start);
+                   vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = new Date(event.end);
+               };
+
+               /* add custom event*/
+               $scope.addEvent = function () {
+
+               };
+
                /* remove event */
                $scope.remove = function (index) {
-                   $scope.events.splice(index, 1);
+                   vm.selectedEventId = -1;
+                   if (vm.treeview.currentNode.calendar.down[0][index].id != "00000000-0000-0000-0000-000000000000") {
+                       vm.treeview.currentNode.todelete.push(vm.treeview.currentNode.calendar.down[0][index - 1].id);
+                   }
+                   vm.treeview.currentNode.calendar.down[0].splice(index, 1);
+
+
                };
                /* Render Tooltip */
@@ -163,7 +261,8 @@
                $scope.uiConfig = {
                    calendar: {
-                       height: 600,
+                       height: 500,
                        editable: true,
-                       defaultView:'agendaWeek',
+                       defaultView: 'agendaWeek',
+                       firstDay: 1,
                        header: {
                            left: 'title',
@@ -179,5 +278,5 @@
                        dayClick: $scope.calendarClick
 
-               
+
                    }
                };
