//<![CDATA[

function InitProseClock(id)
{
  var pc = new ProseClock(id);
  pc.SetPhrases();
  pc.Display();
}

function ProseClock(id)
{
  this.ID                   = id;
  this.current_time         = new Object();
  this.min_phrase           = "";
  this.mul_phrase           = "";
  this.hour_phrase          = "";
  this.mul                  = null;
  this.RefreshTimeInSeconds = 20;

  // change this to suit your preference
  this.TitleBarDisplay      = false;
}

ProseClock.prototype.Display = function()
{
  var el = document.getElementById(this.ID);
  var prose_time = this.GetProseTime();
  el.innerHTML = prose_time;

  // display clock in title bar
  if (this.TitleBarDisplay)
  {
    document.title = prose_time;
  }

  var self = this;
  setTimeout(function() {self.Display(); }, self.RefreshTimeInSeconds * 1000);
}

ProseClock.prototype.GetProseTime = function()
{
  this.get_time();
  this.set_minute();
  this.set_hour();
  var text = this.min_phrase + " " + this.mul_phrase + " " + this.hour_phrase;
  return text.replace(/ {2,}/, " ");
}

ProseClock.prototype.set_minute = function()
{
  var minutes = this.current_time.minutes;

  var remainder = minutes % 5;

  this.mul = minutes - remainder;

  // switch frame of reference
  if (remainder >= 3)
  {
    this.mul += 5;
  }

  this.min_phrase = this.minute_phrases[remainder];

  if (this.multiples[this.mul] != null)
  {
    this.mul_phrase = this.multiples[this.mul];
  }
  else
  {
    this.mul_phrase = "";
  }
}

ProseClock.prototype.set_hour = function()
{
  var hours = this.current_time.hours;

  if (this.mul >= 35)
  {
    if (hours == 23)
    {
      hours = 0;
    }
    else
    {
      hours += 1;
    }
  }
  
  this.hour_phrase = this.hour_phrases[hours];

  if ((hours == 0) && (this.current_time.minutes < 3))
  {
    this.mul_phrase = "";
  }
}

ProseClock.prototype.get_time = function()
{
  var d = new Date();
  this.current_time.hours   = d.getHours();
  this.current_time.minutes = d.getMinutes();
}

ProseClock.prototype.SetPhrases = function()
{
  this.minute_phrases = new Array("exactly",
                                  "just after",
                                  "a little after",
                                  "coming up to",
                                  "almost");

  this.hour_phrases = new Array("midnight",
                                "one in the morning",
                                "two in the morning",
                                "three in the morning",
                                "four in the morning",
                                "five in the morning",
                                "six in the morning",
                                "seven in the morning",
                                "eight in the morning",
                                "nine in the morning",
                                "ten in the morning",
                                "eleven in the morning",
                                "noon",
                                "one in the afternoon",
                                "two in the afternoon",
                                "three in the afternoon",
                                "four in the afternoon",
                                "five in the afternoon",
                                "six at night",
                                "seven at night",
                                "eight at night",
                                "nine at night",
                                "ten at night",
                                "eleven at night");

  this.multiples = new Object();
  this.multiples["0"]  = "";
  this.multiples["5"]  = "five past";
  this.multiples["10"] = "ten past";
  this.multiples["15"] = "quarter past";
  this.multiples["20"] = "twenty past";
  this.multiples["25"] = "twenty-five past";
  this.multiples["30"] = "half past";
  this.multiples["35"] = "twenty-five to";
  this.multiples["40"] = "twenty to";
  this.multiples["45"] = "quarter to";
  this.multiples["50"] = "ten to";
  this.multiples["55"] = "five to";

  return;
}

//]]>
