function traverse(generation,className)
{
	var ritem=generation;
	generation.ancestors().each(function(item){
		if(item.hasClassName(className))
		{
			ritem=item;
			throw $break;
		}
	});
	return ritem;
}

Event.observe(window,"load",function(){
	
	$$('.module-head').each(function(item,count){
		
		if(!item.down())
			return;
		var toggle=item.down();
		/* some modules aren't collapsable */
		if(toggle.readAttribute("rel")==null)
			return;
		
		var hidden=toggle.readAttribute("rel")+"-closed";
		/* mouse over for hiddens */
		Event.observe(hidden,"mouseover",function(evt)
		{
            var e=traverse(Event.element(evt),"module");
			e.setStyle({cursor:'pointer'});
		});
		
		/* mouse out for hiddens */
		Event.observe(hidden,"mouseout",function(evt)
		{
            var e=traverse(Event.element(evt),"module");
			e.setStyle({cursor:'default'});
		});
		
		Event.observe(hidden,"click",function(evt)
		{
			var e=traverse(Event.element(evt),"module");
			var module=e.previous().show();
	
			var head=e.previous().previous();
			var rel_id=e.identify().gsub("-closed","")
			var down=head.down();
			
			//change the header
			head.removeClassName('closed-head');
			//change the window icon
			down.addClassName('close');
			down.removeClassName('open');
			//switch the content
			$(rel_id).show();
			e.hide();
			
		});
		
		Event.observe(toggle,"click",function(evt){
			var e=Event.element(evt);
			var rel_id=e.readAttribute("rel");
			var head=e.up();
			
			if(head.hasClassName('closed-head'))
			{
				//change the header
				head.removeClassName('closed-head');
				//change the window icon
				e.addClassName('close');
				e.removeClassName('open');
				//switch the content
				$(rel_id).show();
				$(rel_id+'-closed').hide();
			} else {
				//change the header
				head.addClassName('closed-head');
				//change the window icon
				e.addClassName('open');
				e.removeClassName('close');
				//switch the content
				$(rel_id).hide();
				$(rel_id+'-closed').show();
			}
				
		});
		
	});
	
});
