var oTodoInterval = null;
var iLastPollDelay = 0;
var iLastServerTime = 0;

function RefreshBackendTodoCallback( id , ret ) {
	$("#"+id).html( ret.html );
	if (typeof ret.js=="undefined") return;
	if (!ret.js.length) return;
	eval( ret.js );
}

function RefreshBackendTodo( id ) {
	iLastServerTime = 0;
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					inajax : 1,
					ajaxevent : "refresh_todolist",
					last_poll_delay : iLastPollDelay
				}
			),
			success: function(ret) {
				iLastPollDelay = ret.next_poll;
				RefreshBackendTodoCallback( id , ret );
				iLastServerTime = new Date();
				setTimeout( "RefreshBackendTodo( '" + id + "');" , ret.next_poll );
			}
		}
	);
}

function BackendTodoInterval( id ) {
	if (typeof iLastServerTime != "object" ) return;
	var d = new Date();
	var e = d.getTime() - iLastServerTime.getTime();
	if (e<1000) return;
	$("#server_time_elapsed").html( "?" );
}

function InitBackendTodo( id , poll , initjs ) {
	if (poll) oTodoInterval = setTimeout( "RefreshBackendTodo('"+id+"');" , 1000 );
	setInterval( "BackendTodoInterval('"+id+"');" , 500 );
		
	eval( initjs );
}

function StopBackendTodo() {
	if (!oTodoInterval) return;
	// clearInterval( oTodoInterval );
}
