/**
 * @author apellet
 */
var icon_vert = null;
var icon_rouge = null;
var map = null;

function initMap ( )
{
	if ( GBrowserIsCompatible() )
	{
		map = new GMap2 ( document.getElementById ('map') );
		map.setCenter ( new GLatLng(46,2), 6 );
		
		//geocoder = new GClientGeocoder ();
		//map.enableScrollWheelZoom() ;
		map.addControl ( new GOverviewMapControl () );
		
	
		var pan_control = new PanControl ( map, "./images/carto/control/", 25, 45 );
		map.addControl ( pan_control );		
		
		//Barre de zoom
		jQuery('#mapzoomcontrol').gmap_zoom( {  
			map: map,
			b_center:true,
			size : 25,
			prefix_path:'',
			rep_img:'./styles/controls/images/'
		});
		
		//Type de plan
		jQuery('#maptypecontrol').gmap_type( {  
			map: map,  
			types: [0,1,2,3],
			type:1,
			f_size:10
		});
		
		var icon_basic = new GIcon ();
		icon_basic.iconSize = new GSize ( 32, 32 );
		icon_basic.shadowSize = new GSize (32,40);
		icon_basic.iconAnchor = new GPoint  ( 16, 31 );
		icon_basic.infoWindowAnchor = new GPoint  ( 16, 0 );

		icon_vert = new GIcon( icon_basic, "./images/carto/vert.png" );
		icon_rouge = new GIcon( icon_basic, "./images/carto/rouge.png" );

	
	}
}

function hideAll ()
{
	for ( var i in all_entreprise )
	{
		var obj = all_entreprise[i];
		if ( obj.m != null )
		{
			obj.m.hide ();
		}
		
	}
}

function displayEntreprises ()
{
	var cur_bounds = new GLatLngBounds ();
	var nb = 0;
	var ic = icon_vert;
	var dispo = "Disponible";
	
	map.clearOverlays();

	for ( var i in all_entreprise )
	{
		var obj = all_entreprise [  i  ];
		if ( obj.dispo_actuelle == 0 )
		{
			ic = icon_rouge;
			dispo = "Indisponible";
		}
		else
		{
			ic = icon_vert;
			dispo = "Disponible";
		}
		nb ++;
		var m = new GMarker ( new GLatLng ( obj.lat, obj.lng ), {title:obj.libelle+" : " + dispo, icon:ic} );
		map.addOverlay ( m );
		GEvent.bind ( m, "click", this, GEvent.callbackArgs (this, openInfoWindow, obj.id) );
		
		
		obj['m'] = m;
		cur_bounds.extend ( m.getLatLng () );
			
		
		
	}
	
	var z = map.getBoundsZoomLevel(cur_bounds);
	var c = cur_bounds.getCenter ();
	if ( nb == 1 )
	{
		z = 8;
	}
	else if ( nb == 0 )
	{
		z = map.getZoom();
		c = map.getCenter();
	}
	
	if  ( z > 14 )
	{
		z = 14;
	}
	map.setCenter ( c, z );
		
	
}

function openInfoWindow ( id_ent )
{
	var obj = all_entreprise [id_ent ];
	
	if ( obj.load_info == 0 )
	{
		loadInfoEntreprise ( obj );
	}
	else
	{
		obj.m.openInfoWindowHTML ( obj.fiche_info );
	}
}

