/**
 * Manager für die Navigation
 *
 * @param {Array} contents Liste der Inhalte
 * @author Thomas Scholz
 * @created 10.12.08
 */
function NaviMgr(contents)
{
	/** Typ = kein Inhalt */
	//this.TYP_LEER = 0;
	/** Typ = eigene Seite */
	//this.TYP_SEITE = 1;
	/** Typ = URL */
	//this.TYP_URL = 2;
	/** Typ = Datei */
	//this.TYP_DATEI = 3;
	
	/** Navigationsbaum */
	var tree = null;
	/** Navigationsliste (Baum als flache Struktur) */
	var list = null;
	
	/**
	 * Gibt den Navigationsbaum zurück
	 * (und erzeugt ihn bei Bedarf)
	 *
	 * @return {Object}
	 */
	this.getTree = function()
	{
		if (tree == null)
			tree = createTree(new Node(), contents);
		
		return tree;
	}
	
	/**
	 * Gibt die Navigationsliste zurück
	 * (und erzeugt sie bei Bedarf)
	 *
	 * @return {Array}
	 */
	this.getList = function()
	{
		if (list == null)
			list = this.getTree().asList();
		
		return list;
	}
	
	/**
	 * Erzeugt rekursiv eine Baumstruktur
	 *
	 * @param object $node Node
	 * @param array $contents Liste mit Contentobjekten
	 * @return object mit Kindelementen modifiziertes Node
	 */
	function createTree(node, contents)
	{
		var i, content, childNode;
		var nodeContent = node.getContent();
		
		//foreach ($contents as $content) {
		for (i = 0, ii = contents.length; i < ii; i++) {
			content = contents[i];
			
			// wenn Knoten-lfdNr = 0 (= 1. Ebene) oder gleich der lfdNr des aktuellen Inhalts
			if ((node.isRoot() && content.getLfdNrParent() == 0)
			|| (!node.isRoot() && content.getLfdNrParent() == nodeContent.getLfdNr())) {
	
				// rekursiv die Kindelemente dieses Kindelements ermitteln ...
				childNode = createTree(new Node(content), contents);
				// ... und sich selbst als Kindelement anhängen
				node.addChild(childNode);
			}
		}
		
		return node;
	}
	
	/**
	 * Ermittelt einen Inhalt über eine URL
	 * 
	 * @param {String} file Dateiname
	 * @return {Object}
	 */
	this.findContentByURL = function(file)
	{
		var lfdNr, content;
		
		for (lfdNr in this.getList()) {
			content = this.getList()[lfdNr].getContent();
			
			if (file == content.getTypInhalt())
				return content;
		}
		
		return false;
	}
}
