Subversion Repositories eFlore/Applications.cel

Rev

Rev 1039 | Rev 1042 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
836 jpm 1
/*+--------------------------------------------------------------------------------------------------------+*/
2
// PARAMÊTRES et CONSTANTES
941 jpm 3
// Mettre à true pour afficher les messages de débogage
950 jpm 4
var DEBUG = false;
1041 aurelien 5
/**
6
 * Indication de certaines variables ajoutée par php
7
* var communeImageUrl ;
8
* var pointImageUrl ;
9
* var groupeImageUrlTpl ;
10
*/
836 jpm 11
var pointsOrigine = null;
12
var boundsOrigine = null;
13
var markerClusterer = null;
14
var map = null;
15
var infoBulle = new google.maps.InfoWindow();
980 jpm 16
var stations = null;
836 jpm 17
var pointClique = null;
980 jpm 18
var carteCentre = new google.maps.LatLng(25, 10);
836 jpm 19
var carteOptions = {
980 jpm 20
	zoom: 3,
21
	center:carteCentre,
836 jpm 22
	mapTypeId: google.maps.MapTypeId.ROADMAP,
23
	mapTypeControlOptions: {
24
		mapTypeIds: ['OSM',
25
		             google.maps.MapTypeId.ROADMAP,
26
		             google.maps.MapTypeId.HYBRID,
27
		             google.maps.MapTypeId.SATELLITE,
28
		             google.maps.MapTypeId.TERRAIN]
29
	}
30
};
31
var osmMapType = new google.maps.ImageMapType({
32
	getTileUrl: function(coord, zoom) {
33
		return "http://tile.openstreetmap.org/" +
34
		zoom + "/" + coord.x + "/" + coord.y + ".png";
35
	},
36
	tileSize: new google.maps.Size(256, 256),
37
	isPng: true,
38
	alt: "OpenStreetMap",
39
	name: "OSM",
40
	maxZoom: 19
41
});
980 jpm 42
var ctaLayer = null;
915 jpm 43
var pagineur = {'limite':50, 'start':0, 'total':0, 'stationId':null, 'format':'tableau'};
44
var station = {'commune':'', 'obsNbre':0};
45
var obsStation = new Array();
46
var obsPage = new Array();
939 jpm 47
var taxonsCarte = new Array();
980 jpm 48
var mgr = null;
49
var marqueursCache = new Array();
50
var zonesCache = new Array();
1035 aurelien 51
var requeteChargementPoints;
836 jpm 52
/*+--------------------------------------------------------------------------------------------------------+*/
53
// INITIALISATION DU CODE
54
 
55
//Déclenchement d'actions quand JQuery et le document HTML sont OK
56
$(document).ready(function() {
57
	initialiserWidget();
58
});
59
 
60
function initialiserWidget() {
980 jpm 61
	definirTailleTitre();
836 jpm 62
	initialiserAffichageCarte();
63
	initialiserAffichagePanneauLateral();
64
	initialiserCarte();
980 jpm 65
	initialiserGestionnaireMarqueurs()
836 jpm 66
	initialiserInfoBulle();
941 jpm 67
	initialiserFormulaireContact();
836 jpm 68
	chargerLimitesCommunales();
1032 aurelien 69
	attribuerListenerCarte();
70
	programmerRafraichissementCarte();
836 jpm 71
}
72
 
915 jpm 73
/*+--------------------------------------------------------------------------------------------------------+*/
74
// AFFICHAGE GÉNÉRAL
75
 
980 jpm 76
function afficherTitreCarte() {
77
	if (stations != null && taxonsCarte.length > 0) {
1039 aurelien 78
		var obsNbre = stations.stats.observations;
79
		var obsNbreFormate = stations.stats.observations.formaterNombre();
80
		var plteNbre = taxonsCarte.length;
81
		var plteNbreFormate = taxonsCarte.length.formaterNombre();
82
		var communeNbre = stations.stats.communes;
83
		var communeNbreFormate = stations.stats.communes.formaterNombre();
980 jpm 84
 
1039 aurelien 85
		var titre = obsNbreFormate+' observation';
980 jpm 86
		titre += (obsNbre > 1) ? 's' : '' ;
87
 
88
		if (nt == '*') {
1039 aurelien 89
			titre += ' de '+plteNbreFormate+' plante';
980 jpm 90
			titre += (plteNbre > 1) ? 's' : '' ;
91
		} else {
92
			if (taxonsCarte[0]) {
93
				var taxon = taxonsCarte[0];
94
				titre += ' pour '+taxon.nom;
95
			}
96
		}
97
 
1039 aurelien 98
		titre += ' sur '+communeNbreFormate+' commune';
980 jpm 99
		titre += (communeNbre > 1) ? 's' : '' ;
100
 
101
		$('#carte-titre-infos').text(titre);
102
	}
836 jpm 103
}
104
 
980 jpm 105
function definirTailleTitre() {
106
	var largeurViewPort = $(window).width();
107
	var taille = null;
108
	if (largeurViewPort < 400) {
109
		taille = '0.8';
110
	} else if (largeurViewPort >= 400 && largeurViewPort < 800) {
111
		taille = '1.0';
112
	} else if (largeurViewPort >= 800) {
113
		taille = '1.6';
114
	}
115
	$("#carte-titre").css('font-size', taille+'em');
116
}
117
 
915 jpm 118
/*+--------------------------------------------------------------------------------------------------------+*/
119
// CARTE
120
 
