summaryrefslogtreecommitdiff
path: root/www/js/MontageHistoryCtrl.js
blob: 0c8bf2a0bc9836faad6a4cc00265369121ed8a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
// Controller for the montage view
/* jshint -W041, -W093, -W083 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console,ionic,Masonry,moment,Packery, Draggabilly, imagesLoaded, Chart */
// FIXME: This is a copy of montageCtrl - needs a lot of code cleanup
angular.module('zmApp.controllers').controller('zmApp.MontageHistoryCtrl', ['$scope', '$rootScope', 'NVR', 'message', '$ionicSideMenuDelegate', '$timeout', '$interval', '$ionicModal', '$ionicLoading', '$http', '$state', '$ionicPopup', '$stateParams', '$ionicHistory', '$ionicScrollDelegate', '$ionicPlatform', 'zm', '$ionicPopover', '$controller', 'imageLoadingDataShare', '$window', '$translate', 'qHttp', '$q', '$sce',function ($scope, $rootScope, NVR, message, $ionicSideMenuDelegate, $timeout, $interval, $ionicModal, $ionicLoading, $http, $state, $ionicPopup, $stateParams, $ionicHistory, $ionicScrollDelegate, $ionicPlatform, zm, $ionicPopover, $controller, imageLoadingDataShare, $window, $translate, qHttp, $q, $sce) {
  var broadcastHandles = [];
  var isMultiPort = false;
  $scope.isMultiPort = isMultiPort;
  var areStreamsStopped = false;
  var viewCleaned = false;
  $scope.isScreenReady = false;




  //--------------------------------------------------------------------------------------
  // Handles bandwidth change, if required
  //
  //--------------------------------------------------------------------------------------
  var bc = $scope.$on("bandwidth-change", function (e, data) {
    // nothing to do for now
    // eventUrl will use lower BW in next query cycle
  });

  $scope.getLocalTZ = function () {
    return moment.tz.guess();
  };
  //--------------------------------------
  // formats events dates in a nice way
  //---------------------------------------
  $scope.prettifyDateTimeFirst = function (str) {
    if (NVR.getLogin().useLocalTimeZone)
      return moment.tz(str, NVR.getTimeZoneNow()).tz(moment.tz.guess()).format(NVR.getTimeFormat() + '/MMM Do');
    else
      return moment(str).format(NVR.getTimeFormat() + '/MMM Do');
  };
  $scope.prettifyDate = function (str) {
    return moment(str).format('MMM Do, YYYY ' + NVR.getTimeFormat());
  };

  
  $scope.prettifyTime = function (str) {
    if (NVR.getLogin().useLocalTimeZone)
      return moment.tz(str, NVR.getTimeZoneNow()).tz(moment.tz.guess()).format('h:mm a');
    else
      return moment(str).format('h:mm a');
  };
  $scope.prettify = function (str) {
    if (NVR.getLogin().useLocalTimeZone)
      return moment.tz(str, NVR.getTimeZoneNow()).tz(moment.tz.guess()).format(NVR.getTimeFormat() + ' on MMMM Do YYYY');
    else
      return moment(str).format(NVR.getTimeFormat() + ' on MMMM Do YYYY');
  };
  $scope.humanizeTime = function (str) {
    // if (NVR.getLogin().useLocalTimeZone)
    return moment.tz(str, NVR.getTimeZoneNow()).fromNow();
    //  else    
    //     return moment(str).fromNow();

  };
  // if you change date in footer, change hrs
  $scope.dateChanged = function () {
    $scope.datetimeValueFrom.hrs = Math.round(moment.duration(moment().diff(moment($scope.datetimeValueFrom.value))).asHours());
  };
  // if you change hrs in footer, change date 
  $scope.hrsChanged = function () {
    $scope.datetimeValueFrom.value = moment().subtract($scope.datetimeValueFrom.hrs, 'hours').toDate();
    timefrom.toDate();
  };

  
  //--------------------------------------
  // pause/unpause nph-zms
  //---------------------------------------
  $scope.togglePause = function (mid) {
    //console.log ("TOGGLE PAUSE " + mid);
    var m = -1;
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      if ($scope.MontageMonitors[i].Monitor.Id == mid) {
        m = i;
        break;
      }
    }
    if (m != -1) {

      $scope.MontageMonitors[m].Monitor.isPaused = !$scope.MontageMonitors[m].Monitor.isPaused;
      var cmd = 1;
      NVR.debug("Sending CMD:" + cmd + " for monitor " + $scope.MontageMonitors[m].Monitor.Name);
      controlEventStream(cmd, "", $scope.MontageMonitors[m].Monitor.connKey, -1);
    }
  };

  function sendCmd(mid, cmd, extra) {

    var m = -1;
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      if ($scope.MontageMonitors[i].Monitor.Id == mid) {
        m = i;
        break;
      }
    }
    if (m != -1) {
      NVR.debug("Sending CMD:" + cmd + " for monitor " + $scope.MontageMonitors[m].Monitor.Name);
      return controlEventStream(cmd, "", $scope.MontageMonitors[m].Monitor.connKey, -1, extra);
    }

  }
  $scope.seek = function (mid, p) {
    NVR.debug("Slider called with mid=" + mid + " progress=" + p);

    var m = -1;
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      if ($scope.MontageMonitors[i].Monitor.Id == mid) {
        m = i;
        break;
      }
    }
    if (m != -1) {
      $scope.MontageMonitors[i].Monitor.seek = true;
    }

    sendCmd(mid, '14', "&offset=" + p)
      .then(function (success) {
          //console.log ("Removing seek status from "  + $scope.MontageMonitors[i].Monitor.Name);
          $scope.MontageMonitors[i].Monitor.seek = false;

        },
        function (err) {
          //console.log ("Removing seek status from "  + $scope.MontageMonitors[i].Monitor.Name);
          $scope.MontageMonitors[i].Monitor.seek = false;
        });

  };
  $scope.moveFaster = function (mid) {
    sendCmd(mid, 4);
  };
  $scope.moveSlower = function (mid) {
    sendCmd(mid, 5);
  };
  $scope.movePlay = function (mid) {

    var m = -1;
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      if ($scope.MontageMonitors[i].Monitor.Id == mid) {
        m = i;
        break;
      }
    }
    if (m != -1) {
      $scope.MontageMonitors[m].Monitor.isPaused = false;
      var cmd = 2;
      NVR.debug("Sending CMD:" + cmd + " for monitor " + $scope.MontageMonitors[m].Monitor.Name);
      controlEventStream(cmd, "", $scope.MontageMonitors[m].Monitor.connKey, -1);
    }
  };
  //--------------------------------------
  // Called when ion-footer collapses
  // note that on init it is also called
  //---------------------------------------
  $scope.footerExpand = function () {
    // console.log ("**************** EXPAND CALLED ***************");
    $ionicSideMenuDelegate.canDragContent(false);
  };


  $scope.footerCollapse = function () {
    footerCollapse();
  };
  /* Note this is also called when the view is first loaded */
  function footerCollapse() {

    NVR.debug("Inside footerCollapse");
    if (readyToRun == false) {
      NVR.debug("fake call to footerCollapse - ignoring");
      return;
    }


    if ($scope.MontageMonitors == undefined) {
      NVR.debug("montage array is undefined and not ready");
      return;
    }

    $interval.cancel($rootScope.eventQueryInterval);
    $ionicLoading.show({
      template: $translate.instant('kPleaseWait'),
      noBackdrop: true,
      duration: zm.httpTimeout
    });

    $scope.dragBorder = "";
    $scope.isDragabillyOn = false;
    $ionicSideMenuDelegate.canDragContent(false);

    var apiurl;
    var ld = NVR.getLogin();
    $scope.sliderVal.realRate = $scope.sliderVal.rate * 100;

    var TimeObjectFrom = moment($scope.datetimeValueFrom.value).format("YYYY-MM-DD HH:mm");
    var TimeObjectTo = moment().format('YYYY-MM-DD HH:mm');

    // At this point of time, we need to ensure From and To are changed to server time
    //if (NVR.getLogin().useLocalTimeZone)

    var localtz = moment.tz.guess();
    var servertz = NVR.getTimeZoneNow();

    NVR.log("Local timezone conversion is on, converting from " + localtz + " to " + servertz);
    NVR.log("Original From: " + TimeObjectFrom + " Original To: " + TimeObjectTo);

    TimeObjectFrom = moment.tz(TimeObjectFrom, localtz).tz(servertz).format("YYYY-MM-DD HH:mm");
    TimeObjectTo = moment.tz(TimeObjectTo, localtz).tz(servertz).format("YYYY-MM-DD HH:mm");

    NVR.log("Converted From: " + TimeObjectFrom + " Converted To: " + TimeObjectTo);



    areStreamsStopped = true; // kill current view

    $timeout(function () {

      var i;
      if ($rootScope.platformOS != 'ios') {
        NVR.debug("Killing existing streams, if alive...");
        for (i = 0; i < $scope.MontageMonitors.length; i++) {
          if ($scope.MontageMonitors[i].Monitor.listDisplay == 'show' && $scope.MontageMonitors[i].Monitor.eventUrl != 'img/noimage.png') NVR.killLiveStream($scope.MontageMonitors[i].Monitor.connKey, $scope.MontageMonitors[i].Monitor.controlURL, $scope.MontageMonitors[i].Monitor.Name);
        }
      } else {
        NVR.stopNetwork("montage-history footerCollapse");
      }


      //NVR.regenConnKeys();
      //$scope.monitors = NVR.getMonitorsNow();
      //$scope.MontageMonitors = angular.copy($scope.monitors);

      NVR.debug(">>Initializing monitor array with history specific stuff...");
      for (i = 0; i < $scope.MontageMonitors.length; i++) {
        //$scope.MontageMonitors[i].Monitor.connKey='';
        //$scope.MontageMonitors[i].Monitor.connKey = (Math.floor((Math.random() * 99999) + 1)).toString();
        $scope.MontageMonitors[i].Monitor.eventUrl = 'img/noimage.png';
        $scope.MontageMonitors[i].Monitor.eventType = "";
        $scope.MontageMonitors[i].Monitor.eid = "-1";
        $scope.MontageMonitors[i].Monitor.eventUrlTime = "";
        $scope.MontageMonitors[i].Monitor.isPaused = false;
        $scope.MontageMonitors[i].Monitor.gridScale = "50";
        $scope.MontageMonitors[i].Monitor.selectStyle = "";
        $scope.MontageMonitors[i].Monitor.alarmState = 'color:rgba(0,0,0,0);';
        $scope.MontageMonitors[i].Monitor.sliderProgress = {
          progress: 0
        };
      }

      // let stopNetwork finish
      $timeout(function () {
        getNextSetHistory();
      });


    });


    function getNextSetHistory() {

      // grab events that start on or after the time 
      apiurl = ld.apiurl + "/events/index/StartTime >=:" + TimeObjectFrom + "/AlarmFrames >=:" + (ld.enableAlarmCount ? ld.minAlarmCount : 0) + ".json?sort=StartTime&direction=asc";
      NVR.log("Grabbing history using: " + apiurl);
      // make sure there are no more than 5 active streams (noevent is ok)
      $scope.currentLimit = $scope.monLimit;
      //qHttp.get(apiurl)
      //console.log ("GETTING "+apiurl);
      $http({
        method: 'get',
        url: apiurl
      }).then(function (succ) {
        var data = succ.data;
        var ld = NVR.getLogin();
        NVR.debug("Got " + data.events.length + "new history events...");
        console.log (JSON.stringify(data));
        var eid, mid, stime;
        for (i = 0; i < data.events.length; i++) {
          mid = data.events[i].Event.MonitorId;
          eid = data.events[i].Event.Id;
          
          var eType = (data.events[i].Event.DefaultVideo != '')? 'video':'jpeg';
          //console.log ("====SETTING video type to "+eType+" for "+mid);

          //console.log ("Event ID:"+eid);
          stime = data.events[i].Event.StartTime;
          // only take the first one for each monitor
          for (var j = 0; j < $scope.MontageMonitors.length; j++) {
            $scope.MontageMonitors[j].Monitor.isPaused = false;
            // that's the earliest match and play gapless from there
            if ($scope.MontageMonitors[j].Monitor.Id == mid) {
              if ($scope.MontageMonitors[j].Monitor.eventUrl == 'img/noimage.png') {
                // console.log ("Old value of event url " + $scope.MontageMonitors[j].eventUrl);
                //console.log ("ldurl is " + ld.streamingurl);
                var bw = NVR.getBandwidth() == "lowbw" ? zm.eventMontageQualityLowBW : ld.montageHistoryQuality;

                if (eType=='video') {
                  var videoURL= $scope.MontageMonitors[j].Monitor.baseURL  + "/index.php?view=view_video&eid=" + eid;

                  if ($rootScope.authSession != 'undefined') videoURL += $rootScope.authSession;
                  if ($rootScope.basicAuthToken) videoURL = videoURL + "&basicauth=" + $rootScope.basicAuthToken;

          
                  $scope.MontageMonitors[j].Monitor.videoObject = {
                    config: {
                      autoPlay: true,
                      responsive: false,
                      nativeControls: false,
                      nativeFullScreen: true,
        
                      playsInline: true,
                      sources: [{
                          src: $sce.trustAsResourceUrl(videoURL),
                          type: "video/mp4"
                        }
        
                      ],
        
                      theme: "lib/videogular-themes-default/videogular.css",
                      cuepoints: {
                        theme: {
                          url: "lib/videogular-cuepoints/cuepoints.css"
                        },
                        points: [],
                      }
                    }
                  };
                }
                
                $scope.MontageMonitors[j].Monitor.eventType = eType;
                $scope.MontageMonitors[j].Monitor.eventUrl = $scope.MontageMonitors[j].Monitor.streamingURL + "/nph-zms?source=event&mode=jpeg&event=" + eid + "&replay=gapless&rate=" + $scope.sliderVal.realRate + "&connkey=" + $scope.MontageMonitors[j].Monitor.connKey + "&scale=" + bw + $rootScope.authSession;
                //console.log ("Setting event URL to " +$scope.MontageMonitors[j].Monitor.eventUrl);
                //   console.log ("SWITCHING TO " + $scope.MontageMonitors[j].eventUrl);
                $scope.MontageMonitors[j].Monitor.eventUrlTime = stime;
                $scope.MontageMonitors[j].Monitor.eid = eid;
                $scope.MontageMonitors[j].Monitor.eventDuration = data.events[i].Event.Length;
                $scope.MontageMonitors[j].Monitor.sliderProgress = {
                  progress: 0
                };
                //console.log(">>> Setting Event for " + $scope.MontageMonitors[j].Monitor.Name + " to " + eid);
                // now lets get the API for that event for graphing
                $scope.MontageMonitors[j].Monitor.noGraph = true;

              }
            }
          } // for

        }
        // make sure we do our best to get that duration for all monitors
        // in the above call, is possible some did not make the cut in the first page
        NVR.log("Making sure all monitors have a fair chance...");
        var promises = [];
        for (i = 0; i < $scope.MontageMonitors.length; i++) {
          //console.log("Fair chance check for " + $scope.MontageMonitors[i].Monitor.Name);
          if ($scope.MontageMonitors[i].Monitor.eventUrl == 'img/noimage.png') {
            var indivGrab = ld.apiurl + "/events/index/MonitorId:" + $scope.MontageMonitors[i].Monitor.Id + "/StartTime >=:" + TimeObjectFrom + "/AlarmFrames >=:" + (ld.enableAlarmCount ? ld.minAlarmCount : 0) + ".json";
            NVR.debug("Monitor " + $scope.MontageMonitors[i].Monitor.Id + ":" + $scope.MontageMonitors[i].Monitor.Name + " does not have events, trying " + indivGrab);
            var p = getExpandedEvents(i, indivGrab);
            promises.push(p);

          }

        }
        $q.all(promises).then(function () {
            $scope.isScreenReady = true;
            $timeout(function () {
              doPackery();
            });

          }

        );

        // At this stage, we have both a general events grab, and specific event grabs for MIDS that were empty

        function doPackery() {
          // $ionicLoading.hide();
          //console.log("REDOING PACKERY & DRAG");
          NVR.debug("Re-creating packery and draggy");

          // remove current draggies
          if (draggies)
            draggies.forEach(function (drag) {
              drag.destroy();
            });
          draggies = [];
          // destroy existing packery object
          if (pckry) pckry.destroy();
          initPackery();

          $interval.cancel($rootScope.eventQueryInterval);
          $rootScope.eventQueryInterval = $interval(function () {
            checkAllEvents();
          }.bind(this), zm.eventHistoryTimer);

        }
      }, function (err) {
        NVR.debug("history  ERROR:" + JSON.stringify(err));
      });

      function getExpandedEvents(i, indivGrab) {
        var d = $q.defer();
        var ld = NVR.getLogin();
        // console.log ("Expanded API: " + indivGrab);
        $http({
          method: 'get',
          url: indivGrab
        }).then(function (succ) {
            var data = succ.data;
             //console.log ("EXPANDED DATA FOR MONITOR " + i + JSON.stringify(data));
            if (data.events.length > 0) {
              if (!NVR.isBackground()) {
                var bw = NVR.getBandwidth() == "lowbw" ? zm.eventMontageQualityLowBW : ld.montageHistoryQuality;
                var eType = data.events[0].Event.DefaultVideo != ''? 'video':'jpeg';

                var eid =  data.events[0].Event.Id;

                if (eType=='video') {
                  var videoURL= $scope.MontageMonitors[i].Monitor.baseURL  + "/index.php?view=view_video&eid=" + eid;

                  if ($rootScope.authSession != 'undefined') videoURL += $rootScope.authSession;
                  if ($rootScope.basicAuthToken) videoURL = videoURL + "&basicauth=" + $rootScope.basicAuthToken;

          
                  $scope.MontageMonitors[i].Monitor.videoObject = {
                    config: {
                      autoPlay: true,
                      responsive: false,
                      nativeControls: false,
                      nativeFullScreen: true,
        
                      playsInline: true,
                      sources: [{
                          src: $sce.trustAsResourceUrl(videoURL),
                          type: "video/mp4"
                        }
        
                      ],
        
                      theme: "lib/videogular-themes-default/videogular.css",
                      cuepoints: {
                        theme: {
                          url: "lib/videogular-cuepoints/cuepoints.css"
                        },
                        points: [],
                      }
                    }
                  };
                }
                $scope.MontageMonitors[i].Monitor.eventType = eType;
                $scope.MontageMonitors[i].Monitor.eventUrl = $scope.MontageMonitors[i].Monitor.streamingURL + "/nph-zms?source=event&mode=jpeg&event=" + data.events[0].Event.Id + "&frame=1&replay=gapless&rate=" + $scope.sliderVal.realRate + "&connkey=" + $scope.MontageMonitors[i].Monitor.connKey + "&scale=" + bw + $rootScope.authSession;
                //console.log ("SWITCHING TO " + $scope.MontageMonitors[i].eventUrl);
                $scope.MontageMonitors[i].Monitor.eventUrlTime = data.events[0].Event.StartTime;
                $scope.MontageMonitors[i].Monitor.eid = data.events[0].Event.Id;
                $scope.MontageMonitors[i].Monitor.noGraph = true;
                $scope.MontageMonitors[i].Monitor.sliderProgress = {
                  progress: 0
                };
                $scope.MontageMonitors[i].Monitor.eventDuration = data.events[0].Event.Length;
                //console.log(">>> Setting Event for " + $scope.MontageMonitors[i].Monitor.Name + " to " + data.events[0].Event.Id);
                NVR.log("Found expanded event " + data.events[0].Event.Id + " for monitor " + $scope.MontageMonitors[i].Monitor.Id);
              } else {
                // $scope.MontageMonitors[i].eventUrl="img/noimage.png";
                //    $scope.MontageMonitors[i].eventUrlTime = "";
                //    NVR.log ("Setting img src to null as data received in background");
              }
            }
            d.resolve(true);

            return d.promise;

          },
          function (err) {
            d.resolve(true);

            return d.promise;

          }

        );
        return d.promise;
      }

    } // getNextHistory


  }
  //---------------------------------------------------------
  // This is periodically called to get the current playing 
  // event by zms. I use this to display a timestamp
  // Its a 2 step process - get event Id then go a Event
  // API call to get time stamp. Sucks
  //---------------------------------------------------------
  function checkAllEvents() {
    //console.log("Timer:Events are checked....");

    //if (pckry && !$scope.isDragabillyOn) pckry.shiftLayout();

    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      // don't check for monitors that are not shown
      // because nph connkey won't exist and the response
      // will fail
      if ($scope.MontageMonitors[i].Monitor.eventUrl != "" && $scope.MontageMonitors[i].Monitor.eventUrl != 'img/noimage.png' && $scope.MontageMonitors[i].Monitor.connKey != '' && $scope.MontageMonitors[i].Monitor.Function != 'None' && $scope.MontageMonitors[i].Monitor.listDisplay != 'noshow' && $scope.MontageMonitors[i].Monitor.Enabled != '0' &&
      $scope.MontageMonitors[i].Monitor.eventType == "jpeg") {
        // NVR.debug("Checking event status for " + $scope.MontageMonitors[i].Monitor.Name + ":" + $scope.MontageMonitors[i].Monitor.eventUrl + ":" + $scope.MontageMonitors[i].Monitor.Function + ":" + $scope.MontageMonitors[i].Monitor.listDisplay);
        // console.log ("Sending query 99 for " + $scope.MontageMonitors[i].Monitor.Name + " with ck="+$scope.MontageMonitors[i].Monitor.connKey);
       controlEventStream('99', '', $scope.MontageMonitors[i].Monitor.connKey, i);
      }
    }
  }
  //--------------------------------------------------------------
  //  Used to control zms for a connkey. If ndx is not -1,
  // then it also calls an event API for the returned eid
  // and stores its time in the montage monitors array
  //--------------------------------------------------------------
  $scope.controlEventStream = function (cmd, disp, connkey, ndx) {
    controlEventStream(cmd, disp, connkey, ndx);
  };

  function timedControlEventStream(mTime, cmd, disp, connkey, ndx) {
    var mMtime = mTime || 2000;
    NVR.debug("Deferring control " + cmd + " by " + mMtime);
    $timeout(function () {
      subControlStream(cmd, connkey);
    }, mMtime);
  }

  function subControlStream(cmd, connkey) {
    var loginData = NVR.getLogin();
    var myauthtoken = $rootScope.authSession.replace("&auth=", "");
    //&auth=
    var req = qHttp({
      method: 'POST',
      /*timeout: 15000,*/
      url: loginData.url + '/index.php',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', //'Accept': '*/*',
      },
      transformRequest: function (obj) {
        var str = [];
        for (var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        var foo = str.join("&");
        //console.log("****SUB RETURNING " + foo);
        return foo;
      },
      data: {
        view: "request",
        request: "stream",
        connkey: connkey,
        command: cmd,
        auth: myauthtoken, // user: loginData.username,
        // pass: loginData.password
      }
    });
    req.then(function (succ) {
      NVR.debug("subControl success:" + JSON.stringify(succ));
    }, function (err) {
      NVR.debug("subControl error:" + JSON.stringify(err));
    });
  }

  function controlEventStream(cmd, disp, connkey, ndx, extras) {
    // console.log("Command value " + cmd);

    var d = $q.defer();
    if (disp) {
      $ionicLoading.hide();
      $ionicLoading.show({
        template: $translate.instant('kPleaseWait') + "...",
        noBackdrop: true,
        duration: zm.loadingTimeout,
      });
    }
    var loginData = NVR.getLogin();
    /*
    var CMD_NONE = 0;
    var CMD_PAUSE = 1;
    var CMD_PLAY = 2;
    var CMD_STOP = 3;
    var CMD_FASTFWD = 4;
    var CMD_SLOWFWD = 5;
    var CMD_SLOWREV = 6;
    var CMD_FASTREV = 7;
    var CMD_ZOOMIN = 8;
    var CMD_ZOOMOUT = 9;
    var CMD_PAN = 10;
    var CMD_SCALE = 11;
    var CMD_PREV = 12;
    var CMD_NEXT = 13;
    var CMD_SEEK = 14;
    var CMD_QUERY = 99;
    */
    // You need to POST commands to control zms
    // Note that I am url encoding the parameters into the URL
    // If I leave it as JSON, it gets converted to OPTONS due
    // to CORS behaviour and ZM/Apache don't seem to handle it
    //console.log("POST: " + loginData.url + '/index.php');
    //console.log ("AUTH IS " + $rootScope.authSession);
    var myauthtoken = $rootScope.authSession.replace("&auth=", "");
    //&auth=

    var cmdUrl = loginData.url + '/index.php?view=request&request=stream'+'&connkey='+connkey+'&command='+cmd+$rootScope.authSession;
    if (extras)
      cmdUrl = cmdUrl+extras;

    NVR.debug ("Control: Sending "+cmdUrl);

    var req = $http({
      method: 'GET',
      url:cmdUrl
    });
    
    /* var req = qHttp({
      method: 'POST',
      url: loginData.url + '/index.php?view=console',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      transformRequest: function (obj) {
        var str = [];
        for (var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        var foo = str.join("&");
        if (extras) foo = foo + extras;
        return foo;
      },
      data: {
        view: "request",
        request: "stream",
        connkey: connkey,
        command: cmd,
        auth: myauthtoken, // user: loginData.username,
        // pass: loginData.password
      }
    });*/
    req.then(function (succ) {
      var resp = succ.data;

      console.log ("zms response: " + JSON.stringify(resp));

      // move progress bar if event id is the same
      if (resp.result == "Ok" && ndx != -1 && (resp.status && resp.status.event == $scope.MontageMonitors[ndx].Monitor.eid)) {
        if (!$scope.MontageMonitors[ndx].Monitor.seek) {
          $scope.MontageMonitors[ndx].Monitor.sliderProgress.progress = resp.status.progress;
        } else {
          NVR.debug("Skipping progress as seek is active for " + $scope.MontageMonitors[ndx].Monitor.Name);
        }
      }

      if (resp.result == "Ok" && resp.status &&  ndx != -1 && ((resp.status.event != $scope.MontageMonitors[ndx].Monitor.eid) || $scope.MontageMonitors[ndx].Monitor.noGraph == true)) {
        $scope.MontageMonitors[ndx].Monitor.noGraph = false;
        // $scope.MontageMonitors[ndx].Monitor.sliderProgress.progress = 0;
        NVR.debug("Fetching details, as event changed for " + $scope.MontageMonitors[ndx].Monitor.Name + " from " + $scope.MontageMonitors[ndx].Monitor.eid + " to " + resp.status.event);
        var ld = NVR.getLogin();
        var apiurl = ld.apiurl + "/events/" + resp.status.event + ".json";
        //console.log ("API " + apiurl);
        qHttp({
          method: 'get',
          url: apiurl
        }).then(function (succ) {
          var data = succ.data;
          var currentEventTime = moment(data.event.Event.StartTime);
          var maxTime = moment();
          //NVR.debug ("Monitor: " + $scope.MontageMonitors[ndx].Monitor.Id + " max time="+maxTime + "("+$scope.datetimeValueTo.value+")"+ " current="+currentEventTime + "("+data.event.Event.StartTime+")");

          NVR.debug("creating graph for " + $scope.MontageMonitors[ndx].Monitor.Name);
          var framearray = {
            labels: [],
            datasets: [{
              backgroundColor: 'rgba(242, 12, 12, 0.5)',
              borderColor: 'rgba(242, 12, 12, 0.5)',
              data: [],
            }]
          };
          framearray.labels = [];
          var ld = NVR.getLogin();
          //console.log(">>>>> GRAPH");
          for (i = 0; i < data.event.Frame.length; i++) {
            var ts = moment(data.event.Frame[i].TimeStamp).format(timeFormat);
            //console.log ("pushing s:" + event.Frame[i].Score+" t:"+ts);
            framearray.datasets[0].data.push({
              x: ts,
              y: data.event.Frame[i].Score
            });
            framearray.labels.push("");
          }
          $timeout(function () {
            drawGraph(framearray, $scope.MontageMonitors[ndx].Monitor.Id);
          }, 100);
          var element = angular.element(document.getElementById($scope.MontageMonitors[ndx].Monitor.Id + "-timeline"));
          element.removeClass('animated flipInX');
          element.addClass('animated flipOutX');
          $timeout(function () {
            element.removeClass('animated flipOutX');
            element.addClass('animated flipInX');
            $scope.MontageMonitors[ndx].Monitor.eventUrlTime = data.event.Event.StartTime;
            var bw = NVR.getBandwidth() == "lowbw" ? zm.eventMontageQualityLowBW : ld.montageHistoryQuality;

            // you don't have to change url - its taken care of in cmd?

            // $scope.MontageMonitors[ndx].Monitor.eventUrl = $scope.MontageMonitors[ndx].Monitor.streamingURL + "/nph-zms?source=event&mode=jpeg&event=" + data.event.Event.Id + "&frame=1&replay=gapless&rate=" + $scope.sliderVal.realRate + "&connkey=" + $scope.MontageMonitors[ndx].Monitor.connKey + "&scale=" + bw + $rootScope.authSession;
            $scope.MontageMonitors[ndx].Monitor.eid = data.event.Event.Id;
            $scope.MontageMonitors[ndx].Monitor.sliderProgress = {
              progress: 0
            };
            $scope.MontageMonitors[ndx].Monitor.eventDuration = data.event.Event.Length;
            //console.log(">>> Setting Event for " + $scope.MontageMonitors[ndx].Monitor.Name + " to " + data.event.Event.Id);
          }, 700);

        }, function (err) {
          NVR.debug("skipping graph as detailed API failed for " + $scope.MontageMonitors[ndx].Monitor.Name);
          $scope.MontageMonitors[ndx].Monitor.eventUrlTime = "-";
        });
      }
      d.resolve(true);
      return d.promise;
    }, function (err) {
      d.reject(false);
      NVR.log("Error sending event command " + JSON.stringify(err), "error");
      return d.promise;
    });
    return d.promise;
  }
  $scope.isBackground = function () {
    return NVR.isBackground();
  };
  //----------------------------------------------------------------
  // Alarm notification handling
  //----------------------------------------------------------------
  $scope.handleAlarms = function () {
    $rootScope.isAlarm = !$rootScope.isAlarm;
    if (!$rootScope.isAlarm) {
      $rootScope.alarmCount = "0";
      $ionicHistory.nextViewOptions({
        disableBack: true
      });
      $state.go("app.events", {
        "id": 0,
        "playEvent": false
      }, {
        reload: true
      });
      return;
    }
  };
  $scope.handleAlarmsWhileMinimized = function () {
    $rootScope.isAlarm = !$rootScope.isAlarm;
    $scope.minimal = !$scope.minimal;
    NVR.debug("MontageHistoryCtrl: switch minimal is " + $scope.minimal);
    ionic.Platform.fullScreen($scope.minimal, !$scope.minimal);
    $interval.cancel(intervalHandle);
    $interval.cancel($rootScope.eventQueryInterval);
    if (!$rootScope.isAlarm) {
      $rootScope.alarmCount = "0";
      $ionicHistory.nextViewOptions({
        disableBack: true
      });
      $state.go("app.events", {
        "id": 0,
        "playEvent": false,
      }, {
        reload: true
      });
      return;
    }
  };
  //-------------------------------------------------------------
  // this is checked to make sure we are not pulling images
  // when app is in background. This is a problem with Android,
  // for example
  //-------------------------------------------------------------
  $scope.isBackground = function () {
    //console.log ("Is background called from Montage and returned " +    
    //NVR.isBackground());
    return NVR.isBackground();
  };
  $scope.toggleControls = function () {
    $scope.showControls = !$scope.showControls;
  };
  $scope.toggleSelectItem = function (ndx) {
    if ($scope.MontageMonitors[ndx].Monitor.selectStyle !== "undefined" && $scope.MontageMonitors[ndx].Monitor.selectStyle == "dragborder-selected") {
      $scope.MontageMonitors[ndx].Monitor.selectStyle = "";
    } else {
      $scope.MontageMonitors[ndx].Monitor.selectStyle = "dragborder-selected";
    }
    //console.log ("Switched value to " + $scope.MontageMonitors[ndx].Monitor.selectStyle);
  };
  //---------------------------------------------------------------------
  // Called when you enable/disable dragging
  //---------------------------------------------------------------------
  $scope.dragToggle = function () {
    dragToggle();
  };

  function dragToggle() {
    var i;
    $scope.isDragabillyOn = !$scope.isDragabillyOn;
    $ionicSideMenuDelegate.canDragContent($scope.isDragabillyOn ? false : true);
    //$timeout(function(){pckry.reloadItems();},10);
    NVR.debug("setting dragabilly to " + $scope.isDragabillyOn);
    if ($scope.isDragabillyOn) {
      $scope.showSizeButtons = true;
      $scope.dragBorder = "dragborder";
      NVR.debug("Enabling drag for " + draggies.length + " items");
      for (i = 0; i < draggies.length; i++) {
        draggies[i].enable();
        draggies[i].bindHandles();
      }
      // reflow and reload as some may be hidden
      //  $timeout(function(){pckry.reloadItems();$timeout(function(){pckry.layout();},300);},100);
    } else {
      $scope.dragBorder = "";
      NVR.debug("Disabling drag for " + draggies.length + " items");
      for (i = 0; i < draggies.length; i++) {
        draggies[i].disable();
        draggies[i].unbindHandles();
      }
      for (i = 0; i < $scope.MontageMonitors.length; i++) {
        $scope.MontageMonitors[i].Monitor.selectStyle = "";
      }
      // reflow and reload as some may be hidden
      $timeout(function () {
        $timeout(function () {
          pckry.shiftLayout();
          /*var positions = pckry.getShiftPositions('data-item-id');
          //console.log ("POSITIONS MAP " + JSON.stringify(positions));
          var ld = NVR.getLogin();
          ld.packeryPositions = JSON.stringify(positions);
          NVR.setLogin(ld);*/
        }, 300);
      }, 100);
    }
  }


  //---------------------------------------------------------------------
  // Show/Hide PTZ control in monitor view
  //---------------------------------------------------------------------
  $scope.togglePTZ = function () {
    $scope.showPTZ = !$scope.showPTZ;
  };
  $scope.callback = function () {
    // console.log("dragging");
  };
  $scope.onDropComplete = function (index, obj, event) {
    //console.log("dragged");
    var otherObj = $scope.MontageMonitors[index];
    var otherIndex = $scope.MontageMonitors.indexOf(obj);
    $scope.MontageMonitors[index] = obj;
    $scope.MontageMonitors[otherIndex] = otherObj;
  };
  //---------------------------------------------------------------------
  // changes order of montage display
  //---------------------------------------------------------------------
  $scope.toggleMontageDisplayOrder = function () {
    $scope.packMontage = !$scope.packMontage;
    loginData.packMontage = $scope.packMontage;
    NVR.setLogin(loginData);
    //console.log ("Switching orientation");
  };


  $scope.skipVideo= function (m,d) {

    $scope.playbackFinished(m,d);
  };

  $scope.playbackFinished = function(m,d) {

    console.log ("******* VIDEO PLAYBACK FINISHED FOR MONITOR:"+m.Monitor.Id+ " EVENT:" +m.Monitor.eid);
    getNextEvent( m.Monitor.eid,d)
    .then (function (success) {
      NVR.debug ("next event for monitor:"+m.Monitor.Id+" is "+success.eid);

      if (success.eid != "null" && success.eid != m.Monitor.eid && success.eid !="-1") {
       
        var videoURL= m.Monitor.baseURL  + "/index.php?view=view_video&eid=" + success.eid;

                  if ($rootScope.authSession != 'undefined') videoURL += $rootScope.authSession;
                  if ($rootScope.basicAuthToken) videoURL = videoURL + "&basicauth=" + $rootScope.basicAuthToken;

                  m.Monitor.videoObject = {
                    config: {
                      autoPlay: true,
                      responsive: false,
                      nativeControls: false,
                      nativeFullScreen: true,
        
                      playsInline: true,
                      sources: [{
                          src: $sce.trustAsResourceUrl(videoURL),
                          type: "video/mp4"
                        }
        
                      ],
        
                      theme: "lib/videogular-themes-default/videogular.css",
                      cuepoints: {
                        theme: {
                          url: "lib/videogular-cuepoints/cuepoints.css"
                        },
                        points: [],
                      }
                    }
                  };
            

                  

      //  m.Monitor.videoObject.config.sources[0].src = $sce.trustAsResourceUrl(videoURL);

      var element = angular.element(document.getElementById(m.Monitor.Id + "-timeline"));
      element.removeClass('animated flipInX');
      element.addClass('animated flipOutX');

      $timeout (function () {

        element.removeClass('animated flipOutX');
        element.addClass('animated flipInX');

        NVR.debug ("--->updating videoURL for mid="+m.Monitor.Id+ "to:"+videoURL);
        m.Monitor.eid = success.eid;
        m.Monitor.eventUrlTime = success.stime;
        $timeout (function () {
          m.Monitor.handle.play();
        });

      },700);

        
        
      
      // m.Monitor.handle.play();
      
       
        }

    });
  };


  $scope.onVideoError = function (event) {
    $ionicLoading.hide();
    NVR.debug("player reported a video error:" + JSON.stringify(event));

  };

  $scope.onPlayerReady = function (api,m) {

    // we need this timeout to avoid load interrupting
    // play -- I suppose its an angular digest foo thing
    //console.log ("*********** ON PLAY READY")
    m.Monitor.handle = api;
    
    NVR.debug("On Play Ready invoked");
  
    m.Monitor.handle.mediaElement.attr("playsinline", "");

    $ionicLoading.show({
      template: "<ion-spinner icon='ripple' class='spinner-energized'></ion-spinner><br/>" + $translate.instant('kVideoLoading') + "...",

    });
    NVR.debug("Player is ready");
    $timeout(function () {
      m.Monitor.handle.pause();

      $timeout(function() {
        m.Monitor.handle.setPlayback(NVR.getLogin().videoPlaybackSpeed);
      m.Monitor.handle.play();
      NVR.debug("*** Invoking play");
      playerReady = true;
      },300);
      

    }, 300);

    // window.stop();
  };

  function getNextEvent(eid,dirn) {

    var d = $q.defer();
    // now get event details to show alarm frames
    var loginData = NVR.getLogin();
    var myurl = loginData.apiurl + '/events/' + eid + ".json";
    //console.log (">> 1: getting: "+myurl);

    var r = {
      eid:"",
      stime:""
    };

    $http.get(myurl)
    .then( function (succ) {
      //console.log (JSON.stringify(succ));
      var target = (dirn == -1) ? succ.data.event.Event.PrevOfMonitor: succ.data.event.Event.NextOfMonitor;
      //console.log (">> 2: dirn: "+dirn+" target: "+target);
      if (!target) target = 'null'; // fallback incase in some API this doesn't exist;
      if (target == 'null') {
        r.eid = "-1";
        r.stime = "-1";
        d.resolve(r);
        return d.promise;
      }
      else {
        r.eid = target;
        // now get time of that event
        myurl = loginData.apiurl+'/events/'+target + '.json';
        $http.get (myurl)
        .then (function (succ) {
            r.stime = succ.data.event.Event.StartTime;
            d.resolve(r);
            return d.promise;
        },function (err) {
          NVR.debug ("Error getting start time of neighbor:"+JSON.stringify(err));
           r.stime = "-1";
           d.resolve(r);
           return d.promise;
        });
        return d.promise;
      }
    }, function (err) {
        NVR.debug ("Error getting neighbors:"+JSON.stringify(err));
        r.eid = "-1";
        r.stime = "-1";
        d.resolve(r);
        return d.promise;

    });
    return (d.promise);

  }
  //---------------------------------------------------------------------
  // In Android, the app runs full steam while in background mode
  // while in iOS it gets suspended unless you ask for specific resources
  // So while this view, we DON'T want Android to keep sending 1 second
  // refreshes to the server for images we are not seeing
  //---------------------------------------------------------------------
  function onPause() {
    NVR.debug("MontageHistoryCtrl: onpause called");
    viewCleanup();
    viewCleaned = true;
  }

  function viewCleanup() {

    if (viewCleaned) {
      NVR.debug("Montage History View Cleanup was already done, skipping");
      return;
    }


    $interval.cancel($rootScope.eventQueryInterval);
    if (pckry) pckry.destroy();


    broadcastHandles = [];

    areStreamsStopped = true;

    $timeout(function () {

      NVR.debug("Killing all streams in montage to save memory/nw...");
      for (var i = 0; i < $scope.MontageMonitors.length; i++) {
        if ($scope.MontageMonitors[i].Monitor.listDisplay == 'show' && $scope.MontageMonitors[i].Monitor.eventUrl != 'img/noimage.png') NVR.killLiveStream($scope.MontageMonitors[i].Monitor.connKey, $scope.MontageMonitors[i].Monitor.controlURL, $scope.MontageMonitors[i].Monitor.Name);

      }

    });

  }

  function onResume() {
    areStreamsStopped = false;
  }

  $scope.openMenu = function () {
    $timeout(function () {
      $rootScope.stateofSlide = $ionicSideMenuDelegate.isOpen();
    }, 500);
    $ionicSideMenuDelegate.toggleLeft();
  };
  $scope.$on('$destroy', function () {
    NVR.debug("Cancelling eventQueryInterval");
    $interval.cancel($rootScope.eventQueryInterval);
  });
  $scope.$on('$ionicView.loaded', function () {
    //console.log("**VIEW ** MontageHistoryCtrl Loaded");
  });
  $scope.$on('$ionicView.enter', function () {
    NVR.debug("**VIEW ** MontageHistory Ctrl Entered");
    var ld = NVR.getLogin();
    //console.log("Setting Awake to " + NVR.getKeepAwake());
    NVR.setAwake(NVR.getKeepAwake());
    NVR.debug("query timer started");
    $interval.cancel($rootScope.eventQueryInterval);
    //console.log ("****************** TIMER STARTED INSIDE ENTER");
    $rootScope.eventQueryInterval = $interval(function () {
      checkAllEvents();
    }.bind(this), zm.eventHistoryTimer);
  });

  $scope.$on('$ionicView.beforeLeave', function () {
    //console.log("**VIEW ** Event History Ctrl Left, force removing modal");
    areStreamsStopped = true;
    viewCleanup();
    viewCleaned = true;


    // if ($scope.modal) $scope.modal.remove();
    NVR.log("Cancelling event query timer");
    $interval.cancel($rootScope.eventQueryInterval);
    // NVR.log("MontageHistory:Stopping network pull...");
    // make sure this is applied in scope digest to stop network pull
    // thats why we are doing it beforeLeave
    pckry.destroy();
    
  });
  $scope.$on('$ionicView.unloaded', function () {});
  $scope.sliderChanged = function (dirn) {
    //console.log("SLIDER CHANGED");
    if ($scope.sliderChanging) {
      // console.log ("too fast my friend");
      //$scope.slider.monsize = oldSliderVal;
      // return;
    }
    $scope.sliderChanging = true;
    var somethingReset = false;
    // this only changes items that are selected
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      var curVal = parseInt($scope.MontageMonitors[i].Monitor.gridScale);
      curVal = curVal + (10 * dirn);
      if (curVal < 10) curVal = 10;
      if (curVal > 100) curVal = 100;
      //console.log ("For Index: " + i + " From: " + $scope.MontageMonitors[i].Monitor.gridScale + " To: " + curVal);
      if ($scope.isDragabillyOn) {
        // only do this for selected monitors
        if ($scope.MontageMonitors[i].Monitor.selectStyle == "dragborder-selected") {
          $scope.MontageMonitors[i].Monitor.gridScale = curVal;
          somethingReset = true;
        }
      } else {
        $scope.MontageMonitors[i].Monitor.gridScale = curVal;
        //somethingReset = true;
      }
    }
    // this changes all items if none were selected
    if (!somethingReset && $scope.isDragabillyOn) // nothing was selected
    {
      for (i = 0; i < $scope.MontageMonitors.length; i++) {
        var cv = parseInt($scope.MontageMonitors[i].Monitor.gridScale);
        cv = cv + (10 * dirn);
        if (cv < 10) cv = 10;
        if (cv > 100) cv = 100;
        $scope.MontageMonitors[i].Monitor.gridScale = cv;
      }
    }
    //pckry.reloadItems();
    pckry.once('layoutComplete', function () {
      /* $timeout(function () {
           var positions = pckry.EHgetShiftPositions('eh-data-item-id');
           //console.log ("POSITIONS MAP " + JSON.stringify(positions));
           var ld = NVR.getLogin();
           ld.EHpackeryPositions = JSON.stringify(positions);
           NVR.setLogin(ld);
           $ionicLoading.hide();
           $scope.sliderChanging = false;
       }, zm.packeryTimer);*/
    });
    if (!somethingReset) {
      //console.log (">>>SOMETHING NOT RESET");
      $timeout(function () {
        pckry.layout();
      }, zm.packeryTimer);
    } else {
      //console.log (">>>SOMETHING  RESET");
      $timeout(function () {
        layout(pckry);
      }, zm.packeryTimer);
    }
  };

  function layout(pckry) {
    pckry.shiftLayout();
  }
  $scope.resetSizes = function () {
    var somethingReset = false;
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      if ($scope.isDragabillyOn) {
        if ($scope.MontageMonitors[i].Monitor.selectStyle == "dragborder-selected") {
          $scope.MontageMonitors[i].Monitor.gridScale = "50";
          somethingReset = true;
        }
      } else {
        $scope.MontageMonitors[i].Monitor.gridScale = "50";
        // somethingReset = true;
      }
    }
    if (!somethingReset && $scope.isDragabillyOn) // nothing was selected
    {
      for (i = 0; i < $scope.MontageMonitors.length; i++) {
        $scope.MontageMonitors[i].Monitor.gridScale = "50";
      }
    }
    $timeout(function () {
      pckry.reloadItems();
      $timeout(function () {
        pckry.layout();
      }, zm.packeryTimer); // force here - no shiftlayout
    }, 100);
  };

  function isEmpty(obj) {
    for (var prop in obj) {
      return false;
    }
    return true;
  }
  // called by afterEnter to load Packery
  function initPackery() {
    areStreamsStopped = true;
    $ionicLoading.show({
      template: $translate.instant('kArrangingImages'),
      noBackdrop: true,
      duration: zm.loadingTimeout
    });
    var progressCalled = false;
    draggies = [];
    var layouttype = true;
    var ld = NVR.getLogin();

    var elem = angular.element(document.getElementById("mygrid"));
    pckry = new Packery('.grid', {
      itemSelector: '.grid-item',
      percentPosition: true,
      columnWidth: '.grid-sizer',
      gutter: 0,
      initLayout: true

    });
    //console.log ("**** mygrid is " + JSON.stringify(elem));
    imagesLoaded(elem).on('progress', function (instance, img) {
      var result = img.isLoaded ? 'loaded' : 'broken';
      // NVR.debug('~~loaded image is ' + result + ' for ' + img.img.src);
      $timeout(function () {
        pckry.layout();
      }, 100);

      progressCalled = true;
      // if (layouttype) $timeout (function(){layout(pckry);},100);
    });
    imagesLoaded(elem).once('always', function () {
      //console.log("******** ALL IMAGES LOADED");
      //$scope.$digest();
      areStreamsStopped = false;

      NVR.debug("All images loaded");
      $ionicLoading.hide();

      $scope.areImagesLoading = false;

      if (!progressCalled) {
        NVR.log("***  PROGRESS WAS NOT CALLED");
        //pckry.reloadItems();
      }

      $timeout(function () {

        pckry.getItemElements().forEach(function (itemElem) {
          draggie = new Draggabilly(itemElem);
          pckry.bindDraggabillyEvents(draggie);
          draggies.push(draggie);
          draggie.disable();
          draggie.unbindHandles();
        });

        pckry.on('dragItemPositioned', itemDragged);

        /*if (!isEmpty(positions)) {
            NVR.log("Arranging as per packery grid");

            for (var i = 0; i < $scope.MontageMonitors.length; i++) {
                for (var j = 0; j < positions.length; j++) {
                    if ($scope.MontageMonitors[i].Monitor.Id == positions[j].attr) {
                        $scope.MontageMonitors[i].Monitor.gridScale = positions[j].size;
                        $scope.MontageMonitors[i].Monitor.listDisplay = positions[j].display;
                        NVR.debug("Setting monitor ID: " + $scope.MontageMonitors[i].Monitor.Id + " to size: " + positions[j].size + " and display:" + positions[j].display);
                    }
                    //console.log ("Index:"+positions[j].attr+ " with size: " + positions[j].size);
                }
            }


            NVR.debug("All images loaded, doing image layout");
            $timeout(function () {
                pckry.initShiftLayout(positions, 'data-item-id');
            }, 0);
        }*/

        $timeout(function () {
          NVR.log("Force calling resize");
          pckry.layout();
          $scope.packeryDone = true;
        }, zm.packeryTimer); // don't ask

      }, zm.packeryTimer);

    });

    function itemDragged(item) {
      NVR.debug("drag complete");
    }
  }
  $scope.$on('$ionicView.beforeEnter', function () {

    $scope.$on ( "process-push", function () {
      NVR.debug (">> MontageHistoryCtrl: push handler");
      var s = NVR.evaluateTappedNotification();
      NVR.debug("tapped Notification evaluation:"+ JSON.stringify(s));
      $ionicHistory.nextViewOptions({
        disableAnimate:true,
        disableBack: true
      });
      $state.go(s[0],s[1],s[2]);
    });
    
    // This rand is really used to reload the monitor image in img-src so it is not cached
    // I am making sure the image in montage view is always fresh
    // I don't think I am using this anymore FIXME: check and delete if needed
    // $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
    $scope.showControls = true;
    $scope.packeryDone = false;
    readyToRun = false;
    $scope.isScreenReady = false;
    // $scope.MontageMonitors = message;


    isMultiPort = NVR.getCurrentServerMultiPortSupported() && !NVR.getLogin().disableSimulStreaming;


    // don't do this - we are simulstreaming in this view 
    /* if ($rootScope.platformOS == 'ios') {
       isSimulStreaming = false;
       NVR.log("IOS detected, disabling simulstreaming");
     }*/
    $scope.isMultiPort = isMultiPort;
    areStreamsStopped = true;

    NVR.regenConnKeys();
    $scope.monitors = NVR.getMonitorsNow();
    $scope.MontageMonitors = angular.copy($scope.monitors);

    var loginData = NVR.getLogin();
    // init monitors

    NVR.debug(">>Initializing connkeys and images...");
    for (var i = 0; i < $scope.MontageMonitors.length; i++) {
      //$scope.MontageMonitors[i].Monitor.connKey='';
      $scope.MontageMonitors[i].Monitor.eid = "-1";
      $scope.MontageMonitors[i].Monitor.eventUrl = 'img/noimage.png';
      $scope.MontageMonitors[i].Monitor.eventType = "";
      $scope.MontageMonitors[i].Monitor.eid = "-1";
      $scope.MontageMonitors[i].Monitor.eventUrlTime = "";
      $scope.MontageMonitors[i].Monitor.isPaused = false;
      $scope.MontageMonitors[i].Monitor.gridScale = "50";
      $scope.MontageMonitors[i].Monitor.selectStyle = "";
      $scope.MontageMonitors[i].Monitor.alarmState = 'color:rgba(0,0,0,0);';
      $scope.MontageMonitors[i].Monitor.sliderProgress = {
        progress: 0
      };
    }
    doInitCode();

  });
  $scope.reloadView = function () {
    $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
    NVR.log("User action: image reload " + $rootScope.rand);
  };
  $scope.doRefresh = function () {
    //console.log("***Pull to Refresh, recomputing Rand");
    NVR.log("Reloading view for montage view, recomputing rand");
    $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
    $scope.MontageMonitors = [];
    imageLoadingDataShare.set(0);
    var refresh = NVR.getMonitors(1);
    refresh.then(function (data) {
      $scope.MontageMonitors = data.data;
      $scope.$broadcast('scroll.refreshComplete');
    });
  };

  function drawGraph(f, mid) {
    //console.log("Graphing on " + "eventchart-" + mid);
    var cv = document.getElementById("eventchart-" + mid);
    var ctx = cv.getContext("2d");
   // ctx.height=30;
    frameoptions = {
      maintainAspectRatio: false,
      responsive: true,
      legend: false,
      title: {
        display: false,
        text: ""
      },
      scales: {
        yAxes: [{
          display: false,
          scaleLabel: {
            display: false,
            labelString: 'value',
          }
        }],
        xAxes: [{
          type: 'time',
          display: false,
          time: {
            format: timeFormat,
            tooltipFormat: 'll HH:mm',
            min: f.datasets[0].data[0].x,
            max: f.datasets[0].data[f.datasets[0].data.length - 1].x,
            displayFormats: {}
          },
          scaleLabel: {
            display: false,
            labelString: ''
          }
        }]
      }
    };
    $timeout(function () {
      var myChart = new Chart(ctx, {
        type: 'line',
        data: f,
        options: frameoptions,
      });
    });
  }
  //---------------------------------------------------------------------
  // Controller main
  //---------------------------------------------------------------------
  var intervalHandle;
  var modalIntervalHandle;
  var timeFormat;
  var curYear;
  var readyToRun;
  var frameoptions;
  var timeto, timefrom;
  var sizeInProgress;
  var ld;
  var pckry;
  var draggies;
  var i;
  var loginData;
  var draggie;
  var oldmonitors;
  var gridcontainer;
  var montageOrder, hiddenOrder;

  $scope.sliderVal = {
    rate: 2,
    realRate: 200,
    hideNoEvents: false,
    enableGapless: true,
    exactMatch: false,
    showTimeline: true
  };
  $scope.timeFormat = "yyyy-MM-dd " + NVR.getTimeFormat();
  $scope.displayDateTimeSliders = true;
  $scope.showtimers = true;
  $scope.loginData = NVR.getLogin();

  $scope.slider_modal_options_rate = {
    from: 1,
    to: 10,
    realtime: true,
    step: 1,
    className: "mySliderClass", //modelLabels:function(val) {return "";},
    callback: function (value, released) {
      //console.log("CALLBACK"+value+released);
      $ionicScrollDelegate.freezeScroll(!released);
      //NVR.debug("EventCtrl: freezeScroll called with " + !released);

    },
    smooth: false,
    dimension: 'X',
    css: {
      background: {
        "background-color": "silver"
      },
      before: {
        "background-color": "purple"
      },
      default: {
        "background-color": "white"
      }, // default value: 1px
      after: {
        "background-color": "green"
      }, // zone after default value
      pointer: {
        "background-color": "red"
      }, // circle pointer
      range: {
        "background-color": "red"
      } // use it if double value
    },

  };

  $scope.datetimeValueFrom = {
    value: "",
    hrs: ""
  };
  $scope.datetimeValueTo = {
    value: ""
  };

  $rootScope.eventQueryInterval = "";


  $scope.constructStream = function (monitor) {


    var stream;
    if (areStreamsStopped) return "";
    //if (monitor.Monitor.isPaused) return "";
    stream = monitor.Monitor.eventUrl; //eventUrl already has all the foo

    stream += NVR.insertBasicAuthToken();
    // console.log("STREAM=" + stream);
    return stream;

  };

  function appendConnKey(ck) {
    // always streaming
    return "&connkey=" + ck;

  }

  function doInitCode()

  {

    $scope.isModalActive = false;

    $scope.hrsAgo = 4;
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);

    timeFormat = 'MM/DD/YYYY HH:mm:ss';
    curYear = new Date().getFullYear();
    readyToRun = false;

    frameoptions = [];
    // default = start of day
    timeto = moment();
    timefrom = moment().startOf('day');
    $scope.datetimeValueTo.value = timeto.toDate();
    $scope.sliderVal.rate = 1;
    $scope.sliderVal.realRate = $scope.sliderVal.rate * 100;

    $scope.datetimeValueFrom.value = timefrom.toDate();
    $scope.datetimeValueFrom.hrs = Math.round(moment.duration(moment().diff(moment($scope.datetimeValueFrom.value))).asHours());


    $scope.monitorSize = []; // array with montage sizes per monitor
    $scope.scaleDirection = []; // 1 = increase -1 = decrease
    // The difference between old and original is this:
    // old will have a copy of the last re-arranged monitor list
    // while original will have a copy of the order returned by ZM
    var oldMonitors = []; // To keep old order if user cancels after sort;
    // Montage display order may be different so don't
    // mangle monitors as it will affect other screens
    // in Montage screen we will work with this local copy
    //$scope.MontageMonitors = angular.copy ($scope.monitors);
    var montageOrder = []; // This array will keep the ordering in montage view
    var hiddenOrder = []; // 1 = hide, 0 = don't hide
    var tempMonitors = message;
    /* if (tempMonitors.length == 0)
        {
            $rootScope.zmPopup = $ionicPopup.alert(
            {
                title: $translate.instant('kNoMonitors'),
                template: $translate.instant('kPleaseCheckCredentials'),
                okText: $translate.instant('kButtonOk'),
                cancelText: $translate.instant('kButtonCancel'),
            });
            $ionicHistory.nextViewOptions(
            {
                disableBack: true
            });
            $state.go("app.login");
            return;
        }
*/
    NVR.log("Inside MontageHistoryCtrl:We found " + $scope.MontageMonitors.length + " monitors");
    // $scope.MontageMonitors = NVR.applyMontageMonitorPrefs(message, 1)[0];


    // --------------------------------------------------------
    // Handling of back button in case modal is open should
    // close the modal
    // --------------------------------------------------------                               
    $ionicPlatform.registerBackButtonAction(function (e) {
      e.preventDefault();
      if ($scope.modal && $scope.modal.isShown()) {
        // switch off awake, as liveview is finished
        NVR.debug("Modal is open, closing it");
        NVR.setAwake(false);
        $scope.modal.remove();
        $scope.isModalActive = false;
      } else {
        NVR.debug("Modal is closed, so toggling or exiting");
        if (!$ionicSideMenuDelegate.isOpenLeft()) {
          $ionicSideMenuDelegate.toggleLeft();
        } else {
          navigator.app.exitApp();
        }
      }
    }, 1000);
    $scope.isRefresh = $stateParams.isRefresh;
    sizeInProgress = false;
    $ionicSideMenuDelegate.canDragContent(false);
    $scope.LoginData = NVR.getLogin();
    $scope.monLimit = $scope.LoginData.maxMontage;
    $scope.currentLimit = $scope.LoginData.maxMontage;
    ld = NVR.getLogin();

    if (!isMultiPort || ld.disableSimulStreaming) {

      NVR.log("Limiting montage to 5, thanks to max connection  per domain limit");
      $scope.currentLimit = 5;
      $scope.monLimit = 5;
    } else {

      NVR.log("You have multiport on, so no montage limits");
    }



    $timeout(function () {
      // initPackery();
      readyToRun = true;
      NVR.debug("Calling footerCollapse from doInit");
      footerCollapse();
    }, zm.packeryTimer);


  }

}]);