// dates.js
function addunit(string, number, unit, last)
{
      if (number)
      {
	  if (last) string = string + " and ";
	  string = string + number + "&nbsp;" + unit;
	  if (number > 1)
	  {
	      string = string + "s";
         }
	  string = string + " ";
      }
      
      return string;
}

function stripspace(string)
{
    while (string.length && string.charAt(0) == ' ')
	string = string.substr(1);
    while (string.length && string.charAt(string.length - 1) == ' ')
	string = string.substr(0, string.length - 1);
    return string;
}

function isLeapYear(date)
{
    var year = date.getFullYear();
    return (date % 4) == 0;
}

function daysof(date)
{
    var month = date.getMonth();
    var days;
    ++month;
    if (month == 1 || month == 3 || month == 5 || month == 7
	|| month == 8 || month == 10 || month == 12)
    {
	days = 31;
    }
    else if (month == 2)
    {
	if (isLeapYear(date)) {
	    days = 29;
	}
	else {
	    days = 28;
	}
    }
    else
	days = 30;
    return days;
}

function getDayNum(when)
{
    var newYear = new Date(when.getFullYear(),0,1);
    var daynum = Math.floor((when.getTime() - newYear.getTime()) / 86400000) + 1;
    return daynum;
}

function DateDiff(startdate, today)
{
    this.secs = this.mins = this.hours = this.days = 0;
    this.months = this.years = 0;
    this.wkdays = this.weeks = this.wkyears = 0;

    if (typeof today == "undefined") today = new Date();

    if (startdate.getTime() > today.getTime())
    {
	var tmp = today;
	today = startdate;
	startdate = tmp;
    }
    
    var value = startdate.getSeconds();
    var target = today.getSeconds();
    var inc = 0;

    this.secs = target - value;
    value = startdate.getMinutes();
    target = today.getMinutes();

    if (this.secs < 0)
    {
	this.secs += 60;
	value += 1;
    }

    this.mins = target - value;
    value = startdate.getHours();
    target = today.getHours();

    if (this.mins < 0)
    {
	this.mins += 60;
	value += 1;
    }

    this.hours = target - value;

    var value1 = getDayNum(startdate);
    var target1 = getDayNum(today);

    value = startdate.getDate();
    target = today.getDate();

    if (this.hours < 0)
    {
	this.hours += 24;
	value1 += 1;
	value += 1;
    }

    inc = 0;

    this.days = target - value;
    this.wkdays = target1 - value1;

    value = startdate.getMonth();
    target = today.getMonth();
	
    if (this.days < 0)
    {
	// care about the month _before_
	var newDate =
	    new Date(today.getFullYear(), target - 1, 1);
	this.days += daysof(newDate);
	target -= 1;
    }
	
    this.months = target - value;
    value = value1 = startdate.getFullYear();
    target = today.getFullYear();

    if (this.wkdays < 0)
    {
	this.wkdays += 365;
	if (isLeapYear(startdate)) ++this.wkdays;
	value1 += 1;
    }

    this.weeks = Math.floor(this.wkdays / 7);
    this.wkdays = this.wkdays % 7;

    if (this.months < 0) {
	this.months += 12;
	value += 1;
    }

    this.years = target - value;
    this.wkyears = target - value1;
}

function datefrom_ish(date)
{
    var dd = new DateDiff(new Date(date));

    string = "" + dd.years;
    if (dd.months >= 9) string = string + " and three quarters";
    else if (dd.months >= 8) string = string + " and two thirds";
    else if (dd.months >= 6) string = string + " and a half";
    else if (dd.months >= 4) string = string + " and a third";
    else if (dd.months >= 3) string = string + " and a quarter";

    return stripspace(string);
}

function datefrom(date, ish)
{
    var dd = new DateDiff(new Date(date));

    if (typeof ish == "undefined") ish = 0;

    string = "";

    string = addunit(string, dd.years, "year", 0);
    if (ish <= 4) string = addunit(string, dd.months, "month",  ish == 4);
    if (ish <= 3) string = addunit(string, dd.days,   "day",    ish == 3);
    if (ish <= 2) string = addunit(string, dd.hours,  "hour",   ish == 2);
    if (ish <= 1) string = addunit(string, dd.mins,   "minute", ish == 1);
    if (ish == 0) string = addunit(string, dd.secs,   "second", 1);

    return stripspace(string);
}

function datefrom_wks(date, ish)
{
    var dd = new DateDiff(new Date(date));

    if (typeof ish == "undefined") ish = 0;

    string = "";

    string = addunit(string, dd.wkyears, "year", 0);
    if (ish <= 4) string = addunit(string, dd.weeks,  "week",   ish == 4);
    if (ish <= 3) string = addunit(string, dd.wkdays, "day",    ish == 3);
    if (ish <= 2) string = addunit(string, dd.hours,  "hour",   ish == 2);
    if (ish <= 1) string = addunit(string, dd.mins,   "minute", ish == 1);
    if (ish == 0) string = addunit(string, dd.secs,   "second", 1);

    return stripspace(string);
}

function anniversary(date, next)
{
    var today = new Date();
    var startdate = new Date(date);

    startdate.setFullYear(today.getFullYear());
    if (next && startdate.getTime() < today.getTime())
    {
	startdate.setFullYear(startdate.getFullYear() + 1);
    }
    else if (!next && startdate.getTime() > today.getTime())
    {
	startdate.setFullYear(startdate.getFullYear() - 1);
    }

    return startdate.toString();
}

function datefromanniversary(date, next, ish)
{
    return datefrom(anniversary(date, next), ish);
}

function showstring(string, layerstring)
{
    var layer = document.getElementById(layerstring);
    if (layer) layer.innerHTML = string;
}

function showfrom(date, layerstring, prefix, suffix)
{
    if (typeof prefix == "undefined") prefix = "";
    if (typeof suffix == "undefined") suffix = "";
    showstring(prefix + datefrom(date, 0) + suffix, layerstring);
}

function showfromanniversary(date, next, layerstring, prefix, suffix)
{
    if (typeof prefix == "undefined") prefix = "";
    if (typeof suffix == "undefined") suffix = "";
    showstring(prefix + datefromanniversary(date, next, 0) + suffix,
	       layerstring);
}

function age(date, next)
{
    var anni = new Date(anniversary(date, next));
    date = new Date(date);
    return anni.getFullYear() - date.getFullYear();
}

function nth(year, html)
{
    var th = "th";
    if ((year % 10) == 1 && (year % 100) != 11) th = "st";
    if ((year % 10) == 2 && (year % 100) != 12) th = "nd";
    if (html) th = "<sup>" + th + "</sup>";
    return year + th;
}
