Event.observe(window, 'load', init);

function init()
{
	var uls = $$('ul.tree');
	
	uls.each(
	
		function(o)
		{
			o.hide();
		}
	);
	
	var links = $$('a.treecollection');
	
	links.each(
	
		function (o)
		{					
			var el = o.up('li').next('ul');
					
			//if (el != null)
			//{			
				var id = o.id.split('_')[1];
				
				var a = $(document.createElement('a'));
				a.id = 'expand_' + id;	
				a.addClassName('treeExpand');
				a.title = 'Expand this level';
				
				var img = $(document.createElement('img'));
				img.src = '/images/icons/expand.gif';
				
				a.appendChild(img);
				
				o.up('li').insertBefore(a, o.up('li').firstChild);	
										
				Event.observe(a, 'click', showLevel);
			//}
			//else
			//{
			//	alert ('f');
			//}
		}		
	);
	
	setTall();
}		

function showLevel(e)
{						
	var el = Event.element(e);
	
	if (el.tagName.toLowerCase() == 'img')
	{
		el = el.up('a');
	}				
	
	var id = el.id.split('_')[1];

	var ul = $('level_' + id);
	
	if (ul != null)
	{
		if (ul.visible())
		{
			//Effect.BlindUp (ul, { duration:0.2 });
			ul.hide();
			el.title = 'Expand this level';
			el.down('img').src = '/images/icons/expand.gif';
		}
		else
		{			
			ul.show();	
			//Effect.BlindDown (ul, { duration:0.2 });
			el.title = 'Collapse this level';
			el.down('img').src = '/images/icons/collapse.gif';
		}
	}
	else
	{	
		if (el.title == 'Expand this level')
		{
			el.title = 'Collapse this level';
			el.down('img').src = '/images/icons/collapse.gif';
		}
		else
		{
			el.title = 'Expand this level';
			el.down('img').src = '/images/icons/expand.gif';
		}
	}
	
	setTall();
}