836 jpm 121
function initialiserAffichageCarte() {
122
	$('#carte').height($(window).height() - 35);
123
	$('#carte').width($(window).width() - 24);
980 jpm 124
 
836 jpm 125
	if (nt != '*') {
126
		$('#carte').css('left', 0);
127
	}
128
}
129
 
130
function initialiserCarte() {
131
	map = new google.maps.Map(document.getElementById('carte'), carteOptions);
132
	// Ajout de la couche OSM à la carte
133
	map.mapTypes.set('OSM', osmMapType);
134
}
135
 
980 jpm 136
function initialiserGestionnaireMarqueurs() {
137
	mgr = new MarkerManager(map);
138
}
836 jpm 139
 
140
function chargerLimitesCommunales() {
141
	if (urlsLimitesCommunales != null) {
142
		for (urlId in urlsLimitesCommunales) {
143
			var url = urlsLimitesCommunales[urlId];
1039 aurelien 144
			ctaLayer = new google.maps.KmlLayer(url, {preserveViewport: false});
836 jpm 145
			ctaLayer.setMap(map);
146
		}
147
	}
148
}
1039 aurelien 149
 
980 jpm 150
var listener = null;
151
var timer = null;
1032 aurelien 152
function attribuerListenerCarte() {
980 jpm 153
	listener = google.maps.event.addListener(map, 'bounds_changed', function(){
1032 aurelien 154
		programmerRafraichissementCarte();
980 jpm 155
	});
1032 aurelien 156
	listener = google.maps.event.addListener(map, 'zoom_changed', function(){
157
		programmerRafraichissementCarte();
158
	});
980 jpm 159
}
1032 aurelien 160
function programmerRafraichissementCarte() {
161
	if(timer != null) {
162
        window.clearTimeout(timer);
1035 aurelien 163
    }
164
	if(requeteChargementPoints != null) {
165
		requeteChargementPoints.abort();
166
	}
1032 aurelien 167
	timer = window.setTimeout(function() {
168
		var zoom = map.getZoom();
1039 aurelien 169
		var NELatLng = (map.getBounds().getNorthEast().lat())+'|'+(map.getBounds().getNorthEast().lng());
170
		var SWLatLng = (map.getBounds().getSouthWest().lat())+'|'+(map.getBounds().getSouthWest().lng());
1032 aurelien 171
		chargerMarqueurs(zoom, NELatLng, SWLatLng);
172
    }, 400);
173
}
836 jpm 174
 
980 jpm 175
var marqueurs = new Array();
176
function chargerMarqueurs(zoom, NELatLng, SWLatLng) {
177
	var url = stationsUrl+
178
		'&zoom='+zoom+
179
		'&ne='+NELatLng+
180
		'&sw='+SWLatLng;
181
 
1032 aurelien 182
	if(infoBulleOuverte) {
183
		return;
184
	}
185
 
1035 aurelien 186
	if(requeteChargementPoints != null) {
187
		requeteChargementPoints.abort();
1037 aurelien 188
		cacherMessageChargementPoints();
1035 aurelien 189
	}
190
 
191
	afficherMessageChargementPoints();
192
	requeteChargementPoints = $.getJSON(url, function(data) {
193
		rafraichirMarqueurs(data);
194
		cacherMessageChargementPoints();
980 jpm 195
	});
196
}
197
 
1035 aurelien 198
function afficherMessageChargementPoints() {
199
	$('#zone-chargement-point').css('display','block');
200
}
201
 
202
function cacherMessageChargementPoints() {
203
	$('#zone-chargement-point').css('display','none');
204
}
205
 
206
function rafraichirMarqueurs(data) {
207
	$.each(marqueurs, function(index, marqueur) {
208
		marqueur.setMap(null);
209
	});
210
	marqueurs = new Array();
211
 
212
	stations = data;
213
	afficherTitreCarte();
214
 
215
	$.each(stations.points, function (index, station) {
216
		if(station != null) {
1039 aurelien 217
			var nouveauMarqueur = creerMarqueur(station);
218
			marqueurs.push(nouveauMarqueur);
1035 aurelien 219
		}
220
	});
221
}
222
 
980 jpm 223
function creerMarqueur(station) {
1037 aurelien 224
	var titre = '';
225
	if(station.nbreMarqueur) {
226
		titre = station.nbreMarqueur+' points renseignés';
227
	} else {
228
		if(station.nom) {
229
			titre = station.nom;
230
		}
231
	}
232
	//var titre = station['nbreMarqueur'];
980 jpm 233
	var icone = attribuerImageMarqueur(station['id'], station['nbreMarqueur']);
234
	var latLng = new google.maps.LatLng(station['lat'], station['lng']);
235
	var marqueur = new google.maps.Marker({
236
		position: latLng,
237
		icon: icone,
238
		title: ''+titre,
239
		map: map,
240
		stationInfos: station
241
	});
242
	attribuerListenerClick(marqueur, station['id']);
243
	marqueur.setMap(map);
244
    return marqueur;
245
}
246
 
1039 aurelien 247
function rendrePointsVisibles(bounds) {
248
	map.setCenter(bounds.getCenter());
249
	map.fitBounds(bounds);
250
	map.panToBounds(bounds);
251
}
252
 
