﻿////////////////////////////////////////////////////////////////////////////////////
//
//  Creation d'une carte GMap en fonction de balises VCard et Geo dans un ou deux Blog Blogger
//  <div id="BloggerToMaps" url="xxx.blogger.com" ...> avec height et width
//  Le blog lu par JSON est 
//    1° le ATTRIBUTE url=
//    2° le blog lui même si pas de url=
//    3° les ATTRIBUTE url1=" url2= etc... url9=.
//  La carte est centrée :
//    1° sur l'attribut geohome de la balise BloggerToMaps uniquement sur la home page
//    2° sur l'attribut geo de la balise BloggerToMaps
//    3° sur le clientLocation sur la home Page
//    Sur les pages autres
//    1° sur l'attribut geo de la balise BloggerToMaps
//    2° sur la premiere balise geo du document
//    3° sur le centre des balises geo du document
//    4° sur le clientLocation
//  pour les css
//    id = "btm_map"
//    id = "btm_menu" 
//    id = "btm_context" 
// 
// 
////////////////////////////////////////////////////////////////////////////////////

//Variables globales
var btm_Map; // Carte google maps
var btm_MarkerManager;
var btm_Markers_Array = new Array();
var btm_urls_Array = new Array(); // Tableau des urls en parametre4
var btm_urls_Current = 0;
var btm_head = document.getElementsByTagName('head').item(0);
var btm_context;
var btm_location; // Marker clinet location.

var btm_icon_boundary = "http://maps.google.com/mapfiles/kml/pal3/icon43.png";
var btm_icon_boundary_Selected = "http://maps.google.com/mapfiles/kml/pal3/icon35.png";
var btm_icon_Vcard = "http://maps.google.com/mapfiles/kml/pal4/icon8.png";
var btm_icon_Vcard_Selected = "http://maps.google.com/mapfiles/kml/pal4/icon0.png";
var btm_icon_Vevent = "http://maps.google.com/mapfiles/kml/pal3/icon62.png";
var btm_icon_Vevent_Selected = "http://maps.google.com/mapfiles/kml/pal3/icon54.png";
var btm_icon_File = "http://maps.google.com/mapfiles/kml/pal3/icon62.png"

function btm_geo(LatLng, Nom, Type, Url, Vcard) {
   this.GLatLng = LatLng; 
   this.Nom = Nom;
   this.Type = Type; // Point, Locality, Region, Country 
   this.Url = Url;
   this.VCard = Vcard;
   this.VEvents = new Array();
   this.GMarker = null;
   this.MenuNode = null;
   this.InfoWindowTabs = new Array();
   this.ZoomMin = 17;
   this.ZoomMax = 0;
   // Boundary
   this.GLatLngBounds = null;
}
var btm_geo_Array = new Array();

function btm_boundary(Nom, Type) 
{
   this.Nom = Nom;
   this.Type = Type; // Locality, Region, Country 
   this.GLatLngBounds = new google.maps.LatLngBounds();
   this.GMarker = null;
   this.Geos = new Array(); // Tableau Markers inclus.
}
var btm_boundary_Array = new Array();

var Type_Point = "Point";
var Type_Locality = "Locality";
var Type_Region = "Region";
var Type_Country = "Country";

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

if(google.maps == null)
  google.load("maps", "2.x");     

