//@author Halil Cet
//@Date: 21/04/2009

   var newPoint = null;
   var DEBUG = 0;

   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 renderMap(point, div, marker, mapControl)
	{
		if (isCompatible()) {
			//map = new GMap2(document.getElementById(div), new GSize(200, 100));
			
			//resize the window, if window is closed.
			if (mapControl != "large"){
				var map = new GMap2( document.getElementById(div),
				   { size:new GSize(228, 176)} ); 
			}
			else {
				var map = new GMap2( document.getElementById(div));
			}

			latLng = point.split(",");
			if (latLng.length == 2)
			{
				var point = new GLatLng(latLng[0], latLng[1])
				var marker = new GMarker(point);
				map.setCenter(point, 13);
				map.addOverlay(marker);
				if (mapControl == "large")
				{
					map.addControl(new GLargeMapControl());
					//marker.openInfoWindowHtml('PropertyAdd');
				}
				else {
					map.addControl(new GSmallMapControl());
					//marker.openInfoWindowHtml('PropertyAdd');
				}

			}
			else {
				if (DEBUG == 1)
				{
					alert("Could not render map, please try again later.");
				}
			}
      }
	}

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

	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);
    
	}