1032 aurelien 253
function programmerRafraichissementCarteSauv() {
980 jpm 254
 
836 jpm 255
	var points = [];
256
	var bounds = new google.maps.LatLngBounds();
980 jpm 257
	for (var i = 0; i < stations.points.length; ++i) {
258
		var point = stations.points[i];
259
		var maLatLng = new google.maps.LatLng(point.lat, point.lng);
260
		var stationImage = attribuerImageMarqueur(point.id);
836 jpm 261
		var point = new google.maps.Marker({
262
			position: maLatLng,
263
			map: map,
980 jpm 264
			icon: stationImage,
265
			stationInfos: point
836 jpm 266
		});
267
		bounds.extend(maLatLng);
980 jpm 268
		google.maps.event.addListener(point, 'click', surClickMarqueur);
836 jpm 269
		points.push(point);
270
	}
271
 
272
	if (pointsOrigine == null && boundsOrigine == null) {
273
		pointsOrigine = points;
274
		boundsOrigine = bounds;
275
	}
276
 
277
	executerMarkerClusterer(points, bounds);
278
}
279
 
980 jpm 280
function attribuerImageMarqueur(id, nbreMarqueur) {
281
	var marqueurImage = null;
282
	if (etreMarqueurCommune(id)) {
283
		marqueurImage = new google.maps.MarkerImage(communeImageUrl, new google.maps.Size(24, 32));
284
	} else if (etreMarqueurStation(id)) {
285
		marqueurImage = new google.maps.MarkerImage(pointImageUrl, new google.maps.Size(16, 16));
286
	} else if (etreMarqueurGroupe(id)) {
287
		var type = 0;
288
		var largeur = 0;
289
		var hauteur = 0;
290
		if (nbreMarqueur != null) {
291
			if (nbreMarqueur >= 2 && nbreMarqueur < 100 ) {
292
				type = '1';
293
				largeur = 53;
294
				hauteur = 52;
295
			} else if (nbreMarqueur >= 100 && nbreMarqueur < 1000 ) {
296
				type = '2';
297
				largeur = 56;
298
				hauteur = 55;
299
			} else if (nbreMarqueur >= 1000 && nbreMarqueur < 10000 ) {
300
				type = '3';
301
				largeur = 66;
302
				hauteur = 65;
303
			} else if (nbreMarqueur >= 10000 && nbreMarqueur < 20000 ) {
304
				type = '4';
305
				largeur = 78;
306
				hauteur = 77;
307
			} else if (nbreMarqueur >= 20000) {
308
				type = '5';
309
				largeur = 66;
310
				hauteur = 65;
311
			}
312
		}
313
		groupeImageUrl = groupeImageUrlTpl.replace(/\{type\}/, type);
314
		groupeImageUrl = groupeImageUrl.replace(/\{nbre\}/, nbreMarqueur);
315
		marqueurImage = new google.maps.MarkerImage(groupeImageUrl, new google.maps.Size(largeur, hauteur));
316
	}
317
	return marqueurImage
318
}
319
 
320
function attribuerListenerClick(marqueur, id) {
321
	if (etreMarqueurCommune(id) || etreMarqueurStation(id)) {
322
		google.maps.event.addListener(marqueur, 'click', surClickMarqueur);
323
	} else if (etreMarqueurGroupe(id)) {
324
		google.maps.event.addListener(marqueur, 'click', surClickGroupe);
325
	}
326
}
327
 
1032 aurelien 328
function surClickMarqueur(event) {
1039 aurelien 329
 
330
	if(infoBulleOuverte) {
331
	    infoBulle.close();
332
	}
333
 
1035 aurelien 334
	pointClique = this;
980 jpm 335
	infoBulle.open(map, this);
336
	actualiserPagineur();
1035 aurelien 337
 
338
	var limites = map.getBounds();
339
	var centre = limites.getCenter();
340
	var nordEst = limites.getNorthEast();
341
	var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
342
	map.panTo(centreSudLatLng);
343
 
980 jpm 344
	afficherInfoBulle();
345
}
346
 
347
function surClickGroupe() {
348
	map.setCenter(this.getPosition());
349
	var nouveauZoom = map.getZoom() + 2;
350
	map.setZoom(nouveauZoom);
351
	mgr.clearMarkers();
352
}
353
 
354
function etreMarqueurGroupe(id) {
355
	var groupe = false;
356
	var motif = /^GROUPE/;
357
	if (motif.test(id)) {
358
		groupe = true;
359
	}
360
	return groupe;
361
}
362
 
363
function etreMarqueurCommune(id) {
364
	var commune = false;
365
	var motif = /^COMMUNE:/;
366
	if (motif.test(id)) {
367
		commune = true;
368
	}
369
	return commune;
370
}
371
 
372
function etreMarqueurStation(id) {
373
	var station = false;
374
	var motif = /^STATION:/;
375
	if (motif.test(id)) {
376
		station = true;
377
	}
378
	return station;
379
}
380
 
381
function deplacerCarteSurPointClique() {
915 jpm 382
	map.panTo(pointClique.position);
383
}
384
 
385
function executerMarkerClusterer(points, bounds) {
386
	if (markerClusterer) {
387
		markerClusterer.clearMarkers();
836 jpm 388
	}
980 jpm 389
	markerClusterer = new MarkerClusterer(map, points, {gridSize: 50, maxZoom: 18});
915 jpm 390
	map.fitBounds(bounds);
391
}
392
 