// appelé dans load()
function BloggerToMaps() 
  {
  // Si il y a un menu c'est que c'est deja lancé
  if( document.getElementById("btm_menu") != null) 
    return;  
  var btm_container = document.getElementById("BloggerToMaps");
  if( btm_container == null) 
    return;  
  btm_container.style.display = '';
  btm_container.style.textAlign = 'left';
  if( google.maps == null )
    btm_container.innerHTML = "Google Maps est indisponible";
  else
    btm_container.innerHTML = "";

  var locations = document.location.href.split('/');
// Recherche des url de blog en attribute
// Si pas d'URL='xxx' on se sert de l'url par defaut
  var url = btm_container.getAttribute("url");
  if( url == null)
    {
    url = "http://" + document.location.host + "/";
    }

   for( var ja = 1; ja < 10; ja++)
    {
    var urlNum = btm_container.getAttribute("url" + ja);
    if( urlNum != null )
      {
      btm_urls_Array.push(urlNum);
      }
    }

//  btm_container.style.overflow = "hidden";
//  Menu a gauche
  btm_container.style.border = "1px solid" // #000000"

  var div_menu = document.createElement("div");
  div_menu.id = "btm_menu";
  div_menu.style.styleFloat = "left"; // IE 
  div_menu.style.cssFloat = "left"; // FF !!! uniquement par css.... 
  var menuwidth = 200;
  if(btm_container.style.width * 0.25 > 200)
    menuwidth = btm_container.style.width * 0.25;
  div_menu.style.width = menuwidth + "px";
  div_menu.style.height = "100%";
//  div_menu.style.backgroundColor = "#FFFFFF";
  div_menu.style.paddingLeft = "3px";
  div_menu.style.paddingRight = "3px";
  div_menu.style.overflow = "auto";
  div_menu.style.borderRight = "1px solid" // #000000"
  btm_container.appendChild(div_menu);
  
//  img = document.createElement('img');
//  img.src = 'http://ysa.jean.googlepages.com/animation-wait.gif';
//  div_menu.appendChild(img);

  var div_map = document.createElement("div");
  div_map.id = "btm_map";
  div_map.style.height = "100%";
  menuwidth += 7;
  div_map.style.marginLeft = menuwidth + "px"
  btm_container.appendChild(div_map);

  btm_Map = new google.maps.Map2(document.getElementById("btm_map"),
    {googleBarOptions:
      {onMarkersSetCallback:markersSetCB} //showOnLoad:true
    })
  btm_Map.setUIToDefault();

//  btm_Map.addControl(new google.maps.LargeMapControl())
//  btm_Map.addControl(new google.maps.MapTypeControl());
//  btm_Map.enableScrollWheelZoom();
  btm_Map.enableContinuousZoom();
  btm_Map.enableGoogleBar();
  btm_MarkerManager = new google.maps.MarkerManager(btm_Map);
  var adsManager = new GAdsManager(btm_Map, "ca-pub-6734354841637423");
  adsManager.enable(); 

  setContextMenu()
  
  // Si le container a un format geo on centre dessus. <div title="43;5" id="google_map">
  var btm_geo = btm_container.getAttribute("geo");
  var btm_geohome = btm_container.getAttribute("geohome");
  var isCenterSet = false;
  var doc_geos = getElementsByClass('geo', document.body);
  // Centrage de la home page
  if(locations.length == 4) 
    {
    if(btm_geohome != null)
      {
      coords = btm_geohome.split(";");
      btm_Map.setCenter(new google.maps.LatLng(coords[0], coords[1]), 8);
      isCenterSet = true;
      }
    else if(btm_geo != null)
      {
      coords = btm_geo.split(";");
      btm_Map.setCenter(new google.maps.LatLng(coords[0], coords[1]), 8);
      isCenterSet = true;
      }
    }
  else // Centrage des autres pages
    {
    if(btm_geo != null)
      {
      coords = btm_geo.split(";");
alert( coords[0] + " " + coords[1] );
      btm_Map.setCenter(new google.maps.LatLng(coords[0], coords[1]), 8);
      isCenterSet = true;
      }
    else if((doc_geos != null) &&( doc_geos.length == 1 ))
      {
      coords = doc_geos[0].title.split(";");
      btm_Map.setCenter(new google.maps.LatLng(coords[0], coords[1]), 8);
      isCenterSet = true;
      }
    else if ((doc_geos != null) &&( doc_geos.length > 1 ))
      {
      doc_LatLngBounds = new google.maps.LatLngBounds();
      for( var ga = 0; ga < doc_geos.length; ga++)
        {
        coords = doc_geos[ga].title.split(";");
        doc_LatLngBounds.extend( new google.maps.LatLng(coords[0], coords[1]));
        }
      btm_Map.setCenter(doc_LatLngBounds.getCenter(),btm_Map.getBoundsZoomLevel(doc_LatLngBounds)); 
      isCenterSet = true;
      }
   }   
  // On crée un marker sur le Client Location et on centre si pas de geo
  if (google.loader.ClientLocation) 
    {
    var cl = google.loader.ClientLocation;
    var location = [cl.address.city, cl.address.region, cl.address.country].join(', ');
    var cl_point = new google.maps.LatLng(cl.latitude, cl.longitude);
    
    if (!isCenterSet)
      {
      btm_Map.setCenter(cl_point, 9);
      isCenterSet = true;
      }

    var icon = new google.maps.Icon(G_DEFAULT_ICON);
    icon.image = "http://maps.google.com/mapfiles/kml/pal4/icon47.png";
    icon.iconSize = new google.maps.Size(32,32);
    icon.iconAnchor = new google.maps.Point(16, 16);
    icon.shadowSize = new GSize(16, 16);
    icon.infoWindowAnchor = new GPoint(0, 0);
    
    btm_location = new google.maps.Marker(cl_point,{ icon:icon, title:location });
    
    google.maps.Event.addListener( btm_location, 'click', function()
      { 
      btm_Map.setCenter( btm_location.getLatLng(), 9); 
      });

    btm_Map.addOverlay(btm_location);

    // creation d'un menu sur le location point
    var img_location =  document.createElement('img');
    img_location.src = "http://maps.google.com/mapfiles/kml/pal4/icon47.png";
    img_location.align = "middle";
    img_location.style.borderWidth = "0px"; 
    img_location.style.padding = "0px"; 
    img_location.style.margin = "0px"; 
    img_location.style.paddingRight = "0.25em"; 
    img_location.style.width = "1em"; 
    img_location.style.height = "1em";
    
    var a_location = document.createElement('a');
    a_location.appendChild(document.createTextNode( "Près de chez vous" ));
    a_location.href = "javascript:GEvent.trigger(btm_location,\"click\")";

    var h_location = document.createElement('h5');
    h_location.appendChild(img_location);
    h_location.appendChild(a_location);
    h_location.style.margin = "0";
    
    document.getElementById("btm_menu").appendChild(h_location);

//    btm_Map.openInfoWindow(btm_Map.getCenter(),
//    "Promenez vous avec la carte. <br/>Clickez sur les <img src='http://maps.google.com/mapfiles/kml/pal4/icon8.png' alt='Clickez moi' > et lisez les fiches.");

    }
    else if (!isCenterSet)
      btm_Map.setCenter(new google.maps.LatLng(43.5,5), 9);
    
    btm_Map.savePosition()

    google.maps.Event.addListener(btm_Map, "moveend", function() 
      {
      for( var aa = 0; aa < btm_geo_Array.length; aa++)
        {
        btm_geo_Array[aa].MenuNode.style.display = 'none';
        if( btm_Map.getBounds().containsLatLng( btm_geo_Array[aa].GLatLng ))
          {
          if( ( btm_geo_Array[aa].ZoomMin <= btm_Map.getZoom() )
            && (  btm_Map.getZoom() <= btm_geo_Array[aa].ZoomMax))
            {
            btm_geo_Array[aa].MenuNode.style.display = '';
            }
          }
        else if( btm_geo_Array[aa].GLatLngBounds != null )
          {
          if(( btm_Map.getBounds().intersects( btm_geo_Array[aa].GLatLngBounds )) 
            && ( !btm_Map.getBounds().containsBounds( btm_geo_Array[aa].GLatLngBounds )))
            btm_geo_Array[aa].MenuNode.style.display = '';
          }
        }
        // On affiche le menu des boundaries qui sont sur la carte à un niveau supèrieur.
      });
  if(btm_Map.getSize().width <= 1450)
    {
    google.maps.Event.addListener(btm_Map, "infowindowopen", function() 
      {
      // Test stat avec Max content
      //pageTracker._trackPageview ('/btm/test.html');
      btm_Map.getInfoWindow().maximize(true);
      });
    }  
  if( url != null)
    {
    var flux = url + 'feeds/posts/default?max-results=1000&redirect=false&alt=json-in-script&callback=ReadBlogCB';
    if(( locations[4] == "label" ) && ( locations[5] != "Annuaire"))
      {
      var flux = url + 'feeds/posts/default/-/' + encodeURI(locations[5]) + '?max-results=1000&alt=json-in-script&callback=ReadBlogCB';
      }
   
    var ScriptJsonUrl0 = document.createElement('script');
    ScriptJsonUrl0.setAttribute('src', flux );
    ScriptJsonUrl0.setAttribute('type', 'text/javascript');
    btm_head.appendChild(ScriptJsonUrl0);
    }
  }

