
(function () {
	
	//------------------------------------------------/ NavigationEntry /-

	var NavigationEntry = function (navigation) {
		this._navigation = navigation;
		this._object = null;
		this._active = false;
		
		this._listitem = HTML.createListItem ('entry');
		this._labelanchor = HTML.createAnchor ('', '#');
		this._labelspan = HTML.createSpan ('label');
		this._labelnode = HTML.createTextNode ('');

		HTML.nest (this._navigation.list, this._listitem, this._labelanchor, this._labelspan, this._labelnode);
		
		HTML.addOnClickHandler (this._listitem, this, 'onClick');
	};
	
	NavigationEntry.prototype.destroy = function () {
		HTML.remove (this._navigation.list, this._listitem);
		HTML.removeOnClickHandler (this._listitem);
	};
	
	NavigationEntry.prototype.update = function (object, repr, active) {
		HTML.setClass (this._listitem, (active ? 'activeentry' : 'entry'));
		HTML.setTextContent (this._labelnode, repr);
		this._object = object;
		this._active = active;
	};
	
	NavigationEntry.prototype.onClick = function (event) {
		if (!this._active && this._object) {
			new Request.Navigate (this._navigation.session, this._object).send ();
		}
	};




	//-----------------------------------------------------/ Navigation /-

	Navigation = function (session) {
		this.session = session;
		this._entries = [];

		this._navigation = HTML.getElementById ('WFNavigation');
		if (this._navigation) {
			this.list = HTML.createUnorderedList ('');
			HTML.add (this._navigation, this.list);
		}
	};
	
	Navigation.prototype.destroy = function () {
		if (this._navigation) {
			HTML.remove(this._navigation, this.list);
			while (this._entries.length > 0) {
				this._entries.pop ().destroy ();
			}
		}
	};
	
	Navigation.prototype.startUpdate = function () {
		this._stored = this._entries;
		this._entries = [];
	};
	
	Navigation.prototype.add = function (object, repr, active) {
		if (this.list) {
			var entry = this._stored.shift () || new NavigationEntry (this);
			entry.update (object, repr, active);
			this._entries.push (entry);
		}
	};
	
	Navigation.prototype.endUpdate = function () {
		while (this._stored.length > 0) {
			this._stored.shift ().destroy ();
		}
	};
	
	Navigation.prototype.hide = function (hidden) {
		if (this._navigation) {
			if (hidden)
				HTML.hide(this._navigation);
			else
				HTML.show(this._navigation);
		}
	};
}) ();

