/**
 * (c) 2005 Primary Brokers S.A. - http://www.primary.com.ar/
 */

	function Clock(divid) {
		this.divid=divid;
		this.id="obj"+divid;
		//this.format="yyyy/MM/dd hh:mm";
		this.format="hh:mm";
		this.last=0;
		this.millis=0;
		this.debug=0;

	this.generate=function() {
		eventHandler.addListener(this);
		this.setDate(top.x.lastServerTime);
		this.millis=top.x.lastServerTime;
		this.update();
		this.log("generado");
	};

	this.log=function(msg) {
		try{
			if (this.debug > 0) {
				pstatus("Clock: " + msg);
			}
		} catch(err) {}
	};	

	this.update=function() {
		this.millis +=1000;
		this.setDate(this.millis);
	};	
	// onData method called by pushlet frame
	this.onData=function (pushletEvent) {
		try {
			//this.log("event para el clock=" + pushletEvent);
			//this.setDate(pushletEvent.get("p_time") * 1000);
		} catch (err) {
			this.log("EXCEPTION=" + err);
		}
	};
	
	this.setDate=function(millis) {
		try{
			this.log("millis = " + millis + " last"  +this.last );
			if (isNaN(millis)){
				this.log("millis is NaN");
			}else{
				var diff=millis - this.last;
				this.log("diff=" + diff);
				if (diff >= 1000) {
					var mydiv = getRawObject(this.divid);
					layerWrite(mydiv,formatDate(new Date(millis),this.format)+" (hora server)");	
				} else { 
					this.log("diff=" + diff);
				}
				this.last=millis;
			}
		}catch (err){
			this.log("hubo un error no se actualizo el clock: " + err);
		}
	};
}

