Index: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js
===================================================================
--- branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js	(revision 13758)
+++ branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/wwwroot/js/hubs/calendarhubber.js	(revision 13768)
@@ -2,5 +2,5 @@
 angular.module('wjm', ['ui.bootstrap', 'ui.calendar', 'angularTreeview', 'ngDialog']).
 
-           controller('resourceCtrl', function ($scope, uiCalendarConfig, $compile, ngDialog , $timeout) {
+           controller('resourceCtrl', function ($rootScope, $scope, uiCalendarConfig, $compile, ngDialog, $timeout) {
                var vm = $scope;
 
@@ -29,4 +29,12 @@
                        $scope.buildTree();
 
+                       $scope.$apply();
+                   };
+                   hubber.client.savingCurrentDone = function () {
+                       $scope.clearCurrentCalendar();
+                       $scope.$apply();
+                   };
+                   hubber.client.savingAllDone = function () {
+                       clearAllCalendarsFunc();
                        $scope.$apply();
                    };
@@ -51,5 +59,6 @@
                                end: new Date(json[i].EndDate),
                                allDay: json[i].AllDayEvent,
-                               color: col
+                               color: col,
+                               changed: false
                            });
                        }
@@ -81,9 +90,113 @@
                    });
                }
+               $scope.buildTree = function () {
+                   vm.tree = [];
+                   vm.temptree = [];
+                   vm.top = false;
+                   for (; vm.data.length > 0;) {
+
+                       if (vm.data[0].ParentResourceId == null) {
+                           var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
+                           vm.tree.push(curr);
+                       }
+                       else {
+                           var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
+                           vm.temptree.push(curr);
+                       }
+                   }
+
+               };
+
+               $scope.seekChildren = function (current) {
+                   current.calendar = [];
+                   current.children = [];
+                   current.todelete = [];
+                   current.changes = false;
+                   current.collapsed = true;
+                   for (var t = 0; t < vm.temptree.length;) {
+                       if (current.Id == vm.temptree[t].ParentResourceId) {
+                           current.children.push(vm.temptree.splice(t, 1)[0]);
+                       }
+                       else {
+                           t++;
+                       }
+                   }
+                   var childc = current.children.length;//Remembers count of children received from temp tree;
+                   for (var t = 0; t < vm.data.length;) {
+                       if (current.Id == vm.data[t].ParentResourceId) {
+                           current.children.push(vm.data.splice(t, 1)[0]);
+                       }
+                       else {
+                           t++;
+                       }
+                   }
+                   for (var t = childc; t < current.children.length; t++) {
+                       current.children[t] = $scope.seekChildren(current.children[t]);
+                   }
+                   return current;
+               }
+               function collectInfoToSave(node, refresh, last) {
+                   var arr = node.calendar.down[0];
+                   var toadd = [];
+                   var todel = node.todelete;
+                   var toupd = [];
+                   for (var i = 0; i < arr.length; i++) {
+                       if (arr[i].id === '00000000-0000-0000-0000-000000000000') {
+                           var t = arr[i].allDay.toString();
+                           toadd.push([
+                               arr[i].title,
+                               "" + Date.parse(arr[i].start),
+                               "" + Date.parse(arr[i].end),
+                               t,
+                               node.Id
+                           ]);
+                       }
+                       else if (arr[i].changed === true) {
+                           var t = arr[i].allDay.toString();
+                           toupd.push([
+                               arr[i].id,
+                               arr[i].title,
+                               "" + Date.parse(arr[i].start),
+                               "" + Date.parse(arr[i].end),
+                               t
+                           ]);
+                       }
+                   }
+                   vm.sendtoserv = [toadd, toupd, todel];
+                   hubber.server.saveCalendar(node.Id, todel, toadd, toupd, refresh, last);
+               }
                $scope.saveCurrentCalendar = function () {
-                   console.log("Save Current cal");
+                   collectInfoToSave(vm.treeview.currentNode, true, false);
+                   //true for refresh, false for showing it's only one calendar saved.
                }
                $scope.saveAllCalendars = function () {
-                   console.log("Save all");
+                   $scope.allSave = [];
+                   for (var i = 0; i < $scope.tree.length; i++)
+                       createSaveRequests($scope.tree[i]);
+                   ngDialog.openConfirm({
+                       template:
+                        '<p>Are you sure you want to save all changes made to ' + $scope.allSave.length + ' calendars?</p>' +
+                        '<div>' +
+                        '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
+                        '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
+                        '</button></div>',
+                       plain: true
+                   }).then(function (success) {
+                       for (var i = 0; i < $scope.allSave.length; i++) {
+                           if (i >= $scope.allSave.length - 1)
+                               collectInfoToSave($scope.allSave[i], false, true);
+                           else
+                               collectInfoToSave($scope.allSave[i], false, false);
+                       }
+                   });
+                   
+                   
+               }
+               function createSaveRequests(node) {
+                   if (node.changes === true)
+                       $scope.allSave.push(node);
+                   for (var i = 0; i < node.children.length; i++) {
+                       createSaveRequests(node.children[i]);
+                   }
                }
                $scope.clearCurrentCalendar = function () {
@@ -91,7 +204,11 @@
                    $scope.selectedEventId = -1;
                    $scope.treeview.currentNode.calendar = null;
+                   $scope.treeview.currentNode.todelete = [];
+                   $scope.treeview.currentNode.changes = false;
                    $scope.currentcal = [];
                    hubber.server.requestDownTime(vm.treeview.currentNode.Id);
                    $(".selected.ng-binding").removeClass('changed');
+
+
                }
                $scope.clearAllCalendars = function () {
@@ -105,15 +222,18 @@
                        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');
-                   });
-                  
+                       clearAllCalendarsFunc();
+                   });
+
+               }
+               function clearAllCalendarsFunc() {
+                   $(".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) {
@@ -121,5 +241,5 @@
                    node.calendar = [];
                    node.todelete = [];
-
+                   node.changes = false;
                    for (var i = 0; i < node.children.length; i++) {
                        removeCalendarsRecurse(node.children[i]);
@@ -127,48 +247,59 @@
                }
 
-               $scope.buildTree = function () {
-                   vm.tree = [];
-                   vm.temptree = [];
-                   vm.top = false;
-                   for (; vm.data.length > 0;) {
-
-                       if (vm.data[0].ParentResourceId == null) {
-                           var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
-                           vm.tree.push(curr);
-                       }
-                       else {
-                           var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
-                           vm.temptree.push(curr);
-                       }
-                   }
-
-               };
-
-               $scope.seekChildren = function (current) {
-                   current.calendar = [];
-                   current.children = [];
-                   current.todelete = [];
-                   current.collapsed = true;
-                   for (var t = 0; t < vm.temptree.length;) {
-                       if (current.Id == vm.temptree[t].ParentResourceId) {
-                           current.children.push(vm.temptree.splice(t, 1)[0]);
-                       }
-                       else {
-                           t++;
-                       }
-                   }
-                   var childc = current.children.length;//Remembers count of children received from temp tree;
-                   for (var t = 0; t < vm.data.length;) {
-                       if (current.Id == vm.data[t].ParentResourceId) {
-                           current.children.push(vm.data.splice(t, 1)[0]);
-                       }
-                       else {
-                           t++;
-                       }
-                   }
-                   for (var t = childc; t < current.children.length; t++) {
-                       current.children[t] = $scope.seekChildren(current.children[t]);
-                   }
-                   return current;
+               $scope.deleteAllPreviousEvents = function () {
+                   vm.selectedEventId = -1;
+                   var node = vm.treeview.currentNode.calendar.down[0];
+                   for (var i = 0; i < node.length;) {
+                       if (node[i].end < Date.now()) {
+                           if (node[i].id != '00000000-0000-0000-0000-000000000000')
+                            vm.treeview.currentNode.todelete.push(node[i].id);
+                           $scope.setChanged();
+                           node.splice(i, 1);
+                       }
+                       else
+                           i++;
+                   }
+                   ngDialog.open({
+                       template:
+                        '<p>All previous events have been deleted. Save the changes to confirm deletion</p>',
+                       plain: true
+                   });
+               }
+
+
+               $scope.setChanged = function () {
+                   if (vm.selectedEventId != -1) {
+                       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.calendar.down[0][vm.selectedEventId].changed = true;
+                   }
+                   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;
+                   var dat = date.toDate();
+                   var end = new Date(dat);
+                   end.setHours(dat.getHours() + 2);
+                   console.log(end);
+                   vm.treeview.currentNode.calendar.down[0].push({
+                       id: '00000000-0000-0000-0000-000000000000',
+                       title: 'Unavailable',
+                       start: dat,
+                       end: end,
+                       allDay: date._ambigTime,
+                       _id: (newid)
+                   });
+                   vm.selectedEventId = vm.treeview.currentNode.calendar.down[0].length - 1;
+                   vm.setChanged();
+                   //  $("#resourcecalendar").fullCalendar('refetchEvents');
+
                }
                function checkId(id) {
@@ -179,54 +310,29 @@
                    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: 'Unavailable',
-                       start: date.toDate(),
-                       end: null,
-                       allDay: date._ambigTime,
-                       _id: (newid)
-                   });
-
-                   vm.selectedEventId = vm.treeview.currentNode.calendar.down[0].length - 1;
-                   vm.setChanged();
-
-               }
                /* alert on eventClick */
-               $scope.alertOnEventClick = function (date, jsEvent, view) {
+               $scope.eventClick = function (date, jsEvent, view) {
                    vm.selectedEventId = checkId(date._id);
                };
                /* alert on Drop */
-               $scope.alertOnDrop = function (event, delta, revertFunc, jsEvent, ui, view) {
+               $scope.dragandDrop = function (event, delta, revertFunc, jsEvent, ui, view) {
                    vm.selectedEventId = checkId(event._id);
                    vm.setChanged();
+                   if (event.end == null) {
+                       event.end = moment(event.start).add(2, 'hours');
+                   }
                    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].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);
+                   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) {
+               $scope.resizeEvent = function (event, delta, revertFunc, jsEvent, ui, view) {
                    vm.selectedEventId = checkId(event._id);
                    vm.setChanged();
@@ -235,14 +341,10 @@
                };
 
-               /* add custom event*/
-               $scope.addEvent = function () {
-
-               };
-
                /* remove event */
                $scope.remove = function (index) {
                    vm.selectedEventId = -1;
+                   vm.setChanged();
                    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.todelete.push(vm.treeview.currentNode.calendar.down[0][index].id);
                    }
                    vm.treeview.currentNode.calendar.down[0].splice(index, 1);
@@ -272,7 +374,7 @@
                        timezone: 'UTC',
                        timeFormat: 'HH:mm',
-                       eventClick: $scope.alertOnEventClick,
-                       eventDrop: $scope.alertOnDrop,
-                       eventResize: $scope.alertOnResize,
+                       eventClick: $scope.eventClick,
+                       eventDrop: $scope.dragandDrop,
+                       eventResize: $scope.resizeEvent,
                        eventRender: $scope.eventRender,
                        dayClick: $scope.calendarClick
@@ -281,4 +383,23 @@
                    }
                };
+               $rootScope.$on('$locationChangeStart', function (event, newUrl, oldUrl) {
+                   console.log($(".changed").length);
+                   if ($(".changed").length != 0) {
+                       ngDialog.openConfirm({
+                           template:
+                            '<p>You have unsaved changes. Are you sure you want to leave this page? All changes will be lost. </p>' +
+                            '<div>' +
+                            '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
+                            '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
+                            '</button></div>',
+                           plain: true
+                       }).then(function (success) {
+                           $rootScope.allowNavigation();
+                       });
+                   }
+                   
+                   event.preventDefault(); // This prevents the navigation from happening
+               }
+    );
 
            });
