var bounds = new GLatLngBounds();
var geocoder = new GClientGeocoder();
var australiaCoor = new GLatLng(-29.509726, 135.625);


var GoogleMap = {
	initialize_map_search: function() {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallMapControl());
		map.setCenter(australiaCoor, 4);
		if(Listings) {
			this.showProperties(map);
			map.setZoom(map.getBoundsZoomLevel(bounds));
			map.setCenter(bounds.getCenter());
		}

	},

	
	createMarker: function(map,point, number, message) {

		// Create our "tiny" marker icon
		var custIcon = new GIcon(G_DEFAULT_ICON);
		custIcon.image = MarkerIcon;
		custIcon.iconSize = new GSize(23, 30);
		custIcon.shadowSize =  new GSize(36, 30);
		custIcon.iconAnchor = new GPoint(12, 30);

		// Set up our GMarkerOptions object
		markerOptions = { icon:custIcon };
		var marker = new GMarker(point,markerOptions);
		marker.value = number;
		if(message!='') {
			GEvent.addListener(marker, "click", function() {
				map.openInfoWindowHtml(point, message);
			});
		}
		map.addOverlay(marker);
	},

	showProperties: function(map) {
		var point =  new GLatLng(0, 0);
		var message = '';
		for(var i in Listings['lt_lat']) {
			if(Listings['no_record']) {
				message = "<div id='google-property' style='width: 320px; height: 100px;'><br /><h4>"+Listings['office_name'][i]+"</h4><h4>"+Listings['full_address'][i]+"</h4><p>No property can be shown on the map because property addresses are hidden.</p></div>";			
				}else {
					message = "<script>$('a.bookmark').fancybox();</script><div id='google-property' style='width: 320px; height: 100px;'><h4><span>"+Listings['suburb'][i]+"</span>&nbsp;"+Listings['address'][i]+"</h4><dl style='height: 90px;'><dt><img width='103' height='83' src='"+Listings['photo'][i]+"'></dt><dd><h5>"+Listings['price'][i]+"</h5><p>"+Listings['headline'][i]+"</p><p>"+Listings['bed'][i]+Listings['bath'][i]+Listings['car'][i]+"</p><p class='actions'><a href='"+Listings['BookmarkLink'][i]+"' class='"+Listings['BookmarkExt'][i]+"'><div id='bookmark'></div></a>&nbsp;<a href='"+Listings['DetailsLink'][i]+"'><div id='view'></div></a></p></dd></dl></div>";			
				}

			//"<img src='"+Listings['photo'][i]+"' /> <div>"+Listings['full_address'][i]+"</div>";
			//message = 'aaa';
			if(Listings['lt_lat'][i]!==0 && Listings['lt_lat'][i]!==1 & Listings['lt_log'][i]!==0 && Listings['lt_log'][i]!==1) {
				point = new GLatLng(Listings['lt_lat'][i], Listings['lt_log'][i]);
				bounds.extend(point);
				this.createMarker(map,point, i+1, message);
			} else if (Listings['lt_lat'][i]===0 || Listings['lt_log'][i]===0) {
				this.showAddress(map, Listings['full_address'][i], i, message);
			}
		}
	}, 

	showAddress: function(map, address, i, message) {
		geocoder.getLatLng(
			address,
			function(point) {
			  if (point) {
				if(message == '') {
					map.setCenter(point, 17);
				} else {
					bounds.extend(point);
				}
				GoogleMap.createMarker(map, point, i+1, message);
			  }
			}
		  );
	},
	
	loadSmallMap: function(div, coor, address) {

		var map = new GMap2(document.getElementById(div));
		map.addControl(new GSmallMapControl());
		if(coor) {
			map.setCenter(coor, 17);
			this.createMarker(map, coor, 0, '');
		} else if (address) {
			this.showAddress(map, address, 0, '');
		}
	},

	loadStreetViewMap: function(div, coor, address) {
		if(coor) {
			var panoramaOptions = { latlng:coor };
			var myPano = new GStreetviewPanorama(document.getElementById(div), panoramaOptions);
		} else if (address)  {
			this.showAddressStreetView(address, div);
		}
	},

	showAddressStreetView: function(address,div) {
		geocoder.getLatLng(
			address,
			function(point) {
			  if (point) {
				panoramaOptions = { latlng:point };
				var myPano = new GStreetviewPanorama(document.getElementById(div), panoramaOptions);
			  }
			}
		  );
	}
	
};

