var map;
var markerHash = {};

// var PropertyLogo = new GIcon();
// PropertyLogo.image = '/images/map_icon.png';
// PropertyLogo.iconSize = new GSize(75, 81);
// PropertyLogo.iconAnchor = new GPoint(37, 14);
// PropertyLogo.infoWindowAnchor = new GPoint(37, 40);

function addMarker(lat, lng, id) {
  var marker = new GMarker(new GLatLng(lat, lng));
  GEvent.addListener(marker, 'click',
    function() {
      markerHash[id].marker.openInfoWindowHtml(markerHash[id].full_address);
    }
  );

  map.addOverlay(marker);
  return marker;
}

function loadMap() {
  map = new GMap2(document.getElementById("map"));
  // map.enableScrollWheelZoom();
  map.addControl(new GLargeMapControl3D());
  var location = new GLatLng(lat, lng);
  map.setCenter(location, startZoom);
  for (var i = markers.length - 1; i >= 0; i--){
    var current = markers[i];
    marker = addMarker(current.lat, current.lng, current.id);
    markerHash[current.id]={marker:marker,full_address:current.full_address,visible:true};
  }


  // Add GHierarchicalMapTypeControl
  map.addMapType(G_PHYSICAL_MAP);
  var hControl = new GHierarchicalMapTypeControl();
  hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
  map.addControl(hControl);
  
   // Add ContextMenuControl to the map
  map.addControl(new ContextMenuControl());

  // the next two lines select the appropriate map upon window load
  jQuery('#active').trigger('click').addClass('selected_map_link');
  jQuery('#sold').trigger('click').addClass('selected_map_link');
  
}

function filter(type) {
  for(i=0; i < markers.length; i++) {
    var current=markers[i];
    if (current.status == type || 'All' == type) {
      if (!markerHash[current.id].visible) {
        map.addOverlay(markerHash[current.id].marker);
        markerHash[current.id].visible=true;
      }
    } else {
      if (markerHash[current.id].visible) {
        map.removeOverlay(markerHash[current.id].marker);
        markerHash[current.id].visible=false;
      }
    }
  }  
}


jQuery(function($) {
  window.onload = loadMap;
  
  $('.map_toolbar_link_function').bind('click', function(event) {
    $('.map_toolbar_link_function').removeClass('selected_map_link');
    $(this).addClass('selected_map_link');
  });
  
  window.onunload = GUnload;
});