// Marker de la search box
function markersSetCB(info) {
        for (var i=0; i<info.length; i++) {
          var marker = info[i].marker;
          marker.setImage("http://maps.google.com/mapfiles/kml/pal4/icon47.png");
        }
      }

function ReadBlogCB(json) 
  {
  var menu_container = document.getElementById("btm_menu");

//  var img_html =  document.createElement('img');
//  img_html.src = "http://maps.google.com/mapfiles/kml/pal3/icon56.png";
//  img_html.align = "middle";
//  img_html.style.borderWidth = "0px"; 
//  img_html.style.padding = "0px"; 
//  img_html.style.margin = "0px"; 
//  img_html.style.paddingRight = "0.25em"; 
//  img_html.style.width = "1em"; 
//  img_html.style.height = "1em";

//  var html = document.createElement('h5');
//  html.appendChild(img_html);
//  html.appendChild(document.createTextNode( json.feed.title.$t ));
//  html.style.margin = "0";
//  
//  menu_container.appendChild(html);
  
  // Info bulle avec le titre / sous titre  du blog et des explications
  if( btm_urls_Current == 0 )
    {
    var title = document.createElement('h1');
    title.style.margin = "0";
    title.appendChild(document.createTextNode( json.feed.title.$t ));

//    var subtitle = document.createElement('p');
//    subtitle.style.margin = "0";
//    subtitle.appendChild(document.createTextNode( json.feed.subtitle.$t ));
//    
    var img_explain =  document.createElement('img');
    img_explain.src = "http://maps.google.com/mapfiles/kml/pal4/icon8.png";
    img_explain.align = "middle";
    img_explain.style.borderWidth = "0px"; 
    img_explain.style.padding = "0px"; 
    img_explain.style.margin = "0px"; 
    img_explain.style.paddingRight = "0.25em"; 
    img_explain.style.width = "2em"; 
    img_explain.style.height = "2em";
      
    var explain = document.createElement('p');
    explain.style.margin = "0";
    explain.appendChild(document.createTextNode( "Promenez vous avec la carte."));
    explain.appendChild(document.createElement('br'));
    explain.appendChild(document.createTextNode( "Clickez sur les "));
    explain.appendChild(img_explain);
    explain.appendChild(document.createTextNode( " et lisez les fiches." ));
     
    var info = document.createElement('span');
    info.appendChild(title);
//    info.appendChild(subtitle);
    info.appendChild(explain);
    
    btm_Map.openInfoWindow(btm_Map.getCenter(),info);
    }
    
  var ul = document.createElement('ul');

  if( json.feed.entry != null )
  {
  for (var ba = 0; ba < json.feed.entry.length; ba++) 
    {
    var entry = json.feed.entry[ba];
    var entry_Node = document.createElement('span');
    entry_Node.style.borderWidth = "1px"; 
    entry_Node.style.padding = "4px"; 
    entry_Node.style.margin = "4px";     
    entry_Node.innerHTML = entry.content.$t + "<br/>";
    
    var entry_Url = "";  
    for (var bc = 0; bc < entry.link.length; bc++) 
      {
      if (entry.link[bc].rel == 'alternate') 
       {
       entry_Url = entry.link[bc].href;
       break;
       }
      }
      ReadPost_vcard(entry, entry_Node, entry_Url, json.feed.title.$t);
      ReadPost_vevent(entry, entry_Node, entry_Url, json.feed.title.$t);
      }
    }
  if( btm_urls_Current < btm_urls_Array.length )
    {
    var locations = document.location.href.split('/');
    var flux = btm_urls_Array[btm_urls_Current] + 'feeds/posts/default?max-results=1000&redirect=false&alt=json-in-script&callback=ReadBlogCB';
    if(( locations[4] == "label" ) && ( locations[5] != "Annuaire"))
      var flux = btm_urls_Array[btm_urls_Current] + 'feeds/posts/default/-/' + encodeURI(locations[5]) + '?max-results=1000&alt=json-in-script&callback=ReadBlogCB';

    var ScriptJsonUrl = document.createElement('script');
    ScriptJsonUrl.setAttribute('src', flux );
    ScriptJsonUrl.setAttribute('type', 'text/javascript');
    btm_head.appendChild(ScriptJsonUrl);
    btm_urls_Current++;
    }
  else // dernier Blog on peut envoyer la gestion des markers.
    {
    for( var bou = 0; bou < btm_boundary_Array.length; bou++ )
      {
      var zoom = btm_Map.getBoundsZoomLevel( btm_boundary_Array[bou].GLatLngBounds );
      var boundary = btm_boundary_Array[bou];
      var ZoomMin = 0;
      var ZoomMax = 17;
      if( boundary.Type == Type_Country )
        {
          ZoomMin = 2;
          ZoomMax = 4;
        }
      else if( boundary.Type == Type_Region )
        {
          ZoomMin = 5;
          ZoomMax = 7;
        }
      else if( boundary.Type == Type_Locality )
        {
          ZoomMin = 8;
          ZoomMax = 10;
        }
      if(( boundary.Type == Type_Country ) || ( boundary.Type == Type_Region ) || ( boundary.Type == Type_Locality ))
        {
        if( btm_boundary_Array[bou].Geos.length > 1 )
          {
          geo_element = new btm_geo( boundary.GLatLngBounds.getCenter(),
            boundary.Nom + " (" + btm_boundary_Array[bou].Geos.length + ")", boundary.Type);
          geo_element.GLatLngBounds = boundary.GLatLngBounds;

          btm_geo_Array.push( geo_element );
          createMenuMarker( geo_element, btm_icon_boundary, btm_icon_boundary_Selected, entry_Node);

          geo_element.ZoomMin = ZoomMin;
          geo_element.ZoomMax = ZoomMax;
          btm_MarkerManager.addMarker( geo_element.GMarker, geo_element.ZoomMin, geo_element.ZoomMax); 
         // Si c'est une ville les geo restant sont visible à 11
         if( boundary.Type == Type_Locality )
            {
            for( var ge = 0; ge < boundary.Geos.length; ge++)
              {
              boundary.Geos[ge].ZoomMin = ZoomMax + 1;
              boundary.Geos[ge].ZoomMax = 17;
              btm_MarkerManager.addMarker( boundary.Geos[ge].GMarker, ZoomMax + 1, 17); 
              }
            }
          }
        else
          {
          boundary.Geos[0].ZoomMin = ZoomMin;
          boundary.Geos[0].ZoomMax = 17;
          btm_MarkerManager.addMarker( boundary.Geos[0].GMarker, ZoomMin, 17); 
          }
        }
      else
        {
//          btm_MarkerManager.addMarker( btm_boundary_Array[bou].Geos[0].GMarker, 0);
        }
      }
      btm_geo_Array.sort(sortGeo);
      if( menu_container != null )
        {
        for( var be = 0; be < btm_geo_Array.length; be++)
          {
          if( btm_geo_Array[be].MenuNode != null )
            {
            menu_container.appendChild(btm_geo_Array[be].MenuNode);
            btm_geo_Array[be].MenuNode
            addAttribute( btm_geo_Array[be].MenuNode, "onmouseover", "GEvent.trigger(btm_geo_Array[" + be + " ].GMarker,\"mouseover\")");
            addAttribute( btm_geo_Array[be].MenuNode, "onmouseout", "GEvent.trigger(btm_geo_Array[" +  be + " ].GMarker,\"mouseout\")");
            var trigger = getElementsByClass('trigger', btm_geo_Array[be].MenuNode);  
            trigger[0].href = "javascript:GEvent.trigger(btm_geo_Array[" + be + " ].GMarker,\"click\")";
            }
          }
        }
      GEvent.trigger( btm_Map, "moveend");
    }
  }

