function printLitColor()
{
	today = new Date();
	navColor = getLitColor(today);
	
	document.writeln('<b><BR>Il colore liturgico di oggi è il <span class="litcolor">' +
		navColor + '</span></b>.');
				
}

function styleLitColor()
{
	today = new Date();
	todayLitColor = getLitColor(today);
		
	document.writeln('<link href="' + todayLitColor + '/style.css" rel="stylesheet" type="text/css" media="screen" >');
	
	document.writeln('<link href="' + todayLitColor + '/menu.css" rel="stylesheet" type="text/css" media="screen" >');
	
		
}

function getLitColor(today)
{
	jToday = cnv2JulianDate(today);
	mToday = today.getMonth();

	if (mToday == 0) {
		lColor = janColor(today,jToday);
	}
	else if ((mToday >= 6) && (mToday <= 9)) {
		lColor = "verde";
	}
	else if (mToday >= 10) {
		lColor = adventColor(today,jToday);
	}
	else {
		lColor = easterColor(today,jToday);
	}
	
	lColor=giorniparticolari(lColor);

	
	return lColor;

}

function janColor(today,jToday) {

	Jan7 = new Date();
        Jan7.setMonth(0);
	Jan7.setDate(7);
	Jan7.setYear(today.getFullYear());

	if (today.getTime() < Jan7.getTime()) {
		return "bianco";
	}
	nextSunday = cnv2JulianDate(Jan7);
	if (Jan7.getDay() > 0) {
		nextSunday += (7 - Jan7.getDay());
	}

	if (jToday == nextSunday) {
		return "bianco";
	}
	else {
		return "verde";
	}

}

function giorniparticolari(Colore) {
	var today = new Date();
	
		
	// novembre --------------------------------------------------------------
	if (((today.getDate() == 16) || (today.getDate() == 30 ) ) && (today.getMonth() == 10)) {
		return "rosso";
	}
		
		
	// dicembre
	
	if ((today.getDate() == 3 || today.getDate() == 7 || today.getDate() == 8|| today.getDate() == 14 || today.getDate() == 25 || today.getDate() == 27 || today.getDate() == 29 || today.getDate() == 30 || today.getDate() == 31) && (today.getMonth() == 11)) {
		return "bianco";
	}
	
	if ((today.getDate() == 13 || today.getDate() == 26 || today.getDate() == 28 ) && (today.getMonth() == 11)) {
		return "rosso";
	}
	
	// rossa gennaio
	if ( (today.getDate() == 21 ||  today.getDate() == 19) && (today.getMonth() == 0)) {
		return "rosso";
	}
	
	// bianche gennaio
	if ((today.getDate() == 7 || today.getDate() == 8 || today.getDate() == 17  || today.getDate() == 24 || today.getDate() == 25|| today.getDate() == 26 || today.getDate() == 28 || today.getDate() == 31) && (today.getMonth() == 0)) {
		return "bianco";
	}
	
	//bianche febbraio
	if ((today.getDate() == 2 || today.getDate() == 10 || today.getDate() == 14 ) && (today.getMonth() == 1)) {
		return "bianco";
	}
	
	//bianche marzo
	if ((today.getDate() == 19 || today.getDate() == 26 ) && (today.getMonth() ==2)) {
		return "bianco";
	}
	
	//rosa marzo
	if ((today.getDate() == 18) && (today.getMonth() ==2)) {
		return "rosa";
	}
	
	
	
return Colore;
}


function adventColor(today,jToday) {

	if ((today.getDate() == 1) && (today.getMonth() == 10)) {
		return "bianco";
	}
	
	firstAdvent = getFirstAdvent(today);

	if (jToday == (firstAdvent - 7)) {
		return "bianco";
	}
	
	
	//if (jToday < firstAdvent) {
	//	return "viola";
	//}
	
	Dec24 = new Date();
        Dec24.setMonth(11);
	Dec24.setDate(24);
	Dec24.setYear(today.getFullYear());

	if (today.getTime() > Dec24.getTime()) {
		return "bianco";
	}
	
	
if (jToday == (firstAdvent - 14)) {
		return "rosa";
	}
	return "viola";
}

function easterColor(today,jToday) {

	Easter = getEaster(today.getFullYear());

	if (jToday == (Easter - 7)) {
		return "rosso"
	}
	else if (jToday == (Easter - 49)) {
		return "bianco"
	}

	AshWednesday = Easter - 46;
	if (jToday < AshWednesday) {
		return "verde"
	}
	else if (jToday <= (Easter - 3)) {
		return "viola"
	}

	Pentecost = Easter + 49;
	if ((jToday >= Pentecost) && (jToday <= (Pentecost + 6))) {
		return "rosso";
	}
	else if (jToday > (Pentecost + 7)) {
		return "verde";
	}
	return "bianco";

}

function cnv2JulianDate(d) {

	if (Math.floor(d.getFullYear() % 4) > 0) {
		var numDayz = new Array(0,31,59,90,120,151,181,212,243,273,304,334);
	}
	else {
		var numDayz = new Array(0,31,60,91,121,152,182,213,244,274,305,335);
	}

	jd = Math.round(numDayz[d.getMonth()]) + Math.round(d.getDate());
	return jd;

}

function getFirstAdvent(d) {

	Dec25 = new Date();
        Dec25.setMonth(11);
	Dec25.setDate(25);
	Dec25.setYear(today.getFullYear());

	advent1st = cnv2JulianDate(Dec25);
	if (Dec25.getDay() > 0) {
		advent1st -= (21 + Dec25.getDay());
	}
	return advent1st;

}

function getEaster(year) {

	a = Math.round(year%19);
	b = Math.floor(year/100);
	c = Math.round(year%100);
	d = Math.floor(b/4);
	e = Math.round(b%4);
	f = Math.floor((b+8)/25);
	g = Math.floor((b-f+1)/3);
	h = Math.round((19*a+b-d-g+15)%30);
	i = Math.floor(c/4);
	k = Math.round(c%4);
	l = Math.round((32+2*e+2*i-h-k)%7);
	m = Math.floor((a+11*h+22*l)/451);
	easterMonth = Math.floor((h+l-7*m+114)/31);
	p = Math.round((h+l-7*m+114)%31);
	easterDate = p+1;

	easter = new Date();
        easter.setMonth(easterMonth-1);
	easter.setDate(easterDate);
        easter.setYear(year);

	jEaster = cnv2JulianDate(easter);

	return jEaster;
}
