
//*************** INTITIALISATION DE LA LISTE DES CARTES **********************

var listeMaps = Array();
var map = '';
var markers = [];
var list_markers = [];

//***************** FONCTIONS GOOGLE MAPS ***********************************

/**
 * Cette fonction créer un nouveau marqueur google map et le renvoie
 * lat, lng : les coordonnées du marqueur
 * info : le texte à afficher dans la bulle lors d'un clic.
 * Si info = null alors l'icone ne comportera pas de description
 * bounds
 * (optionnel) icon : une image pour redéfinir l'affichage du marqueur
 */
function createMarker( lat, lng, info, bounds, icon, label, infobulle, map, node_id, params )
{

    var point = new GLatLng(lat, lng);
/*
    icon.iconAnchor = new GPoint(16, 16);
    icon.infoWindowAnchor = new GPoint(25, 7);
    opts = { 
        "icon": icon,
        "clickable": true,
        "labelText": label,
        "labelOffset": new GSize(-6, -10)
    };

    var marker = new LabeledMarker(point, opts);
*/
    var marker = new GMarker( point, {icon:icon} );

    if(info != null)
    {
        GEvent.addListener(marker, "click", function() {

            marker.openInfoWindowHtml( '<div class="location">'+$('#location_'+ node_id).html()+'</div>' );

            if( $('#location_'+ node_id).hasClass('empty') && infobulle == 'ajax' )
            {
                var url = "/content/view/infobulle/"+node_id;
                if( params != '' )
                {
                    url += '/(nodes)/'+params;
                }
                $.ajax({  	
                   type: "GET",  
                   url: url,
                   dataType: 'text/html',
                   success: function(databack) {  				   	
                     $('#location_'+node_id).html(databack);
                     marker.openInfoWindowHtml(   '<div class="location">'+$('#location_'+node_id).html()+'</div>');
                     $('#location_'+node_id).removeClass('empty');

                   },
                   error:function(XMLHttpRequest, textStatus, errorThrown) {
                       $("#ajaxregion").html('Error has occure');  
                   }
                });  
            }

        });
    }
    if (bounds)
    {
        bounds.extend(point);
    }
    return marker;      
}


/**
 * Cette fonction affiche une carte google maps dans un bloc
 * blockId : L'id du block dans lequel insérer la map
 * datas : Un tableau multidimensionnel contenant la liste des points
 * à afficher ainsi que leurs options. Exemple :
 *          data[i] = Array(
 *                      "latitude" => 33.3333
 *                      "longitude" => 22.22222
 *                      "icon" => "blue"
 *                      "contentobject_id" => 135
 *                  )
 * zoom : le niveau de zoom de la carte
 * center : le point sur lequel centrer la map Ex: Array(33.333, 22.2222)
 * display_info : booleen indiquant si on affiche ou non la description des
 * puces lorsque l'on clique dessus
 */
 
 
function displayMap(blockId, datas, zoom, center, display_info, active_control,active_popup, infobulle){
    
    tmp_zoom = zoom;
    if( zoom == 'auto' )
    {
        tmp_zoom = 7;
    }

    map = new GMap2(document.getElementById(blockId));
    map.setCenter(new GLatLng(center[0],center[1]), tmp_zoom);
    var bounds = new GLatLngBounds();
    map.setMapType(G_NORMAL_MAP);
	
	
    if (active_control == 0) {
	    map.disableScrollWheelZoom();
	    map.disableDragging();
	    map.disableContinuousZoom();
	    map.disableDoubleClickZoom();
	    map.disableGoogleBar(); 	
    } else {
	    map.setUIToDefault();
    }
	if (active_popup == 0) {

		map.disableInfoWindow();
	}
  
   
/**  Ici on passe au marker un tableau style qui permet de redefinir limage pour le cluster 
 	La syntaxe provient du fichier non compresser JS markercluster.js **/
 
	
    var index = 1;
    markers = [];
    var styles_ = [];
    for (i = 1; i <= 5; ++i) {
	  styles_.push({
	    'url': picto_map_cluster.image,
	    'height': 31,
	    'width': 25
	  });
    }	
    
    var mcOptions = { gridSize: 20, maxZoom: 10, styles: styles_};


    // Gestion des point au meme coordonées
    var markers = Array();

    for(i=0; i < datas.length; i++)
    {
        data = datas[i];
        key = data.latitude+'_'+data.longitude;
        if( typeof( markers[key] ) == 'undefined' )
        {
            markers[key] = Array();
        }

        markers[key][markers[key].length] = data;
    }

    for(key in markers)
    {

        var data = markers[key][0];
//        var data = datas[i];
        // On récupère la description de notre point
        if( data )
        {
            var test = data.latitude;
            var latitude = test;
            var longitude = data.longitude;
            var label = data.label;
            var icon = eval( data.icon );
            var node_id = data.node_id;
            
            if( infobulle == 'ajax' )
            {
               $('#locations').append('<div class="empty ajax_wait location" id="location_'+ node_id +'">Loading...</div>');
            }

            var text = $('#location_' + node_id).html();
            
            if(display_info == false)
            {
                text = null;
            }
            
            params = Array();
            if( markers[key].length > 1 )
            {
                for( var i=1; i< markers[key].length; i++ )
                {
                    params[i-1] = markers[key][i]['node_id'];
                }
            }

            params = params.join(',');
            
            // On ajoute le point sur la carte
            var marker = createMarker( latitude, longitude, text, bounds, icon, label, infobulle, map, node_id, params );
            markers.push(marker);
            list_markers.push( marker );
        }
    }
    
    if( zoom == 'auto' )
    {
        map.setCenter( bounds.getCenter(), map.getBoundsZoomLevel( bounds ) );
    }
    else
    {
        map.setCenter( bounds.getCenter(), parseInt(zoom) );
    }
    var markerCluster = new MarkerClusterer(map, markers, mcOptions);

}


var gmapExistingOnload = null;
/*
window.addEvent('domready',function() {
		
    if (GBrowserIsCompatible())
    {            
        for(var j=0; j<listeMaps.length; j++)
        {
            var tab = listeMaps[j];
            displayMap(tab[0], tab[1], tab[2], tab[3], tab[4]);
        }
    }
});
*/




