
// Override voor de originele statelets om een text-representatie te bieden
// voor de help, authentication en settings statelets. Te gebruiken in
// combinatie met 'textstatelets.css'. Let op! De .js moet toegevoegd worden
// aan de website onder de originele statelets.js. De .css moet de originele
// statelets.css volledig vervangen (dus niet overriden).

namespace ('Statelets');

Framework.addResource ('EN', 'statelets.help', 'Help');
Framework.addResource ('NL', 'statelets.help', 'Help');
Framework.addResource ('EN', 'statelets.login', 'Log in');
Framework.addResource ('NL', 'statelets.login', 'Inloggen');
Framework.addResource ('EN', 'statelets.logout', 'Log out');
Framework.addResource ('NL', 'statelets.logout', 'Uitloggen');
Framework.addResource ('EN', 'statelets.loggedas', 'Logged on as ');
Framework.addResource ('NL', 'statelets.loggedas', 'Ingelogd als ');
Framework.addResource ('EN', 'statelets.settings', 'My settings');
Framework.addResource ('NL', 'statelets.settings', 'Mijn instellingen');

//-------------------------------------------------/ Statelets.Base /-

Statelets.Base = function () {
};

Statelets.Base.prototype = {
	destroy: function () {	},
	getHTMLRoot: function () {	}
};


//-------------------------------------------------/ Statelets.Help /-

Statelets.Help = function () {
	this.statelet = HTML.createSpan('statelet-help');
	this.helpanchor = HTML.createAnchor('', '#');
	HTML.add(
		this.statelet,
		HTML.createTextNode(' | '),
		HTML.add(
			this.helpanchor,
			HTML.createTextNode(Framework.getResource ('statelets.help'))));
	HTML.addOnClickHandler(this.helpanchor, this, 'handleOnClick');
	HTML.hide(this.statelet);
	this.tooltip = null;
	this.text = '';
};

Statelets.Help.prototype = new Statelets.Base ();

Statelets.Help.prototype.destroy = function () {
	if (this.tooltip)
		this.tooltip.destroy ();
	HTML.removeOnClickHandler(this.helpanchor);
};

Statelets.Help.prototype.setText = function (msg) {
	this.text = msg;
	if (msg)
		HTML.show(this.statelet);
	else
		HTML.hide(this.statelet);
};

Statelets.Help.prototype.getHTMLRoot = function () {
	return this.statelet;
};

Statelets.Help.prototype.handleOnClick = function(ev){
	if (!this.tooltip) {
		this.tooltip = new Shards.Tooltip(ev.clientX, ev.clientY, this.text, true);
		this.tooltip.lock();
		this.tooltip.onDestroy = callback(this, function () {
			this.tooltip = null;
		});
	}
};


//---------------------------------------/ Statelets.Authentication /-

Statelets.Authentication = function (session) {
	this.session = session;
	this.authenticated = false;
	this.statelet = HTML.createSpan('statelet-authentication');
	this.logspan = HTML.createAnchor('', '#');
	this.logtext = HTML.createTextNode('');
	this.usertext = HTML.createTextNode('');
	HTML.add(this.statelet, this.usertext, HTML.add (this.logspan, this.logtext));
	this.popup = new Shards.Popup (this.logspan, Shards.Popup.Kinds.Click, Shards.Popup.Anchors.Center, Shards.Popup.Directions.Left + Shards.Popup.Directions.Down);
	this.popup.onOpening = callback (this, this.onPopupOpening);
	this.popup.onContent = callback (this, this.onPopupContent);
	this.popup.onClosed = callback (this, this.onPopupClosed);
};

Statelets.Authentication.prototype = new Statelets.Base ();

Statelets.Authentication.prototype.destroy = function ()  {
	this.popup.destroy ();
};