393
/*+--------------------------------------------------------------------------------------------------------+*/
394
// INFO BULLE
1032 aurelien 395
var infoBulleOuverte = false;
915 jpm 396
function initialiserInfoBulle() {
397
	google.maps.event.addListener(infoBulle, 'domready', initialiserContenuInfoBulle);
980 jpm 398
	google.maps.event.addListener(infoBulle, 'closeclick', surFermetureInfoBulle);
399
	google.maps.event.addListener(infoBulle, 'content_changed', definirLargeurInfoBulle);
915 jpm 400
}
401
 
980 jpm 402
function surFermetureInfoBulle() {
1032 aurelien 403
	infoBulleOuverte = false;
404
	programmerRafraichissementCarte();
980 jpm 405
}
406
 
407
function centrerInfoBulle() {
408
	var limites = map.getBounds();
409
	var centre = limites.getCenter();
410
	var nordEst = limites.getNorthEast();
411
	var centreSudLatLng = new google.maps.LatLng(nordEst.lat(), centre.lng());
412
	map.panTo(centreSudLatLng);
413
}
414
 
915 jpm 415
function afficherInfoBulle() {
416
	var obsHtml = $("#tpl-obs").html();
980 jpm 417
	var largeur = definirLargeurInfoBulle();
418
	obsHtml = obsHtml.replace(/\{largeur\}/, largeur);
915 jpm 419
	infoBulle.setContent(obsHtml);
1035 aurelien 420
	chargerObs(0, 0);
1032 aurelien 421
	infoBulleOuverte = true;
915 jpm 422
}
423
 
980 jpm 424
function definirLargeurInfoBulle() {
425
	var largeurViewPort = $(window).width();
426
	var lageurInfoBulle = null;
427
	if (largeurViewPort < 800) {
428
		largeurInfoBulle = 400;
429
	} else if (largeurViewPort >= 800 && largeurViewPort < 1200) {
430
		largeurInfoBulle = 500;
431
	} else if (largeurViewPort >= 1200) {
432
		largeurInfoBulle = 600;
433
	}
434
	return largeurInfoBulle;
435
}
436
 
915 jpm 437
function afficherMessageChargement(element) {
438
	if ($('#chargement').get() == '') {
439
		$('#tpl-chargement').tmpl().appendTo(element);
440
	}
441
}
442
 
1035 aurelien 443
function afficherMessageChargementTitreInfoBulle() {
444
	$("#obs-station-titre").text("Chargement des observations");
445
}
446
 
915 jpm 447
function supprimerMessageChargement() {
448
	$('#chargement').remove();
449
}
450
 
451
function chargerObs(depart, total) {
939 jpm 452
	if (depart == 0 || depart < total) {
915 jpm 453
		var limite = 300;
454
		if (depart == 0) {
1036 aurelien 455
			viderTableauObs();
915 jpm 456
		}
1035 aurelien 457
 
939 jpm 458
		var urlObs = observationsUrl+'&start={start}&limit='+limite;
980 jpm 459
		urlObs = urlObs.replace(/\{stationId\}/g, pointClique.stationInfos.id);
939 jpm 460
		urlObs = urlObs.replace(/\{nt\}/g, nt);
915 jpm 461
		urlObs = urlObs.replace(/\{start\}/g, depart);
462
 
463
		$.getJSON(urlObs, function(observations){
1036 aurelien 464
			surRetourChargementObs(observations, depart, total);
1035 aurelien 465
			chargerObs(depart+limite, observations.total);
915 jpm 466
		});
1036 aurelien 467
	}
468
}
469
 
470
function viderTableauObs() {
471
	obsStation = new Array();
472
	surClicPagePagination(0, null);
473
}
474
 
475
function surRetourChargementObs(observations, depart, total) {
476
	obsStation = obsStation.concat(observations.observations);
477
	if (depart == 0) {
478
		actualiserInfosStation(observations);
479
		creerTitreInfoBulle();
1041 aurelien 480
		surClicPagePagination(0, null);
915 jpm 481
	}
1036 aurelien 482
 
1039 aurelien 483
	afficherPagination();
1036 aurelien 484
	actualiserPagineur();
1041 aurelien 485
	selectionnerOnglet("#obs-vue-"+pagineur.format);
915 jpm 486
}
487
 
1035 aurelien 488
function actualiserInfosStation(infos) {
489
	pointClique.stationInfos.commune = infos.commune;
490
	pointClique.stationInfos.obsNbre = infos.total;
491
}
492
 
493
function creerTitreInfoBulle() {
494
	$("#obs-total").text(station.obsNbre);
495
	$("#obs-commune").text(station.commune);
496
	var titre = '';
497
	titre += pointClique.stationInfos.obsNbre+' observation';
498
	titre += (pointClique.stationInfos.obsNbre > 1) ? 's': '' ;
499
	titre += ' pour ';
500
	if (etreMarqueurCommune(pointClique.stationInfos.id)) {
501
		nomStation = 'la commune : ';
502
	} else {
503
		nomStation = 'la station : ';
504
	}
505
	titre += pointClique.stationInfos.nom;
506
	$("#obs-station-titre").text(titre);
507
}
508
 
939 jpm 509
function actualiserPagineur() {
980 jpm 510
	pagineur.stationId = pointClique.stationInfos.id;
1035 aurelien 511
	pagineur.total = pointClique.stationInfos.obsNbre;
939 jpm 512
	if (pagineur.total > 4) {
513
		pagineur.format = 'tableau';
514
	} else {
515
		pagineur.format = 'liste';
516
	}
517
}
518
 
