var map = null;
var geocoder = null;
var tooltip = null;
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;


    function load() {
      if (GBrowserIsCompatible()) {
        
       
map = new GMap2(document.getElementById("map"));
geocoder=new GClientGeocoder();
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(42.24122708941077, -83.6220932006836), 13);
       tooltip = document.createElement("div");
      document.getElementById("map").appendChild(tooltip);
      tooltip.style.visibility="hidden";
      }
    }



//      // ====== set up marker mouseover tooltip div ======

//
//
//      // Read the data from example.xml
//      var request = GXmlHttp.create();
//      request.open("GET", "example.xml", true);
//      request.onreadystatechange = function() {
//        if (request.readyState == 4) {
//          var xmlDoc = request.responseXML;
//          // obtain the array of markers and loop through it
//          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
//          
//          for (var i = 0; i < markers.length; i++) {
//            // obtain the attribues of each marker
//            var lat = parseFloat(markers[i].getAttribute("lat"));
//            var lng = parseFloat(markers[i].getAttribute("lng"));
//            var point = new GLatLng(lat,lng);
//            var html = markers[i].getAttribute("html");
//            var label = markers[i].getAttribute("label");
//            // create the marker
//            var marker = createMarker(point,label,html);
//          }
//          // put the assembled side_bar_html contents into the side_bar div
//          document.getElementById("side_bar").innerHTML = side_bar_html;
//        }
//      }
//      request.send(null);
//      }
//    } 



      // A function to create the marker and set up the event window
      function createMarker(point,name,html) {
        var marker = new GMarker(point);
        // === store the name so that the tooltip function can use it ===
        marker.tooltip = '<div class="tooltip">'+name+'</div>';
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        side_bar_html += '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + name + '</a><br>';
        i++;
        //alert(side_bar_html);
        map.addOverlay(marker);

        //  ======  The new marker "mouseover" and "mouseout" listeners  ======
        GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
        });        
      }

      // ====== This function displays the tooltip ======
      // it can be called from an icon mousover or a side_bar mouseover
      function showTooltip(marker) {
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
      }

      // ===== This function is invoked when the mouse goes over an entry in the side_bar =====
      // It launches the tooltip on the icon      
      function mymouseover(i) {
        showTooltip(gmarkers[i])
      }
      // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
      // It hides the tooltip      
      function mymouseout() {
	tooltip.style.visibility="hidden";
      }

      // This function picks up the side_bar click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }

function getSideBar() {
	return side_bar_html;
}
      
    

   


function showAddress(address, hotel) {
  try {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " can't find!");
      } else {
      	
            var html = buildMarkerHtml(hotel);
            var label = buildMarkerLabel(hotel);
            // create the marker
            var marker = createMarker(point,label,html);


      //  map.setCenter(point, 13);
//        var marker = new GMarker(point);
//        map.addOverlay(marker);
//        marker.openInfoWindowHtml(buildMarker(hotel));
        
        
      }
    }
  );
}
catch ( e) {
	
}
}
function buildMarkerHtml(hotel) {
	return hotel.name + "\n"+hotel.address + "\n"+hotel.phone;
}

function buildMarkerLabel (hotel) {
	return hotel.name;
}

var request = null; 

 
  function getFullAddress()  {
    return getAddress()+", "+getCity()+", "+getState();
  }

function updateMap()  {
  showAddress(getFullAddress());
  }



 function createRequest()  {
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft)   {
      try  {
        request = new ActiveXObject("Msxm12.XMLHTTP");
      } catch (othermicrosoft)  {
        try  {
         request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed)  {
          request = null;
        }
      }
     }

if (request == null)
  alert("Error creating request object!");
 }
