// Auto update function - display different text depending on the date

if (new Date(document.lastModified) - new Date() > 172800000
	  && !document.cookie) {
	alert('The date is incorrectly set on the machine you are using.  Some of '+
	'the information on Stewart\'s Place may therefore be inaccurate.');
	document.cookie = 'datewarned=yes';
}

function autoupdate(before, d, m, y, after) {
	var today = new Date();
	var cmpdate = new Date(y, m - 1, d);

	if(today < cmpdate) {
		document.write(before);
	}
	else {
		document.write(after);
	}
}

function newtag(d, m, y, tagpath) {
	autoupdate('<img src="' + tagpath +
	  'new.gif" alt="New!" width="37" height="17">',
	  d, m, y, '');
}

function countdown(d, m, y, intro, plural, singular, after) {
	var cmpdate = new Date(y, m - 1, d);

	cdbody(cmpdate, intro, plural, singular, after, after);
}

function countdown2(d, m, y, intro, plural, singular, ontheday, after) {
	var cmpdate = new Date(y, m - 1, d);

	cdbody(cmpdate, intro, plural, singular, ontheday, after);
}

function cdbody(cmpdate, intro, plural, singular, ontheday, after) {
	var today = new Date();
	var days = Math.ceil((cmpdate - today) / 86400000);

	if(today < cmpdate) {
		document.write(intro);
		document.write(days);
		if(days == 1) {
			document.write(singular);
		}
		else {
			document.write(plural);
		}
	} else if (days == 0) {
		document.write(ontheday);
	} else {
		document.write(after);
	}
}

function josephage() {
	var today = new Date();

	if (today >= new Date(2010, 8, 15)) return age(15, 8, 2008);

	var months = today.getMonth() - 7;
	months += 12 * (today.getFullYear() - 2008);
	//if (today.getFullYear() == 2010) months += 12;
	if (today.getDate() < 15) months--;

	if (months == 1) {
		return '1 month';
	} else {
		return months + ' months';
	}
}

function age(day, month, year) {
	var today = new Date();
	var a = today.getFullYear() - year;

	var monthd = today.getMonth() + 1 - month;
	var tday = today.getDate();

	switch(monthd) {
		case -1:
			if(tday < day) {
				return '' + (a - 1);
			} else {
				return '' + (a - 1) + ', almost ' + a;
			}
			break;
		case 0:
			if(tday < day) {
				return '' + (a - 1) + ', almost ' + a;
			} else {
				return 'just turned ' + a;
			}
			break;
		case 1:
			if(tday < day) {
				return 'just turned ' + a;
			} else {
				return '' + a;
			}
		default:
			return '' + (a - (monthd < 0));
	}
}

function myage() {
	var today = new Date();
	var a = today.getFullYear() - 1979;

	var tmonth = today.getMonth();
	var tday = today.getDate();

	switch(tmonth) {
		case 7:
			if(tday < 24) {
				return 'I am ' + (a - 1) + ' years old';
			} else {
				return 'I am ' + (a - 1) + ' years old (almost ' + a + ')';
			}
			break;
		case 8:
			if(tday < 24) {
				return 'I am ' + (a - 1) + ' years old (almost ' + a + ')';
			} else {
				return 'I&#8217;ve just turned ' + a;
			}
			break;
		case 9:
			if(tday < 24) {
				return 'I&#8217;ve just turned ' + a;
			} else {
				return '' + a;
			}
		case 10: case 11:
			return 'I am ' + a + ' years old';
		default:
			return 'I am ' + (a - 1) + ' years old';
	}
}
