var map;
var icons = new Array();
var markerHash = {};

google.load("maps", "2");
google.setOnLoadCallback(initialize);

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        icons["red"] = new GIcon(G_DEFAULT_ICON); 
        icons["red"].image = "/images/map_icons/marker_red.png"; 
	for (var i = 0; i < markers.length; i++) {
          var current = markers[i];
	  if(current.image_path){
            marker = addMarker(current.id, current.latitude,current.longitude, current.description, current.image_path);
            markerHash[current.id] = {marker:marker, description:current.description, visible:true};
          }
        }
    }
}

function reinitialize() {
    if (GBrowserIsCompatible()) {
        //structuresmatch1 = '/media/location_category_attachments/01/31-p.png';
        //lotsmatch = '/media/location_category_attachments/00/50-parkingp.png';
        lotsmatch = '/media/location_category_attachments/01/31-p.png';
        structuresmatch1 = '/media/location_category_attachments/00/50-parkingp.png';
        map = new google.maps.Map2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        icons["red"] = new GIcon(G_DEFAULT_ICON);
        icons["red"].image = "/images/map_icons/marker_red.png";
        for (var i = 0; i < markers.length; i++) {
          var current = markers[i];
          if(current.image_path==lotsmatch)
                if($('lots').checked)
                        marker = addMarker(current.id, current.latitude,current.longitude, current.description, current.image_path);
          if(current.image_path==structuresmatch1)
                if($('structures').checked)
                        marker = addMarker(current.id, current.latitude,current.longitude, current.description, current.image_path);
          markerHash[current.id] = {marker:marker, description:current.description, visible:true};
        }
    }
}


function get_icon(image_path) {
   if ((typeof(image_path)=="undefined") || (image_path==null)) { 
      image_path = "/images/map_icons/marker_red.png"; 
   }
   if (!icons[image_path]) {
      icons[image_path] = new GIcon();
      icons[image_path].image = image_path;
      icons[image_path].iconSize = new GSize(20, 20);
      icons[image_path].iconAnchor = new GPoint(12, 16);
      icons[image_path].infoWindowAnchor = new GPoint(16, 16);
   } 
   return icons[image_path];
}

function focusPoint(id) {
  markerHash[id].marker.openInfoWindowHtml(markerHash[id].description);
}

// A function to create the marker and set up the event window
// function createMarker(point,name,html) {
function createMarker(point,html,image_path) {
	// FF 1.5 fix
  html = '<div style="white-space:nowrap;">' + html + '</div>';
  var marker = new GMarker(point);
	if (image_path) {
  	marker = new GMarker(point, get_icon(image_path));
  }
  return marker;
}

function addMarker(id, latitude, longitude, description, image_path) {
		var point = new GLatLng(latitude, longitude)
    var marker = createMarker(point,description,image_path);
    GEvent.addListener(marker, 'click',
      function() {
        focusPoint(id);
      }
    );
    map.addOverlay(marker);
    return marker;
}
