// Timezones array format: Place, hours, minutes time difference from UTC

timezones = Array(Array('UTC', 0, 0), Array('Samoa', -11, 0), Array('Hawaii', -10, 0), Array('Anchorage', -9, 0), Array('Los Angeles', -8, 0),
Array('Denver', -7, 0), Array('New Orleans', -6, 0), Array('New York', -5, 0), Array('Caracas', -4, 0),
Array('Rio de Janeiro', -3, 0), Array('Montevideo', -2, 0), Array('Azoren', -1, 0), Array('London', 0, 0),
Array('Berlin', 1, 0), Array('Athens', 2, 0), Array('Moscow', 3, 0), Array('Abu Dhabi', 4, 0), Array('Karachi', 5, 0),
Array('Colombo', 6, 0), Array('Bangkok', 7, 0), Array('Hong Kong', 8, 0), Array('Tokio', 9, 0), Array('Sydney', 10, 0),
Array('Magadan', 11, 0), Array('Fiji', 12, 0), Array('Bombay', 5, 30));


var clock_tick = 1000; // Clock tick every 1000 msecs (1 second)
var refresh_time = 300000; // Refresh time from server every 5 minutes

//--------------------------------------
// Don't change anything below this line
//--------------------------------------
var NTP_TIME; // NTP time in miliseconds
var ntpdate = new Date(); // NTP Date() Object
var worlddate = new Date(); // World NTP Date() Object
var tid = -1; // setInterval Timer ID
var refr_id = -1; // setInterval for Time Refresh
var clock_refreshing = true;
var localtransmit_msecs = -1;
var localrecv_msecs = -1;
var flashObject;

function getSWF(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
	
    else
    {
        return document[movieName];
    }
}

function changeTime(time)
{
    if(typeof flashObject.changeTime == 'undefined') return;
    flashObject.changeTime(time);
}

function ntpClock(clock_div_id, flashID, ntp_table_id)
{
	 if(typeof(flashID) != 'undefined' && flashID != '')
	 {
		flashObject=getSWF(flashID);
	 }
	 
	if( (typeof(clock_div_id) == 'undefined' || clock_div_id == '') && 
		(typeof(ntp_table_id) == 'undefined' || ntp_table_id == '') ){
		alert("You need to set at least one of the areas for the clock to work.");
		return false;
	}
	
	if(typeof(clock_div_id) != 'undefined' && clock_div_id != '')
		clock_div = document.getElementById(clock_div_id);
	if(typeof(ntp_table_id) != 'undefined' && ntp_table_id != '')
		table_el = document.getElementById(ntp_table_id);
	
	refr_id = window.setInterval('ntpRefresh()', refresh_time);
	localtransmit_msecs = (new Date()).getTime();
	$.getJSON("ntpDB.php?random="+Math.floor(Math.random()*100000000), set_NTP_time);
}


// Set the NTP_TIME variable and start the clock at exactly the begin
// of the next second
// Server returns an array with NTP time in milisecs and time of execution
// of the script in milisecs
function set_NTP_time(val, status)
{
	localrecv_msecs = (new Date()).getTime();
	precise_ntp_time = parseInt(val[0] + (localrecv_msecs-localtransmit_msecs)/2. - val[1]);
	
	NTP_TIME = precise_ntp_time;
	ntpClockTick();
	
	NTP_TIME = parseInt(precise_ntp_time/1000)*1000 + 1000; // Precisely at the next second
	ntpdate.setTime(NTP_TIME);
	var start_ticking = NTP_TIME - precise_ntp_time;
	
	window.setTimeout('ntpStartClock()', start_ticking);
}

function ntpStartClock()
{
	tid = setInterval("ntpClockTick()", clock_tick);
	ntpClockTick();
	clock_refreshing = false;
}


function ntpClockTick()
{
	ntpdate.setTime(NTP_TIME);
	NTP_TIME += clock_tick;
	var hours; var mins; var secs;
	
	if(typeof(flashObject) != 'undefined'){
		hours = ntpdate.getHours();
		mins = ntpdate.getMinutes();
		secs = ntpdate.getSeconds();
	
		if(hours.toString().length == 1) hours = '0'+hours;
		if(mins.toString().length == 1) mins = '0'+mins;
		if(secs.toString().length == 1) secs = '0'+secs;
		
		 changeTime(hours+":"+mins+":"+secs);
	}
	
	
	if(typeof(clock_div) != 'undefined'){
		hours = ntpdate.getHours();
		mins = ntpdate.getMinutes();
		secs = ntpdate.getSeconds();
	
		if(hours.toString().length == 1) hours = '0'+hours;
		if(mins.toString().length == 1) mins = '0'+mins;
		if(secs.toString().length == 1) secs = '0'+secs;
		
		clock_div.innerHTML = hours+':'+mins+':'+secs;
	}
	
	if(typeof(table_el) != 'undefined'){
		table_html = '<table>';
		for(i = 0; i < timezones.length; i++){
			worlddate.setTime(ntpdate.getTime()+ntpdate.getTimezoneOffset()*60000+timezones[i][1]*3600000+timezones[i][2]*60000);
			
			hours = worlddate.getHours();
			mins = worlddate.getMinutes();
			secs = worlddate.getSeconds();
			
			if(hours.toString().length == 1) hours = '0'+hours;
			if(mins.toString().length == 1) mins = '0'+mins;
			if(secs.toString().length == 1) secs = '0'+secs;
			
			var time_diff = ''; hdiff = timezones[i][1]; mdiff = timezones[i][2];
			if(hdiff == 0){ // Only minutes time difference
				if(mdiff == 0) time_diff += '0:00'
				else if(mdiff > 0) time_diff += '+00:'+(mdiff<10?'0':'')+mdiff;
				else if(mdiff < 0) time_diff += '-00:'+(mdiff>-10?'0':'')+Math.abs(mdiff);
			} else { // Hours and possibly minutes time difference
				if(hdiff > 0) time_diff += '+'+hdiff;
				else if(hdiff < 0) time_diff += hdiff;
				
				time_diff += ':'+(Math.abs(mdiff)<10?'0':'')+Math.abs(mdiff);
			}
			
			table_html += '<tr><td>'+timezones[i][0]+'</td><td align="right">'+time_diff+'</td><td>' + hours+':'+mins+':'+secs+'</td></tr>';
		}
		table_html += '</table>';
		table_el.innerHTML = table_html;
	}
}

function ntpRefresh()
{
	// To avoid any race condistions caused by fast clicks on the "Refresh" button
	if(clock_refreshing) return true;
	else clock_refreshing = true;
	
	clearInterval(tid); tid = -1;
	clearInterval(refr_id);
	refr_id = setInterval('ntpRefresh()', refresh_time);
	
	localtransmit_msecs = (new Date()).getTime();
	$.getJSON("ntpDB.php?random="+Math.floor(Math.random()*100000000), set_NTP_time);
	
	return true;
}