// Lit les vcards du post et créé des markers
// Marker Manager de 7 à 17
function ReadPost_vcard(entry, entry_Node, entry_Url, blog_name)
  {
  var vcards = getElementsByClass('vcard', entry_Node);
  if( vcards.length > 0 )
    {
    var geo_Nodes = getElementsByClass('geo', vcards[0])
    if( geo_Nodes.length > 0 )
      {
      var geo_element = null;
      // Verif si le point n'existe pas deja
      for( var ha = 0; ha < btm_geo_Array.length; ha++ )
        {
        if( btm_geo_Array[ha].Nom == geo_Nodes[0].innerHTML )
          {
          geo_element = btm_geo_Array[ha];
          }
        }

      if( geo_element == null )
        {
        var geo_coord = geo_Nodes[0].title.split(";");
        geo_element = new btm_geo( new google.maps.LatLng(geo_coord[0],geo_coord[1]),
        geo_Nodes[0].innerHTML, Type_Point, entry_Url, vcards[0]);
        btm_geo_Array[btm_geo_Array.length] = geo_element;
        createMenuMarker( geo_element, btm_icon_Vcard, btm_icon_Vcard_Selected, entry_Node);
        createBoundaries(vcards[0], geo_element);
        }
      addDomMarkerVcard(geo_element, vcards, entry_Node, entry_Url, blog_name, btm_icon_Vcard);
      }
    }
  }

