var currentAjaxRequest;
/*effet accordeon horizontal*/
jQuery.fn.extend({
  haccordion: function(params){
	var jQ = jQuery;
	var params = jQ.extend({
	  speed: 500,
	  headerclass: "header",
	  contentclass: "content",
	  contentwidth: 634
	},params);
	return this.each(function(){
	  jQ("."+params.headerclass,this).click(function(){
		var p = jQ(this).parent()[0];
		if (p.opened != "undefined"){
		  jQ(p.opened).next("div."+params.contentclass).animate({
			width: "0px"
		  },params.speed);
		}
		p.opened = this;
		jQ(this).next("div."+params.contentclass).animate({
		  width: params.contentwidth + "px"
		}, params.speed);
	  });
	});
  }
});

	// SeViR Simple Horizontal Accordion @2007
function initRecherche(){	
	$('.haccordion').haccordion();
	$('#contentonglet1').click(); 		
	
	//calendrier	
	$('#datemin').datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImageOnly: true, buttonImage: 'graphics/cal.gif'});
	$('#datemin2').datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImageOnly: true, buttonImage: 'graphics/cal.gif'});
	$('#datemax').datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImageOnly: true, buttonImage: 'graphics/cal.gif'});
	$('#datemax2').datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImageOnly: true, buttonImage: 'graphics/cal.gif'});
	//popup d'aide
	$('.contactform a').Tooltip({ showURL: false });
	
	//bouton de recherche
	$('#searchbuttonsearch').click(function () {
			$('#errorphoto').hide('slow');
			var nbcritere=0;
			var erreur = '';
			//on vérifie les champs
			if($('#field_reference').val()!=''){
				if(isNaN($('#field_reference').val()))erreur = '<u>Erreur : </u> la référence de la photo doit être un nombre';
			}else{
				if($('#field_keywordp').val()!='')nbcritere++;
				if($('#field_typep').val()>0)nbcritere++;
				if($('#datemin').val()!='')nbcritere++;
				if($('#datemax').val()!='')nbcritere++;
				var tmp = $('#field_coureur').val();
				if(tmp && tmp.length>=3)nbcritere++;
				//cas ou aucun critere n'est renseigné
				if(nbcritere == 0)erreur = '<u>Erreur : </u> vous devez saisir au moins un critère de recherche.';
			}
			
			//affichage du message d'erreur si besoin
			if(erreur.length >0){
				$('#errorphoto').html(erreur);
				$('#errorphoto').show('slow');
			}else{
				$('#fotosearch').submit();
			}
	});
	$('#searchbuttonalbum').click(function () {
			$('#erroralbum').hide('slow');
			var nbcritere=0;
			var erreur = '';
			//on vérifie les champs
			if($('#field_keyworda').val()!='')nbcritere++;
			if($('#field_typea').val()>0)nbcritere++;
			if($('#datemin2').val()!='')nbcritere++;
			if($('#datemax2').val()!='')nbcritere++;
	
			//affichage du message d'erreur si besoin
			if(nbcritere == 0){
				$('#erroralbum').html('<u>Erreur : </u> vous devez saisir au moins un critère de recherche.');
				$('#erroralbum').show('slow');
			}else{
				$('#albumsearch').submit();
			}
	});
	
	//chargement des épreuves
	$('#field_typea').change(function () {
		$('#event').html('Chargement des &eacute;v&egrave;nements...');
		var param = $('#field_typea').val();
		if(param > 0){
			$.ajax({
				url: 'dynamiquegestion.php',
				type: 'POST',
				dataType: 'html',
				data:{'param1':'listevent','param2':param},
				timeout: 1000,
				error: function(){
					alert('Error loading XML document');
				},
				success: function(xml){
						// On se sert de innerHTML pour rajouter les options a la liste
						$('#event').html(xml);
				}
			});
		}else{
			$('#event').html('Veuiller sélectionner un type d\'évènement.');
		}
	});
	
	//initialiser
	$('#effaceralbum').click(function () {
		$('#event').html('Veuiller sélectionner un type d\'évènement.');
	});
	
	$('#effacerphoto').click(function () {
		$('#loadriders').hide('slow');
		$('#liste_results').hide('slow');	
	});
	
	/*$('#field_rider').keyup(function () {
		var param = $('#field_rider').val();
		var param2 = $('#field_rider_cache').val();
		$('#field_rider_cache').val($('#field_rider').val());
		if(param.length > 2){
			if(param!=param2){
				$('#liste_results').hide('slow');
				$('#loadriders').show('slow');
				$.ajax({
					async: 'false',
					url: 'dynamiquegestion.php',
					type: 'POST',
					dataType: 'xml',
					data:{'param1':'listerider','param2':param},
					timeout: 1000,
					error: function(){
						alert('Error loading XML document');
					},
					success: function(xml){
						//on vide la zone
						$('#liste_results').empty();
						$(xml).find('rider').each(function(){
							var nom = $(this).find('nom').text();
							var id = $(this).find('id').text();
							var prenom = $(this).find('prenom').text();
							var nat = $(this).find('nat').text();
							var team = $(this).find('team').text();
							$('<input type=\'radio\' name=\'coureur\' value=\''+id+'\'/>'+nom+' '+prenom+' ('+nat+'/'+team+')<br/>').appendTo('#liste_results');
						});
						if($('#liste_results').text()=='')$('#liste_results').html('Aucun coureur correspondant à votre saisie');
						$('#loadriders').hide('slow');
						$('#liste_results').show('slow');					
					}
				});
			}
		}else{
			$('#loadriders').hide('slow');
			$('#liste_results').hide('slow');								
		
	});}*/
	
	$('#idtabs a').click(function () {
		var tit = $(this).text();						   
		$('#field_rider').attr('value',tit);
		majRechercheCoureur(tit);
	});
	
	$('#field_rider').keyup(function () {
		majRechercheCoureur(this.value);
	});
	
	
}

