
//=========================================================/ Session /=

Session = function (id, queue, newaccount, recoverpassword) {
	this.id = id;
	this.queue = queue;
	this.newaccount = newaccount;
	this.recoverpassword = recoverpassword;

	this.onDestroy = new EventHook ();

	this.dialogStack = [];
	this.authenticated = false;

	this.settings = null;
	this.authentication = new Statelets.Authentication (this);
	Framework.addStatelet (this.authentication);

	this.help = new Statelets.Help ();
	Framework.addStatelet (this.help);

	this.heartbeat = new Heartbeat (this);
	this.navigation = new Navigation (this);
	this.history = new History (this);

	var root = HTML.getElementById('WebFramework');
	this.dialogStack.push (Dialogs.createRootDialog (this, root));

	var qval = QueryValueList ();
	new Request.InitSession (this, qval['entrypoint']).send ();
};

Session.prototype.destroy = function (unloading) {
	this.onDestroy.notify (this);

	while (this.dialogStack.length > 0) {
		var dialog = this.dialogStack.pop ();
		dialog.destroy (unloading);
	}
	
	Framework.removeStatelet (this.help);
	this.help.destroy ();

	Framework.removeStatelet (this.authentication);
	this.authentication.destroy ();

	if (this.settings) {
		Framework.removeStatelet (this.settings);
		this.settings.destroy ();
	}

	this.heartbeat.destroy ();
	this.navigation.destroy ();
	this.history.destroy ();
};

Session.prototype.setHelp = function (msg) {
	this.help.setText (msg);
};

Session.prototype.setAuthenticated = function (user, impersonatee, canimpersonate) {
	this.authenticated = !!user;
	this.authentication.setAuthenticated (user, impersonatee, canimpersonate);
	if (this.authenticated && !this.settings) {
		this.settings = new Statelets.Settings (this);
		Framework.addStatelet (this.settings);
	}
	if (!this.authenticated && this.settings) {
		Framework.removeStatelet (this.settings);
		this.settings.destroy ();
		this.settings = null;
	}
};

Session.prototype.calcFileManUrl = function (up) {
	qs = {httphandlercall: 'WFFileManager', object: up, session: this.id};
	return httphandler + toQueryString (qs);
};

Session.prototype.getActiveDialog = function () {
	var dialog = this.dialogStack.pop ();
	if (dialog) {
		this.dialogStack.push (dialog);
	}
	return dialog;
};

Session.prototype.createModalDialog = function () {
	var dialog = this.dialogStack.pop ();
	if (dialog) {
		this.dialogStack.push (dialog);
		dialog.deactivate ();
	}
	this.dialogStack.push (Dialogs.createModalDialog (this));
};

Session.prototype.closeModalDialog = function () {
	var dialog = this.dialogStack.pop ();
	if (dialog) {
		dialog.destroy ();
	}

	dialog = this.dialogStack.pop ();
	if (dialog) {
		this.dialogStack.push (dialog);
		dialog.activate ();
	}
};

Session.prototype.isModal = function () {
	return (this.dialogStack.length > 1);
};

Session.prototype.updateHistory = function (data) {
	this.history.startUpdate ();
	for (var idx in data) {
		this.history.add (data[idx].instance, data[idx].repr);
	}
	this.history.endUpdate ();
};

Session.prototype.updateNavigation = function (data) {
	var tabs = data.tabs;
	this.navigation.startUpdate ();
	if (tabs) {
		for (var idx in tabs) {
			this.navigation.add(tabs[idx].object, tabs[idx].repr, (tabs[idx].object == data.activetab));
		}
	}
	this.navigation.endUpdate ();
	this.navigation.hide(data.hidden == 'true');
};