// Lit les vevents du post et créé des markers
// Marker Manager de 7 à 17
function ReadPost_vevent(entry, entry_Node, entry_Url, blog_name)
  {
  var vevents = getElementsByClass('vevent', entry_Node);
  for( var vev = 0; vev < vevents.length; vev++)
    {
    var geo_Nodes = getElementsByClass('geo', vevents[vev])
    if( geo_Nodes.length > 0 )
      {
      var geo_element = null;
      // Verif si le point n'existe pas deja
      for( var ha = 0; ha < btm_geo_Array.length; ha++ )
        {
        if( btm_geo_Array[ha].Nom == geo_Nodes[0].innerHTML )
          {
          geo_element = btm_geo_Array[ha];
          }
        }

      if( geo_element == null )
        {
        var geo_coord = geo_Nodes[0].title.split(";");
        geo_element = new btm_geo( new google.maps.LatLng(geo_coord[0],geo_coord[1]),
        geo_Nodes[0].innerHTML, Type_Point, entry_Url, vevents[vev]);
        btm_geo_Array[btm_geo_Array.length] = geo_element;
        createMenuMarker( geo_element, btm_icon_Vevent, btm_icon_Vevent_Selected, entry_Node);
        createBoundaries(vevents[vev], geo_element);
        }
      addDomMarkerVevent(geo_element, vevents[vev], entry_Node, entry_Url, blog_name, btm_icon_Vevent);
      }
    }
  }

function addDomMarkerVcard(geo_element, vcards, entry_Node, entry_Url, blog_name, icon)
  {
  if( geo_element == null )
    return false;
  // Creation du DOM infoWindow
  var geo_dom = document.createElement('span');
  geo_dom.appendChild( document.createElement('br')); // Un espace en haut...
  var ul_dom = document.createElement('ol');
  // Si il y'a plusieurs vcards dans la page
  for( var v = 0; v < vcards.length; v++)
    {
    var vcard = vcards[v];
    var geos = getElementsByClass('geo', vcard);
    if( geos.length > 0 )
    {
    var org = getElementsByClass('org', vcard);
    var summary = getElementsByClass('summary', vcard);
    var locality = getElementsByClass('locality', vcard);
    
    // Pour la detection des microformats.
    var vcard_copy = vcard.cloneNode(true);
    vcard_copy.style.display = 'none';
    geo_dom.appendChild(vcard_copy);

    var img_dom =  document.createElement("img");
    img_dom.src = icon;
    img_dom.align = "middle";
    img_dom.style.borderWidth = "0px"; 
    img_dom.style.padding = "0px"; 
    img_dom.style.margin = "0px"; 
    img_dom.style.paddingRight = "0.25em"; 

    var h_dom = document.createElement('h5');
    h_dom.style.margin = "0px";
    h_dom.style.marginBottom = "0.5em";

    var title = document.createElement('span');
    title.appendChild(img_dom);

    if( org.length > 0 )
      title.appendChild( document.createTextNode( org[0].innerHTML ));
    else if( summary.length > 0 )
      title.appendChild( document.createTextNode( summary[0].innerHTML ));
      
    // Pas de lien si on est dans la page.
    if(( entry_Url != null ) && (entry_Url != ("http://" + document.location.host + document.location.pathname)))
      {
      title.appendChild( document.createTextNode(" sur "));
      title.appendChild( document.createTextNode( blog_name ));
    
      var a_dom = document.createElement('a');
      a_dom.href = entry_Url + "?btm=yes";
      a_dom.appendChild(title);
      if( entry_Url.indexOf( document.location.host ) < 0 )
        a_dom.target = "_blank";
      h_dom.appendChild(a_dom);
      }
    else
      h_dom.appendChild(title);
  
    geo_dom.appendChild(h_dom);

    var calendars = getElementsByClass("cal", vcard);
    for( var cal = 0; cal < calendars.length; cal++ )
      {
      var img_calendar =  document.createElement("img");
      img_calendar.src = "https://www.google.com/accounts/calendar20x20.gif";
      img_calendar.align = "middle";
      img_calendar.style.borderWidth = "0px"; 
      img_calendar.style.padding = "0px"; 
      img_calendar.style.margin = "0px"; 
      img_calendar.style.marginLeft = "6px"; 
      img_calendar.style.marginRight = "6px"; 
      img_calendar.style.paddingRight = "0.25em"; 

      var a_calendar = document.createElement('a');
      a_calendar.href = entry_Url + '?btm=yes';
      a_calendar.appendChild(document.createTextNode("Calendrier"));
      
      var h_calendar = document.createElement('h6');
      h_calendar.appendChild(img_calendar);
      h_calendar.appendChild(a_calendar);
      h_calendar.style.margin = "0px";
      h_calendar.style.marginBottom = "0.5em";
      geo_dom.appendChild(h_calendar);
      }

    var rsss = getElementsByClass("rss", vcard);
    for( var rss = 0; rss < rsss.length; rss++ )
      {
      var img_rss =  document.createElement("img");
      img_rss.src = "https://www.google.com/accounts/rss20x20.gif";
      img_rss.align = "middle";
      img_rss.style.borderWidth = "0px"; 
      img_rss.style.padding = "0px"; 
      img_rss.style.margin = "0px"; 
      img_rss.style.marginLeft = "6px"; 
      img_rss.style.marginRight = "6px"; 
      img_rss.style.paddingRight = "0.25em"; 

      var a_rss = document.createElement('a');
      a_rss.href = entry_Url + '?btm=yes';
      a_rss.appendChild(document.createTextNode("rssendrier"));
      
      var h_rss = document.createElement('h6');
      h_rss.appendChild(img_rss);
      h_rss.appendChild(a_rss);
      h_rss.style.margin = "0px";
      h_rss.style.marginBottom = "0.5em";
      geo_dom.appendChild(h_rss);
      }

    var li_dom = document.createElement('li');
    var categories = getElementsByClass('category', vcard);
    for( var da = 0; da < categories.length; da++)
      {
      li_dom.appendChild(document.createTextNode( categories[da].innerHTML + " " ));
      }
      ul_dom.appendChild( li_dom );
    }
    }// Boucle sur les vcards

  var actus = getElementsByClass("vevent", entry_Node);
  for( var act = 0; act < actus.length; act++ )
    {
    var img_actu =  document.createElement("img");
    img_actu.src = "https://www.google.com/accounts/news20x20.gif";
    img_actu.align = "middle";
    img_actu.style.borderWidth = "0px"; 
    img_actu.style.padding = "0px"; 
    img_actu.style.margin = "0px"; 
    img_actu.style.marginLeft = "6px"; 
    img_actu.style.marginRight = "6px"; 
    img_actu.style.paddingRight = "0.25em"; 

    var a_actu = document.createElement('a');
    var sum_actu = getElementsByClass("summary", actus[act]);
    if( sum_actu.length > 0 )
      {
      a_actu.href = entry_Url + '?btm=yes';
      a_actu.appendChild(document.createTextNode(sum_actu[0].innerHTML));
      }
        
    var h_actu = document.createElement('h6');
    h_actu.appendChild(img_actu);
    h_actu.appendChild(a_actu);
    h_actu.style.margin = "0px";
    h_actu.style.marginBottom = "0.5em";
    geo_dom.appendChild(h_actu);
    }

  var reviews = getElementsByClass("hreview", entry_Node);
  for( var rev = 0; rev < reviews.length; rev++ )
    {
    var img_review =  document.createElement("img");
    img_review.src = "https://www.google.com/accounts/notebook20x20.gif";
    img_review.align = "middle";
    img_review.style.borderWidth = "0px"; 
    img_review.style.padding = "0px"; 
    img_review.style.margin = "0px"; 
    img_review.style.marginLeft = "6px"; 
    img_review.style.marginRight = "6px"; 
    img_review.style.paddingRight = "0.25em"; 

    var a_review = document.createElement('a');
    var sum_review = getElementsByClass("summary", reviews[rev]);
    if( sum_review.length > 0 )
      {
      a_review.href = entry_Url + '?btm=yes';
      a_review.appendChild(document.createTextNode(sum_review[0].innerHTML));
      }
    
    var h_review = document.createElement('h6');
    h_review.appendChild(img_review);
    h_review.appendChild(a_review);
    h_review.style.margin = "0px";
    h_review.style.marginBottom = "0.5em";
    geo_dom.appendChild(h_review);
    }
  geo_dom.appendChild( ul_dom );

  var split = blog_name.split(' ');
  var initiale = "";
  for( var la = 0; la < split.length; la++)
    {
    initiale += split[la].substring(0,1);
    }
  geo_element.InfoWindowTabs.push( 
    new google.maps.InfoWindowTab( initiale , geo_dom) );

  // Info bulle quand on est sur la bonne page
  
  if(entry_Url == ("http://" + document.location.host + document.location.pathname) )
    {
    var infoBulle = document.createElement('span');
    infoBulle.appendChild(h_dom.cloneNode(true));

    btm_Map.openInfoWindow(btm_Map.getCenter(), infoBulle );
    }
  }

