var map;

function initGMap() 
{

    var latlng = new google.maps.LatLng(47.25, 1.7);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      navigationControl: true,
      scaleControl: true,
      mapTypeControl: true
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    //--> Déclaration du géocoder
    var geocoder = new google.maps.Geocoder();
    var results;
    var status;

    //--> Lecture du fichier XML pour récupération des adresses, de l'icône et du titre
    $.ajax({
      type: "GET",
      url: "http://www.arbonis.com/index.php/layout/set/xmlImplantation",
      dataType: "xml",
      success: function(xml) {
        $(xml).find('marker').each(function(){
          var id        = $(this).attr('id');
          var adresse   = $(this).attr('adresse');
          var titre     = 'Cliquez pour voir les coordonnées';//$(this).attr('titre');
          var icone     = $(this).attr('icone');
//           var imageIco  = new google.maps.MarkerImage(icone, 
//                         new google.maps.Size(100, 100), 
//                         new google.maps.Point(0,0), 
//                         new google.maps.Point(0, 0)); 

          
          if (geocoder) {
              geocoder.geocode( { 'address': adresse}, function(results, status) {
                //--> Définition de la position
                var MarkerLatLng = results[0].geometry.location;
                if (status == google.maps.GeocoderStatus.OK) {
                  var myMarker = new google.maps.Marker({
                      map: map,
                      title: titre,
                      position: MarkerLatLng,
                      icon:icone
                  });
                  
                  var str = $("#infos-"+id).html();
                  var myInfoWindow = new google.maps.InfoWindow({content:str});
                  if(!map.infoWindows){map.infoWindows=[]}
                  map.infoWindows.push(myInfoWindow);
                  google.maps.event.addListener(myMarker, 'click', function() {
                  	closeMapInfoWindow();
                    myInfoWindow.open(map,myMarker);
                  });
                } else {
                  var erreur = '';
                }
      
              });
            }





        });
      }
    });

} //--> End initGGMAPS

function closeMapInfoWindow(){
  for(var i=0;i<map.infoWindows.length;i++){
    map.infoWindows[i].close();
  }
}

//--> Affichage d'un point sur la carte en fonction d'un onClick par exemple
function zoomAddress(idDestination) {
    //--> Lecture du fichier XML pour récupération des adresses, de l'icône et du titre
        
    //--> Déclaration du géocoder
    var geocoder = new google.maps.Geocoder();
    var results;
    var status;
    
    $.ajax({
      type: "GET",
      url: "http://www.arbonis.com/index.php/layout/set/xmlImplantation",
      dataType: "xml",
      success: function(xml) {
        $(xml).find('marker').each(function(){
          var id        = $(this).attr('id');
          var adresse   = $(this).attr('adresse');
          var titre     = 'Cliquez pour voir les coordonnées'; //$(this).attr('titre');
          var icone     = $(this).attr('icone');
//           var imageIco  = new google.maps.MarkerImage(icone, 
//                         new google.maps.Size(100, 100), 
//                         new google.maps.Point(0,0), 
//                         new google.maps.Point(0, 0)); 

          if (id == idDestination) {
            if (geocoder) {
                geocoder.geocode( { 'address': adresse}, function(results, status) {
                  //--> Définition de la position
                  var MarkerLatLng = results[0].geometry.location;
                  map.setCenter(MarkerLatLng);
                  map.setZoom(16);
                  if (status == google.maps.GeocoderStatus.OK) {
                    var myMarker = new google.maps.Marker({
                        map: map,
                        title: titre,
                        position: MarkerLatLng,
                        icon:icone
                    });
                    
                    var str = $("#infos-"+id).html();
                    var myInfoWindow = new google.maps.InfoWindow({content:str});
                    google.maps.event.addListener(myMarker, 'click', function() {
                    	myInfoWindow.open(map,myMarker);
                    });
                  } else {
                    var erreur = '';
                  }
        
                });
              }
            }
        });
      }
    });

} //--> End zoomAddress

function travelFrom(origin,destination)
{
//     $("#roadmap").html('');
    $("#roadmap").slideDown('fast');
    $("#printRoadMap").slideDown('fast');
    
    var latlng = new google.maps.LatLng(47.25, 1.7);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      navigationControl: true,
      scaleControl: true,
      mapTypeControl: true
    };
    
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var directionDisplay;
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("roadmap"));
    var directionsService = new google.maps.DirectionsService();
    var request = {
        origin: origin,
        destination: destination,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setDirections(response);
        }
    });
    return false;
} //--> End travelFrom

