/*
 * Lunar Standard Time (LST) version 1.0
 *
 * Public Domain 2010 by Anthony Howe.
 *
 * See lunarclock.org and http://en.wikipedia.org/wiki/Lunarseconds_per_day
 *
 * unix_epoch	= 1 Jan 1970 00:00:00 +0000 = 0s
 * lunar_epoch	= 21 Jul 1969 02:56:15 +0000 = unix_epoch - 14159025s
 * lunarseconds_per_day	= (synodic period) 29d 12h 44m 3s = 29.530589d
 * lunar_second	= lunarseconds_per_day / 30 cycles = 0.984352967s
 */

// Public constant properties.

LST.lunar_epoch_offset		= 14159025;
LST.lunarseconds_per_day	= 29.530589;
LST.lunar_second			= 0.984352967;
LST.synodic_period			= LST.lunarseconds_per_day;

LST.seconds_per_minute 		= 60;
LST.seconds_per_hour 		= 60 * LST.seconds_per_minute;
LST.seconds_per_cycle		= 24 * LST.seconds_per_hour;
LST.cycles_per_day			= 30;
LST.seconds_per_day			= LST.cycles_per_day * LST.seconds_per_cycle;
LST.days_per_year			= 12;
LST.seconds_per_year		= LST.days_per_year * LST.seconds_per_day;
LST.symbol					= "&nabla;";

LST.lunar_day_name			= [
	'Armstrong', 'Aldrin', 'Conrad', 'Bean',
	'Shepard', 'Mitchell', 'Scott', 'Irwin',
	'Young', 'Duke', 'Cernan', 'Schmitt'
];

// Public instance properties.

LST.prototype.seconds = null;

LST.prototype.year = null;
LST.prototype.day = null;
LST.prototype.cycle = null;
LST.prototype.hour = null;
LST.prototype.minute = null;
LST.prototype.second = null;

// Private class methods

LST._double_digits = function(n)
{
	return n < 10 ? '0' + n : n;
}

// Private instance methods

LST.prototype._getTime = function()
{
	return LST._double_digits(this.hour)+':'+LST._double_digits(this.minute)+':'+LST._double_digits(this.second);
};

LST.prototype._getDate = function(now, gmt_time)
{
	return (this.year+1)+'-'+LST._double_digits(this.day+1)+'-'+LST._double_digits(this.cycle+1);
};

// Public methods.

LST.prototype.getTime = function()
{
	return LST.symbol+"&nbsp;"+this._getTime();
};

LST.prototype.getDate = function()
{
	return this._getDate()+"&nbsp;"+LST.symbol;
};

LST.prototype.getDateTime = function()
{
	return this._getDate()+"&nbsp;"+LST.symbol+"&nbsp;"+this._getTime();
}

function LST()
{
	// Optional arguments[0] = GMT time in milliseconds.
	this.seconds = (arguments.length == 0 ? new Date().getTime() : arguments[0]) / 1000 + LST.lunar_epoch_offset;
	var s = Math.floor(this.seconds / LST.lunar_second);

	this.year = Math.floor(s / LST.seconds_per_year);
	s -= this.year * LST.seconds_per_year;

	this.day = Math.floor(s / LST.seconds_per_day);
	s -= this.day * LST.seconds_per_day;

	this.cycle = Math.floor(s / LST.seconds_per_cycle);
	s -= this.cycle * LST.seconds_per_cycle;

	this.hour = Math.floor(s / LST.seconds_per_hour);
	s -= this.hour * LST.seconds_per_hour;

	this.minute = Math.floor(s / LST.seconds_per_minute);
	s -= this.minute * LST.seconds_per_minute;

	this.second = s;
};

