function start_nav ()
{
	var list = get_children(get_child('nav', 'UL'), 'LI');
	
	for (var i = 0; i < list.length; i++)
	{
		list[i].pleasechange = true;
		add_event(list[i], 'mouseover', entering);
		add_event(list[i], 'mouseout', leaving);
		
		if (get_child(list[i], 'UL'))
		{
			list[i].isparent = true;
		}
	}

	list = get_descendants('nav', 'a')
	
	for (var i = 0; i < list.length; i++)
	{
		add_event(list[i], 'focus', entering);
		add_event(list[i], 'blur', leaving);
	}

	add_event(window, 'click', nav_all_close);
}

function nav_open (obj)
{
	if (current_nav != obj)
	{
		nav_close();
		add_class(obj, 'hover');
		current_nav = obj;
	}
}

function nav_close ()
{
	if (current_nav)
	{
		remove_class(current_nav, 'hover');
		current_nav = null;
	}
}

function entering (e)
{
	current_obj(this);
}

function leaving (e)
{
	var current;
	
	if (e.relatedTarget)
	{
		current = e.relatedTarget;
	}
	else if (e.toElement)
	{
		current = e.toElement;
	}
	else
	{
		current = window;
	}
	
	current_obj(current);
}

function nav_all_close ()
{
	current_obj(window, true);
}

function current_obj (obj, doitnow)
{
	while (obj && (! obj.pleasechange))
	{
		obj = obj.parentNode;
	}

	window.clearTimeout(nav_timeout);

	if (obj && obj.pleasechange)
	{
		nav_open(obj);
	}
	else
	{
		if (current_nav && current_nav.isparent && (! doitnow))
		{
			nav_timeout = window.setTimeout("nav_close();", 500);
		}
		else
		{
			nav_close();
		}
	}
}

var current_nav = null;
var nav_timeout = null;

add_load_event(start_nav);