function addDomMarkerVevent(geo_element, vevent, entry_Node, entry_Url, blog_name, icon)
  {
  if( geo_element == null )
    return false;
  // Creation du DOM infoWindow
  var geo_dom = document.createElement('span');
  geo_dom.appendChild( document.createElement('br')); // Un espace en haut...
  var ul_dom = document.createElement('ol');
  var geos = getElementsByClass('geo', vevent);
  if( geos.length > 0 )
    {
    var org = getElementsByClass('org', vevent);
    var summary = getElementsByClass('summary', vevent);
    var locality = getElementsByClass('locality', vevent);

    // Pour la detection des microformats.
    var vevent_copy = vevent.cloneNode(true);
    vevent_copy.style.display = 'none';
    geo_dom.appendChild(vevent_copy);

    var img_dom =  document.createElement("img");
    img_dom.src = icon;
    img_dom.align = "middle";
    img_dom.style.borderWidth = "0px"; 
    img_dom.style.padding = "0px"; 
    img_dom.style.margin = "0px"; 
    img_dom.style.paddingRight = "0.25em"; 

    var h_dom = document.createElement('h5');
    h_dom.style.margin = "0px";
    h_dom.style.marginBottom = "0.5em";

    var title = document.createElement('span');
    title.appendChild(img_dom);

    var dtstarts = getElementsByClass('dtstart', vevent);
    for( var ds = 0; ds < dtstarts.length; ds++)
      {
      title.appendChild(document.createTextNode( dtstarts[ds].getAttribute("title") + " " ));
      }

    if( org.length > 0 )
      title.appendChild( document.createTextNode( org[0].innerHTML ));
    else if( summary.length > 0 )
      title.appendChild( document.createTextNode( summary[0].innerHTML ));
      
    // Pas de lien si on est dans la page.
    if(( entry_Url != null ) && (entry_Url != ("http://" + document.location.host + document.location.pathname)))
      {
      title.appendChild( document.createTextNode(" sur "));
      title.appendChild( document.createTextNode( blog_name ));

      var a_dom = document.createElement('a');
      a_dom.href = entry_Url + "?btm=yes";
      a_dom.appendChild(title);
      if( entry_Url.indexOf( document.location.host ) < 0 )
        a_dom.target = "_blank";
      h_dom.appendChild(a_dom);
      }
    else
      h_dom.appendChild(title);

    geo_dom.appendChild(h_dom);

    var li_dom = document.createElement('li');

    var categories = getElementsByClass('category', vevent);
    for( var da = 0; da < categories.length; da++)
      {
      li_dom.appendChild(document.createTextNode( categories[da].innerHTML + " " ));
      }
      ul_dom.appendChild( li_dom );
    }

  geo_dom.appendChild( ul_dom );

  var split = blog_name.split(' ');
  var initiale = "";
  for( var la = 0; la < split.length; la++)
    {
    initiale += split[la].substring(0,1);
    }
  geo_element.InfoWindowTabs.push( 
    new google.maps.InfoWindowTab( initiale , geo_dom) );

  // Info bulle quand on est sur la bonne page
  
  if(entry_Url == ("http://" + document.location.host + document.location.pathname) )
    {
    var infoBulle = document.createElement('span');
    infoBulle.appendChild(h_dom.cloneNode(true));

    btm_Map.openInfoWindow(btm_Map.getCenter(), infoBulle );
    }
  }
