// JavaScript Document

	var map;
	var gdir;
    var geocoder = null;
    var addressMarker;
	var poi = new Array();
	var poiMarkers = new Array();

	function inpBox(id, a)
	{
		var m = em(id);
		if (a == 1)
		{
			m.style.color = '#000000';
			if (m.value == 'Please, enter your address...')
			{
				m.value = '';
			}
		}
		else
		{
			if (m.value == '')
			{
				m.style.color = '#999';
				m.value = 'Please, enter your address...';
			}
		}
	}

	function loadMap(lat, long)
	{
		if (GBrowserIsCompatible())
		{
			map = new GMap2(document.getElementById("map"));
			var center = new GLatLng(lat, long);
			map.setCenter(center, 13);
			var bounds = new GLatLngBounds;
			
			var mainIcon = new GIcon(G_DEFAULT_ICON);
			mainIcon.image = "/images/map_icons/mm_20_red.png";
			mainIcon.shadow = "/images/map_icons/mm_20_shadow.png";
			mainIcon.iconSize = new GSize(12, 20);
			mainIcon.shadowSize = new GSize(22, 20);
			mainIcon.iconAnchor = new GPoint(6, 20);
			mainIcon.infoWindowAnchor = new GPoint(5, 1);
			markerOptions = { icon:mainIcon };
			
			var point = new GLatLng(lat, long);
			var marker = new GMarker(point);
			map.addOverlay(marker); // , markerOptions));

			bounds.extend(point);
			map.setZoom(map.getBoundsZoomLevel(bounds)-2);
			gdir = new GDirections(map, document.getElementById("directions"));
	        GEvent.addListener(gdir, "load", onGDirectionsLoad);
	        GEvent.addListener(gdir, "error", handleErrors);			
			map.addControl(new GMapTypeControl());
			map.addControl(new GLargeMapControl3D());
//			var customUI = map.getDefaultUI();
//			customUI.controls.scalecontrol = true;
//			map.setUI(customUI);
			var trafficOptions = {incidents:true};
	        trafficInfo = new GTrafficOverlay(trafficOptions);
	        map.addOverlay(trafficInfo);
		}
	}

	function showPOI(POItype)
	{
		var x = getXMLHttp();
		if (em(POItype).checked)
		{
			// Checked - show POI
			var center = map.getCenter();
			var geo_lat = center.lat().toString();
			var geo_long = center.lng().toString();
			var customer_id = em('customer_id').value;
			var listing_id = em('listing_id').value;
			var query = "type=" + POItype + "&geo_lat=" + geo_lat + "&geo_long=" + geo_long + "&customer_id=" + customer_id + "&listing_id=" + listing_id + "&rnd=" + Math.random();
			x.open('GET', '/get_poi.php?' + query, true);
			x.onreadystatechange = function() { //showPOIresult;
				if (x.readyState == 4)
				{
					var pt = getPOItype(POItype);
					poi[pt] = new Array();
					poiMarkers[pt] = new Array();

					var answer = x.responseText;
					var data = JSON.decode(answer);
					if (data.length > 0)
					{
						for (i = 0; i < data.length; i++)
						{
							poi[pt][i] = data[i];
							var point = new GLatLng(poi[pt][i][2], poi[pt][i][3]);
							var html = '<b>' + data[i][5] + '</b><br />' + data[i][6] + '<br /><br />' + '<a href="' + data[i][7] + '">Profile</a>';
							poiMarkers[pt][i] = createMarker(point, pt, html);
							map.addOverlay(poiMarkers[pt][i]);
						}
					}
				}
			}
			x.send(null);
		}
		else
		{
			// Remove POI
			var pt = getPOItype(POItype);
			for (i = 0; i < poiMarkers[pt].length; i++)
			{
				map.removeOverlay(poiMarkers[pt][i]);
			}
		}
	}

	function createMarker(point, pt, html)
	{
		var mainIcon = new GIcon(G_DEFAULT_ICON);
		if (pt == 0) { mainIcon.image = "/images/map_icons/sign_hotel.png"; }
		if (pt == 1) { mainIcon.image = "/images/map_icons/sign_restaurants.png"; }
		if (pt == 2) { mainIcon.image = "/images/map_icons/sign_drinks.png"; }
		if (pt == 3) { mainIcon.image = "/images/map_icons/sign_parking.png"; }
		mainIcon.iconSize = new GSize(28, 38);
		mainIcon.iconAnchor = new GPoint(10, 35);
		mainIcon.infoWindowAnchor = new GPoint(20, 0);
		mainIcon.shadow = null;
		markerOptions = { icon:mainIcon };
		var marker = new GMarker(point, markerOptions);;
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	}

	function getPOItype(t)
	{
		if (t == 'poi_hotels') { return 0; }
		if (t == 'poi_restaurants') { return 1; }
		if (t == 'poi_bars') { return 2; }
		if (t == 'poi_parking') { return 3; }
	}

	function setDirections(fromAddress, toAddress)
	{
		if ((fromAddress != '') && (fromAddress != 'Please, enter your address...'))
		{
			gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": 'en' });
		}
		else
		{
			alert('Please, enter your address');
		}
    }
	
	function onGDirectionsLoad()
	{
      // Use this function to access information about the latest load()
      // results.
      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
	function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	   else alert("An unknown error occurred.");
	}
