var map = null;
var geocoder = null;
var baseIcon = null;
//var points = new Array;
if (points==null){ var points = new Array; }
var evenementsDeclares = false;
var pointACentrer = null;


function initialiserCarte() {
    map = new GMap2(document.getElementById("map"));
    geocoder = new GClientGeocoder();

    // A TextualZoomControl is a GControl that displays textual "Zoom In"
    // and "Zoom Out" buttons (as opposed to the iconic buttons used in
    // Google Maps).

    // We define the function first
    function SigmaSatelitteControl() {
    }

    // To "subclass" the GControl, we set the prototype object to
    // an instance of the GControl object
    SigmaSatelitteControl.prototype = new GControl();

    // Creates a one DIV for each of the buttons and places them in a container
    // DIV which is returned as our control element. We add the control to
    // to the map container and return the element for the map class to
    // position properly.
    SigmaSatelitteControl.prototype.initialize = function(map) {

    var mapImg = document.createElement("img");
    var satImg = document.createElement("img");
    var mixImg = document.createElement("img");

    mapMapImg="../../projet-ressources/images/bt_plan";
    mapSatelliteImg="../../projet-ressources/images/bt_sat";
    mapMixteImg="../../projet-ressources/images/bt_mix";

    mapImg.src=mapMapImg+"_on.gif";mapImg.width=61;mapImg.height=18;
    satImg.src=mapSatelliteImg+".gif";satImg.width=84;satImg.height=18;
    mixImg.src=mapMixteImg+".gif";mixImg.width=65;mixImg.height=18;

    try{mapImg.style.cursor=satImg.style.cursor=mixImg.style.cursor="pointer";}catch(e){}

    var container = document.createElement("div");

    container.appendChild(mapImg);
    container.appendChild(satImg);
    container.appendChild(mixImg);
    GEvent.addDomListener(mapImg, "click", function() {map.setMapType(G_NORMAL_MAP);
        mapImg.src=mapMapImg+"_on.gif";
        satImg.src=mapSatelliteImg+".gif";
        mixImg.src=mapMixteImg+".gif";
        return false;
    });
    
    GEvent.addDomListener(satImg, "click", function() {map.setMapType(G_SATELLITE_MAP);
        mapImg.src=mapMapImg+".gif";
        satImg.src=mapSatelliteImg+"_on.gif";
        mixImg.src=mapMixteImg+".gif";
        return false;
    });
    
    GEvent.addDomListener(mixImg, "click", function() {map.setMapType(G_HYBRID_MAP);
        mapImg.src=mapMapImg+".gif";
        satImg.src=mapSatelliteImg+".gif";
        mixImg.src=mapMixteImg+"_on.gif";
        return false;
    });

      map.getContainer().appendChild(container);
      return container;
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    SigmaSatelitteControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
    }

    map.addControl(new SigmaSatelitteControl());
    map.setCenter(new GLatLng(37.441944, -122.141944), 13);

    var mapTypeControl = new GMapTypeControl();
    //map.addControl(mapTypeControl, topRight);
    map.addControl(new GSmallMapControl());
    geocoder.setBaseCountryCode("FR");

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    baseIcon = new GIcon();
    baseIcon.image = "http://labs.google.com/ridefinder/images/mm_20_white.png";
    baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    baseIcon.iconSize = new GSize(12, 20);
    baseIcon.shadowSize = new GSize(22, 20);
    baseIcon.iconAnchor = new GPoint(6, 20);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
}

function surDemasquerCarte(sender, eventArgs, position)
{
    var accordeon = rechercheAccordeon(); 
    if (accordeon)
    {
        if (accordeon.get_SelectedIndex() == position)
        {
            // on affiche la carte
            document.getElementById("map").style.display = "block";
            GUnload()
            map=null;
            initialiserCarte();
            centrer();
            window.setTimeout("positionnerPoints();", 550);
        }
    }
}

function positionnerPoints()
{
    map.clearOverlays();
    for (i=0; i < points.length; i++)
    {
        var monPoint = new GLatLng(points[i].lat, points[i].lng);
        map.addOverlay(createMarker(monPoint, i, points[i].libelle));
    }
    window.clearTimeout();
}

function surMasquerCarte(sender, eventArgs)
{
    document.getElementById("map").style.display = "none";
    //GUnload()
    //map=null;
}

function ouvrirAccordeonCarte(posX, posY, position)
{
    pointACentrer = {x: posX, y: posY};
    var accordeon = rechercheAccordeon(); 
    if (accordeon)
    {
        accordeon.set_SelectedIndex(position);
    }
    
}
function centrer()
{
    if (!pointACentrer)
    {
        map.setCenter(new GLatLng(49.442367, 1.098492), 8);
    }
    else
    {
        map.setCenter(new GLatLng(pointACentrer.x, pointACentrer.y), 15); //10);
    }
    pointACentrer = "";
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index, label) {
  // Set up our GMarkerOptions object
  var letter = String.fromCharCode("A".charCodeAt(0) + index);  
  var letteredIcon = new GIcon(baseIcon);  
  //letteredIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  letteredIcon.image = "../../projet-ressources/images/mm_20_red.png";
  // Set up our GMarkerOptions object  
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(label);
  });
  return marker;
}


function afficheAdresse(adresse) {
  geocoder.getLatLng(
    adresse,
    function(point) {
      if (!point) {
        alert(adresse + " not found");
      } else {
        map.setCenter(point, 8);         
        map.addOverlay(createMarker(point, 1));
      }
    }
  );
}


