/*
Fonctions utilitaires pour affichage des cartes Google Maps
*/

var map;
var center;
var dZoom;

var window_left;
var window_right;
var window_top;
var window_bottom;

var default_dZoom = 11;

// Détermine la taille de la carte dynamiquement en fonction de la liste de points passée en paramètres
function GetWindowInfo( geopoints, dept) {
    trouve=false;
    if (geopoints.length > 0) {
	for (var i=0; i<geopoints.length; i++) {
	    if (geopoints[i][7].substr(0,2)=='75') {
		var lat = 48.858616;
		var long  = 2.334355;
	    }
	    else {
		var lat	= geopoints[i][1];
		var long= geopoints[i][2];
	    }
	    if (i==0) {	
		window_left_max = lat;
		window_right_max  = lat;
		window_top_max  = long;
		window_bottom_max = long;
	    } else {
		if ( lat < window_left_max )	window_left_max		= lat;
		if ( lat > window_right_max )	window_right_max	= lat;
		if ( long < window_top_max )	window_top_max		= long;
		if ( long > window_bottom_max )	window_bottom_max	= long;
	    }

	    if ((typeof dept=='undefined') || (geopoints[i][7].substr(0,2)==dept)) { 
		if (!trouve) {
		    window_left = lat;
		    window_right  = lat;
		    window_top  = long;
		    window_bottom = long;
		    trouve=true;
		}
		else {
		    if ( lat < window_left )	window_left	= lat;
		    if ( lat > window_right )	window_right	= lat;
		    if ( long < window_top )	window_top	= long;
		    if ( long > window_bottom )	window_bottom	= long;
		}
		window_left	= parseFloat( window_left);
		window_right	= parseFloat( window_right);
		window_top	= parseFloat( window_top);
		window_bottom	= parseFloat( window_bottom);
	    }
	}
	if(!trouve)
	{	window_left	= parseFloat( window_left_max);
		window_right	= parseFloat( window_right_max);
		window_top	= parseFloat( window_top_max);
		window_bottom	= parseFloat( window_bottom_max);
	}
	else
	{	window_left	-= 0.05;
		window_right	+= 0.05;
		window_top	-= 0.05;
		window_bottom	+= 0.05;
	}
    }
}

// Recentre la carte sur la variable center
function resetCenter() {
	map.setZoom(dZoom);
	map.panTo(center);
}

// Initialisation de la carte avec les points passés en paramètre
function googleMapSetup( geopoints, events, dept) {
	if ( GBrowserIsCompatible() ) {
		GetWindowInfo( geopoints, dept);
		map		= new GMap2( document.getElementById('map_canvas'));
		center	= new GLatLng( (window_left+window_right)/2, (window_top+window_bottom)/2);

		dZoom	= map.getBoundsZoomLevel( new GLatLngBounds( new GLatLng( window_left,window_top), new GLatLng( window_right, window_bottom)));
		if ( dZoom > default_dZoom ) dZoom = default_dZoom;
 		if (dept==75 || dept==92 || dept==94) dZoom++;  // Patch effectué le 03/05/2010

		map.setCenter( center, dZoom);
		map.disableScrollWheelZoom();
		map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10)));  //GSmallMapControl
		//map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));

		var point;
		var marker;
		for ( var i=0 ; i<geopoints.length ; i++ ) 
		{	if (!geopoints[i][0]) { dZoom-=3; map.setZoom(dZoom); continue;}
		
			point	= new GLatLng( geopoints[i][1],geopoints[i][2]);
			marker	= new GMarker( point);

			marker.idPrestataire	= geopoints[i][0];
			marker.nom				= geopoints[i][3];
			marker.adresse			= geopoints[i][4];
			marker.cp				= geopoints[i][5];
			marker.ville			= geopoints[i][6];

         onclick=events['click']?events['click']:'';
			if (onclick) GEvent.addListener( marker, "click", function(){onclick(this);});
         ondblclick=events['dblclick']?events['dblclick']:'';
			if (ondblclick) GEvent.addListener( marker, "dblclick", function(){ondblclick(this);});
         onmouseover=events['mouseover']?events['mouseover']:'';
			if (onmouseover) GEvent.addListener( marker, "mouseover", function(){onmouseover(this);});
         onmouseout=events['mouseout']?events['mouseout']:'';
			if (onmouseout) GEvent.addListener( marker, "mouseout", function(){onmouseout(this);});
				
			map.addOverlay(marker);
		}
	}
}

