function ViewTreeManager(id) {	
	this.id = id;
	this.path = new Array(id);
	
	this.toggleTreeNode = function(id) {
		id = this.id + '' + id;
		var path = this.getPath(id);
		this.togglePath(path);
	};
	
	this.showHide = function(id) {
		var e = dojo.byId(id);
		var t = dojo.byId(id + '_Twisty');
		
		if(e != null) {
			var currentStyle = dojo.style(e, 'display');
			if(currentStyle == 'none' || currentStyle == '') {
				dojo.style(e, 'display', 'block');
				dojo.removeClass(t, 'twistyClosed');
				dojo.addClass(t, 'twistyOpened');
			} else {
				dojo.style(e, 'display', 'none');
				dojo.removeClass(t, 'twistyOpened');
				dojo.addClass(t, 'twistyClosed');
				this.path.pop();
			}
		} else {
			console.log('Error in ViewTreeManager: Element "',id,'" not found');
		}
	};
	
	this.getParentNode = function(node) {
		if(node !=null) {
			var parent = node.parentNode;
			
			// Because of HTML elements, we need the Grandparent, we will go one parent up.
			if(parent != null) {
				parent = parent.parentNode;
			} else {
				return;
			}
			
			if(parent.tagName == 'UL') {
				return parent;
			} else {
				return;
			}
		} else {
			return;
		}
	};
	
	this.getPath = function(id) {
		var path = new Array();
		path[0] = id;
		
		var node = dojo.byId(id);
		var parentNode = this.getParentNode(node);
		while(parentNode != null) {
			path[path.length] = parentNode.id;
			parentNode = this.getParentNode(parentNode);
		}
		path.reverse();
		
		return path;
	};
	
	/* return true if the node was already open */
	this.togglePath = function(path) {
		var currentPath = this.path;
		var openStart = 0;
		
		// close previous path
		for (i = currentPath.length - 1; i >= 0; i--) {
			if(path[i] != currentPath[i]) {
				this.showHide(currentPath[i]);
			} else {
				openStart = i + 1;
				i = -1;
			}
		}
		
		this.path = path;
		
		// open new path
		for (i = openStart; i < path.length; i++) {
			this.showHide(path[i]);
		}
		
		// to close the node when the user clicks on the current opened node
		if(openStart == path.length) {
			var id = path[path.length-1];
			this.showHide(id);
			
			return true;
		} else {
			return false;
		}
	};
}