915 jpm 519
function afficherPagination(observations) {
520
	$(".navigation").pagination(pagineur.total, {
521
		items_per_page:pagineur.limite,
522
		callback:surClicPagePagination,
523
		next_text:'Suivant',
524
		prev_text:'Précédent',
525
		prev_show_always:false,
526
		num_edge_entries:1,
953 jpm 527
		num_display_entries:4,
915 jpm 528
		load_first_page:true
529
	});
530
}
531
 
532
function surClicPagePagination(pageIndex, paginationConteneur) {
533
	var index = pageIndex * pagineur.limite;
534
	var indexMax = index + pagineur.limite;
535
	pagineur.depart = index;
536
	obsPage = new Array();
537
    for(index; index < indexMax; index++) {
538
    	obsPage.push(obsStation[index]);
539
    }
540
 
541
    supprimerMessageChargement();
542
    mettreAJourObservations();
543
	return false;
544
}
545
 
546
function mettreAJourObservations() {
547
	$("#obs-"+pagineur.format+"-lignes").empty();
548
   	$("#obs-vue-"+pagineur.format).css('display', 'block');
549
   	$(".obs-conteneur").css('counter-reset', 'item '+pagineur.depart);
550
	$("#tpl-obs-"+pagineur.format).tmpl(obsPage).appendTo("#obs-"+pagineur.format+"-lignes");
551
 
552
	// Actualisation de Fancybox
941 jpm 553
	ajouterFomulaireContact("a.contact");
915 jpm 554
	if (pagineur.format == 'liste') {
836 jpm 555
		ajouterGaleriePhoto("a.cel-img");
556
	}
557
}
558
 
915 jpm 559
function initialiserContenuInfoBulle() {
980 jpm 560
	afficherMessageChargement('#observations');
561
	cacherContenuOnglets();
915 jpm 562
	afficherOnglets();
563
	ajouterTableauTriable("#obs-tableau");
564
	afficherTextStationId();
565
	corrigerLargeurInfoWindow();
566
}
567
 
980 jpm 568
function cacherContenuOnglets() {
569
	$("#obs-vue-tableau").css("display", "none");
570
	$("#obs-vue-liste").css("display", "none");
571
}
572
 
915 jpm 573
function afficherOnglets() {
574
	var $tabs = $('#obs').tabs();
575
	$('#obs').bind('tabsselect', function(event, ui) {
576
		if (ui.panel.id == 'obs-vue-tableau') {
577
			surClicAffichageTableau();
578
		} else if (ui.panel.id == 'obs-vue-liste') {
579
			surClicAffichageListe();
580
		}
581
	});
980 jpm 582
	if (pointClique.stationInfos.nbre > 4) {
583
		$tabs.tabs('select', "#obs-vue-tableau");
584
	} else {
585
		$tabs.tabs('select', "#obs-vue-liste");
586
	}
587
 
915 jpm 588
}
589
 
939 jpm 590
function selectionnerOnglet(onglet) {
980 jpm 591
	$(onglet).css('display', 'block');
939 jpm 592
	$('#obs').tabs('select', onglet);
593
}
594
 
915 jpm 595
function afficherTextStationId() {
980 jpm 596
	$('#obs-station-id').text(pointClique.stationInfos.id);
915 jpm 597
}
598
 
599
function corrigerLargeurInfoWindow() {
953 jpm 600
	$("#info-bulle").width($("#info-bulle").width() - 17);
915 jpm 601
}
602
 
603
function surClicAffichageTableau(event) {
604
	pagineur.format = 'tableau';
605
	mettreAJourObservations();
606
	mettreAJourTableauTriable("#obs-tableau");
607
}
608
 
609
function surClicAffichageListe(event) {
610
	pagineur.format = 'liste';
611
	mettreAJourObservations();
612
	ajouterGaleriePhoto("a.cel-img");
613
}
614
 
836 jpm 615
function ajouterTableauTriable(element) {
616
	// add parser through the tablesorter addParser method
617
	$.tablesorter.addParser({
618
		// Définition d'un id unique pour ce parsseur
619
		id: 'date_cel',
620
		is: function(s) {
915 jpm 621
			// doit retourner false si le parsseur n'est pas autodétecté
622
			return /^\s*\d{2}[\/-]\d{2}[\/-]\d{4}\s*$/.test(s);
836 jpm 623
		},
915 jpm 624
		format: function(date) {
836 jpm 625
			// Transformation date jj/mm/aaaa en aaaa/mm/jj
915 jpm 626
			date = date.replace(/^\s*(\d{2})[\/-](\d{2})[\/-](\d{4})\s*$/, "$3/$2/$1");
836 jpm 627
			// Remplace la date par un nombre de millisecondes pour trier numériquement
915 jpm 628
			return $.tablesorter.formatFloat(new Date(date).getTime());
836 jpm 629
		},
630
		// set type, either numeric or text
631
		type: 'numeric'
632
	});
633
	$(element).tablesorter({
634
        headers: {
915 jpm 635
			1: {
636
            	sorter:'date_cel'
637
        	}
638
    	}
639
	});
836 jpm 640
}
641
 
915 jpm 642
function mettreAJourTableauTriable(element) {
643
	$(element).trigger('update');
644
}
645
 