/**
 * Lancement de la recherche de coureurs, avec communication AJAX
 **/
function majRechercheCoureur(param){
		
		$('#liste_resultsby').hide('slow');
		$('#errorcoureur').hide('slow');
		$('#mess').hide('slow');
		$('#errorcoureur').html("");
		$('#loadridersby').show('slow');
		if(currentAjaxRequest)currentAjaxRequest.abort();
		currentAjaxRequest = $.ajax({
			async: 'false',
			url: 'dynamiquegestion.php',
			type: 'POST',
			dataType: 'xml',
			data:{'param1':'listerider','param2':param},
			timeout: 1000,
			error: function(){
				/*if($('#liste_resultsby').text()==''){
					$('#errorcoureur').html('Erreur technique lors de la recherche... Veuillez réessayer ultérieurement...');
					$('#errorcoureur').show('slow');
				}*/
			},
			success: function(xml){
				//on vide la zone
				$('#liste_resultsby').empty();
				$(xml).find('rider').each(function(){
					var nom = $(this).find('nom').text();
					var id = $(this).find('id').text();
					var prenom = $(this).find('prenom').text();
					var nat = $(this).find('nat').text();
					var team = $(this).find('team').text();
					$('<li><a href=\'coureur;'+id+';voir-coureur.html\'><b>'+nom+' '+prenom+'</b> <i>('+nat+(team==''?'':'/'+team)+')</i></a></li>').appendTo('#liste_resultsby');
				});
				if($('#liste_resultsby').text()==''){
					if($('#field_rider').attr('value')==''){
						$('#mess').show('slow');
					}else{
						$('#errorcoureur').html('Aucun coureur correspondant à votre saisie');
						$('#errorcoureur').show('slow');
					}
				}
				$('#loadridersby').hide('slow');
				$('#liste_resultsby').show('slow');							
			}
		});	
}
