var FAQ = {};
FAQ.elementRelation = {PARENT: 1,PREVIOUS_SIBLING: 2,NEXT_SIBLING: 3,CHILD: 4};
FAQ.config = {event: 'click',root: {tagName: 'ul',className: 'contenu-detail'},reactiveElement: {tagName: 'li'},targetElement: {tagName: 'li'},elementRelation: FAQ.elementRelation.PARENT,activeElement: {className: 'active'},toggleActive: false};
FAQ.panelCollection = [];
FAQ.reactiveCollection = [];

FAQ.panelToggle = function(ev)
{
	var currentPanel;
	
	if(FAQ.config['elementRelation'] == FAQ.elementRelation.PARENT)
	{
		currentPanel = Event.findElement(ev,FAQ.config['reactiveElement']['tagName']);
	}
	else if(FAQ.config['elementRelation'] == FAQ.elementRelation.NEXT_SIBLING)
	{
		currentPanel = Event.element(ev).next(FAQ.config['targetElement']['tagName'],0);
	}
	else if(FAQ.config['elementRelation'] == FAQ.elementRelation.PREVIOUS_SIBLING)
	{
		currentPanel = Event.element(ev).previous(FAQ.config['targetElement']['tagName'],0);
	}
	
	if(Object.isInitialized(currentPanel))
	{
		if(Object.isInitialized(currentPanel.parentNode))
		{
			/* On vérifie que l'élément est un enfant de la liste et non un descendant de niveau inférieur */
			if(Element.hasClassName(currentPanel.parentNode,FAQ.config['root']['className']))//currentPanel.parentNode.className == FAQ.config['root']['className'])
			{
				FAQ.panelCollection.each(function(elem)
				{
					if(currentPanel == elem)
					{
						if(FAQ.config['toggleActive'])
						{
							if(elem.hasClassName(FAQ.config['activeElement']['className']))
							{
								elem.removeClassName(FAQ.config['activeElement']['className']);
							}
							else
							{
								elem.addClassName(FAQ.config['activeElement']['className']);
							}
						}
						else
						{
							elem.addClassName(FAQ.config['activeElement']['className']);
						}
					}
					else
					{
						elem.removeClassName(FAQ.config['activeElement']['className']);
					}
				}
				);
			}
		}
	}
}

FAQ.init = function()
{
	if(Object.isInitialized(window.FAQ_CONFIG))
	{
		for(prop in FAQ.config)
		{
			if(Object.isInitialized(FAQ_CONFIG[prop]))
			{
				if((typeof FAQ.config[prop]) == 'object')
				{
					if((typeof FAQ_CONFIG[prop]) == 'object')
					{
						for(subprop in FAQ.config[prop])
						{
							if(Object.isInitialized(FAQ_CONFIG[prop][subprop]))
							{
								FAQ.config[prop][subprop] = FAQ_CONFIG[prop][subprop];
							}
						}
					}
				}
				else
				{
					FAQ.config[prop] = FAQ_CONFIG[prop];
				}
			}
		}
	}
	
	FAQ.panelCollection = Element.select(document,FAQ.config['root']['tagName']+'.'+FAQ.config['root']['className']+' > '+FAQ.config['targetElement']['tagName']);
	FAQ.reactiveCollection = Element.select(document,FAQ.config['root']['tagName']+'.'+FAQ.config['root']['className']+' > '+FAQ.config['reactiveElement']['tagName']);
	
	FAQ.reactiveCollection.each(function(elem)
	{
		elem.observe(FAQ.config['event'],FAQ.panelToggle);
	}
	);
}

if(window.Prototype)
{
	document.observe('dom:loaded',FAQ.init);
}