/*** Gestion de l'affichage par onglet du moteur de rechercher principal ***/
function displaySearchForm(lien, idForm, type) {
	// Gestion de la classe des liens
	if(type == 2)
		var others = $$(".navFormBis[id!="+lien+"]");
	else var others = $$(".navForm[id!="+lien+"]");

	others.each(function(e){e.removeClassName('active')});
	$(lien).addClassName('active');

	// On affiche maintenant le bon formulaire
	var others2 = $$("."+$(idForm).className+"[id!="+idForm+"]");
	others2.each(function(e){e.hide();});

	$(idForm).show();

	return false;
}

/*** Gestion de l'encart de sélection des langues ***/
function manageFlags(id) {
	if($(id).visible()) {
		Effect.BlindUp(id, { duration: 0.2 });
	}
	else Effect.BlindDown(id, { duration: 0.2 });

	return false;
}

/*** Gestion de l'affichage des activités lors de l'inscription de la salle ***/
function manageListeActivites(id, idLink) {
	if($(id).visible()) {
		Effect.BlindUp(id, { duration: 0.2 });

		if($(idLink).className == 'valideActive')
			$(idLink).className = 'valide';
		else $(idLink).className = '';
	}
	else {
		Effect.BlindDown(id, { duration: 0.2 });
		if($(idLink).className == 'valide')
			$(idLink).className = 'valideActive';
		else $(idLink).className = 'active';
	}

	return false;
}

function closeActivites(id, idLink) {

	Effect.BlindUp(id, { duration: 0.2 });
	$(idLink).className = '';

	// On regarde si au moins une des activités est cochées
	var others = $$("#"+id+" INPUT.chk");
	test = 0;
	others.each(function(e){
		if(e.checked == true){
			test = 1;
		}
	});

	if(test)
		$(idLink).className = "valide";

	return false;
}

function displayTxtAcces(id) {
	if($(id).visible()) {
		Effect.BlindUp(id, { duration: 0.2 });
	}
	else {
		Effect.BlindDown(id, { duration: 0.2 });
	}
	return false;
}

function managelisteSalleActivite(id) {

	if($('port'+id).visible()) {
		Effect.BlindUp($('port'+id), { duration: 0.2, afterFinish: function() { $('link'+id).className = ''; } });

	}
	else {
		Effect.BlindDown($('port'+id), { duration: 0.2 });
		$('link'+id).className = 'active';
	}
	return false;
}

function manageZone(key) {

	key = key.substr(0, 2);

	/** Département **/
	var others = $$("#departement OPTION");

	others.each(function(e){
		if(e.value == key)
			e.selected = true;

		if(key == "20" && e.value=="2A")
			e.selected = true;
	});

	/*** region ***/
	switch(key) {
		case "67":
		case "68":
			val = 1;
			break;
		case "24":
		case "33":
		case "40":
		case "47":
		case "64":
			val = 2;
			break;
		case "03":
		case "15":
		case "43":
		case "63":
			val = 3;
			break;
		case "14":
		case "50":
		case "61":
			val = 4;
			break;
		case "21":
		case "58":
		case "71":
		case "89":
			val = 5;
			break;
		case "22":
		case "29":
		case "35":
		case "56":
			val = 6;
			break;
		case "18":
		case "28":
		case "36":
		case "37":
		case "41":
		case "45":
			val = 7;
			break;
		case "08":
		case "10":
		case "51":
		case "52":
			val = 8;
			break;
		case "20":
		case "20":
			val = 9;
			break;
		case "25":
		case "39":
		case "70":
		case "90":
			val = 10;
			break;
		case "27":
		case "76":
			val = 11;
			break;
		case "75":
		case "77":
		case "78":
		case "91":
		case "92":
		case "93":
		case "94":
		case "95":
			val = 12;
			break;
		case "11":
		case "30":
		case "34":
		case "48":
		case "66":
			val = 13;
			break;
		case "19":
		case "23":
		case "87":
			val = 14;
			break;
		case "54":
		case "55":
		case "57":
		case "88":
			val = 15;
			break;
		case "09":
		case "12":
		case "31":
		case "32":
		case "46":
		case "65":
		case "81":
		case "82":
			val = 16;
			break;
		case "59":
		case "62":
			val = 17;
			break;
		case "44":
		case "49":
		case "53":
		case "72":
		case "85":
			val = 18;
			break;
		case "02":
		case "60":
		case "80":
			val = 19;
			break;
		case "16":
		case "17":
		case "79":
		case "86":
			val = 20;
			break;
		case "04":
		case "05":
		case "06":
		case "13":
		case "83":
		case "84":
			val = 21;
			break;
		case "01":
		case "07":
		case "26":
		case "38":
		case "42":
		case "69":
		case "73":
		case "74":

			val = 22;
			break;
		case "98":
			val = 23;
			break;
		case "971":
		case "972":
		case "973":
		case "974":
			val = 24;
			break;
	}

	/** Département **/
	var others = $$("#region OPTION");

	others.each(function(e){
		if(e.value == val)
			e.selected = true;
	});
}