//--> Ouvrir une fiche implantation
function openImplantation (idDestination) {
  $("#titre").slideUp('fast');
  $("#centre").slideUp('fast');
  $("#centreInfos").slideDown('fast');
  $("#roadmapBlock").slideDown('fast');
  $("#centreInfosRetour").slideDown('fast');

            //--> Fermeture de tout ce qui traine lol
  for( x=0;x<1000;x++ ) {
    $("#ficheImp-"+x).slideUp('fast');
  }
  for( x=0;x<1000;x++ ) {
    $("#push-"+x).slideUp('fast');
  }
            
  //--> Lecture du fichier XML des implantations afin de récup adresse et titre
  
      $.ajax({
      type: "GET",
      url: "http://www.arbonis.com/index.php/layout/set/xmlImplantation",
      dataType: "xml",
      success: function(xml) {
        $(xml).find('marker').each(function(){
          var id        = $(this).attr('id');
          var adresse   = $(this).attr('adresse');
          var titre     = 'Cliquez pour voir les coordonnées'; //$(this).attr('titre');
          var icone     = $(this).attr('icone');
          if (id == idDestination) {
            //--> Pour afficher proprement la destination
            $("#destination").html('<strong>'+titre+'</strong><br />'+adresse+'<input type="hidden" size="25" id="toAddress" name="to" value="'+adresse+'" />');

            //--> Ouverture de la fiche concernée
            $("#push-"+idDestination).slideDown('fast');
            $("#ficheImp-"+idDestination).slideDown('fast');
            
            //--> On centre la carte sur ce point
            zoomAddress(idDestination);

          }
        });
      }
    });
  return false;  
} //--> End openImplantation

//--> Fermer une fiche implantation
function closeImplemantation () {
  $("#titre").slideDown('fast');
  $("#centre").slideDown('fast');
  $("#centreInfos").slideUp('fast');
  $("#centreInfosRetour").slideUp('fast');
  $("#roadmapBlock").slideUp('fast');
  $("#printRoadMap").slideUp('fast');
  
  for( x=0;x<1000;x++ ) {
    $("#ficheImp-"+x).slideUp('fast');
  }
  for( x=0;x<1000;x++ ) {
    $("#push-"+x).slideUp('fast');
  }
  $("#roadmap").html('');
  $("#roadmap").slideUp('fast');
  $("#destination").html('<em>Sélectionnez une agence, merci.</em>');
  //--> Réinitialisation de la carte
  initGMap();
  
  return false;  
} //End closeImplemantation


//--> Fermer une fiche implantation
// function openStreetView (idDestination) {
// 
//       //--> Déclaration du géocoder
//       var geocoder = new google.maps.Geocoder();
//       var results;
//       var status;
//       
//       $.ajax({
//       type: "GET",
//       url: "http://www.arbonis.com/index.php/layout/set/xmlImplantation",
//       dataType: "xml",
//       success: function(xml) {
//         $(xml).find('marker').each(function(){
//           var id        = $(this).attr('id');
//           var adresse   = $(this).attr('adresse');
//           var titre     = $(this).attr('titre');
//           var icone     = $(this).attr('icone');
//           if (id == idDestination) {
//             if (geocoder) {
//                 geocoder.geocode( { 'address': adresse}, function(results, status) {
//                   //--> Définition de la position
//                   var MarkerLatLng = results[0].geometry.location;
//                   if (status == google.maps.GeocoderStatus.OK) {
// 
//                     mapG2 = new GMap2(document.getElementById("streetViewImplantation"));
//                     var myPano = new GStreetviewPanorama(document.getElementById("pano"));
//                     fenwayPark = new GLatLng(MarkerLatLng);
//                     myPOV = {yaw:0,pitch:5};
//                     myPano.setLocationAndPOV(fenwayPark, myPOV);
// 
// 
//                   } else {
//                     var erreur = '';
//                   }
//         
//                 });
//               }
// 
//           }
//         });
//       }
//     });
// 
//   return false;
// } //End openStreetView















