﻿var _map;
var _geocoder;
var _marker;
var _objLocation = null;
var _address = null;
var _response = null;

function reLoadMap()
{
	LoadGoogleMap(_address);
}

function LoadGoogleMap(objLocation)
{
	if (GBrowserIsCompatible())
	{
		var _mapObject = findObject("googleMap");
		if (_mapObject == null)
		{
			return;
		}

		if (_address == null)
		{
			_objLocation = objLocation;
			var _regEx = new RegExp('^[0-9]+[.][0-9]+[,]([ ]+)?[0-9]+[.][0-9]+$');
			if (!_objLocation.match(_regEx))
			{
				_address = 'NL, ' + _objLocation;
				while (_address.indexOf('&quot;') != -1)
				{
					_address = _address.replace('&quot;', '\'');
				}
			}
			else
			{
				_address = _objLocation;
			}
		}

		_map = new GMap2(_mapObject);
		_map.setCenter(new GLatLng(51.436538, 5.478097), 10);		
		_map.setMapType(G_HYBRID_MAP);
		_map.addControl(new GSmallMapControl());
		_map.addControl(new GMapTypeControl());
		_map.checkResize();

		_geocoder = new GClientGeocoder();		
		_geocoder.getLocations(_address, addToMap);
	}
}

function addToMap(response)
{
	try
	{
		_response = response;
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		_map.setCenter(point, 16);

		icon = new GIcon();
		icon.image = './Graphics/Icons/googlemaps.png';
		icon.iconSize = new GSize(51, 51);
		icon.iconAnchor = new GPoint(11, 35);
		_marker = new GMarker(point, icon);
		if (_marker != null)
		{
			_map.addOverlay(_marker);
		}		
	}
	catch (e)
	{
		alert('De positie kan niet worden bepaald');
		_address = null;
	}
}



var _panorama = null;
function LoadGoogleStreetView(objLocation)
{
	if (GBrowserIsCompatible())
	{
		if (_panorama == null)
		{
			var _panoObject = findObject("googleStreetview");
			if (_panoObject == null)
			{
				return;
			}

			var panoOpts = { features: { streetView: true, userPhotos: false} };
			_panorama = new GStreetviewPanorama(_panoObject, panoOpts);
		}

		var _geocoder = new GClientGeocoder();

		var _address = objLocation;
		var _regEx = new RegExp('^[0-9]+[.][0-9]+[,]([ ]+)?[0-9]+[.][0-9]+$');
		if (!objLocation.match(_regEx))
		{
			_address = 'NL, ' + objLocation;
			while (_address.indexOf('&quot;') != -1)
			{
				_address = _address.replace('&quot;', '\'');
			}
		}

		_geocoder.getLocations(_address, addToPanorama);
	}
}

function addToPanorama(objResponse)
{
	try
	{
		GEvent.addListener(_panorama, "error", function(errorCode) { return; });
		var place = objResponse.Placemark[0];
		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		_panorama.setLocationAndPOV(point);
	}
	catch (e)
	{
		alert('De positie kan niet worden bepaald');
	}
}
