/**
 * Folder span element title attribute messages for expanded and collapsed
 * folders.
 */
var _linkTreeMessages = {
	// folder span element title attribute message for expanded folders
	ulE:'Click here to hide the contents of this folder',
	// folder span element title attribute message for collapsed folders
	ulC:'Click here to view the contents of this folder',
	// folder span element title attribute message for empty folders
	ulX:'This folder is empty'
};

/**
 * Captures the click event of a list item containing a collapsed or
 * expanded folder (child unordered list) in an unordered list generated
 * by LinkTree. Swaps the CSS class on the list item containing
 * the folder (child unordered list) and the child unordered list itself.
 * Also sets the title text on the first span element found under the list
 * item that was clicked on.
 */
function getLinkTreeClickHandler()
{
	return function(span)
	{
		if (!span)
		{
			return;
		}

		var li;

		for (var el = span; (el = el.parentNode);)
		{
			if ('string' == typeof el.tagName &&
				'LI' == el.tagName.toUpperCase())
			{
				li = el;
				break;
			}
		}

		if (!li || !li.getElementsByTagName)
		{
			return;
		}

		var ul = li.getElementsByTagName('ul');

		if (!ul || !(ul = ul[0]) || 'string' != typeof ul.className)
		{
			return;
		}

		if ('ulC' == span.className)
		{
			span.className = li.className = ul.className = 'ulE';
		}
		else if ('ulE' == span.className)
		{				
			span.className = li.className = ul.className = 'ulC';
		}

		var title;

		if ('string' == typeof (title = _linkTreeMessages[span.className]))
		{
			span.title = title;
		}
	}
}

/**
 * Captures the onmouseover event of a span element containing the name of
 * a folder (child unordered list) in an unordered list generated by
 * LinkTree. Ensures the span title text is initialized to the
 * appropriate value for the current state of the folder. Once the span
 * title text has been initialized (or set by a click inside the folder)
 * this event handler detaches itself.
 */
function getLinkTreeMouseOverHandler()
{
	return function(span)
	{
		if (!span)
		{
			return;
		}

		span.onmouseover = null;

		if (span.title)
		{
			return;
		}

		for (var el = span; (el = el.parentNode);)
		{
			if ('string' == typeof el.tagName &&
				'LI' == el.tagName.toUpperCase())
			{
				var title;

				if ('string' == typeof el.className &&
					'string' == typeof (title = _linkTreeMessages[el.className]))
				{
					span.title = title;
				}
				break;
			}
		}
	}
}