/*** Gestion du Scroll pour le footer ***/
footerFirst = null;
include("scroller.js", function() {
	if($("footerFirst")) {
		var tmp1 = new Scroller("footerFirst");
		footerFirst = tmp1;
	}
});

/*** Calcul de distances gmap ***/
function distance(lat1, lon1, lat2, lon2, unit) {
	var radlat1 = Math.PI * lat1/180
	var radlat2 = Math.PI * lat2/180
	var radlon1 = Math.PI * lon1/180
	var radlon2 = Math.PI * lon2/180
	var theta = lon1-lon2
	var radtheta = Math.PI * theta/180
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist)
	dist = dist * 180/Math.PI
	dist = dist * 60 * 1.1515
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist
}

/*function displayDistance(clubLat, clubLng, startLat, startLng){
	var dist = distance(clubLat, clubLng, startLat, startLng, 'K'); // exprimé en Km.
	if(dist < 1){
		dist = dist * 1000;
		$('displayDistance').update('<strong>Vous vous situez à '+ dist.nb() + ' m du club.</strong>');
	}
	else
		$('displayDistance').update('<strong>Vous vous situez à '+ dist.nb() + ' km du club.</strong>');

	Effect.BlindDown('distanceCalculee', { duration: 0.2 });
}*/

function displayDistance(clubLat, clubLng, startLat, startLng){
	var dist = distance(clubLat, clubLng, startLat, startLng, 'K'); // exprimé en Km.
	dist = dist * 1000;

	tmpPieds   = Math.ceil(dist / 83);
	tmpVelo    = Math.ceil(dist / 250);
	tmpVoiture = Math.ceil(dist / 533);

	//$('displayDistance').update('<strong>Vous vous situez à '+ dist.nb() + ' m du club.</strong>');
	$('displayDistance').update('<strong class="tempsParcours">Votre temps de parcours</strong><strong class="tempsPieds">' + tmpPieds + ' min</strong><strong class="tempsVelo">' + tmpVelo + ' min</strong><strong class="tempsVoiture">' + tmpVoiture + ' min</strong>');

	Effect.BlindDown('distanceCalculee', { duration: 0.2 });
}

function calculerDistance(){

	adresseInternaute = $F('adresseInternaute');
	if(adresseInternaute == 'ex : 45, avenue des Champs Elysées, 75008, Paris' || adresseInternaute == '' )
		return false;
	else{
		geocoder = new GClientGeocoder();
		if(geocoder){
        geocoder.getLatLng(
          adresseInternaute,
          function(point) {
           	if(!point)
					alert("Désolé, votre adresse n'a pas été trouvée.");
				else{
					clubLat = $F('clubLat');
					clubLng = $F('clubLng');
					startLat = point.lat();
					startLng = point.lng();
					displayDistance(clubLat, clubLng, startLat, startLng);
				}
          }
        );
      }
	}
}

