function TreeNav()
{
	this.disabled = false;
}

TreeNav.prototype.rootElement;
TreeNav.prototype.rootNodes;
TreeNav.prototype.disabled;
TreeNav.prototype.names;
TreeNav.prototype.lastSelectedUrl;
TreeNav.prototype.nav;
TreeNav.prototype.split;
TreeNav.prototype.g_firstlink;
TreeNav.prototype.g_nextlink;
TreeNav.prototype.g_prevlink;
TreeNav.prototype.g_lastlink;
TreeNav.prototype.closedToggler;
TreeNav.prototype.openToggler;
TreeNav.prototype.hiddenToggler;
TreeNav.prototype.nodeCreatedCallback;
TreeNav.prototype.selectionChanged;


TreeNav.prototype.initialise = function() 
{
	var tree = this;
	
	this.lastSelectedUrl = null
	
	this.nav = true;
	this.split = false;
	this.g_firstlink = null;
	this.g_nextlink = null;
	this.g_prevlink = null;
	this.g_lastlink = null;
	
	this.closedToggler = "images/folderclosed.gif";
	this.openToggler = "images/folderopen.gif";
	this.hiddenToggler = "images/hiddentoggle.gif";
	
	this.rootNodes = [];
}


TreeNav.prototype.nodeCreatedCallback = function(node)
{
	if (!node.details.url) 
	{
		node.link.removeAttr("href");
		node.label.filter("a").hide();
		node.link.unbind("click");
	} 
	else 
	{
		node.label.filter("span").hide();
	}
}


TreeNav.prototype.selectionChangedCallback = function(old)
{
	if (this.selected) 
	{
		this.lastSelectedUrl = this.selected.details.url;
		$(".content-base").attr("src", this.lastSelectedUrl);			
	}
	return false;
}


TreeNav.prototype.select = function(node)
{
	if (node && this.selected != node) 
	{
		TreeLib.clickNode(this, node);
		TreeLib.ensureVisible(this, node);
	}
	this.updateNavigation();
	return false;
}


TreeNav.prototype.setupTopNav = function(nextlink, prevlink, firstlink, lastlink) 
{
	var tree = this;
	
	this.g_firstlink = firstlink;
	this.g_nextlink = nextlink;
	this.g_prevlink = prevlink;
	this.g_lastlink = lastlink;
	
	nextlink.bind("click", function (event) 
	{
		return tree.select(tree.findNext(tree.selected, false, true));
	});
	
	prevlink.bind("click", function (event) 
	{
		return tree.select(tree.findPrev(tree.selected, false));
	});
	
	firstlink.bind("click", function (event) 
	{
		return tree.select(tree.findNext(null, false, true));
	});
	
	lastlink.bind("click", function (event) 
	{
		return tree.select(tree.findPrev(null, false));
	});
	
	this.updateNavigation();
}


TreeNav.prototype.findNext = function(current, checkthis, intochild) 
{
	if (current == null) 
	{
		current = this.rootNodes[0];
		checkthis = true;
		intochild = true;
	}
	if (checkthis && current.details.url) 
	{
		return current;
	}
	if (intochild && current.children.length > 0) 
	{
		return this.findNext(current.children[0], true, true);
	}
	
	var ind = current.childIndex + 1;
	var parentList = TreeLib.getSiblings(this, current);
	
	if (ind >= parentList.length) 
	{
		if (!current.parent) 
		{
			return null;
		}
		return this.findNext(current.parent, false, false);
	}
	return this.findNext(parentList[ind], true, true);
}


TreeNav.prototype.findPrev = function(current, checkthis) 
{
	if (current == null) 
	{
		current = this.rootNodes[this.rootNodes.length - 1];
		while (current.children.length > 0) 
		{
			current = current.children[current.children.length - 1];
		}
		checkthis = true;
	}
	
	if (checkthis && current.details.url) 
	{
		return current;
	}
	
	var ind = current.childIndex - 1;
	var parentList = TreeLib.getSiblings(this, current);
	if (ind < 0) 
	{
		if (!current.parent) 
		{
			return null;
		}
		return this.findPrev(current.parent, true);
	}
	
	current = parentList[ind];
	while (current.children.length > 0) 
	{
		current = current.children[current.children.length - 1];
	}
	return this.findPrev(current, true);
}


TreeNav.prototype.updateNavigation = function() 
{
	if (!this.g_nextlink || !this.g_prevlink) 
	{
		return;
	}
	
	if (this.findNext(this.selected, false)) 
	{
		this.g_nextlink.removeClass("disabled");
		this.g_lastlink.removeClass("disabled");
	} 
	else 
	{
		this.g_nextlink.addClass("disabled");
		this.g_lastlink.addClass("disabled");
	}
	
	if (this.findPrev(this.selected, false)) 
	{
		this.g_prevlink.removeClass("disabled");
		this.g_firstlink.removeClass("disabled");
	} 
	else 
	{
		this.g_prevlink.addClass("disabled");
		this.g_firstlink.addClass("disabled");
	}
}






function initTreeNav(treedef) 
{	
	var tree = new TreeNav();
	
	tree.rootElement = $("#root");
	tree.names = {open:"open", selected:"sel"};
	
	tree.initialise();
	
	TreeLib.addNodes(tree, null, treedef);
	
	tree.updateNavigation();
	tree.setupTopNav($("#next"), $("#prev"), $("#first"), $("#last"));
	tree.select(tree.findNext(null, false, false));
	
	return tree;
}

