/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (c) 2008 Christopher Davaz. All rights reserved.
 */

function Birthday (b_year, b_month, b_date, b_hour, b_minute) {
	var time = new Date();
	time.setFullYear(b_year);
	time.setMonth(b_month);
	time.setDate(b_date);
	time.setHours(b_hour);
	time.setMinutes(b_minute);

	this.time = time;

  this.daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	this.add_s = function (num) { return num == 1 ? "" : "s"; }

	this.isLeapYear = function (date) {
		var years = date.getFullYear();
		var since2k = years - 2000;
		if (since2k % 4) {
			return true;
		} else {
			return false;
		}
	}

	this.yearsOld = function () {
		var now     = new Date();
		var n_month = now.getMonth();
		var n_date  = now.getDate();
		var b_month = time.getMonth();
		var b_date  = time.getDate();
		var years   = now.getFullYear() - time.getFullYear();
		// If birthday hasn't occurred yet, subtract 1
		if (n_month < b_month || (n_month == b_month && n_date < b_date)) {
			years--;
		}
		return years;
	}

	this.monthsOld = function () {
		var now = new Date();
		var months = now.getMonth() - this.time.getMonth();
		if (months < 0) {
			months += 12;
		}
		if (months > 0) {
			// If less than a month has passed, reduce the number of months
			if (now.getDate() - this.time.getDate() < 0) {
				months--;
			}
		}
		return months;
	}

	this.daysOld = function () {
		var now     = new Date();
		var days    = now.getDate();
		var n_month = now.getMonth();
		var b_date  = this.time.getDate();

		n_month--;
		if (n_month < 0)
			n_month += 12;

		var d_in_m = this.daysInMonth[n_month];

		if (n_month == 1 && this.isLeapYear(now))
			d_in_m++;

		days -= b_date;
		if (days < 0) {
			days += d_in_m;
		}

		return days;
	}

	this.hoursOld = function () {
		var now    = new Date();
		var n_hour = now.getHours();
		var b_hour = this.time.getHours();
		var hours  = n_hour - b_hour;
		if (hours < 0)
			hours += 24;
		return hours;
	}

	this.minutesOld = function () {
		var now   = new Date();
		var n_min = now.getMinutes();
		var b_min = this.time.getMinutes();
		var mins  = n_min - b_min;
		if (mins < 0)
			mins += 24;
		return mins;
	}

	this.elapsedTime = function () {
		var years  = this.yearsOld();
		var months = this.monthsOld();
		var days   = this.daysOld();
		var hours  = this.hoursOld();
		var mins   = this.minutesOld();
		var rv     = "";

		if (years > 0) {
			rv += years + " year" + this.add_s(years) + " ";
		}

		if (months > 0) {
			rv += months + " month" + this.add_s(months) + " ";
		}

		if (days > 0) {
			rv += days + " day" + this.add_s(days) + " ";
		}

		if (hours > 0) {
			rv += hours + " hour" + this.add_s(hours) + " ";
		}

		if (mins > 0) {
			rv += mins + " minute" + this.add_s(mins) + " ";
		}

		var now = new Date();

		rv += now.getSeconds() + " second" + this.add_s(now.getSeconds());

		rv += " old";
		return rv;
	}
}

window.onload = function () {
	var bday  = new Birthday(2008, 11, 31, 1, 13); // December 31st at 1:13am 2008
	var timer = document.getElementById("elapsedTimeSinceBirthday");
	displayElapsedTime = function () {
		timer.innerHTML = bday.elapsedTime();
	}
	displayElapsedTime();
	setInterval(displayElapsedTime, 1000);
}