836 jpm 646
function ajouterGaleriePhoto(element) {
647
	$(element).fancybox({
915 jpm 648
		transitionIn:'elastic',
649
		transitionOut:'elastic',
650
		speedIn	:600,
651
		speedOut:200,
652
		overlayShow:true,
653
		titleShow:true,
654
		titlePosition:'inside',
655
		titleFormat:function (titre, currentArray, currentIndex, currentOpts) {
656
			var motif = /urn:lsid:tela-botanica[.]org:cel:img([0-9]+)$/;
657
			motif.exec(titre);
658
			var id = RegExp.$1;
659
			var info = $('#cel-info-'+id).clone().html();
660
			var tpl =
661
				'<div class="cel-legende">'+
662
				'<p class="cel-legende-vei">'+'Image n°' + (currentIndex + 1) + ' sur ' + currentArray.length +'<\/p>'+
663
				(titre && titre.length ? '<p>'+info+'<\/p>' : '' )+
664
				'<\/div>';
665
			return tpl;
836 jpm 666
		}
1032 aurelien 667
	}).live('click', function(e) {
668
		if (e.stopPropagation) {
669
			e.stopPropagation();
670
		}
671
		return false;
672
	});
836 jpm 673
}
674
 
941 jpm 675
function ajouterFomulaireContact(element) {
676
	$(element).fancybox({
677
		transitionIn:'elastic',
678
		transitionOut:'elastic',
679
		speedIn	:600,
680
		speedOut:200,
681
		scrolling: 'no',
682
		titleShow: false,
683
		onStart: function(selectedArray, selectedIndex, selectedOpts) {
684
			var element = selectedArray[selectedIndex];
685
 
686
			var motif = / contributeur-([0-9]+)$/;
687
			motif.exec($(element).attr('class'));
688
			var id = RegExp.$1;
950 jpm 689
			//console.log('Destinataire id : '+id);
941 jpm 690
			$("#fc_destinataire_id").attr('value', id);
691
 
692
			var motif = / obs-([0-9]+) /;
693
			motif.exec($(element).attr('class'));
694
			var id = RegExp.$1;
950 jpm 695
			//console.log('Obs id : '+id);
941 jpm 696
			chargerInfoObsPourMessage(id);
697
		},
698
		onCleanup: function() {
950 jpm 699
			//console.log('Avant fermeture fancybox');
941 jpm 700
			$("#fc_destinataire_id").attr('value', '');
701
			$("#fc_sujet").attr('value', '');
702
			$("#fc_message").text('');
703
		},
704
		onClosed: function(e) {
950 jpm 705
			//console.log('Fermeture fancybox');
941 jpm 706
			if (e.stopPropagation) {
707
				e.stopPropagation();
708
			}
709
			return false;
710
		}
711
	});
712
}
713
 
714
function chargerInfoObsPourMessage(idObs) {
980 jpm 715
	var nomSci = jQuery.trim($(".cel-obs-"+idObs+" .nom-sci:eq(0)").text());
716
	var date = jQuery.trim($(".cel-obs-"+idObs+" .date:eq(0)").text());
717
	var lieu = jQuery.trim($(".cel-obs-"+idObs+" .lieu:eq(0)").text());
953 jpm 718
	var sujet = "Observation #"+idObs+" de "+nomSci;
719
	var message = "\n\n\n\n\n\n\n\n--\nConcerne l'observation de \""+nomSci+'" du "'+date+'" au lieu "'+lieu+'".';
941 jpm 720
	$("#fc_sujet").attr('value', sujet);
721
	$("#fc_message").text(message);
722
}
723
 
724
function initialiserFormulaireContact() {
950 jpm 725
	//console.log('Initialisation du form contact');
941 jpm 726
	$("#form-contact").validate({
727
		rules: {
728
			fc_sujet : "required",
729
			fc_message : "required",
953 jpm 730
			fc_utilisateur_courriel : {
731
				required : true,
732
				email : true}
941 jpm 733
		}
734
	});
735
	$("#form-contact").bind("submit", envoyerCourriel);
950 jpm 736
	$("#fc_annuler").bind("click", function() {$.fancybox.close();});
941 jpm 737
 
738
}
739
 
