/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.cometd.timesync"]){
dojo._hasResource["dojox.cometd.timesync"]=true;
dojo.provide("dojox.cometd.timesync");
dojo.require("dojox.cometd._base");
dojox.cometd.timesync=new function(){
this._window=10;
this._lags=[];
this._offsets=[];
this.lag=0;
this.offset=0;
this.samples=0;
this.getServerTime=function(){
return new Date().getTime()+this.offset;
};
this.getServerDate=function(){
return new Date(this.getServerTime());
};
this.setTimeout=function(_1,_2){
var ts=(_2 instanceof Date)?_2.getTime():(0+_2);
var tc=ts-this.offset;
var _5=tc-new Date().getTime();
if(_5<=0){
_5=1;
}
return setTimeout(_1,_5);
};
this._in=function(_6){
var _7=_6.channel;
if(_7&&_7.indexOf("/meta/")==0){
if(_6.ext&&_6.ext.timesync){
var _8=_6.ext.timesync;
var _9=new Date().getTime();
var l=(_9-_8.tc-_8.p)/2-_8.a;
var o=_8.ts-_8.tc-l;
this._lags.push(l);
this._offsets.push(o);
if(this._offsets.length>this._window){
this._offsets.shift();
this._lags.shift();
}
this.samples++;
l=0;
o=0;
for(var i in this._offsets){
l+=this._lags[i];
o+=this._offsets[i];
}
this.offset=parseInt((o/this._offsets.length).toFixed());
this.lag=parseInt((l/this._lags.length).toFixed());
}
}
return _6;
};
this._out=function(_d){
var _e=_d.channel;
if(_e&&_e.indexOf("/meta/")==0){
var _f=new Date().getTime();
if(!_d.ext){
_d.ext={};
}
_d.ext.timesync={tc:_f,l:this.lag,o:this.offset};
}
return _d;
};
};
dojox.cometd._extendInList.push(dojo.hitch(dojox.cometd.timesync,"_in"));
dojox.cometd._extendOutList.push(dojo.hitch(dojox.cometd.timesync,"_out"));
}
