/**
 *	clock.js -> http://locusonus.org/
 *
 *	date format syntaxe (like PHP Date object parameters)
 *	-----------------------------------------------------
 *
 *	day:
 * 		d => day of the month, 2 digits with leading zeros (01 to 31)
 *		D => a textual representation of a day, three letters (Sun-Sat)
 *		j => day of the month without leading zeros (1-31)
 *		l => (lowercase 'L') a full textual representation of the day of the week (Sunday-Saturday)
 
 *	month:
 *		F => a full textual representation of a month (January-December)
 *		m => numeric representation of a month, with leading zeros (01-12)
 *		M => a short textual representation of a month, three letters (Jan-Dec)
 *		n => numeric representation of a month, without leading zeros (1-12)
 
 *	year :
 *		Y => a full numeric representation of a year, 4 digits (1999 or 2003)
 *		y => a two digit representation of a year (99 or 03)
 *
 *	time:
 *		a => lowercase Ante meridiem and Post meridiem (am or pm)
 *		A => uppercase Ante meridiem and Post meridiem (AM or PM)
 *		g => 12-hour format of an hour without leading zeros (1-12)
 *		G => 24-hour format of an hour without leading zeros (0-23)
 *		h => 12-hour format of an hour with leading zeros (01-12)
 *		H => 24-hour format of an hour with leading zeros (00-23)
 *		m => minutes with leading zeros (00 to 59)
 *		s => seconds, with leading zeros (00-59)
 *		B => milliseconds (000-999)
 *
 *	timezone:
 *		O => difference to Greenwich time (GMT) in hours (+0200)
 *		Z => timezone offset in seconds.
 */


/**
 * Returns the string representation of the GMT time zone offset in hours
 *
 * @param int		the offset value (in minutes)
 * @return String
 */
Date.prototype.getTimezoneOffsetString = function( min ) {
	
	/-?(.*)/.exec( min.toString() );
	
	var tzoh = RegExp.$1;
	while( tzoh.length<2 ) tzoh  = "0"+tzoh;
	while( tzoh.length<4 ) tzoh += "0";
	
	return	( min<0 ? "-" : "+" )+tzoh;
}

/**
 * Fix the GMT time zone offset in hours
 *
 * @param int		the offset value (in minutes)
 * @return String
 */
Date.prototype.toGMT = function( tz_offset ) {
	
	/(.*)GMT.*/.exec( this.toString() );
	
	return	RegExp.$1+" GMT"+this.getTimezoneOffsetString( tz_offset );
}

/**
 * Returns a string formatted according to the given format string.
 *
 * @param String	the date format
 * @return String
 */
Date.prototype.format = function( format ) {
	
	var months	= {
			full : ['January','February','March','April','May','June','July','August','September',
					'October','November','December'],
			abbr : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
		};
		
	var days	= {
			full : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
			abbr : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
		};
		
	
	var out  = "";
	var mon  = this.getMonth();
	var date = this.getDate();
	var day  = this.getDay();
	var hour = this.getHours();
	var min	 = this.getMinutes();
	var sec  = this.getSeconds();
	var ms   = this.getMilliseconds();
	var tzo	 = this.getTimezoneOffset();
	
	
	for( var i=0; i<format.length; i++ ) {
		var c = format.charAt(i);
		switch( c ) {
			case 'd' : out += c2d(date); break;
			case 'D' : out += days.abbr[day]; break;
			case 'j' : out += date; break;
			case 'l' : out += days.full[day]; break;
			case 'F' : out += months.full[mon]; break;
			case 'm' : out += c2d(mon+1); break;
			case 'M' : out += months.abbr[mon]; break;
			case 'n' : out += mon+1; break;
			case 'Y' : out += this.getFullYear(); break;
			case 'y' : out += this.getYear(); break;
			case 'a' : out += hour>11 ? "pm":"am"; break;
			case 'A' : out += hour>11 ? "PM":"AM"; break;
			case 'g' : out += hour==0 ? 12 : (hour>12?hour-12:hour); break;
			case 'G' : out += hour; break;
			case 'h' : out += hour==0 ? 12 : (hour>12?c2d(hour-12):c2d(hour)); break;
			case 'H' : out += c2d(hour); break;
			case 'i' : out += c2d(min); break;
			case 's' : out += c2d(sec); break;
			case 'B' : out += c3d(ms); break;
			case 'O' : out += this.getTimezoneOffsetString(-tzo/60); break;
			case 'Z' : out += tzo*3600; break;
			// otherwise
			default: out += c; break;
		}
	}
	
	// format number two/three digits ?
	function c2d(x) { return x<10?"0"+x:x; }
	function c3d(x) { return x<10?"00"+x:(x<100?"0"+x:x); }
	
	// result
	return out;
}

/**
 * Display the clock… around the world
 *
 * @param Element	the target element
 * @param int		[ the clock's time zone offset (from GMT) ]
 * @param String	[ the date format ]
 * @param boolean	[ TRUE while require milliseconds ]
 */
function Clock( elem, offset, format, millis ) {

	if ( !offset ) offset = 0;
	
	setInterval(
		function() {
			var lt = new Date();
			var ms = lt.getTime()+(lt.getTimezoneOffset()*60000)+((offset/3600)*3600000);
			var d  = new Date(ms);
			document.getElementById( elem ).innerHTML =
				!format ? d.toGMT( offset/3600 ) : d.format( format );
		},
		millis ? 1 : 1000
	);
	
}

