
Event.observe(window, 'load', function(){
	registerChecklist('candidatheque_secteur_activite-checklist');
	registerChecklist('candidatheque_pays-checklist');
	registerChecklist('candidatheque_grande_fonction-checklist');

	registerChecklist('candidatheque_region-checklist');
	registerChecklist('candidatheque_type_formation-checklist');

	registerChecklist('candidatheque_pays_sejours-checklist');
});

function registerChecklist(list)
{
	$$('#' + list + ' .checklist-select .checklist-select-control').first().observe('click', function(){
		optionsElt = $$('#' + list + ' .checklist-options').first();
		if(Element.visible(optionsElt))
		{
			hideList(optionsElt);
		}
		else
		{
			showList(optionsElt);
		}
	});

	// Evenement sur bouton "appliquer"
	$(list + '-apply').observe('click', function(){
		optionsElt = $$('#' + list + ' .checklist-options').first();
		if(Element.visible(optionsElt))
		{
			hideList(optionsElt);
		}
		return false;
	});

	categories = $$('#' + list + ' .checklist-options-cat');
	// Evenement sur chaque "grande option" pour (dé)cocher toutes les options filles
	categories.each(function(s,i) {
		s.observe('click', function(e){

			// On deroule la liste de sous options si clic sur le titre
			/*
			group = this.next();
			if('input'!=Event.element(e).tagName);
			{
				alert(Event.element(e).tagName);
				group.toggle();
			}
			// */

			options = this.next().select('input');
			cat = this.select('input').first();
			options.each(function(el) {
				el.checked = cat.checked;
			});
		});
	});

	subcategories = $$('#' + list + ' .checklist-options-subcat');
	// Evenement sur chaque "sous option" pour décocher l'option mère
	subcategories.each(function(s,i) {
		s.observe('click', function(){
			cat = this.ancestors()[1].previous().select('input').first();
			subcat = this.select('input').first();
			if(cat.checked && !subcat.checked)
				cat.checked = false;
		});
	});


}

//marche pas (évenement censé etre créé a la volée pour cacher la checklist quand elle "perd le focus" (clic ailleurs sur la page))
function checklistUnFocusHandler(event, optionsElt)
{
	if(Event.element(event) != optionsElt)
	{
		hideList(optionsElt);
		Event.stop(event);
	}
}
var hdl;

function showList(optionsElt)
{
	optionsElt.show();
	optionsElt.scrollTo();
	hdl = checklistUnFocusHandler(optionsElt).bindAsEventListener();
	document.observe('click', hdl);
}


function hideList(optionsElt)
{
	optionsElt.hide();
	document.stopObserving('click', hdl);
}
