/*
* Map functions
*
* Copyright 2010 Shariffa MADAVJEE
* 
* Licensed under the Creative Commons 2.0
* 
*
* A l'appel de la page index.php, le fichier csvToPhp.php lis data.csv puis crée map_data.php
* Cette architecture a été privilégiée sur les fichiers kml pour, à terme, s'adapter facilement à la lecture d'une BD
* puis map_functions utilise les données de map_data et les traite afin de les afficher sur la carte.
*
*/

var map, manager;

var centerLatitude = -12, centerLongitude = 67, startZoom = 4;

/*var centerLatitude = 40.736462, centerLongitude = -73.98777, startZoom = 12;*/

/*gestion affichage bulle*/
function createMarkerClickHandler(marker, text, nom, date, distance, duree) {
	return function() {
		
		var chemin = new String();
		var link = new String();
		
		if (text=='91421'){
		chemin = 'oiseaux/Tiyab.png';
		link='http://www.reunion-parcnational.fr/Tiyab.html';
		}
			else if (text=='91419'){
			chemin = 'oiseaux/Kayamb.png';
			link='http://www.reunion-parcnational.fr/Kayamb.html';
			}
				else if (text=='91422'){
				chemin = 'oiseaux/Tikaf.png';
				link='http://www.reunion-parcnational.fr/Tikaf.html';
				}
					else if (text=='91424'){
					chemin = 'oiseaux/Dalon.png';
					link='http://www.reunion-parcnational.fr/Tamarin.html';
					}
			
		/*	
			marker.openInfoWindowHtml(
			'<img src="'+ chemin +'" alt="'+ nom +'" />' + '<br />' +
			'photo: B. Lequette - PNRun <br />' +
			'<h3>' + nom + '</h3> <br />' +
			'balise: '+ text + '<br />' +
			'date: ' + date + '<br />' +
			'distance parcourue: ' + distance + ' km <br />' +
			'durée depuis lâcher: ' + duree + ' heures <br />' +
			'<a href="' + link + '" target="_blank">Fiche détaillée</a>'
		);*/
		

		var tabs = [ 
			new GInfoWindowTab(nom,
			'<img src="'+ chemin +'" alt="'+ nom +'" />' + '<br />'
			),
			new GInfoWindowTab('Infos',
			'<b>balise: </b>'+ text + '<br />' +
			'<b>date: </b>' + date + '<br />' +
			'<b>distance parcourue: </b>' + distance + ' kms <br />' +
			'<b>durée depuis lâcher: </b>' + duree + ' heures <br />' +
			'<p><a href="' + link + '" target="_blank">Son historique et ses actualités</a></p>'
			)
		];
		
		marker.openInfoWindowTabsHtml(tabs);
		
		return false;
	};
}


function createMarker(pointData) {

	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	
	/* gestion des marqueurs personnalisés*/
	var icon = new GIcon();
	var color = new String();
	if (pointData.abbr=='91421'){
	icon.image = 'red-marker.png';
	color='red';
	}
		else if (pointData.abbr=='91419'){
		icon.image = 'green-marker.png';
		color='green';
		}
			else if (pointData.abbr=='91422'){
			icon.image = 'yellow-marker.png';
			color='yellow';
			}
				else if (pointData.abbr=='91424'){
				icon.image = 'pink-marker.png';
				color='pink';
				}
	icon.iconSize = new GSize(22, 22);
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(25, 7);
		   		
	opts = {
		"icon": icon,
		"clickable": true,
		//"labelText": pointData.abbr,
		"labelOffset": new GSize(-16, -16)
		
	};
	
	/* Création des marqueurs */
	var marker = new GMarker(latlng, opts);
	
/*Gestion du click sur le marqueur*/	
	var handler = createMarkerClickHandler(marker, pointData.abbr, pointData.name, pointData.date, pointData.distance, pointData.duree);
	GEvent.addListener(marker, "click", handler);
	
/*Gestion des labels*/
	/*
	var listItem = document.createElement('li');
	
	listItem.innerHTML = '<div class="'+ color +'"> </div><a href="' + pointData.wp + '">' + pointData.name + '</a>';
	
	listItem.getElementsByTagName('a')[0].onclick = handler;

	document.getElementById('sidebar-list').appendChild(listItem);
*/
	
	
	return marker;
}




function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case. 
	return 0;
}

function handleResize() {
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
}

function init() {
	handleResize();
	
	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl()); // ajout barre de zoom
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	map.addControl(new GMapTypeControl()); 
	map.addControl(new GScaleControl()); // ajout échelle
	map.setMapType(G_HYBRID_MAP);  // Vue satellite + informations cartographiques
	map.enableScrollWheelZoom(); //controle du zoom par la molette de la souris

	// Création tableau pour les polylines
	var tab=new Array();
	var tab2=new Array();
	var tab3=new Array();
	var tab4=new Array();
	var code=new String();
	
	// Ajout d'un marqueur pour chacune des positions
	for (var i=0 ; i<markers.length ; i++)
	{
		map.addOverlay(createMarker(markers[i]));
		
		// Ajout des polylines
		
			
		
		if (markers[i].abbr=='91421'){
		var coord=new GLatLng(markers[i].latitude,markers[i].longitude);
		tab.push(coord);
		code='#FC6355';
		}
		
		else if (markers[i].abbr=='91419'){
		var coord2=new GLatLng(markers[i].latitude,markers[i].longitude);
		tab2.push(coord2);
		code2='#00E13B';
		}
			else if (markers[i].abbr=='91422'){
			var coord3=new GLatLng(markers[i].latitude,markers[i].longitude);
			tab3.push(coord3);
			code3='#FFFF33';
			}
				else if (markers[i].abbr=='91424'){
				var coord4=new GLatLng(markers[i].latitude,markers[i].longitude);
				tab4.push(coord4);
				code4='#FF66CC';
				}
				
	}

	//Affichage polylines
	var poly=new GPolyline(tab,code);
	map.addOverlay(poly);
	
	var poly2=new GPolyline(tab2,code2);
	map.addOverlay(poly2);
	
	var poly3=new GPolyline(tab3,code3);
	map.addOverlay(poly3);
	
	var poly4=new GPolyline(tab4,code4);
	map.addOverlay(poly4);
	
/*
	manager = new GMarkerManager(map);
	
	// This is a sorting trick, don't worry too much about it.
	markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
	batch = [];
	
	for(id in markers) {
		batch.push(createMarker(markers[id]));
	}
	
	manager.addMarkers(batch, 0,17);
	manager.refresh();
	*/
}

window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;

