$(document).ready(function() {
	// tittle sur liste de stage

	 $(".picto a").append("<em></em>");

	 $(".picto a").hover(function() {
	 $(this).find("em").animate({opacity: "show", top: "-20", right:"0"}, "fast");
	 var hoverText = $(this).attr("title");
	 $(this).find("em").text(hoverText);
	 }, function() {
	 $(this).find("em").animate({opacity: "hide", top: "-20", right:"0"}, "fast");
	 });
	
		
	// Recherche par ville
	$('#ville').autocomplete(
		'/global/includes/villeRecherche.ajax.php',
		{ 
			minChars:1, 
			max: 100
		}
	);

	// Recherche par date : calendrier
	$('#date_stage')
		.datePicker( { startDate: (new Date()).zeroTime().addDays(1).asString() } )
		.bind('dateEmpty', function() { $('#date_stage').val(''); } )
	;

	// Recherche par département
	$('#departement').change( function() {
		if ($(this).val() != '') $('#ville').val('');
	});

	// Liens d'ajout aux favoris
	$('.AjoutFavoris').click( function() {
		$this = $(this);
		$.post(
			'/global/includes/stageFavoris.ajax.php',
			{ fonction: 'ajouter', session_id: session_id, stage: $this.attr('id').replace('AjoutFavoris_', '') },
			function (data) {	
				if (data == '1') {
					$this.fadeOut('slow');
					var nbStages = $('#PanierTaille').html();
					if (!isNaN(nbStages)) nbStages ++; else nbStages = 1;
					$('#PanierTaille').html(nbStages);
					if (nbStages == 1) $('#PanierOffres').html('offre'); else $('#PanierOffres').html('offres');
					$('#Panier').show();
				}
			}
		);
		return false;
	});

	// Lien de suppression de tous les favoris
	$('.ViderFavoris').click( function() {
		$.post(
			'/global/includes/stageFavoris.ajax.php',
			{ fonction: 'vider', session_id: session_id },
			function (data) {	
				if (data == '1') {
					document.location.href ='/';
				}
			}
		);
		return false;
	});

	// Lien d'envoi à un ami
	$('.EnvoyerAmi').fancybox( {overlayShow: true, frameWidth: 400, frameHeight: 250 } );

	// Tableau une ligne sur deux
    $('.tableau tr:even', this).addClass('pair');

	// RSS
	var rssMode = 'liste';
	$('#rssListe').click( function() { 
		$('#rssFiltreLabel').html('Stages du département');
		rssMode = 'liste';
		showFiltre();
		return false;
	});
	$('#rssPromo').click( function() { 
		$('#rssFiltreLabel').html('Promotions du département');
		rssMode = 'promo';
		showFiltre();
		return false;
	});
	$('#formRSS').submit( function() {
		var dptSuffixe, rssLinkId;
		if (rssMode == 'promo') rssLinkId = 'rssPromo'; else rssLinkId = 'rssListe';
		if ($('#rssFiltreDepartement').val() != '') dptSuffixe = '-' + $('#rssFiltreDepartement').val(); else dptSuffixe = '';
		$(this).attr('action', $('#' + rssLinkId).attr('href').replace('.php', dptSuffixe + '.xml'));
		$('#rssFiltre').fadeOut();
		return true;
	});
	showFiltre = function() {
		$('#rssFiltre:hidden').animate( { height: 40 } );
	}
	



});
