function initGoogleMap(lat,lgt,div) {

      geocoder = new GClientGeocoder();
      z = 16;
      initializeMap("",lat,lgt,z,div);
}

var gicons = [];
var infobulle;
var markerMember;
var directionsDisplay;
var directionsService;
var map,geocoder, marker;
var depart,arrivee,ptCheck;
var bounds;

var tabMarker = new Array();
var indiceMarker=0;

function initializeMap(address,a,b,z,div) {

    a=parseFloat(a);
    b=parseFloat(b);

  map = new GMap2(document.getElementById(div));
  map.addControl(new GLargeMapControl3D());
  map.setCenter(new GLatLng(a,b), z);

  gicons[0] = new GIcon(G_DEFAULT_ICON, "http://www.weboblog.fr/flag_blue.png");
  gicons[0].iconSize = new GSize(16,16);
  gicons[0].shadow="";

    markerMember = createMarker(new GLatLng(a,b),gicons[0]); // Ajout du marqueur
    map.addOverlay(markerMember);
}

function initializeEmptyMap(div) {
  map = new GMap2(document.getElementById(div));
 /* wWidth = (document.body.clientWidth-250);
  $("#"+div).append('<ul id="custom_content'+div+'" class="menu"><li class="agrandire opacity"><a href="#?custom=true&width=600&height=500" rel="prettyPhoto[custom]">Agrandir</a></li><li class="itineraire opacity"><a href="#?custom=true&width='+wWidth+'&height=450" rel="prettyPhoto[custom]">Itinéraire</a></li></ul>');

  $(document).ready(function(){

        $("#custom_content"+div+" a[rel^='prettyPhoto']:first").prettyPhoto({
                custom_markup: '<div id="map_canvas" style="width:600px; height:500px"></div>',
                 keyboard_shortcuts: false, /* Set to false if you open forms inside prettyPhoto 
                changepicturecallback: function(){FullMap(latitude,longitude);}
        });

        $("#custom_content"+div+" a[rel^='prettyPhoto']:last").prettyPhoto({
                custom_markup: '<div id="map_canvas" style="width:'+(wWidth/2)+'px; height:400px"></div>',
                 keyboard_shortcuts: false, /* Set to false if you open forms inside prettyPhoto 
                changepicturecallback: function(){FullItineraire(latitude,longitude,adress);}
        });
    });*/
}

function MarkerMapAdd(a,b,map) {

    tabMarker[indiceMarker] = new Array;
    tabMarker[indiceMarker]["latitude"] = a;
    tabMarker[indiceMarker]["longitude"] = b;
    tabMarker[indiceMarker]["map"] = map;
    indiceMarker++;

}

function getSearchFullMap (relative) {


    //$("#map").append('<ul id="custom_content" class="menu"><li class="agrandire opacity"><a href="#?custom=true&width=600&height=500" rel="prettyPhoto[custom]">Agrandir</a></li></ul>');

    $(document).ready(function(){

        $("#custom_content a[rel^='prettyPhoto']:first").prettyPhoto({
                custom_markup: '<div id="map_canvas" style="width:600px; height:500px;z-index:20001"></div>',
                keyboard_shortcuts: false, /* Set to false if you open forms inside prettyPhoto */
                changepicturecallback: function(){
                    initializeEmptyMap("map_canvas");
                    MarkerMap(relative);
                    map.setCenter(bounds.getCenter(),14);
                    $(".pp_pic_holder").attr("z-index","300000");
                }
        });
    });

}

function MarkerMap(relative) {
    
    bounds = new GLatLngBounds( );


    if(indiceMarker>=10)indiceMarker=9;
    var trainline = [];

    for(i=0; i<indiceMarker;i++) {

        bounds.extend( new GLatLng(tabMarker[i]["latitude"],tabMarker[i]["longitude"]) );

        gicons[0] = new GIcon(G_DEFAULT_ICON, relative+"/wm-public/images/googleMap/marker/map"+parseInt(i+1)+".png");
        gicons[0].iconSize = new GSize(13,13);

        //markerMember = createMarker(new GLatLng(tabMarker[i]["latitude"],tabMarker[i]["longitude"]),gicons[0]); // Ajout du marqueur

        trainline.push(new GLatLng(tabMarker[i]["latitude"],tabMarker[i]["longitude"]));

        var markerMember = new GMarker(new GLatLng(tabMarker[i]["latitude"],tabMarker[i]["longitude"]),gicons[0]);
        markerMember.importance = parseInt(10-i+1);
        map.addOverlay(markerMember);

        map.setCenter(bounds.getCenter(),8);
    }

   


}



function initGoogleMapWithOptions(lat,lgt,div) {

      geocoder = new GClientGeocoder();
      z = 16;
      initializeMapWithOptions("",lat,lgt,z,div);
}

function initializeMapWithOptions(address,a,b,z,div) {

    a=parseFloat(a);
    b=parseFloat(b);

    map = new GMap2(document.getElementById(div));

        map.addControl(new GScaleControl()) ;
        map.setUIToDefault();
        map.checkResize();
    
          gicons[0] = new GIcon(G_DEFAULT_ICON, "http://www.weboblog.fr/flag_blue.png");
          gicons[0].iconSize = new GSize(16,16);
          gicons[0].shadow="";

        map.setCenter(new GLatLng(a,b), z);

            markerMember = createMarker(new GLatLng(a,b),gicons[0]); // Ajout du marqueur
        map.addOverlay(markerMember);
}

function createMarker(point,icon){
  var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "click", function() {
  
  });
  return marker;
}

function initItineraire(lat,lgt,div,adress) {

    /*gestion des routes*/
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsService = new google.maps.DirectionsService();
   
    /*emplacement par défaut de la carte (j'ai mis Paris)*/
    var maison = new google.maps.LatLng(lat,lgt);

    /*option par défaut de la carte*/
    var myOptions = {
        zoom:15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: maison
    }

    /*creation de la map*/
    map = new google.maps.Map(document.getElementById(div), myOptions);

    /*connexion de la map + le panneau de l'itinéraire*/
    directionsDisplay.setMap(map);

    directionsDisplay.setPanel(document.getElementById("panel"));

    /*intialise le geocoder pour localiser les adresses */
    geocoder = new google.maps.Geocoder();

   rechercher('adrArr',false);

}

function rechercher(src,code) {

        $("#errorAdress").html("");

        ptCheck = code; /*adresse de départ ou arrivée ? */
        if (geocoder) {

            geocoder.geocode( {'address': document.getElementById(src).value}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                /*ajoute un marqueur à l'adresse choisie*/
                map.setCenter(results[0].geometry.location);
                
                if (marker) {
                    marker.setMap(null);
                }

                marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                
                /*on remplace l'adresse par celle fournie du geocoder*/
                document.getElementById(src).value = results[0].formatted_address;

                if (ptCheck)
                    {
                        depart = results[0].formatted_address;
                    }
                    else
                        {
                            arrivee = results[0].formatted_address;
                        }

                /*trace la route*/
                trouveRoute();
            } else {

                $("#errorAdress").html("Votre adresse nous est inconnue");

            }
        
        });
    }
}

function trouveRoute() {
/*test si les variables sont bien initialisés*/

if (depart && arrivee)

    {
        /*mode de transport*/
        var choixMode = "DRIVING";

        var request = {
            
            origin:depart,
            destination:arrivee,
            travelMode: google.maps.DirectionsTravelMode[choixMode]
        };
            
        /*appel à l'API pour tracer l'itinéraire*/
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }

        });
    }
    
}
