
//======================================================/ Heartbeat /=
// A Session Manager that sends a signal to the server at a fixed
// interval to notify it that the current session is still active.
//

namespace ('Statelets');
namespace ('Request');
namespace ('Response');


//--------------------------------------------/ Statelets.Heartbeat /-

Statelets.Heartbeat = function () {
	this.statelet = HTML.createDiv ('statelet-heartbeat');
	this.heartbeat = HTML.createDiv ('heartbeat');

	HTML.add (this.statelet, this.heartbeat);
};

Statelets.Heartbeat.prototype = new Statelets.Base ();

Statelets.Heartbeat.prototype.destroy = function () {
};

Statelets.Heartbeat.prototype.getHTMLRoot = function () {
	return this.statelet;
};

Statelets.Heartbeat.prototype.beat = function () {
	HTML.setClass (this.heartbeat, 'heartbeat beat');
};

Statelets.Heartbeat.prototype.unbeat = function () {
	HTML.setClass (this.heartbeat, 'heartbeat');
};


//----------------------------------------------/ Request.Heartbeat /-

Request.Heartbeat = function (session, heartbeat) {
	this.init (session);
	this.heartbeat = heartbeat;
	this.xmlData = XML.element ('heartbeat', null, null, true);
};

Request.Heartbeat.prototype = new Request.Base ();
Request.Heartbeat.prototype.status = 'Sending heartbeat...';

Request.Heartbeat.prototype.send = function () {
	Framework.queue.send (this);
};


//-------------------------------------------/ Response.onHeartbeat /-

Response.onHeartbeat = function (session, request, content) {
	request.heartbeat.onHeartbeat (content);
};

Framework.registerResponse ('6EDB::30::6EDB::3374', Response.onHeartbeat);


//------------------------------------------------------/ Heartbeat /-

Heartbeat = function (session) {
	this.session = session;
	this.statelet = new Statelets.Heartbeat ();
	Framework.addStatelet (this.statelet);
	this.interval = window.setInterval (callback (this, this.onTimer), 30000);
};

Heartbeat.prototype.destroy = function () {
	window.clearInterval (this.interval);
	Framework.removeStatelet (this.statelet);
};

Heartbeat.prototype.onTimer = function () {
	new Request.Heartbeat (this.session, this).send ();
	this.statelet.beat ();
};

Heartbeat.prototype.onHeartbeat = function (content) {
	this.statelet.unbeat ();
};

