// this is temporarily being used for testing when I just load the map not via a callout
document.observe("dom:loaded", function(){
  var err;
  try
  {
      selectionCallout = new SelectionCallout($$('select.map-callout-trigger')[0],'ajax-callout');
  }
  catch(err){ /* alert("saw the dom:loaded error catch statement:" + err.message); */  }
});

function initializeFrontendMap(){
  //bind selection field
  if ($$('select.map-callout-trigger')[0]){
    selectionCallout = new SelectionCallout($$('select.map-callout-trigger')[0],'ajax-callout');
  }
  initializeMap();
}
  
function initializeMap(){
  
  //initialize listing array
  var i=0
  $("map_markers").select("ul li").each(function(s,index){
    if (s.id != 'geo_marker') {
      listings[i] = {
        'id':s.id,
        'latitude':s.readAttribute("latitude"),
        'longitude': s.readAttribute("longitude"),
        'address': s.readAttribute("address"),
        'phone': s.readAttribute("phone"),
        'type': s.readAttribute("type"),
        'thumb': s.readAttribute("thumb"),
        'url': s.readAttribute("url"),
        'name': s.readAttribute("name") }
      i++
    } else {
      geo_marker.id = s.id
      geo_marker.latitude = s.readAttribute("latitude")
      geo_marker.longitude = s.readAttribute("longitude")
      geo_marker.name = s.textContent
    }
  });
  
  //reset all check boxes
  cbPicks = document.getElementsByName('picks')
  for(i = 0; i < cbPicks.length; i++){
    cbPicks[i].checked = false
  }
  
  var zoom = 13
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_div"));
    map.setCenter(new GLatLng(geo_marker['latitude'],geo_marker['longitude']), zoom);
    map_center = new GMarker(new GLatLng(geo_marker['latitude'],geo_marker['longitude']), {icon : geo_icon, title : geo_marker['name']});
    map.addOverlay(map_center);

    for(i=0; i < listings.length; i++) {
      point = new GLatLng(listings[i].latitude,listings[i].longitude);
      markers[listings[i].id] = new GMarker(new GLatLng(listings[i].latitude,listings[i].longitude),{icon : whichIcon(listings[i].type), title : listings[i].name});
      markers[listings[i].id].bindInfoWindow("<img src='" + listings[i].thumb + "' /><strong>" + listings[i].name + "</strong><br />" + listings[i].address + "<br />" + listings[i].phone + "<br /><a href='" + listings[i].url + "'>More ></a>");
      map.addOverlay(markers[listings[i].id],listings[i].name);
      markers[listings[i].id].hide();
    }
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
  }
}


