var bounds = null;

var map;
var geocoder;
var pano;
var initialized = false;
var panoinitialized = false;

function initialize() {

	var mc = document.getElementById("map_canvas");
	map = new GMap2(mc);
	bounds = new GLatLngBounds();
	var ll = new GLatLng(42.345573, -71.098326);
	map.setCenter(ll, 16);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();
}

//addAddressToMap() is called when the geocoder returns an
//answer. It adds a marker to the map with an open info window
//showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {

	if (!response || response.Status.code != 200) {
		// alert("Sorry, we were unable to geocode that address");
	} else {
		//GZ - in future, check to see if we have lat/long already!!!
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
				place.Point.coordinates[0]);

		marker = new GMarker(point);
		
		map.addOverlay(marker);

		bounds.extend(point)

		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		
		var panorama = document.getElementById("pano");

		/*
		 * only add streetview if found the container div
		 */
		if (panorama) {
			
			if (!panoinitialized){

				panoramaOptions = {
					latlng : point
				};
	
				pano = new GStreetviewPanorama(panorama, panoramaOptions);
	
				var iw = map.getInfoWindow();
				iw.show();
				pano.setLocationAndPOV(point);
				
				panoinitialized = true;
			}
		}
	}
}

//It geocodes the address entered into the form
//and adds a marker to the map at that location.

function showLocation(address, address_unit, rd_host, prop_id, image, price, bed, bath, latitude, longitude) {

	if (!initialized) {
		initialize();
		initialized = true;
	}
	
	if ((latitude==0 && longitude==0) || (latitude=='' || longitude=='')){
	geocoder.getLocations(address, 
		function (response) {
			if (!response || response.Status.code != 200) {
				 //alert("Sorry, we were unable to geocode that address");
			} else {			
				place = response.Placemark[0];
			    //GZ - in the future, implement so that missing data is sent back to the listing and saved
				point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				var marker = new GMarker(point);				
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml("<img src=\"" + image + "\" class=\"gmap_pic\" /><div class=\"gmap_text\"><span id=\"price\"><a href=\"" + rd_host + "/property/"+prop_id+"/"+prop_id+"\">" + address_unit + "</a></span><br/><span id=\"price\">" + price + "</span><br/><span id=\"specs\">" + bed + " br, " + bath + " ba</span></div>");
				});
				map.addOverlay(marker);
				bounds.extend(point)
				map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
				var panorama = document.getElementById("pano");
	
				/* only add streetview if found the container div */
				if (panorama) {
					if (!panoinitialized){
						panoramaOptions = {
							latlng : point
						};
						pano = new GStreetviewPanorama(panorama, panoramaOptions);
						var iw = map.getInfoWindow();
						iw.show();
						pano.setLocationAndPOV(point);
						panoinitialized = true;
					}
				}
			}
		});
	} else { 
				point = new GLatLng(latitude, longitude);	
				var marker = new GMarker(point);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml("<img src=\"" + image + "\" class=\"gmap_pic\" /><div class=\"gmap_text\"><span id=\"price\"><a href=\"" + rd_host + "/property/"+prop_id+"/"+prop_id+"\">" + address_unit + "</a></span><br/><span id=\"price\">" + price + "</span><br/><span id=\"specs\">" + bed + " br, " + bath + " ba</span></div>");
				});
				map.addOverlay(marker);
				bounds.extend(point)
				map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
				var panorama = document.getElementById("pano");
	
				/* only add streetview if found the container div */
				if (panorama) {
					if (!panoinitialized){
						panoramaOptions = {
							latlng : point
						};
						pano = new GStreetviewPanorama(panorama, panoramaOptions);
						var iw = map.getInfoWindow();
						iw.show();
						pano.setLocationAndPOV(point);
						panoinitialized = true;
					}
				}
	}
}


function zoomLocation(address){

	/*
	 * force map to be recreated
	 */
	initialize();
    //GZ - in future, check to see if we have lat/long already!!!
	geocoder.getLocations(address, addAddressToMap);
}