/*** Gestion du hover/click sur la liste des activités ***/
function ManageActivites(){
	if($$('.previewThm')){
		$$('.previewThm').each(function(thm){
			Event.observe($(thm), 'mouseover', function(ev) {
				$$('.previewThm').each(function(them){
					$(them).ancestors()[1].removeClassName('hover');
				});
				el = Event.element(ev);
				if(!$(thm).hasClassName('deplie'))
					$(thm).ancestors()[1].addClassName('hover');
			});
			Event.observe($(thm), 'mouseout', function(ev) {
				el = Event.element(ev);
				if(!$(thm).hasClassName('deplie'))
					$(thm).ancestors()[1].removeClassName('hover');
			});
			Event.observe($(thm), 'click', function(ev) {
				$(thm).ancestors()[1].removeClassName('hover');
				if(!$(thm).hasClassName('deplie')){
					$(thm).addClassName('deplie');
					Effect.BlindDown($(thm).next(), { duration: 0.2 });
					$(thm).down('.truncate').hide();
					$(thm).down('.notruncate').show();
				}
				else{
					$(thm).removeClassName('deplie');
					Effect.BlindUp($(thm).next(), { duration: 0.2 });
					truncate = $(thm).down('.truncate');
					notruncate = $(thm).down('.notruncate');
					setTimeout("truncate.show();notruncate.hide();", 195);
				}
			});
		});
	}
}

function manageContact(idLink, idForm) {
	if($(idLink).hasClassName('lienContactActive')) {
		Effect.BlindUp($(idForm), { duration: 0.2, afterFinish: function() { $(idLink).removeClassName('lienContactActive'); } });
	}
	else {
		$(idLink).addClassName('lienContactActive');
		Effect.BlindDown($(idForm), { duration: 0.2 });
	}
	return false;
}

function manageChoice(zone) {
	// On grise tout le monde
	$$('#offresRecap A[id!=offre'+zone+"]").each(function(e){ e.className = 'lienOffre'; });

	// On allume celui en question
	$('offre'+zone).className = 'lienOffreActive';

	// On met à jour la valeur du champ
	$('choixCompte').value = zone;

	return false;
}

function manageChoice2(zone) {
	// On grise tout le monde
	for(i=1;i<=4;i++)
		if(i!=zone && $('choix'+i))
			$('choix'+i).className = "";
	// On allume celui en question
	$('choix'+zone).className = 'active';

	// On met à jour la valeur du champ
	$('choixCompte').value = zone;

	$('formChoixOffre').submit();

	return false;
}


function manageVote(radical, num) {

	if($(radical+num).className == 'active') {
		// $(radical+num).className = '';
		for(var i = num+1;i<6;i++)
			$(radical+i).className = '';
		$(radical+'Note').value = num;
	}
	else {
		$(radical+num).className = 'active';
		for(var i = num;i>0;i--)
			$(radical+i).className = 'active';
		$(radical+'Note').value = num;
	}



	/*** Calcul de la moyenne ***/
	nb1 = parseInt($('accueilNote').value);
	nb2 = parseInt($('materielNote').value);
	nb3 = parseInt($('coursNote').value);
	nb4 = parseInt($('cadreNote').value);

	var moy = (nb1 + nb2 + nb3 + nb4) / 4;

	$('noteMoyenne').value = moy;

	nb = Math.ceil(moy);

	$$("#votreNote .conteneurNotesMoyenne SPAN").each(function(e){e.className = ''});

	for(j=1;j<=nb;j++) {
		$('moy'+j).className = 'active';
	}

	return false;
}

function displayFacturation() {

	if($('inscriptionFacturation').visible()) {
		Effect.BlindUp($('inscriptionFacturation'), { duration: 0.3 });
		$('btnConsulterFermer').className = '';
	}
	else {
		Effect.BlindDown($('inscriptionFacturation'), { duration: 0.3 });
		$('btnConsulterFermer').className = 'btnFermer';
	}
	return false;
}

function displayContactRef() {

	if($('formContactInfos').visible()) {
		Effect.BlindUp($('formContactInfos'), { duration: 0.3 });
		$('btnEmailFermer').removeClassName('btnFermer');
	}
	else {
		Effect.BlindDown($('formContactInfos'), { duration: 0.3 });
		$('btnEmailFermer').addClassName('btnFermer');
	}
	return false;
}

Event.observe(window, 'load', function() {
	tooltip = new Tooltip(null, 255);
	ManageActivites();
	if($('diaporamaSalle') && $('slide2')) {
		include("glider.js", function() {
			new Glider("diaporamaSalle", {duration: 0.5, frequency: 5, autoGlide: true});
		});
	}
	if($("keepAlive"))
		setInterval('new Ajax.Request("keep-alive.php");', 6000);
});
function toto() {
	if($('moisDep').value == '' || $('moisFin').value == '') {
		alert('Merci de remplir les deux champs avant de soumettre le formulaire');
		return false;
	}
}

function managePhoneNumber(id) {

}
