var map;
var geocoder;
var cmaicon;
var markeroptions;
var clinics;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setUIToDefault();
    
    geocoder = new GClientGeocoder();
    
//    cmaicon = new GIcon();
//    cmaicon.iconSize = new GSize(50, 22);
//    cmaicon.iconAnchor = new GPoint(0, 0);
//    cmaicon.infoWindowAnchor = new GPoint(25, 11);
//    cmaicon.image = "/fileadmin/projects/find-your-physician/gmaps-cma.gif";
//    markeroptions = { icon:cmaicon };
    blueicon = new GIcon();
    blueicon.image = "http://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    blueicon.iconSize = new GSize(32, 32);
    blueicon.iconAnchor = new GPoint(0, 0);
    blueicon.infoWindowAnchor = new GPoint(16, 0);
    blueicon.shadow = "http://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/msmarker.shadow.png";
    blueicon.shadowSize = new GSize(59,32);
    markeroptions = { icon:blueicon };
    
    var url = 'includes/findyourphysician/locations.php';
    var docID = getUrlVars()['docID'];
    if(docID) url += '?docID=' + docID;
    MakeRequest(url);
  }
}

function HandleResponse(response)
{
  eval(response);
  if(clinics.length == 1) {
    map.setCenter(new GLatLng(clinics[0].lat, clinics[0].lon), 12);
  } else {
    map.setCenter(new GLatLng(41.251038889, -96.012988889), 11);
  }
  for(i=0;i<clinics.length;i++)
  {
//    alert('adding point at ' + clinics[i].lat + ', ' + clinics[i].lon);
    var hght = 106;
    var wdth = 200;
    if(clinics[i].phone) hght += 16;
    if(clinics[i].hours) hght += 16;
    var html = '<div class="map-info-contents" style="height: ' + hght + 'px; width: ' + wdth + 'px;"><div class="map-clinic-name">' + clinics[i].name + '</div><p>' + clinics[i].address + '<br />' + clinics[i].city + ', ' + clinics[i].state + ' ' + clinics[i].zip;
    if(clinics[i].phone) html += '<br />Phone ' + clinics[i].phone;
    if(clinics[i].hours) html += '<br />Hours ' + clinics[i].hours;
    html += '</p><a href="/doctors/search/?locID=' + clinics[i].id + '">Find a doctor at this clinic.</a><br /><a href="http://maps.google.com/maps?saddr=&daddr=' + escape(clinics[i].address + ' ' + clinics[i].city + ', ' + clinics[i].state + ' ' + clinics[i].zip) + '&z=11" target="_blank">Get driving directions.</a></div>';
    map.addOverlay(createMarker(new GLatLng(clinics[i].lat, clinics[i].lon), html));
  }
}

function createMarker(point,html)
{
  var marker = new GMarker(point, markeroptions);
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
  // The new marker "click" listener
//  GEvent.addListener(marker,"click", function() { marker.openInfoWindowHtml(html); });        
  return marker;
}

function addSearchPoint(str)
{
  if(str.length > 0) {
    geocoder.getLatLng(
      str,
      function(point) {
        if (!point) {
          alert(str + " not found");
        } else {
          map.setCenter(point, 12);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(str);
        }
      }
    );
  } else {
    alert("The search field is empty!");
  }
}

function printMap()
{
  sw = map.getBounds().getSouthWest();
  ne = map.getBounds().getNorthEast();
  c = (sw.lat() + ne.lat())/2 + "," + (sw.lng() + ne.lng())/2;
  window.open('http://maps.google.com/?ll=' + c + '&z=' + map.getZoom() + '&pw=2');
}

function MakeRequest(url)
{
  var xmlHttp = getXMLHttp();
 
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
  var vars = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++)
  {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}
    
function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}