﻿
var aday = 24 * 60 * 60 * 1000;

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var holydays = ["Palm Sunday", "Holy Monday", "Holy Tuesday", "Holy Wednesday", "Maundy Thursday", "Good Friday", "Holy Saturday"];

var holytags = ["PALM", "HM", "HT", "HW", "MT", "GF", "HS"];

var eastertags = ["EASTER", "EM", "ETu", "EW", "ETh", "EF", "ES"];

var ordinals = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth",
		         "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth",
				 "Twentieth", "Twenty-first", "Twenty-second", "Twenty-third", "Twenty-fourth", "Twenty-fifth", "Twenty-sixth", "Twenty-seventh", "Twenty-eighth", "Twenty-ninth",
				 "Thirtieth"];

function amenise (clct) {
	var j = clct.lastIndexOf ("Amen.");
	if (j < 0) return clct
	else return clct.substring (0, j) + "<i>Amen.<\/i>" + clct.substring (j + 5);
}

function lookup (tag) {
	if (tag == "") return "";
	var i = 0;
	while (i < collects.length) {
		if (collects [i] == tag) return collects [i+1];
		else i += 2;
	}
	return "";
}
	
function daysinmonths (m, y) {
	if (m == 1) {
		if (y % 400 == 0) return 29;
		else if (y % 100 == 0) return 28;
		else if (y % 4 == 0) return 29;
		else return 28;	
	} else if (m == 3 || m == 5 || m == 8 || m == 10) return 30;
	else return 31;
}

function div (x, y) {
	//var z = x % y;
	//var res = (x-z) / y;
	return Math.floor (x / y);
}

function getEaster (y) {
	var c = div(y,100);
	var n = y % 19;
	var k = div(c-17,25);
	var i = ((c + 15 + 19*n - div(c,4)) - div(c-k,3)) % 30;
	var p = div(29,i+1) * div(21-n,11);
	i = i - div(i,28) * (1 - p * div(i,28));
	var j = (y + div(y,4) + i + 2 + div(c,4) - c) % 7;
	var q = i - j;
	var m = 3 + div(q+40,44);
	var d = q + 28 - 31 * div(m,4);
	return new Date (y, m-1, d);
}

function getChristmas (y) {
	return new Date (y, 11, 25);
}

function getEpiphany (y) {
	return new Date (y, 0, 6);
}

function getAshWednesday (easter) {
	var d = easter.getTime () - (46 * aday);
	return new Date (d);
	return new Date (dd.getFullYear (), dd.getMonth (), dd.getDate ());
}

function getAdvent (christmas) {
	var d = christmas.getDay ();
	var y = christmas.getFullYear ();
	if (d == 0) return new Date (y, 10, 27);
	else if (d == 1) return new Date (y, 11, 3);
	else if (d == 2) return new Date (y, 11, 2);
	else if (d == 3) return new Date (y, 11, 1);
	else if (d == 4) return new Date (y, 10, 30);
	else if (d == 5) return new Date (y, 10, 29);
	else return new Date (y, 10, 28);
}

function sdays (i) {
	if (i == 1) return "1 day";
	else return i + " days";
}

function ordinal (i) {
	return ordinals [i-1];
}

function ordinalsunday (i, season) {
	return ordinals [i-1] + " Sunday " + season;
}

function LD_toString () {
	return this.weekday + ", " + this.date.getDate () + "/" + (1 + this.date.getMonth ()) + "/" + this.date.getFullYear () + (this.message == "" ? "" : ", " + this.message);
}

