$(document).ready(function(){
	/**
	* 	GESTION DU MENU FORMATION
	*	� optimiser au niveau des switch de class.
	*/
	
	$("div#main-menu ul ul").each(function(){
		
		/* on veut que le premier niveau affiche une page 
		if ($(this).parent().parent().attr('class') == 'level0'){
			if ( $(this).find('li.current').length == 0 )
				$(this).hide();
			if ($(this).parent().attr('class') == 'current') {
				$(this).parent().find('a:first').addClass('open');
				$(this).show();
			}
			if ( $(this).parent().find('li.current').length != 0 ) {
				$(this).parent().find('a:first').removeClass().addClass('open');
				$(this).show();				
			}
			return;	
		}
		*/
		/* LIEN */
		var ali = $(this).parent().find('a:first') ;
		
		/* cherche le page courante pour le menu actif */
		if ( $(this).find('li.current').length == 0 )
		{
			$(this).hide();	
			$(this).parent().find('li:first').addClass('close');
			
		}else{
			$(this).show();
			$(this).parent().find('li:first').addClass('open');
		}
		
		ali.click(function(){
			//ferme toutes les visibles du meme niveau et inf�rieur
			$(this).parent().parent().find('ul:visible').each(function(){
				$(this).slideUp('fast');
				$(this).parent().find('li:first').removeClass().addClass('close');									 
			});
			
			if ($(this).parent().find('ul:visible').length != 0)
			{
				$(this).parent().find('li:first').removeClass().addClass('close');	
				$(this).parent().children('ul').slideUp('fast');
			}else{
				$(this).parent().find('li:first').removeClass().addClass('open');	
				$(this).parent().children('ul').slideDown('fast');
			}
			return false;
		});
	});
	
	
	/**
	*	GENERIC DIV TO HIDE as possible ..
	*/
	// generate hide / show button
	// elt : JQUERY / DOM Element
	function getHideShowButton (elt) {
		
		var txt = $(elt).text(); 
		
		$(elt).empty();
		var linkElt	= '<a>' + txt + '</a>'//$('<a>').text(txt);
		
		var toShowElt = $(linkElt).clone().addClass('button-to-show');
		var toHideElt = $(linkElt).clone().addClass('button-to-hide');
		return new Array(toShowElt, toHideElt);
		
	}
	
	// state changement
	// elt : JQUERY / DOM Element = <div>
	function toggleState(elt, dir, buttons, speed) {
		
		var buttonPrev = $(elt).prev();
		
		if (dir == 'up'){
	
			$(elt).slideUp(speed);
			$(buttonPrev).empty().append(buttons[0])
		
		} else if (dir == 'down') {
	
			$(elt).slideDown(speed);
			$(buttonPrev).empty().append(buttons[1])
		
		}else{ return; }
		
	}
	
	$('div.to-hide').each(function(){
		
		// parameters 
		var speed = 'fast';
		var allHideEvent = true;
		var allThis = $('div.to-hide');

		var This = $(this).hide();
		
		var button = $(this).prev();
		var buttons = getHideShowButton(button);  
		
		$(button)
			.empty()
			.append(buttons[0])
			.click(function(){
				
				if ($(This).css('display') == 'none')
				{
					if (allHideEvent) toggleState(allThis,'up', buttons, speed);
					toggleState(This, 'down', buttons, speed); 
				}else{
					toggleState(This, 'up', buttons, speed); 	
				}
				
			});
			
	});
	
	/* TAbleau des membres */
	$('div.membre table tr.detail').each(function() {
		
		var This = $(this);
		$(this).hide();
		
		$(this).prev().css('cursor', 'pointer')
			.hover(function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			})
			.click(function() {
				
				$('div.membre table tr.detail').hide()
				$('div.membre table tr.detail').prev().removeClass('current');
				$('div.membre table tr.detail').prev().find('td.info span').show();
				//alert($(this).parent().html());//
				$(this).addClass('current');
				$(this).find('td.info span').hide();
				$(This).show();
				
			});
		
		return;
	
	
	});

	/* Print action */
	$('li.printItem').click(function () {
		print();
		return false;
	});
	
	/* Formualaire */
	$('fieldset#profil').hide();
	var button = document.createElement('a');
	$('fieldset#profil').before(button);
	$(button)
		.addClass('button')
		.css('cursor', 'pointer')
		.text('Voir mes coordonnées enregistrées auprés de l\'icom france.')
		.toggle(
			function(){
				$('fieldset#profil').show()	
			},
			function(){
				$('fieldset#profil').hide()
			}
		);
	
	
	
	
	checked = $('form#inscription input#member_oui_oui:checked').length;
	pmember = $('form#inscription input#member_number').parent();
	if(checked)
	{
		$(pmember).show();
	}
	else
	{
		$(pmember).hide();
	}
	$('form#inscription input#member_oui_oui').click(function(){
		$(pmember).show();
	});
	$('form#inscription input#member_non_non').click(function(){
		$(pmember).hide();
	});
	

});