summaryrefslogtreecommitdiff
path: root/index.html
blob: ceeff4dbc31f72dfef66499d5fbdbdd3875de883 (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
<!doctype html>
<html>
<head>
<title>Lodges by Date</title>
<link rel="stylesheet" type="text/css" href="pickadate.css">
<style type="text/css">
html,body { overflow-x: hidden; }
#data, #summer { margin-left: 400px; }
#summer { display: none; }
.bold { font-weight: bold; background: #eee; color: black; }
a { text-decoration: underline; color: blue; cursor: pointer; }
#district { text-transform: capitalize; }
</style>
</head>
<body>

<input type="text" class="datepicker">
<button id="next">Next day</button>
<select id="districts"></select>
<select id="lodges"></select>

<div id="summer">
<img src="palmtree.gif"><br>
<i>no meetings july and august!</i>
</div>

<div id="data">

<p>
Today is <span id="today"></span>, better known as <span id="todayindex"></span>.<br>
</p>

<p>
These lodges meet today: <span id="todaylodges"></span><br>
These lodges meet tomorrow: <span id="tomorrowlodges"></span>
</p>

<p>
<span class="lodgename"></span> meets this month: <span id="estuarymeetings"></span><br>
<span class="lodgename"></span> next meets: <span id="estuarynext"></span>
</p>

<p>
The next full moon is <span id="nextfullmoon"></span>.<br>
</p>

<div id="lodgeinfo">
</div>

<script id="lodgeinfo-template" type="text/html">
<div class="info">
  <p>
  <b id="name"></b> <b>#</b><b id="id"></b><br>
  <span id="dates"></span><br>
  <span id="district"></span>
  </p>
</div>
</script>

</body>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="pickadate.js"></script>
<script type="text/javascript" src="nylodges-grand-lodge.js"></script>
<script type="text/javascript">

var days = "Su Mo Tu We Th Fr Sa".split(" ");
var longdays = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" ");
var months = "January February March April May June July August September October November December".split(" ");
var monthsLookup = lookup(months);
var shortMonths = wrap(map(function(x){ return [ x.slice(0,3), monthsLookup[x] ] }, months));
var daysLookup = lookup(days); 
var Ordinals = "all first second third fourth fifth sixth seventh eighth ninth tenth".split(" ")
var ShortOrdinals = "1st 2nd 3rd 4th".split(" ")

var today = new Date();
var DatesByIndex = {};
var LodgeListByDate = {};

var Lodges = {}
var LodgesByIndex = {}
var LodgesWithMonth = {}
var LodgesByDistrict = {}
var AllLodgesByIndex = LodgesByIndex
LodgeList.forEach(function(l){
  var id = l.title.match(/\d+/)[0]
  l.id = id
  var tpartz = l.title.split("No")
  l.name = tpartz[0]
  l.address = l.address.split(",")
  l.district = l.district.toLowerCase()
  delete l.title
  Lodges[id] = l
  
  var day
  longdays.some(function(d){
    if (l.dates.toLowerCase().match(d.toLowerCase())) {
      day = d.substr(0,2)
      return true
    }
    return false
  })
  
  ShortOrdinals.forEach(function(oo, i){
    if (l.dates.match(oo)) {
      var ok = oo[0] + day
      LodgesByIndex[ok] = LodgesByIndex[ok] || []
      LodgesByIndex[ok].push(id)
      LodgesByDistrict[ l.district ] = LodgesByDistrict[ l.district ] || {}
      LodgesByDistrict[ l.district ][ok] = LodgesByDistrict[ l.district ][ok] || []
      LodgesByDistrict[ l.district ][ok].push(id)
    }
  })
  
})

/*
var LodgesByIndex = {
  '1Mo': [2, 4, 9, 17, 33, 36, 49, 76, 77, 88, 116, 131],
  '1Tu': [8, 12, 21, 38, 40, 55, 57, 63, 67, 108, 113, 148],
  '1We': [5, 7, 11, 18, 25, 30, 34, 43, 64, 66, 103, 125],
  '1Th': [3, 6, 16, 22, 31, 42, 51, 60, 121, 140, 145],
  '1Fr': [10, 89],
  '1Sa': [120, 149],
  '2Mo': [19, 70, 95, 101, 112, 115, 144, 147, 332],
  '2Tu': [14, 28, 29, 69, 73, 78, 85, 87, 102, 104, 110, 146],
  '2We': [15, 39, 44, 79, 97, 107, 119, 142],
  '2Th': [1, 61, 65, 81],
  '2Fr': [122],
  '2Sa': [24],
  '3Mo': [2, 4, 9, 17, 33, 49, 76, 77, 88, 116, 131],
  '3Tu': [8, 12, 21, 38, 40, 51, 57, 63, 67, 143],
  '3We': [5, 7, 11, 13, 18, 25, 30, 34, 43, 64, 66, 103, 125],
  '3Th': [3, 6, 16, 22, 31, 42, 47, 51, 121, 140, 145],
  '3Fr': [10, 89, "PLR"],
  '3Sa': [120, 149],
  '4Mo': [70, 95, 101, 112, 115],
  '4Tu': [14, 28, 29, 73, 78, 85, 102, 104],
  '4We': [39, 44, 97, 119, "MLR"],
  '4Th': [1, 61, 65, 81],
  'MSa': [46], // on or before full moon
  'MTh': [48]
};
var LodgesWithMonth = {
  'PLR': shortMonthLookup("Feb Apr Sep Oct Dec".split(" ")),
  'MLR': shortMonthLookup("Feb Mar May Sep Oct Dec".split(" "))
};
*/
/*
S on or before full moon	46
T on or before full moon 	48
3rd F, Feb/Apr/Sep/Oct/Dec 	PLR
4th W, Feb/Mar/May/Sep/Oct/Dec 	MLR
*/

var daysInMonth = [31,28,31,30, 31,30,31,31, 30,31,30,31];
var daysInMonthSum = [0,31,59,90, 120,151,181,212, 243,273,304,334];
var daysInMonthLeap = [0,31,60,91, 121,152,182,213, 244,274,305,335];

var DatesByLodge = reverseLookup(LodgesByIndex);

Date.prototype.getFirstOfMonth = function(){
  var firstofmonth = new Date();
  firstofmonth.setMonth(this.getMonth());
  firstofmonth.setFullYear(this.getFullYear());
  firstofmonth.setDate(1);
  return firstofmonth;
}
Date.prototype.getWeekOfMonth = function() {
  var firstofmonth = this.getFirstOfMonth();
  var week = this.getWeekOfYear() - firstofmonth.getWeekOfYear();
  if (this.getDay() >= firstofmonth.getDay()) {
    week += 1;
  }
  return week;
}
Date.prototype.getWeekOfYear = function(){
  return Math.floor( (this.getYearDay() - this.getDay() + 10) / 7 );
}
Date.prototype.getYearDay = function(){
  var ord = this.getDate();
  if (this.isLeapYear()) {
    return ord + daysInMonthLeap[ this.getMonth() ];
  } else {
    return ord + daysInMonthSum[ this.getMonth() ];
  }
}
Date.prototype.isLeapYear = function(){
  var year = this.getFullYear();
  if (year % 400 == 0) {
    return true;
  }
  else if (year % 100 == 0) {
    return false;
  }
  else if (year % 4 == 0) {
    return true;
  }
  else {
    return false;
  }
}
Date.prototype.getDaysInMonth = function(){
  var month = this.getMonth();
  if (month == 1 && this.isLeapYear()) {
    return 29;
  } else {
    return daysInMonth[month];
  }
}
Date.prototype.copy = function(){
  return new Date( this.getTime() );
}
Date.prototype.getWeekDayIndex = function(){
  return this.getWeekOfMonth() + days[this.getDay()];
}
Date.prototype.tomorrow = function(){
  var tomorrow = this.copy();
  tomorrow.setDate(this.getDate() + 1);
  return tomorrow;
}
Date.prototype.nextMonth = function(){
  var nextmonth = this.copy();
  nextmonth.setMonth(this.getMonth() + 1);
  var another = nextmonth.copy();
  another.setMonth(this.getMonth() + 2);
  nextmonth.cache();
  another.cache();
  return nextmonth;
}
Date.prototype.display = function(){
  return longdays[this.getDay()] + " " + months[this.getMonth()] + " " + this.getDate() + ", " + this.getFullYear();
}
Date.prototype.dateFromIndex = function(index) {
  return DatesByIndex[this.getFullYear()][this.getMonth()][index];
}
function shortMonth(s) {
  return s.slice(0,3);
}
function shortMonthLookup(a) {
  return lookup(map(function(x){ return shortMonths[x] }, a));
}

function map (f,a) {
  var aa = [];
  for (var i = 0; i < a.length; i++) {
    // console.log(a[i], f(a[i]));
    aa[i] = f(a[i]);
  }
  return aa;
}
function wrap(a) {
  var lookup = {};
  for (var i = 0; i < a.length; i++) {
    lookup[a[i][0]] = a[i][1];
  }
  return lookup;
}

// date.setTime( date.getTime() + date.getTimezoneOffset() * 60000 );
var bluemoon = new Date( 96, 1, 3, 16, 15, 0 );
var lunarperiod = 29 * ( 24 * 3600 * 1000 ) + 12 * ( 3600 * 1000 ) + 44.05 * ( 60 * 1000 );
Date.prototype.moonPhase = function(){
  var phasetime = ( this.getTime() - bluemoon.getTime() ) % lunarperiod;
  return fraction = phasetime / lunarperiod;
}
Date.prototype.nextFullMoon = function(){
  var phasetime = ( this.getTime() - bluemoon.getTime() ) % lunarperiod;
  var fullmoonDaysFromNow = Math.round( ( lunarperiod - phasetime ) / ( 24 * 3600 * 1000 ) )
  var fullmoon = this.copy();

  fullmoonDaysFromNow %= 29;

  fullmoon.setDate( this.getDate() + fullmoonDaysFromNow );
  return fullmoon;
}

Date.prototype.isCached = function() {
  var year = this.getFullYear();
  var month = this.getMonth()
  return (year in DatesByIndex) && (month in DatesByIndex[year]);
}
Date.prototype.cache = function () {
  if (this.isCached()) {
    return;
  }
  var year = this.getFullYear();
  var month = this.getMonth();

  if (!DatesByIndex[year]) DatesByIndex[year] = {};
  if (!DatesByIndex[year][month]) DatesByIndex[year][month] = {};
  if (!LodgeListByDate[year]) LodgeListByDate[year] = {};
  if (!LodgeListByDate[year][month]) LodgeListByDate[year][month] = {};

  var llbd = LodgeListByDate[year][month];

  var fullmoon = this.nextFullMoon();

  // console.log([ this.getDaysInMonth() ]);
  for (var i = 1; i <= this.getDaysInMonth(); i++) {
    var date = this.copy();
    date.setDate(i);
    var week = date.getWeekOfMonth();
    var index = date.getWeekDayIndex();

    DatesByIndex[date.getFullYear()][date.getMonth()][index] = date;
    var lodgelist = date.getLodges();

    for (var j = 0; j < lodgelist.length; j++) {
      var lodge_id = lodgelist[j] + "";
      if (!llbd[lodge_id]) llbd[lodge_id] = [];
      llbd[lodge_id].push(date);
    }

    console.log([
      index,
      date.getWeekOfYear(),
      week, 
      days[date.getDay()], 
      date.getDate(), 
      months[date.getMonth()],
      lodgelist
    ]);
  }
}

function append(car, cdr){
  for (var i in cdr) {
    car.push(cdr[i]);
  }
}
Date.prototype.isSameDay = function(date) {
  return this.getFullYear() == date.getFullYear() && this.getMonth() == date.getMonth() && this.getDate() == date.getDate();
}
Date.prototype.isSummer = function(){
  switch (this.getMonth()) {
    case 6:
    case 7:
      return true;
  }
  return false;
}
function filter(f,a) {
  var l = [];
  if (a) {
    for (var i = 0; i < a.length; i++) {
      f(a[i]) && l.push(a[i])
    }
  }
  return l;
}
Date.prototype.getLodges = function (date) {

  var weekIndex = this.getWeekDayIndex();
  var lodges = [];

  if (weekIndex in LodgesByIndex) {
    append( lodges, LodgesByIndex[weekIndex] );
  }

  var fullmoon = this.nextFullMoon();

  if ( this.isSameDay(fullmoon) ) {
    var index = "M" + days[this.getDay()]
    if ( index in LodgesByIndex ) {
      append( lodges, LodgesByIndex[index] );
    }
  } else if ((fullmoon.getTime() - this.getTime()) < 86400000 * 6) {
    var index = "M" + days[this.getDay()]
    if ( index in LodgesByIndex) {
      append( lodges, LodgesByIndex[index] );
    }
  }

  var month = this.getMonth();

  // filter if a lodge does not meet in the current month
  return filter(function(x){
    return ( !( x in LodgesWithMonth ) || month in LodgesWithMonth[x] );
  }, lodges);

}
function reverseLookup(hash) {
  var rev = {};
  for (var d in hash) {
    var arr = hash[d];
    for (var j = 0; j < arr.length; j++) {
      var num = arr[j];
      rev[num] = rev[num] || [];
      rev[num].push(d);
    }
  }
  return rev;
}
function lookup(arr){
  var lookup = {};
  for (var i = 0; i < arr.length; i++) {
    lookup[arr[i]] = i;
  }
  return lookup;
}


function keys(h) {
  var a = [];
  for (var i in h) {
    if (h.hasOwnProperty(i)) {
      a.push(i);
    }
  }
  return a;
}

function lodge_name(id) {
  if (id+"" in Lodges) {
    var lodge = Lodges[id+""];
    if (parseInt(id)) {
      return lodge.name + " #" + lodge.id;
    } else {
      return lodge.name;
    }
  } else {
    return id;
  }
}

var lodge_id = 23;

var $select = $("select#lodges");
function build_lodge_selector(){
  $select.empty()
  map(function(tup){
    var $option = $("<option>").attr({
      'value': tup[1]
    });
    $option.html( tup[0] );
    $select.append($option);
  },
   map(function(id){ return [lodge_name(id), id] },
    keys(DatesByLodge)
   ).sort(function(a,b){
      if (a[0] < b[0])
        return -1; 
      if (a[0] > b[0])
        return 1;
      return 0;
    })
  );
  $select.val(lodge_id);
}
build_lodge_selector()

$select.change(function(){
  lodge_id = $(this).val();
  render();
});
function selectLodge(id) {
  if (isNaN(parseInt(id))) {
    lodge_id = id;
  } else {
    lodge_id = parseInt(id);
  }
  $select.val(lodge_id);
  render();
}


var $districtSelect = $("select#districts")
Ordinals.forEach(function(o,i){
  var $option = $("<option>").attr({
    'value': o
  })
  if (i == 0) {
    $option.html("All Lodges")
  }
  else {
    $option.html(o[0].toUpperCase() + o.substr(1) + " " + "Manhattan")
  }
  $districtSelect.append($option)
})
$districtSelect.val("all")
$districtSelect.change(function(){
  var district = $(this).val()
  if (district == "all") {
    LodgesByIndex = AllLodgesByIndex
  }
  else {
    LodgesByIndex = LodgesByDistrict[ district.toLowerCase() + " manhattan district" ]
  }
  DatesByLodge = reverseLookup(LodgesByIndex)
  build_lodge_selector()
  var todays_lodges = today.getLodges()
  if (todays_lodges[lodge_id] === -1) {
    lodge_id = Object.keys(DatesByLodge)[0]
  }
  render()
})


var LodgeInfoView = $("#lodgeinfo-template").html();
function renderView(view, lodge) {
  var $view = $(view);
  var lodge_keys = keys(lodge);
  for (var i in lodge_keys) {
    $view.find("#"+lodge_keys[i]).html(lodge[lodge_keys[i]]);
  }
  $view.find("#image").attr("src", lodge["image"]);
  $view.find("a").each(function(){
    $(this).attr("href", lodge[this.className]);
  });
  $view.find("#address").html(lodge.address.join("<br>"));
  return $view;
};

var today = new Date ();
var pickadate;

function init(){
  bind();
  render();
}

function linkLodge(id){
  return "<a onclick=\"selectLodge(\'" + id + "')\">" + id + "</a>";
}
function linkLodges(lodges) {
  if (lodges && lodges.length) {
    return map(linkLodge, lodges).join(", ")
  }
}
function renderLodges(lodges) {
  if (lodges && lodges.length) {
    return map(function(id){ return renderView(LodgeInfoView, Lodges[id+""]).html() }, lodges).join("")
  }
}


function render(){
  if (today.isSummer()) {
    $("#data").hide();
    $("#summer").show();
    return;
  }
  today.cache();
  $("#data").show();
  $("#summer").hide();

  $("#today").html( today.display() );
  $("#todayindex").html( today.getWeekDayIndex() );

  $("#todaylodges").html( linkLodges( today.getLodges() ) || "None!" );
  $("#tomorrowlodges").html( linkLodges( today.tomorrow().getLodges() ) || "None!" );

  $(".lodgename").html( lodge_name(lodge_id) );

  var today_lodges = today.getLodges()
  // if (lodge_id+"" in Lodges) {
  //   var lodge = Lodges[lodge_id+""];
  //   $("#lodgeinfo").html( renderView(LodgeInfoView, lodge) ).show();
  // }
  if (today_lodges.length) {
    $("#lodgeinfo").html( renderLodges( today_lodges ) ).show();
  } else {
    $("#lodgeinfo").hide();
  }

  var lodgeDates = LodgeListByDate[today.getFullYear()][today.getMonth()][lodge_id+""];
  nextMonth = today.nextMonth();

  console.log(lodgeDates, lodge_id);

    $("#estuarymeetings").html( (function(){
      var dd = [];
      for (var i in lodgeDates) {
        dd.push( lodgeDates[i].display() );
      }
      return dd;
    })().join(", "));

    $("#estuarynext").html( (function(){
      for (var i in lodgeDates) {
        var date = lodgeDates[i];
        if ( date > today ) {
          return date.display();
        }
      }
      var dates = LodgeListByDate[ nextMonth.getFullYear() ][ nextMonth.getMonth() ][ lodge_id+"" ];
      if (dates && dates.length) {
        return dates[0].display();
      } else {
        return "";
      }
    })() );

  $("#nextfullmoon").html( today.nextFullMoon().display() );

  boldenLodgeDates();

}

function bind(){
  var input = $( '.datepicker' ).pickadate({
    format: 'ddd, dd mmm yyyy',
    onSelect: function() {
      this.open();
      var dp = this.getDate("yyyy-m-d").split("-");
      var year = parseInt( dp[0] );
      var month = parseInt( dp[1] ) - 1;
      var day = parseInt( dp[2] );
      today = new Date( year, month, day );
      render();
    },
    onChangeMonth: function(){
      boldenLodgeDates();
    }
  });
  var picker = input.data( 'pickadate' )
  picker.open();
  $("#next").click(function(){
    today = today.tomorrow();
    today.nextMonth().cache();
    picker.setDate(1900+today.getYear(), today.getMonth()+1, today.getDate())
  });
}

function boldenLodgeDates(){
  $(".bold").removeClass("bold");

  var pickadate = $( '.datepicker').data( 'pickadate' );
  var dis = pickadate.getMonth();

  switch (dis.MONTH) {
    case 6:
    case 7:
      return;
  }

  (new Date(dis.YEAR, dis.MONTH, 1)).cache();
  var lodgeDates = LodgeListByDate[dis.YEAR][dis.MONTH][lodge_id+""];

  for (var i in lodgeDates) {
    var date = lodgeDates[i];
    $(".pickadate__day[data-date='" + dis.YEAR + "/" + dis.MONTH + "/" + date.getDate() + "']").addClass("bold");
  }
}


init();

</script>
</html>