740
function envoyerCourriel() {
950 jpm 741
	//console.log('Formulaire soumis');
941 jpm 742
	if ($("#form-contact").valid()) {
950 jpm 743
		//console.log('Formulaire valide');
941 jpm 744
		//$.fancybox.showActivity();
745
		var destinataireId = $("#fc_destinataire_id").attr('value');
746
		var urlMessage = "http://www.tela-botanica.org/service:annuaire:Utilisateur/"+destinataireId+"/message"
747
		var erreurMsg = "";
748
		var donnees = new Array();
749
		$.each($(this).serializeArray(), function (index, champ) {
750
			var cle = champ.name;
751
			cle = cle.replace(/^fc_/, '');
953 jpm 752
 
980 jpm 753
			if (cle == 'sujet') {
953 jpm 754
				champ.value += " - Carnet en ligne - Tela Botanica";
755
			}
756
			if (cle == 'message') {
757
				champ.value += "\n--\n"+
980 jpm 758
					"Ce message vous est envoyé par l'intermédiaire du widget carto "+
953 jpm 759
					"du Carnet en Ligne du réseau Tela Botanica.\n"+
760
					"http://www.tela-botanica.org/widget:cel:carto";
761
			}
762
 
941 jpm 763
			donnees[index] = {'name':cle,'value':champ.value};
764
		});
765
		$.ajax({
766
			type : "POST",
767
			cache : false,
768
			url : urlMessage,
769
			data : donnees,
770
			beforeSend : function() {
771
				$(".msg").remove();
772
			},
773
			success : function(data) {
774
				$("#fc-zone-dialogue").append('<pre class="msg info">'+data.message+'</pre>');
775
			},
776
			error : function(jqXHR, textStatus, errorThrown) {
777
				erreurMsg += "Erreur Ajax :\ntype : "+textStatus+' '+errorThrown+"\n";
778
				reponse = jQuery.parseJSON(jqXHR.responseText);
779
				if (reponse != null) {
780
					$.each(reponse, function (cle, valeur) {
781
						erreurMsg += valeur + "\n";
782
					});
783
				}
784
			},
785
			complete : function(jqXHR, textStatus) {
786
				var debugMsg = '';
787
				if (jqXHR.getResponseHeader("X-DebugJrest-Data") != '') {
788
					debugInfos = jQuery.parseJSON(jqXHR.getResponseHeader("X-DebugJrest-Data"));
789
					if (debugInfos != null) {
790
						$.each(debugInfos, function (cle, valeur) {
791
							debugMsg += valeur + "\n";
792
						});
793
					}
794
				}
795
				if (erreurMsg != '') {
796
					$("#fc-zone-dialogue").append('<p class="msg">'+
797
							'Une erreur est survenue lors de la transmission de votre message.'+'<br />'+
798
							'Vous pouvez signaler le disfonctionnement à <a href="'+
799
							'mailto:cel@tela-botanica.org'+'?'+
980 jpm 800
							'subject=Disfonctionnement du widget carto'+
941 jpm 801
							"&body="+erreurMsg+"\nDébogage :\n"+debugMsg+
802
							'">cel@tela-botanica.org</a>.'+
803
							'</p>');
804
				}
805
				if (DEBUG) {
806
					console.log('Débogage : '+debugMsg);
807
				}
950 jpm 808
				//console.log('Débogage : '+debugMsg);
809
				//console.log('Erreur : '+erreurMsg);
941 jpm 810
			}
811
		});
812
	}
813
	return false;
814
}
915 jpm 815
/*+--------------------------------------------------------------------------------------------------------+*/
816
// PANNEAU LATÉRAL
836 jpm 817
 
915 jpm 818
function initialiserAffichagePanneauLateral() {
819
	$('#panneau-lateral').height($(window).height() - 35);
841 jpm 820
 
915 jpm 821
	if (nt == '*') {
822
		$('#pl-ouverture').bind('click', afficherPanneauLateral);
823
		$('#pl-fermeture').bind('click', cacherPanneauLateral);
836 jpm 824
	}
939 jpm 825
	chargerTaxons(0, 0);
836 jpm 826
}
827
 
939 jpm 828
function chargerTaxons(depart, total) {
829
	if (depart == 0 || depart < total) {
830
		var limite = 7000;
950 jpm 831
		//console.log("Chargement des taxons de "+depart+" à "+(depart+limite));
939 jpm 832
		var urlTax = taxonsUrl+'&start={start}&limit='+limite;
833
		urlTax = urlTax.replace(/\{start\}/g, depart);
834
		$.getJSON(urlTax, function(infos) {
835
			taxonsCarte = taxonsCarte.concat(infos.taxons);
950 jpm 836
			//console.log("Nbre taxons :"+taxonsCarte.length);
939 jpm 837
			chargerTaxons(depart+limite, infos.total);
838
		});
839
	} else {
840
		if (nt == '*') {
841
			afficherTaxons();
842
		}
980 jpm 843
		afficherTitreCarte();
939 jpm 844
	}
845
}
846
 
847
function afficherTaxons() {
980 jpm 848
	$(".plantes-nbre").text(taxonsCarte.length);
939 jpm 849
	$("#tpl-taxons-liste").tmpl({'taxons':taxonsCarte}).appendTo("#pl-corps");
850
	$('.taxon').live('click', filtrerParTaxon);
851
}
852
 
853
 
915 jpm 854
function afficherPanneauLateral() {
836 jpm 855
	$('#panneau-lateral').width(300);
856
	$('#pl-contenu').css('display', 'block');
857
	$('#pl-ouverture').css('display', 'none');
858
	$('#pl-fermeture').css('display', 'block');
859
	$('#carte').css('left', '300px');
860
 
861
	google.maps.event.trigger(map, 'resize');
862
};
863
 
915 jpm 864
function cacherPanneauLateral() {
836 jpm 865
	$('#panneau-lateral').width(24);
866
	$('#pl-contenu').css('display', 'none');
867
	$('#pl-ouverture').css('display', 'block');
868
	$('#pl-fermeture').css('display', 'none');
869
	$('#carte').css('left', '24px');
870
 
871
	google.maps.event.trigger(map, 'resize');
872
};
873
 