Statelets.Authentication.prototype.onPopupOpening = function () {
	if (!this.canimpersonate) {
		if (this.authenticated) {
			new Request.LogOff (this.session, true).send ();
		} else {
			new Request.Callback (this.session, callback (this, this.logon)).send ();
		}
		return false;
	}
	return true;
};

Statelets.Authentication.prototype.onPopupContent = function () {
	if (this.impersonatee) {
		this.menu = new Shards.Menu ()
			.addItem ('Log off ' + this.user, callback (this, this.onMenuLogOff))
			.addItem ('End impersonation (' + this.impersonatee + ')', callback (this, this.onMenuEndImpersonate));
	} else if (this.canimpersonate) {
		this.menu = new Shards.Menu ()
			.addItem ('Log off ' + this.user, callback (this, this.onMenuLogOff))
			.addItem ('Impersonate', callback (this, this.onMenuImpersonate));
	}
	return this.menu.element;
};

Statelets.Authentication.prototype.onPopupClosed = function () {
	this.menu.destroy ();
	this.menu = null;
};

Statelets.Authentication.prototype.onMenuLogOff = function () {
	this.popup.close ();
	new Request.LogOff (this.session, true).send ();
};

Statelets.Authentication.prototype.onMenuImpersonate = function () {
	this.popup.close ();
	new Request.Callback (this.session, callback (this, this.impersonate)).send ();
};

Statelets.Authentication.prototype.onMenuEndImpersonate = function () {
	this.popup.close ();
	new Request.LogOff (this.session, false).send ();
};

Statelets.Authentication.prototype.getHTMLRoot = function () {
	return this.statelet;
};

Statelets.Authentication.prototype.openFrame = function (name, registerinfo) {
	var session = this.session;
	session.queue.pause ();
	var onregister = function () {
		return registerinfo;
	};
	var oncomplete = function () {
		session.queue.clear ();
		session.queue.unpause ();
		new Request.Meta (session).send ();
	};
	var oncancel = function(){
		session.queue.unpause ();
	};
	Frames.open (session, name, onregister, oncomplete, oncancel);
};

Statelets.Authentication.prototype.logon = function () {
	if (!this.session.authenticated) {
		this.openFrame ('login', {'newaccount': !!this.newaccount, 'recoverpassword': !!this.recoverpassword });
	}
};

Statelets.Authentication.prototype.impersonate = function () {
	this.openFrame ('impersonate', {});
};

Statelets.Authentication.prototype.setAuthenticated = function (user, impersonatee, canimpersonate, newaccount, recoverpassword) {
	this.authenticated = !!user;
	this.user = user;
	this.impersonatee = impersonatee;
	this.canimpersonate = canimpersonate;
	this.newaccount = newaccount;
	this.recoverpassword = recoverpassword;
	HTML.setTextContent (this.logtext, Framework.getResource (this.authenticated ? 'statelets.logout' : 'statelets.login'));
	HTML.setTextContent (this.usertext, (this.authenticated ? Framework.getResource('statelets.loggedas') + user + (impersonatee ? ' (' + impersonatee + ')': '') + ' | ' : ''));
};



//---------------------------------------------/ Statelets.Settings /-

Statelets.Settings = function (session) {
	this.session = session;
	this.statelet = HTML.createSpan('statelet-settings');
	this.settinganchor = HTML.createAnchor('' ,'#');
	HTML.add(
		this.statelet,
		HTML.createTextNode(' | '),
		HTML.add (
			this.settinganchor,
			HTML.createTextNode (Framework.getResource('statelets.settings'))));
	HTML.addOnClickHandler (this.settinganchor, this, 'onClick');
};

Statelets.Settings.prototype = new Statelets.Base ();

Statelets.Settings.prototype.destroy = function () {
	HTML.removeOnClickHandler (this.statelet);
};

Statelets.Settings.prototype.getHTMLRoot = function () {
	return this.statelet;
};

Statelets.Settings.prototype.onClick = function () {
	new Request.Settings (this.session).send ();
};
