/**
 * Library for real time
 * @author benjamin.povirk@pop-tv.si, 1.4.2008
 * @package time
 * @depend mootools, ajax time.php
 */

//time vars
var timeInterval = 10; //time interval in seconds
var curTime;
var curTimeLocal;
var sDate = new Object(); 

/**
 * function applyes 0 in front of string, if the string length is 1
 * used for dates, etc
 * @param what as string
 */
function padLen(what){
	var output=(what.toString().length==1)? "0"+what : what;
	return output;
}

/**
 * function displays time in the html objects provided for that
 */
function displaytime(){
	sDate.setSeconds(sDate.getSeconds()+timeInterval);
	var datestring=padLen(sDate.getDate())+"."+padLen(sDate.getMonth()+1)+"."+sDate.getFullYear();
	var timestring=padLen(sDate.getHours())+":"+padLen(sDate.getMinutes());

	try
	{
		if($("datestamp")) $("datestamp").innerHTML=datestring;
	}
	catch (e) { null; }
	try
	{
		if($("timestamp")) $("timestamp").innerHTML=timestring;
	}
	catch (e) { null; }
}

/**
 * function calls displaytime and that way, updates html
 */
function dDate(){
	setInterval("displaytime()", parseInt(timeInterval + "000"));
}

//moo domready fires the real time event
window.addEvent('domready', function() { 
	var rnd = Math.floor(Math.random()*1000000)+1;
	new Ajax('/bin/ajax/time.php?format=r'+'&rnd='+rnd, {
		method: 'get',
		onComplete: function(resp) {
			curTimeLocal = new Date().getTime()/1000;
			curTime = resp;
			sDate = new Date(curTime);
			displaytime();
			dDate();
		}
	}).request();
});