var II_LS_Montor = {

	user :null,
	pass :null,
	hostName :null,
	hostDomain :null,
	port :null,
	engine :null,
	lsPage :null,
	inited :false,

	init : function() {
		if (!this.inited) {
			// create a new push page for Lightstreamer, initialize and start
			// the Lightstreamer engine
			this.lsPage = new PushPage();
		
			this.lsPage.context.setDomain(this.hostDomain);
			this.lsPage.context.setDebugAlertsOnClientError(false);


			this.lsPage.bind();

			this.lsPage.onEngineCreation = this._engineIsUnderCreation.bind(this);
			this.lsPage.onEngineReady = this.engineIsReady.bind(this);
			this.lsPage.onEngineLost = this.engineIsLost.bind(this);
			// this.lsPage.createEngine("MonitorEngine", "/lightstreamer/",
			// "NEW_SESSION", true);
			
                        this.lsPage.setCheckEngineTimeout(20000);
			this.lsPage.createEngine("MonitorEngine", "/lib/js/lightstreamer", "SHARE_SESSION",true);
		
			this.inited = true;
		}
	},

	startStreaming : function() {
	try{
			this.init();
	}catch(e){
		
	}
// document.onkeydown = checkEscape;
	// document.onkeypress = checkEscape;
	// function checkEscape(e) {
	// if(!e) e = event;
	// if(e.keyCode == 27) return false;
	// }

	// /////////////TABLE "Stats"
	var table1 = new OverwriteTable("LS_monitoring", "monitor_all", "MERGE");
	table1.onChangingValues = this.formatStats.bind(this);
	table1.setSnapshotRequired(true);
	table1.setClearOnDisconnected(true);
	table1.setPushedHtmlEnabled(false);

	this.lsPage.addTable(table1, "stats");
},

_engineIsUnderCreation : function(engine) {
	this.engine = engine;
	engine.connection.setLSHost(this.hostName);
	engine.connection.setLSPort(this.port);
        engine.connection.setStreamPort(this.port);

	engine.policy.setIdleTimeout(30000);
	engine.policy.setPollingInterval(5000);
        engine.policy.setTimeoutForReconnect(15000);
        engine.policy.setTimeoutForStalled(15000);
        engine.policy.setBufferedStreamingHandled(true);

	engine.connection.setAdapterName("MONITOR");
	// the authentication parameters, if needed,
	// should be found on the lsengine.html URL;
	// LS_extractParam is defined in lsengine.html
	// var enginePage = window.frames[0];
	engine.connection.setUserName(this.user);
	engine.connection.setPassword(this.pass);
        engine.policy.setTimeoutForReconnect(10000);
        engine.policy.setTimeoutForStalled(10000);

	
	// var user = enginePage.LS_extractParam("user");
	// var password = enginePage.LS_extractParam("password");
	//		
	// engine.connection.setUserName(user);
	// engine.connection.setPassword(password);

},

engineIsReady : function(engine) {
	this.engine = engine;
	engine.changeStatus("STREAMING");
//	this.engine.policy.setMaxBandwidth(1);
},

engineIsLost : function() {
	this.engine = null;
},

clientError : function (errorMex){
	alert(errorMex);
},

formatStats : function(item, obj) {
	if (obj == null) {
		return;
	}
//	obj.setHotTime(1000);
//	obj.setRowStyle("lshot", "lscold");
	for ( var i = 10; i <= 12; i++) {
		try{
			var val = obj.getFormattedValue(i);
			if (val) {
				obj.setFormattedValue(i, monitorFormat(val));
			}
		}catch(err){
			
		}
	}
}

}
