
(function () {
	
	//---------------------------------------------------/ HistoryEntry /-

	var HistoryEntry = function (history) {
		this._history = history;
		this._instance = null;
		
		this._listitem = HTML.createListItem ('entry');
		this._labelanchor = HTML.createAnchor ('', '#');
		this._labelnode = HTML.createTextNode ('');
		this._labelarrow = HTML.createTextNode ('\u00BB '); // alt-175

		HTML.add (this._history.list, HTML.add (this._listitem, this._labelarrow, HTML.add (this._labelanchor, this._labelnode)));
		
		HTML.addOnClickHandler (this._listitem, this, 'onClick');
	};
	
	HistoryEntry.prototype.destroy = function () {
		HTML.remove (this._history.list, this._listitem);
		HTML.removeOnClickHandler (this._listitem);
	};
	
	HistoryEntry.prototype.update = function (instance, repr) {
		HTML.setTextContent (this._labelnode, repr);
		this._instance = instance;
	};
	
	HistoryEntry.prototype.onClick = function (event) {
		if (this._instance) {
			new Request.History (this._history.session, this._instance).send ();
		}
	};




	//--------------------------------------------------------/ History /-

	History = function (session) {
		this.session = session;
		this._entries = [];

		this._history = HTML.getElementById ('WFHistory');
		if (this._history) {
			this.list = HTML.createUnorderedList ('');
			HTML.add (this._history, this.list);
		}
	};
	
	History.prototype.destroy = function () {
		if (this._history) {
			HTML.remove(this._history, this.list);
			while (this._entries.length > 0) {
				this._entries.pop ().destroy ();
			}
		}
	};
	
	History.prototype.startUpdate = function () {
		this._stored = this._entries;
		this._entries = [];
	};
	
	History.prototype.add = function (instance, repr, active) {
		if (this.list) {
			var entry = this._stored.shift () || new HistoryEntry (this);
			entry.update (instance, repr);
			this._entries.push (entry);
		}
	};
	
	History.prototype.endUpdate = function () {
		while (this._stored.length > 0) {
			this._stored.shift ().destroy ();
		}
	};
}) ();

