/**
 * Live Time
 * 
 * This script works when you add a following <SPAN> element to your PHP-code:
 * <span title="<?= date ('d-m-Y H:i:s T') ?>" id="livetime"><?= date ('d-m-Y H:i T') ?></span>
 * 
 * Besides that SPAN element, the only requirement is to include this script in your HEAD-tag like this:
 * <head>
 *  <script type="text/javascript" src="js/livetime.js"></script>
 * </head>
 * The script will bind to all objects and events, no initialisation is required.
 * 
 * You may freely use this file, but please leave all comments, including the authors name, in it.
 * 
 * @author Stefan Thoolen <mail@stefanthoolen.nl>
 */

function livetime_init (obj) {
	/**
	 * Fetches both date and time from the title-tag
	 * Afterwards it clears the title-tag
	 */
	var obj = document.getElementById ('livetime');
	if(obj == null) return false;
	var date = obj.title.split (' ')[0];
	var time = obj.title.split (' ')[1];
	timezone_label = obj.title.split (' ')[2];
	obj.title = '';
	/**
	 * Places the date and time into a date object
	 */
	server_datetime.setDate (parseInt (date.split('-')[0], 10));
	server_datetime.setMonth (parseInt (date.split('-')[1], 10) - 1);
	server_datetime.setFullYear (parseInt (date.split('-')[2], 10));
	server_datetime.setHours (parseInt (time.split(':')[0], 10));
	server_datetime.setMinutes (parseInt (time.split(':')[1], 10));
	server_datetime.setSeconds (parseInt (time.split(':')[2], 10));
	
	// Stores the difference between client and server
	server_client_difference = client_datetime.getTime() - server_datetime.getTime();
	
	// Places the date/time in the livetime object
	livetime_update ();
}

function livetime_update () {
	/**
	 * Gets the current date and time and recalculates the server date and time
	 */
	var current_client_datetime = new Date ();
	var current_server_datetime = new Date ();
	current_server_datetime.setTime (current_client_datetime.getTime () - server_client_difference);
	
	/**
	 * Generates a nice human-readeable string and places the new value into the livetime object
	 */
	var str = new String ();
	str += current_server_datetime.getHours().zerofill(2);
	str += ':' + current_server_datetime.getMinutes().zerofill(2);
	str += ':' + current_server_datetime.getSeconds().zerofill(2);

	var obj = document.getElementById ('livetime');
	obj.innerHTML = str;
	// Lets do this again in one second, shall we? ;-)
	window.setTimeout (livetime_update, 1000);
}

var server_datetime = new Date ();
var client_datetime = new Date ();
var server_client_difference = new Number ();
var timezone_label = new String ();

/**
 * Returns the number as string and fills up with zeros until the string reaches a certain length.
 * @param	int		length	The length of the string
 * @return	string			A string of at least {length} characters
 */
String.prototype.zerofill = function (length, character) {
	if(typeof character == 'undefined') var character = '0';
	var str = new String (this);
	for (var i = str.length; i < length; ++i) str = character + str;
	return str;
}

/**
 * Returns the number as string and fills up with zeros until the string reaches a certain length.
 * @param	int		length	The length of the string
 * @return	string			A string of at least {length} characters
 */
Number.prototype.zerofill = function (length, character) {
	return this.toString().zerofill(length, character);
}