function LiturgicalDate (d) {
	this.year = d.getFullYear ();
	this.month = d.getMonth () + 1;
	this.day = d.getDate ();
	this.date = new Date (this.year, this.month - 1, this.day);
	
	var easterday = getEaster (this.year);
	var christmasday = getChristmas (this.year);
	var ashwed = getAshWednesday (easterday);
	var epiphanyday = getEpiphany (this.year);
	var adventsun = getAdvent (christmasday);
	
	var ediff =  easterday.getTime () - this.date.getTime ();
	ediff = - Math.floor (ediff / aday);
	
	var adiff = adventsun.getTime () - this.date.getTime ();
	adiff = - Math.floor (adiff / aday);
	
	var cdiff = christmasday.getTime () - this.date.getTime ();
	cdiff = - Math.floor (cdiff / aday);
	
	var ldiff = ashwed.getTime () - this.date.getTime ();
	ldiff = - Math.floor (ldiff / aday);
	
	var fdiff = epiphanyday.getTime () - this.date.getTime ();
	fdiff = - Math.floor (fdiff / aday);
	
	this.easteroffset = ediff;
	this.christmasoffset = cdiff;
	this.adventday = -1;
	this.lentday = -1;
	this.epiphday = -1;
	this.properday = -1;
	this.christmas = (cdiff == 0);
	this.easter = (ediff == 0);
	var dy = this.date.getDay ();
	this.sunday = (dy == 0);
	this.holyweek = (ediff >= -7 && ediff < 0);
	this.feast = this.holyweek || this.christmas;
	this.message = "";	
	this.weekday = days [dy];
	this.tag = "";
	
	
	if (adiff >= 0 && cdiff < 0) this.adventday = adiff;
	else if (ldiff >= 0 && ediff < -7) this.lentday = ldiff;
	else if (fdiff > 0 && ldiff < 0) this.epiphday = fdiff;
	else if (ediff >= 63 && adiff < 0) this.properday = adiff + 30 * 7;
	

	if (this.christmas) { this.message = "Christmas"; this.tag = "CHRISTMAS"; }
	else if (this.easter) { this.message = "Easter Day"; this.tag = "EASTER"; }
	else if (this.holyweek) { this.message = holydays [7 + ediff]; this.tag = holytags [7 + ediff]; }

	
	else if (this.day == 6 && this.month == 1) { this.message = "Epiphany"; this.feast = true; this.tag = "EPIPHANY"; }
	
	else if (ediff > 0 && ediff <= 6) { this.message = this.weekday + " after Easter"; this.tag = eastertags [ediff]; }
	
	else if (ediff == 39) { this.message = "Ascension Day"; this.feast = true; this.tag = "ASC"; }
	else if (ediff == 49) { this.message = "Pentecost"; this.tag = "PENT"; }
	else if (ediff == 56) { this.message = "Trinity Sunday"; this.tag = "TRINITY"; }
	
	else if (this.adventday == 0) { this.message = "Advent Sunday"; this.tag = "A1"; }
	else if (this.lentday == 0) { this.message = "Ash Wednesday"; this.tag = "ASH"; }
	
	else if (this.sunday && this.adventday >= 0) {
		var i = Math.floor (1 + (this.adventday / 7));
		this.message = ordinalsunday (i, "in Advent");
		this.tag = "A" + i;
	} else if (this.sunday && this.lentday >= 0) {
		var i = Math.floor (1 + (this.lentday / 7));
		this.message = ordinalsunday (i, "in Lent");
		this.tag = "L" + i;
	} else if (this.sunday && this.epiphday > 0) {
		var i = Math.floor ((this.epiphday + 6) / 7);
		this.message = ordinalsunday (i, "after Epiphany");
		this.tag = "F" + i;
		if (ldiff == -3) {
			this.message = "Last Sunday after Epiphany";
			this.tag = "LAST";		
		}
	} else if (this.sunday && ediff >= 63 && adiff < 0) {
		var i = Math.floor ((ediff - 49) / 7);
		var j = Math.floor (this.properday / 7);
		this.message = ordinalsunday (i, "after Pentecost") + " (Proper " + j + ")";
		this.tag = "R" + j;
	} else if (this.sunday && ediff > 0 && ediff < 49) {
		var i = Math.floor ((ediff + 7) / 7);
		this.message = ordinalsunday (i, "of Easter");
		this.tag = "E" + i;
	} else if (this.sunday && cdiff > 0) {
		var i = Math.floor ((cdiff + 6) / 7);
		this.message = ordinalsunday (i, "after Christmas");
		this.tag = "C" + i;
	} else if (this.sunday && fdiff < 0 && this.day == 1) {
		this.tag = "C1";
		this.message = "First Sunday after Christmas (New Year's Day)";
	} else if (this.sunday && fdiff < 0 && this.day > 1) {
		this.tag = "C2";
		this.message = "Second Sunday after Christmas";
	}

	else if (this.month == 9 && this.day == 29) { this.message = "Saint Michael and All Angels"; this.tag = "MICHAEL"; this.feast = true; }
	else if (this.month == 10 && this.day == 18) { this.message = "Saint Luke"; this.tag = "LUKE"; this.feast = true; }
	else if (this.month == 10 && this.day == 23) { this.message = "Saint James of Jerusalem"; this.tag = "JJ"; this.feast = true; }
	else if (this.month == 10 && this.day == 28) { this.message = "Saint Simon and Saint Jude"; this.tag = "JUDE"; this.feast = true; }
	else if (this.month == 11 && this.day == 1) { this.message = "All Saint's Day"; this.tag = "ALL"; this.feast = true; }
	else if (this.month == 11 && this.day == 30) { this.message = "Saint Andrew"; this.tag = "ANDREW"; this.feast = true; }
	else if (this.month == 12 && this.day == 21) { this.message = "Saint Thomas"; this.tag = "THOMAS"; this.feast = true; }
	else if (this.month == 12 && this.day == 26) { this.message = "Saint Stephen"; this.tag = "STEPHEN"; this.feast = true; }
	else if (this.month == 12 && this.day == 27) { this.message = "Saint John"; this.tag = "JOHN"; this.feast = true; }
	else if (this.month == 12 && this.day == 28) { this.message = "The Holy Innocents"; this.tag = "HI"; this.feast = true; }
	else if (this.month == 1 && this.day == 18) { this.message = "Confession of Saint Peter"; this.tag = "PETER"; this.feast = true; }
	else if (this.month == 1 && this.day == 25) { this.message = "Conversion of Saint Paul"; this.tag = "PAUL"; this.feast = true; }
	else if (this.month == 2 && this.day == 2) { this.message = "The Presentation"; this.tag = "PRES"; this.feast = true; }
	else if (this.month == 2 && this.day == 24) { this.message = "Saint Matthias"; this.tag = "MHS"; this.feast = true; }
	else if (this.month == 3 && this.day == 19) { this.message = "Saint Joseph"; this.tag = "JOS"; this.feast = true; }
	else if (this.month == 3 && this.day == 25) { this.message = "The Annunciation"; this.tag = "ANN"; this.feast = true; }
	else if (this.month == 4 && this.day == 25) { this.message = "Saint Mark"; this.tag = "MARK"; this.feast = true; }
	else if (this.month == 5 && this.day == 1) { this.message = "Saint Philip and Saint James"; this.tag = "PJ"; this.feast = true; }
	else if (this.month == 5 && this.day == 31) { this.message = "The Visitation"; this.tag = "VIS"; this.feast = true; }
	else if (this.month == 6 && this.day == 11) { this.message = "Saint Barnabas"; this.tag = "BARN"; this.feast = true; }
	else if (this.month == 6 && this.day == 24) { this.message = "The Nativity of Saint John the Baptist"; this.tag = "BAPT"; this.feast = true; }
	else if (this.month == 6 && this.day == 29) { this.message = "Saint Peter and Saint Paul"; this.tag = "PP"; this.feast = true; }
	else if (this.month == 7 && this.day == 25) { this.message = "Saint James"; this.tag = "JAMES"; this.feast = true; }
	else if (this.month == 7 && this.day == 22) { this.message = "Saint Mary Magdalene"; this.tag = "MAGDA"; this.feast = true; }
	else if (this.month == 8 && this.day == 6) { this.message = "The Transfiguration"; this.tag = "TRANS"; this.feast = true; }
	else if (this.month == 8 && this.day == 15) { this.message = "Saint Mary the Virgin"; this.tag = "VIRGIN"; this.feast = true; }
	else if (this.month == 8 && this.day == 24) { this.message = "Saint Bartholomew"; this.tag = "BART"; this.feast = true; }
	else if (this.month == 9 && this.day == 21) { this.message = "Saint Matthew"; this.tag = "MATTHEW"; this.feast = true; }
	else if (this.month == 9 && this.day == 14) { this.message = "Holy Cross Day"; this.tag = "CROSS"; this.feast = true; }
	
	else if (this.adventday >= 0) this.message = sdays (- this.christmasoffset) + " before Christmas";
	else if (this.lentday >= 0) this.message = sdays (- this.easteroffset) + " before Easter";
	
	else if (this.christmasoffset > 0) this.message = sdays (this.christmasoffset) + " after Christmas";
	//else if (this.easteroffset > 0) this.message = sdays (this.easteroffset) + " after Easter";
	//else this.message = sdays (- this.easteroffset) + " before Easter";

	if (this.adventday >= 0) this.fg = "#0000ff";
	else if (this.lentday >= 0) this.fg = "#8000ff";
	else if (this.sunday || this.feast) this.fg = "#ff0000";
	else this.fg = "#000000";

	this.toString = LD_toString;
}


