/**
* Fonction permettant de centrer la carte par rapport à un nuage de marqueurs
*/
var center_map_from_markers = function (map)
{
  var bounds = new google.maps.LatLngBounds()

  for(var i=0; i<markers.length; i++)
  {
    bounds.extend(markers[i].getLatLng());
  }
    
  var zoom = map.getBoundsZoomLevel(bounds);
    
  var center = bounds.getCenter();
  
  map.setCenter(center,zoom);
}

/**
* Permet de centrer la carte par rapport à un zoom et le centre
*/
var center_map_on_city = function (lat, lng, zoom)
{
  var zone_center = new google.maps.LatLng(lat, lng);
  map.setCenter(zone_center, zoom);
}

/**
* Fonction permettant de centrer la carte sur un marqueur
*/
var marker_click = function (id)
{
  if (map.getZoom()<close_view_zoom)
  {
    map.setCenter(markers[id].getLatLng(), close_view_zoom);
  }
}

/**
* Fonction permettant d'afficher le tooltip
*/
var show_tooltip = function (id, color)
{
  if(map && typeof markers[id] != "undefined")
  {
    var marker = markers[id];
    var tooltip_content = '';
  
    if (map.getZoom()<14)
    {
  	  tooltip_content += "Cliquer pour centrer la carte !";
    }
  
    map.tooltip.innerHTML = '<div class=\"tooltip\"><div class=\"tooltip_title\">'+marker.name+'</div><div class=\"tooltip_content\">'+tooltip_content+'</div></div>';
  
    // coordonnée du bootom_left de la carte
    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
  
    // coordonnée du marker
    var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
  
    //coordonnée de ou est situe l'icone par rapport au marker
    var anchor=marker.getIcon().iconAnchor;
  
    //largeur de l'icone
    var width=Math.max(marker.getIcon().iconSize.width,16);
    var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new google.maps.Size(offset.x - point.x - anchor.x + width, map.getSize().height + offset.y - point.y - anchor.y)); 
    pos.apply(map.tooltip);
	//map.tooltip.innerHTML += map.getSize().height + ' ' + offset.y + ' ' + point.y + ' ' +anchor.y;
	map.tooltip.style.visibility='visible';
	  
	bar_over(markers[id].id, color);
  }
}

/**
* Fonction permettant de masquer le tooltip
*/
var hide_tooltip = function (id, color)
{
  if(map && typeof markers[id] != "undefined")
  {
    map.tooltip.style.visibility='hidden';
    bar_out(markers[id].id, color);
  }
}

/**
* Fonction permettant d'afficher le tooltip sur la carte quand on rollover sur un bar de la liste
*/
var sidebar_over = function (id, color)
{
  if(typeof markers != "undefined")
  {
	  for(var i=0; i<markers.length; i++)
	  {
	    if(markers[i].id == id)
		{
	      var marker_key = i;
		  break;
		}
	  }

	  show_tooltip(marker_key, color);
  }
}

/**
* Fonction permettant de masquer le tooltip sur la carte quand on rollout sur un bar de la liste
*/
var sidebar_out = function (id, color)
{
  if(typeof markers != "undefined")
  {
	for(var i=0; i<markers.length; i++)
	{
	  if(markers[i].id == id)
	  {
	    var marker_key = i;
	    break;
	  }
	}
	  
	hide_tooltip(marker_key, color);
  }
}

/**
* Fonction permettant de changer la couleur de fond de la div quand on rollver sur un bar de la liste
*/
var bar_over = function (bar_id, color)
{
  $('bar_'+bar_id).style.backgroundColor = color;
}

/**
* Fonction permettant de remettre la couleur de fond par défaut de la div quand on rollver sur un bar de la liste
*/
var bar_out = function (bar_id, color)
{
  $('bar_'+bar_id).style.backgroundColor = color;
}

/**
*  Affiche les quartiers suplémentaire
*/
var show_more_districts = function()
{
  $('more-districts').style.display = 'block';
  $('link-more-districts').innerHTML = '<a href="#" onclick="more_districts_close(); return false;" class="green-link">... moins de quartiers &laquo;</a>';
  return false;
}

/**
*  Masque les quartiers suplémentaire
*/
var more_districts_close = function()
{
  $('more-districts').style.display = 'none';
  $('link-more-districts').innerHTML = '<a href="#" onclick="show_more_districts(); return false;" class="green-link">... plus de quartiers &raquo;</a>';
  return false;
}