// Crée un marker et un menu en fonction du Geo Element
function createMenuMarker( geo_element, icon, iconSelected, entry_Node )
  {
  if( geo_element == null )
    return false;
  // Creation du marker
  var geo_Icon = new google.maps.Icon(G_DEFAULT_ICON);
  geo_Icon.image = icon;
  if( geo_element.Type == Type_Point )
    {
    geo_Icon.iconSize = new google.maps.Size(32,32);
    geo_Icon.iconAnchor = new google.maps.Point(8, 32);
    }
  else
    {
    geo_Icon.iconSize = new google.maps.Size(32,32);
    geo_Icon.iconAnchor = new google.maps.Point(16, 16);
    }
  geo_Icon.shadowSize = new GSize(32, 32);
  geo_Icon.infoWindowAnchor = new GPoint(8, 32);    
  
  geo_element.GMarker = new google.maps.Marker(geo_element.GLatLng,
    { icon:geo_Icon, title:geo_element.Nom  });

  google.maps.Event.addListener( geo_element.GMarker, 'click', function()
    { 
      if( geo_element.Type == Type_Point )
        {
        var maxContent = document.createElement("span");
        for( var tab = 0; tab < geo_element.InfoWindowTabs.length; tab++)
          {
          maxContent.appendChild(geo_element.InfoWindowTabs[tab].contentElem);
          }
//        geo_element.GMarker.openInfoWindowTabs(geo_element.InfoWindowTabs,
//          { maxContent: maxContent.innerHTML }); 
        pageTracker._trackPageview ('/map/' + encodeURI(geo_element.Nom) );
        geo_element.GMarker.openInfoWindowTabs(geo_element.InfoWindowTabs,
          { maxContent: entry_Node }); 
        }
      else
        {
        var zoomLevel = btm_Map.getBoundsZoomLevel( geo_element.GLatLngBounds ) - 1
        if( btm_Map.getZoom() == zoomLevel )
          zoomLevel += 1;
        btm_Map.setCenter( geo_element.GMarker.getLatLng(), zoomLevel); 
        }
    });
  google.maps.Event.addListener( geo_element.GMarker,'mouseover',function()
    {
    if( geo_element.GMarker.isHidden() ) 
      return;
    geo_element.GMarker.setImage(iconSelected);
    imgs = geo_element.MenuNode.getElementsByTagName("img");
    if( imgs.length > 0 )
      imgs[0].src = iconSelected;
    });
  google.maps.Event.addListener( geo_element.GMarker,'mouseout',function()
    {
    if( geo_element.GMarker.isHidden() ) 
      return;
    geo_element.GMarker.setImage(icon);
    imgs = geo_element.MenuNode.getElementsByTagName("img");
    if( imgs.length > 0 )
      imgs[0].src = icon;
    });
  // Creation du menu
  var img_menu =  document.createElement("img");
  img_menu.src = icon;
  img_menu.align = "middle";
  img_menu.style.borderWidth = "0px"; 
  img_menu.style.padding = "0px"; 
  img_menu.style.margin = "0px"; 
  img_menu.style.paddingRight = "0.25em"; 
  img_menu.style.width = "1em"; 
  img_menu.style.height = "1em";

  var a_menu =  document.createElement('a');
  a_menu.className = 'trigger';
  a_menu.style.textDecoration = "none";
  a_menu.appendChild(img_menu);
  a_menu.appendChild(document.createTextNode(geo_element.Nom));
  a_menu.href = "#"
  
  var geo_menu = document.createElement('h5');
  geo_menu.appendChild(a_menu);
  geo_menu.style.margin = "0";
  geo_menu.style.display = 'none';
  
  geo_element.MenuNode = geo_menu;
  
  return true;
  }
function createBoundaries( vcard, geo )
{
  var locality = getElementsByClass('locality', vcard);
  if( locality.length > 0 )
    addBoundary(locality[0].innerHTML, geo, Type_Locality );
  var region = getElementsByClass('region', vcard);
  if( region.length > 0 )
    addBoundary(region[0].innerHTML, geo, Type_Region );
  var country = getElementsByClass('country-name', vcard);
  if( country.length > 0 )
    addBoundary(country[0].innerHTML, geo, Type_Country );
}

function addBoundary(nom, geo, type)
{
  for( var bou = 0; bou < btm_boundary_Array.length; bou++ )
    {
    if(nom == btm_boundary_Array[bou].Nom )
      {
      btm_boundary_Array[bou].GLatLngBounds.extend(geo.GMarker.getLatLng());
      btm_boundary_Array[bou].Geos.push(geo);
      return btm_boundary_Array[bou];
      }
    }
// Le boundary est inconnu on en crée un
  var boundary = new btm_boundary(nom, type);
  boundary.GLatLngBounds.extend(geo.GMarker.getLatLng());
  boundary.Geos.push(geo);
  btm_boundary_Array.push(boundary);   
  return boundary;
}


