
function OnLoad()
{
	// Réglages ajax par défaut
	$.ajaxSetup({
		  url: "ajax.html",
		  type: "POST"
		});

	
	
	$('#debug').click(function() {
		toggleDebug($(this));	
	});

	
	
	// Rendre actif/inactif les champs de saisie
	$('.page form :input').focus(function() {
		$(this.parentNode).addClass('actif');
	});
	$('.page form :input').blur(function() {
		$(this.parentNode).removeClass('actif');
	});

	
	// Galerie photos
	$('a.apercu').lightBox({
		imageBlank : '_img/lightbox/lightbox-blank.gif',
		imageLoading: '_img/lightbox/lightbox-ico-loading.gif',
		imageBtnClose:'_img/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: '_img/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: '_img/lightbox/lightbox-btn-next.gif',
		containerResizeSpeed: 400,
		txtOf: 'sur'
	});

	
	Commentaires();

	
	// Effet enfoncé
	$('#in_recherche').mousedown(function() {
		$(this).attr('src', '_img/ok_menu_actif.png');
	}).mouseup(function() {
		$(this).attr('src', '_img/ok_menu.png');
	}).mouseleave(function() {
		$(this).attr('src', '_img/ok_menu.png');
	});
	
	
	
	
	
	// Menu -----------------------------------------------
	
	$('.fo a').click(function() {
		window.open(this.href,'_blank');
		return false;
	})
	
	$('.rubrique').mouseover(function() {
		$('.niv1 div').hide();
		$(this).next().show();
	});
	
	$('.niv1 div').mouseleave(function() {
		$(this).hide();
	});
	
	$('.entete').mouseenter(function() {
		$('.niv1 div').hide();
	});

	$('.niv1 ul ul').hide();
	
	$('.niv1 h5').each(function() {
		
		$(this).click(function() {
			
			var id = $(this).attr('id');
			$('#ul_'+ id.substr(4)).toggle();
			
		});

		$(this).mouseover(function() {
			$(this).css('text-decoration', 'underline');			
		});

		$(this).mouseout(function() {
			$(this).css('text-decoration', 'none');			
		});
		
	});
	

	// Dépliant pied de page
	$('.liste_champ ul').hide();

	$('.liste_champ h5').each(function() {
		
		$(this).click(function() {
			
			var id = $(this).attr('id');
			$('#pul_'+ id.substr(5)).toggle();
			
		});

		$(this).mouseover(function() {
			$(this).css('text-decoration', 'underline');			
		});

		$(this).mouseout(function() {
			$(this).css('text-decoration', 'none');			
		});
		
	});
	
	
	
	// Recherche -------------------------------------------
	
	// Désactiver l'autocomplétion du navigateur
	$('#recherche').attr('autocomplete', 'off');
	
	
	// Bloquer les recherches inutiles
	$('.recherche form').submit(function() {
		
		if(!$('#recherche').val() || $('#recherche').val() == 'Rechercher une équipe / un membre')
		{
			$('#alerte').html('Votre recherche manque de précision :)');
			Alerter();
			return false;
		}
		
		return true;
	});
	
	
	// Effacer le label par défaut
	$('#recherche').focus(function() {
		if($(this).attr('value') == 'Rechercher une équipe / un membre')
			$(this).attr('value', '');
	});

	
	// Cacher le menu
	$('#recherche').blur(function() {
		$('#completion').mouseleave(function() {
			$('#completion').slideUp("slow");		
		});
		
		if(!$(this).val())
			$(this).val('Rechercher une équipe / un membre');
	});

	
	// Autocompléter la recherche
	$('#recherche').keyup(function() {
		
		var saisie = $(this).attr('value');
		
		
		// Pas de recherche si moins de 3 car
		if(saisie.length < 3)
		{
			$('#completion').slideUp("slow");
			return false;
		}
		
		$('#loader_recherche').show();
		
		json = {
				action:'recherche',
				saisie:$(this).attr('value')
				};
		
		var xhr = $.ajax({
						data:json,
						dataType:'html',
						success:function(data, textStatus) {
							$('#completion').html(data);
							$('#completion').slideDown("slow");
							$('#loader_recherche').hide();
						}						
					});
	});
	
} // /OnLoad






/**
 * Afficher l'alerte utilisateur
 * Est masquée aprés une temporisation
 */
function Alerter()
{
	$('#alerte').fadeIn('slow');
	setTimeout("$('#alerte').fadeOut('fast');", 5000);

	$('#alerte').click(function () {
    	$(this).hide('fast');
    });
}



/**
 * Lance une requete Ajax et affiche le résultat dans une alerte
 * @param json
 * @param callback - facultatif
 * @return
 */
function AjaxPost(json)
{
	// Le 2ème argument est le nom de la fonction callback à exécuter après l'ajax
	if(arguments[1])
		var callback = arguments[1];
	 
	$('#alerte').html('Enregistrement en cours...');
	$('#alerte').fadeIn('slow');
	
	$.post('ajax.html',
			json,
			function(data, textStatus) {
		
				if(callback)
					callback(textStatus);

				$('#alerte').html(data);
				setTimeout("$('#alerte').fadeOut('fast');", 2000);

			}
	);

}


function toggleDebug(debug)
{
	debug.toggleClass('debug_large');
	debug.children('dl').toggleClass('display-none');
}


function addTache(type_id, details, mbr_id, urgence) {

	json = {
		action:'addTache',
		tache_type_id:type_id,
		tache_details:details,
		tache_mbr_id:mbr_id,
		tache_urgence:urgence
	}
	
	$.post('ajax.html',
			json,
			function(data, textStatus) {}
	);
}


