/* Author Halil Cet
   Date: 21/04/2009
*/

   var newPoint = null;

   function getNewPoint()
   {
	  return newPoint;
   }

   function setNewPoint(point)
   {
		newPoint = point;
   }

	

/* 

getAddLatLng(address)

This is an async function that retrieves the longitude and latitude, given an address as a string.
Since we can't return a value in the call back function, because it might still be processing,
instead we set the new point using setNewPoint, and then later retrieve it using getNewPoint
(after the call back function completes)

This function is to be only used when inserting new listings, as Google's getLatLng uses alot of resources, and takes time to process.
*/



    function getAddLatLng(address) {
	  if (isCompatible()) {
        geocoder = new GClientGeocoder();
		  if (geocoder) {
			geocoder.getLatLng(
			  address,
			  function(point) { 				
				if (!point) {
				  alert(address + " not found");
				} else {
				  if (point != null){
					 setNewPoint(point);
				  }
				}
			  }
			);
		  }
	  }
    }

	function createIcon(image, shadow, iconsizew, iconsizeh, shadowsizew, shadowsizeh){
		//default icon size w/h: 32,32
		//default shadow icon size w/h: 59/32
		var garageSale = new GIcon(G_DEFAULT_ICON);
		garageSale.image = image;
		garageSale.shadow = shadow;
		garageSale.iconSize = new GSize(iconsizew, iconsizeh);
		garageSale.shadowSize = new GSize(shadowsizew, shadowsizeh);

		return garageSale;
	}

	function createMarker(point, html, icon){
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	}

	function renderMap(point, markerDis)
	{

		if (isCompatible()) {
			map = new GMap2(document.getElementById("map_canvas"));

			latLng = point.split(",");
			if (latLng.length == 2)

			{
				var point = new GLatLng(latLng[0], latLng[1])
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				map.addControl(new GSmallMapControl());
				//marker.openInfoWindowHtml('PropertyAdd');
			}
			else {
				//alert("Could not render map, please try again later.");
			}
      }
	}

	function isCompatible()
	{
		if (GBrowserIsCompatible()) {
			return true ;
		}
		else {
			//alert("Unable to display map, you have an incompatible browser.");
			return false;
		}
	}

	function displayStreetView(MapLatVal, MapLngVal, div){
		var handleNoFlash = "";
		var loc = new GLatLng(MapLatVal, MapLngVal);
		var client = new GStreetviewClient();
		var callback = function (latlng) {
			if (latlng)
			{
				var lat = latlng.lat();
				var lng = latlng.lng();
				var sVLoc = new GLatLng(lat, lng);
				panoramaOptions = { latlng:sVLoc };
				myPano = new GStreetviewPanorama(document.getElementById(div), panoramaOptions);
				GEvent.addListener(myPano, "error", handleNoFlash);
				return;
			}
			else {
				$("#street_view").remove();
			}
		};
		//get the nearest street view for this location...
		client.getNearestPanoramaLatLng(loc, callback);
	}


	function getDirections(route)
	{	
	     var map;
	     var directionsPanel;
	     var directions;
	      map = new GMap2(document.getElementById("map_canvas"));
	      //map.setCenter(new GLatLng(42.351505,-71.094455), 15);
	      directionsPanel = document.getElementById("route");
	      directions = new GDirections(map, directionsPanel);
	      directions.load(route);
    
	}

	
	function setMapCentre(map, mapLat, mapLng){
		var centerPoint = new GLatLng(mapLat, mapLng)	;
		map.setCenter(centerPoint, 9);
	}
