var last_map; // global scope variable
var sold_icon = new GIcon();
    sold_icon.image = '/images/icons/pacifica.png';
    sold_icon.shadow = '/images/icons/shadow.png';
    sold_icon.iconSize = new GSize(24,25);
    sold_icon.shadowSize = new GSize(24,25);
    sold_icon.iconAnchor = new GPoint(9,23);
    sold_icon.infoWindowAnchor = new GPoint(19,0);
    sold_icon.printImage = '/images/icons/pacifica.gif';
    sold_icon.mozPrintImage = '/images/icons/pacifica_mozprint.png';
    sold_icon.printShadow = '/images/icons/shadow.gif';
    sold_icon.transparent = '/images/icons/pacifica_transparent.png';
var sold_options = {
	icon:sold_icon
}
var listed_icon = new GIcon();
    listed_icon.image = '/images/icons/listed.png';
    listed_icon.shadow = '/images/icons/shadow.png';
    listed_icon.iconSize = new GSize(24,25);
    listed_icon.shadowSize = new GSize(24,25);
    listed_icon.iconAnchor = new GPoint(9,23);
    listed_icon.infoWindowAnchor = new GPoint(19,0);
    listed_icon.printImage = '/images/icons/pacifica.gif';
    listed_icon.mozPrintImage = '/images/icons/pacifica_mozprint.png';
    listed_icon.printShadow = '/images/icons/shadow.gif';
    listed_icon.transparent = '/images/icons/pacifica_transparent.png';
var listed_options = {
	icon:listed_icon
}
var markers = [];

function show_map(theMap, addr1, addr2, zip, latitude, longitude, status) {
  if(GBrowserIsCompatible()) {
    if(!document.getElementById(theMap)) return false;
    var map = new GMap2(document.getElementById(theMap));
	last_map = map; // Save map as last_map for later reference
    map.enableContinuousZoom();
    map.enableDoubleClickZoom();
    map.addControl(new GSmallMapControl());
//    map.addControl(new GMapTypeControl());

    var address_0 = {
      street: addr1,
      city: addr2,
      state: 'CA',
      zip: zip,
      country: 'USA',
      infowindow: 'default',
      infowindowtext: address_info(addr1, addr2, zip),
      full: addr1 + ',' + addr2 + ', CA, ' + zip + ', USA',
      isdefault: true
    }

    if (arguments.length == 4) { // If lat/long not provided
        var geocoder = new GClientGeocoder();
		geocoder.getLatLng (
		  address_0.full,
		  function(point) {
			if(point) {
			  if(document.getElementById('Longitude')) // If doc has a Longitude field, update it
				document.getElementById('Longitude').value = point.x;
			  if(document.getElementById('Latitude')) // If doc has a Latitude field, update it
				document.getElementById('Latitude').value = point.y;
	
			  map.setCenter(point, 13);
			  var marker = new GMarker(point, status == 'Sold' ? sold_options : listed_options);
			  GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(address_0.infowindowtext)});
			  map.addOverlay(marker);
			  marker.openInfoWindowHtml(address_0.infowindowtext);
			}
			else {
			  map.setCenter(new GLatLng(37.937009, -122.534931), 13); // Niz' Office lat/long
			}
		  }
		)
	} else
	{
	  var point = new GLatLng(latitude,longitude);
	  map.setCenter(point, 13);
	  var marker = new GMarker(point, sold_options);
	  GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(address_0.infowindowtext);
	  });
	  map.addOverlay(marker);
//	  marker.openInfoWindowHtml(address_0.infowindowtext);
	}
  }
}

function refresh_map(theMap) { // Using current contents of Address1, Address2, Zip, create a new map
	if (theMap == null) theMap = last_map;
	show_map(theMap,
    document.getElementById('Address1').value,
    document.getElementById('Address2').value,
	document.getElementById('Zip').value);
}

function add_map_marker(theMap, id, addr1, addr2, zip, latitude, longitude, status) { // Using current contents of Address1, Address2, Zip, create a new map
  if(GBrowserIsCompatible()) {
	var theLocation = {
	id:id,
	infowindowtext:address_info(addr1, addr2, zip),
	latitude:validate_coord(latitude),
	longitude:validate_coord(longitude),
	status:status
	}
	 
	if (theLocation.latitude == null) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng (
		  addr1 + ',' + addr2 + ', CA, ' + zip + ', USA',
		  function(point) {
			if(point) {
			  theLocation.latitude = point.y;
			  theLocation.longitude = point.x;
			}
//			else alert('No coords for ' + theLocation.infowindowtext); // debug
		  }
		)
	}
  markers.push(theLocation);
  }
}

function validate_coord(theCoord) {
	return (Math.abs(theCoord) > 0) ? theCoord : null;
}

function serialize(obj) { // useful for debugging objects
	var str = '';
	for (key in obj) {
		str += "Key: " + key + ' => ' + obj[key] + '\n';
	}
	return str;
}
	
function address_info(addr1, addr2, zip) {
	return '<span style="font: 12px Verdana, Arial, Helvetica, sans-serif; color: #000;"><strong>Address:</strong><br />' +
	   addr1 + '<br />' + addr2 + ', CA ' + zip + '</span>';
}

// previous calls to add_map_markers has created a list of markers to post
function show_map_with_markers(theMap, zoomLevel, showTypes) {
  if(GBrowserIsCompatible()) {
    if(!document.getElementById(theMap)) return false;
    var map = new GMap2(document.getElementById(theMap));
	last_map = map; // Save map as last_map for later reference
    map.enableContinuousZoom();
    map.enableDoubleClickZoom();
	if (showTypes) {
		map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
	} else {
		map.addControl(new GSmallMapControl());
	}
	// Compute center of map markers
	var latitude = 0.0;
	var longitude = 0.0;
	var n = 0;
	for (i = 0; i < markers.length; i++) {
		theMarker = markers[i];
		if (theMarker.latitude == null) continue;
		latitude += theMarker.latitude;
		longitude += theMarker.longitude;
		n++;
	}
//	alert('Map center: ' + latitude/n + ' ' + longitude/n); // debug
	var point = new GLatLng(latitude/n, longitude/n);
	if (zoomLevel == null) zoomLevel = 10; // Set default soom level
	map.setCenter(point, zoomLevel); // set zoom level to 10
	
	for (i = 0; i < markers.length; i++) {
	  var theMarker = markers[i];
	  if (theMarker.latitude==null||theMarker.longitude==null) continue;
//	  alert ('Adding marker:\n' + serialize(theMarker));
	  var point = new GLatLng(theMarker.latitude,theMarker.longitude);
	  var marker = new GMarker(point, theMarker.status == 'Sold' ? sold_options : listed_options);
	  marker.infowindowtext = theMarker.infowindowtext;
	  marker.id = theMarker.id;
	  GEvent.addListener(marker, 'click', function() {this.openInfoWindowHtml(this.infowindowtext)});
	  GEvent.addListener(marker, 'dblclick', function() {location.href = '../ma/Detail.php?'+this.id});
	  map.addOverlay(marker);
	}
  }
}