874
function filtrerParTaxon() {
875
	var ntAFiltrer = $('.nt', this).text();
876
	infoBulle.close();
1037 aurelien 877
	var zoom = map.getZoom();
878
	var NELatLng = map.getBounds().getNorthEast().lat()+'|'+map.getBounds().getNorthEast().lng();
879
	var SWLatLng = map.getBounds().getSouthWest().lat()+'|'+map.getBounds().getSouthWest().lng();
880
 
836 jpm 881
	$('#taxon-'+nt).removeClass('taxon-actif');
882
	if (nt == ntAFiltrer) {
883
		nt = '*';
1037 aurelien 884
		stationsUrl = stationsUrl.replace(/num_taxon=[*0-9]+/, 'num_taxon='+nt);
885
		chargerMarqueurs(zoom, NELatLng, SWLatLng);
836 jpm 886
	} else {
1035 aurelien 887
		stationsUrl = stationsUrl.replace(/num_taxon=[*0-9]+/, 'num_taxon='+ntAFiltrer);
888
		url = stationsUrl;
889
		url += '&zoom='+zoom+
890
			'&ne='+NELatLng+
891
			'&sw='+SWLatLng;
892
		requeteChargementPoints = $.getJSON(url, function (stationsFiltrees) {
939 jpm 893
			stations = stationsFiltrees;
836 jpm 894
			nt = ntAFiltrer;
895
			$('#taxon-'+nt).addClass('taxon-actif');
1035 aurelien 896
			rafraichirMarqueurs(stations);
836 jpm 897
		});
898
	}
899
};
900
 
915 jpm 901
/*+--------------------------------------------------------------------------------------------------------+*/
902
// FONCTIONS UTILITAIRES
903
 
980 jpm 904
function ouvrirPopUp(element, nomPopUp, event) {
905
	var options =
906
		'width=650,'+
907
		'height=600,'+
908
		'scrollbars=yes,'+
909
		'directories=no,'+
910
		'location=no,'+
911
		'menubar=no,'+
912
		'status=no,'+
913
		'toolbar=no';
914
	var popUp = window.open(element.href, nomPopUp, options);
915
	if (window.focus) {
916
		popUp.focus();
917
	}
918
	return arreter(event);
919
};
920
 
921
function ouvrirNouvelleFenetre(element, event) {
922
	window.open(element.href);
923
	return arreter(event);
924
}
925
 
915 jpm 926
function arreter(event) {
927
	if (event.stopPropagation) {
928
		event.stopPropagation();
929
	} else if (window.event) {
930
		window.event.cancelBubble = true;
931
	}
980 jpm 932
	if (event.preventDefault) {
933
		event.preventDefault();
934
	}
935
	event.returnValue = false;
915 jpm 936
	return false;
937
}
938
 
836 jpm 939
/**
940
 * +-------------------------------------+
941
 * Number.prototype.formaterNombre
942
 * +-------------------------------------+
943
 * Params (facultatifs):
944
 * - Int decimales: nombre de decimales (exemple: 2)
945
 * - String signe: le signe precedent les decimales (exemple: "," ou ".")
946
 * - String separateurMilliers: comme son nom l'indique
947
 * Returns:
948
 * - String chaine formatee
949
 * @author	::mastahbenus::
950
 * @author	Jean-Pascal MILCENT <jpm@tela-botanica.org> : ajout détection auto entier/flotant
951
 * @souce	http://www.javascriptfr.com/codes/FORMATER-NOMBRE-FACON-NUMBER-FORMAT-PHP_40060.aspx
952
 */
953
Number.prototype.formaterNombre = function (decimales, signe, separateurMilliers) {
954
	var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
955
 
956
	function is_int(nbre) {
957
		nbre = nbre.replace(',', '.');
958
		return !(parseFloat(nbre)-parseInt(nbre) > 0);
959
	}
960
 
961
	if (decimales == undefined) {
962
		if (is_int(_sNombre)) {
963
			decimales = 0;
964
		} else {
965
			decimales = 2;
966
		}
967
	}
968
	if (signe == undefined) {
969
		if (is_int(_sNombre)) {
970
			signe = '';
971
		} else {
972
			signe = '.';
973
		}
974
	}
975
	if (separateurMilliers == undefined) {
976
		separateurMilliers = ' ';
977
	}
978
 
979
	function separeMilliers (sNombre) {
980
		var sRetour = "";
981
		while (sNombre.length % 3 != 0) {
982
			sNombre = "0"+sNombre;
983
		}
984
		for (i = 0; i < sNombre.length; i += 3) {
985
			if (i == sNombre.length-1) separateurMilliers = '';
986
			sRetour += sNombre.substr(i, 3) + separateurMilliers;
987
		}
988
		while (sRetour.substr(0, 1) == "0") {
989
			sRetour = sRetour.substr(1);
990
		}
991
		return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
992
	}
993
 
994
	if (_sNombre.indexOf('.') == -1) {
995
		for (i = 0; i < decimales; i++) {
996
			_sDecimales += "0";
997
		}
998
		_sRetour = separeMilliers(_sNombre) + signe + _sDecimales;
999
	} else {
1000
		var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
1001
		if (sDecimalesTmp.length > decimales) {
1002
			var nDecimalesManquantes = sDecimalesTmp.length - decimales;
1003
			var nDiv = 1;
1004
			for (i = 0; i < nDecimalesManquantes; i++) {
1005
				nDiv *= 10;
1006
			}
1007
			_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
1008
		}
1009
		_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
1010
	}
1011
	return _sRetour;
915 jpm 1012
}
1013
 
1014
function debug(objet) {
1015
	var msg = '';
1016
	if (objet != null) {
1017
		$.each(objet, function (cle, valeur) {
1018
			msg += cle+":"+valeur + "\n";
1019
		});
1020
	} else {
1021
		msg = "La variable vaut null.";
1022
	}
1023
	console.log(msg);
836 jpm 1024
}