var globActualTime;
var globSeconds=0;
var globMinutes=0;
var globHours=0;
var globYear = 0;
var globMonth = 0;
var globDay = 0;
var myTimeInterval;
var globStarter;


function loadServerTime(){
	window.clearInterval(myTimeInterval);
	var now = new Date();
	var nowSec = now.getTime();
	globStarter = nowSec;
	var url = "http://freeshell.de/~treaki/uhrzeit/time.php"
	var params = "";
	var myAjax = new Ajax.Request(
	   url,
	       {
	                method: "GET",
	                parameters: params,
	                onComplete: updateTime
	       });	
}	

function updateTime(xnr){
	var now = new Date();
	var nowSec = now.getTime();
	var globAfterResponse = nowSec;
	var difference = globAfterResponse - globStarter;
	difference = difference / 2;
	globActualTime = (xnr.responseText*1000) - difference;
	var tmpDate = new Date(globActualTime);
	globHours = tmpDate.getHours();
	globMinutes = tmpDate.getMinutes();
	globSeconds = tmpDate.getSeconds();
	globDay = tmpDate.getDate();
	if(globDay < 10 ){ globDay = "0" + globDay;}
	globMonth = tmpDate.getMonth() + 1;
	if(globMonth < 10 ){ globMonth = "0" + globMonth;}
	globYear = tmpDate.getYear() - 100;
	if(globYear < 10 ){ globYear = "0" + globYear;}
	if(globYear > 100){
		globYear += "";
		globYear = globYear.substr(2,2);
	}

	myTimeInterval = window.setInterval("updateZeit()", 1000);
//	$('Control').innerHTML = "Starter:" + globStarter + " now:" + globAfterResponse + "server" + xnr.responseText + "used" + globActualTime;
}	


function updateZeit() {
	globSeconds ++;
	if(globSeconds == 60){
		globSeconds = 0;
		globMinutes++;
		if(globMinutes == 60){
			globHours++;
			globMinutes = 0;
			if(globHours == 24){
				globHours=0;
			}
		}
	}
	var tmpGlobHours = globHours;
	var tmpGlobMinutes = globMinutes;
	var tmpGlobSeconds = globSeconds;
	
	if(tmpGlobHours < 10){
		tmpGlobHours = "0" + tmpGlobHours;
	}
	if(tmpGlobMinutes < 10){
		tmpGlobMinutes = "0" + tmpGlobMinutes;
	}
	if(tmpGlobSeconds < 10){
		tmpGlobSeconds = "0" + tmpGlobSeconds;
	}
	
	$('TimeDiv').innerHTML =  globDay + "." + globMonth + "." + globYear + " - " + tmpGlobHours + ":" + tmpGlobMinutes + ":" + tmpGlobSeconds;
	
	
}
loadServerTime();
window.setInterval("loadServerTime()", 60000);



