// JavaScript Document

    var map;
	var box;
	var box2;

	var address = null;
    var numGeocoded = 0;
	var center_new = null;
	var zoomlevel = null;
	var icon = null;
	var icon_school = null;
	var detailBox = 'F';  //This variable is set to prevent map from redrawing markers on the map if the pop up window causes the map to move


    function load(searchStr, lat, lon) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapspace"));
        map.addControl(new GSmallZoomControl());
        map.addControl(new GHierarchicalMapTypeControl());
        map.setCenter(new GLatLng(lat, lon), 13);
        map.setMapType(G_NORMAL_MAP);


		var bounds = map.getBounds();
		var sw = bounds.getSouthWest();
		var ne = bounds.getNorthEast();
		box = new GLatLngBounds(sw, ne);


		// Load main icon
		icon = new GIcon();
		icon.image = "images/gmarker_house.gif";
		addIcon(icon);


		// Get realty data for map zone
		box2 = map.getBounds();
		
		getListings(searchStr);




		// Add event listener to redraw the map with markers within the new map coordinates
/*		GEvent.addListener(map, "moveend", function(){
			// Get new map bounds
			box2 = map.getBounds();
			// redo search with new coordinates
			if (detailBox == 'F') {
				map.clearOverlays();
				getListings();
				} else {
					detailBox = 'F';
					}

			// Redraw new markers			
			});
*/

      }
    }

function createMarker(point, icon, ajax_url) {
	var marker = new GMarker(point, {icon:icon});

	GEvent.addListener(marker, "click", function() {
		detailBox = 'T';
		marker.openExtInfoWindow(map, 
			"custom_info_window_red",
			"<div>Loading Content</div>",
			{
				ajaxUrl: ajax_url,
				beakOffset: 3
				}
			);


		});
	return marker;
	}



function addIcon(icon) { // Add icon attributes

 icon.shadow= "";
 icon.iconSize = new GSize(25, 25);
 icon.shadowSize = new GSize(20, 20);
 icon.iconAnchor = new GPoint(6, 20);
 icon.infoWindowAnchor = new GPoint(5, 1);
}





// Gets listings within the current maps bounds using AJAX

function getListings(searchStr) {
			var box3 = map.getBounds();
			//alert('sw_lat ' + box3.getSouthWest().lat() + ' - sw_lng: ' + box3.getSouthWest().lng() + ' - ne_lat: ' + box3.getNorthEast().lat());
			
			new Ajax.Request('index.php?action=searchresults_json' + searchStr + '&printer_friendly_json=yes', {
			method:'get',
			requestHeaders: {Accept: 'application/json'},
			onSuccess: function(transport) {  // JSON response var name: "addresses"
				var jsonString = transport.responseText;
				jsonString = jsonString.replace(/</g, "{");  // replace the < and > with {  and } openrealty inteprets curly brackets in templates as tags, so I had to use an alternate when creating the json response
				jsonString = jsonString.replace(/>/g, "}");
				eval(jsonString);
				var k = 0;
				for(i = 0; i< address.listings.length; i++) {
					if (address.listings[i].lat != '' && address.listings[i].lng != '' && address.listings[i].lat != 0 && address.listings[i].lng != 0) {
						k++;
						var point = new GLatLng(parseFloat(address.listings[i].lat), parseFloat(address.listings[i].lng));
						map.addOverlay(createMarker(point, icon, "listing.php?listingID=" + address.listings[i].listing_id));
						//if (k == 1) {
						//	map.panTo(point);
						//	}
						box.extend(point)
						}
					}


			},
		onComplete: function() {
			// Recenter map once all markers are set.  NOTE: This is for Firefox browsers
			center_new = box.getCenter();
			//alert('center: ' + center_new);	

			// Reset map center
			zoomlevel = map.getBoundsZoomLevel(box);
			map.setCenter(center_new, zoomlevel);				
			}
		});

		


	}



function growMap() {
	document.getElementById("expandbutton").style.visibility = 'hidden';

	new Effect.Morph('mapspace', {
		style: 'width: 600px; height: 400px;',
		duration: 0.8
	});

	document.getElementById("shrinkbutton").style.visibility = 'visible';



	//setTimeout('map.checkResize()', 500);
	//setTimeout('getListings()', 600);


	}