//////////////////////////////////////////////////
// Tools
//////////////////////////////////////////////////  
function getElementsByClass(searchClass, node, tag) 
  { 
  var classElements = new Array(); 
  if ( node == null ) 
    node = document; 
  if ( tag == null ) 
    tag = '*'; 
  var els = node.getElementsByTagName(tag); 
  var elsLen = els.length; 
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); 
  for (i = 0, j = 0; i < elsLen; i++) 
    { 
    if ( pattern.test(els[i].className) ) 
      { 
      classElements[j] = els[i]; 
      j++; 
      } 
    } 
  return classElements; 
  } 
 
 function sortGeo(a,b)
  {
  return (a.Nom > b.Nom)?1:-1;
  }


function addAttribute(parentNode,attributeName,value)
{
var temp =document.createAttribute(attributeName); 
temp.value= value;
parentNode.setAttributeNode(temp);
if(document.all)
{
if(attributeName =="onmouseover" || attributeName =="onmouseout" ||attributeName =="onclick" )
{
var myfunction =null;
var command = "myfunction =function(){"+value+"}";
eval(command);
if(attributeName =="onmouseover" ){
parentNode.setAttribute('onmouseover',myfunction) ;
parentNode.onmouseover =myfunction;
}
else
if(attributeName =="onmouseout" ){
parentNode.setAttribute('onmouseout',myfunction) ;
parentNode.onmouseout =myfunction;
}
else
if(attributeName =="onclick" ){
parentNode.setAttribute('onclick',myfunction) ;
parentNode.onclick =myfunction;
}
}
}
}

var clickedPixel; 
function createMenuEntry(text, href, icon)
{
  var entry = document.createElement('h5');
  entry.style.margin = "0";
  
  var a_entry =  document.createElement('a');
  a_entry.style.textDecoration = "none";
  a_entry.appendChild(document.createTextNode(text));
  a_entry.href = href; // Voir pour InfoWindowOpen ?
  entry.appendChild(a_entry);

  if( icon != null)
    {
    var img_entry =  document.createElement('img');
    img_entry.src = icon;
    img_entry.align = "middle";
    img_entry.style.borderWidth = "0px"; 
    img_entry.style.padding = "0px"; 
    img_entry.style.margin = "0px"; 
    img_entry.style.paddingRight = "0.25em"; 
    img_entry.style.width = "1em"; 
    img_entry.style.height = "1em";
    a_entry.appendChild(img_entry);
    }

  return entry;
}

// Context menu
function setContextMenu()
  {
  // === Global variable that can be used by the context handling functions ==
  // === create the context menu div ===
  btm_context = document.createElement("div");
  btm_context.className = "btm_context";
  btm_context.style.visibility = "hidden";
  btm_context.style.background = "#ffffff";
  btm_context.style.border="1px solid"// #8888FF";
  btm_context.style.padding = "3px";
  btm_context.appendChild( createMenuEntry("Zoom avant","javascript:zoomIn()"));
  btm_context.appendChild( createMenuEntry("Zoom arrière","javascript:zoomOut()"));
  btm_context.appendChild( createMenuEntry("Zoom avant ici","javascript:zoomInHere()"));
  btm_context.appendChild( createMenuEntry("Zoom arrière ici","javascript:zoomOutHere()"));
  btm_context.appendChild( createMenuEntry("Centrer ici","javascript:centreMapHere()"));

  btm_Map.getContainer().appendChild(btm_context);

  // === listen for singlerightclick ===
  GEvent.addListener(btm_Map,"singlerightclick",function(pixel,tile) 
    {
    // store the "pixel" info in case we need it later
    // adjust the context menu location if near an egde
    // create a GControlPosition
    // apply it to the context menu, and make the context menu visible
    clickedPixel = pixel;
    var x=pixel.x;
    var y=pixel.y;
    if (x > btm_Map.getSize().width - 120) { x = btm_Map.getSize().width - 120 }
    if (y > btm_Map.getSize().height - 100) { y = btm_Map.getSize().height - 100 }
    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
    pos.apply(btm_context);
    btm_context.style.visibility = "visible";
    });
  // === If the user clicks on the btm_Map, close the context menu ===
  GEvent.addListener(btm_Map, "click", function() 
    {
    btm_context.style.visibility="hidden";
    });
  }
  // === functions that perform the context menu options ===
  function zoomIn() {
    // perform the requested operation
    btm_Map.zoomIn();
    // hide the context menu now that it has been used
    btm_context.style.visibility="hidden";
  }      
  function zoomOut() {
    // perform the requested operation
    btm_Map.zoomOut();
    // hide the context menu now that it has been used
    btm_context.style.visibility="hidden";
  }      
  function zoomInHere() {
    // perform the requested operation
    var point = btm_Map.fromContainerPixelToLatLng(clickedPixel)
    btm_Map.zoomIn(point,true);
    // hide the context menu now that it has been used
    btm_context.style.visibility="hidden";
  }      
  function zoomOutHere() {
    // perform the requested operation
    var point = btm_Map.fromContainerPixelToLatLng(clickedPixel)
    btm_Map.setCenter(point,btm_Map.getZoom()-1); // There is no btm_Map.zoomOut() equivalent
    // hide the context menu now that it has been used
    btm_context.style.visibility="hidden";
  }      
  function centreMapHere() {
    // perform the requested operation
    var point = btm_Map.fromContainerPixelToLatLng(clickedPixel)
    btm_Map.setCenter(point);
    
    // hide the context menu now that it has been used
    btm_context.style.visibility="hidden";
  }
  
function log(message) {
//  return;
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}