window.addEvent('load', function()
{
	showGoogleMap();
});

window.addEvent('unload', function()
{
	//GUnload();
});

var objGoogleMap = null;
var objDirections = null;
var objGeocoder = null;
var objAddressMarker = null;
var arrIcons = {};

CustomGetTileUrl = function(a,b)
{
	return 'http://localhost/vrom/www/vrom/image/gmaps/Tile_' + a.x + '_' + a.y + '_' + (17-b) + '.jpg';
}

function showGoogleMap()
{
	if (GBrowserIsCompatible())
	{
		// create google map
		if(document.getElementById('pointmap'))
		{
			var objGoogleMapSmall = new GMap2(document.getElementById('pointmap'));
			objGoogleMapSmall.setMapType(G_PHYSICAL_MAP); 
			objGoogleMapSmall.addControl(new GSmallMapControl());
			objGoogleMapSmall.setCenter(new GLatLng(52.091262, 5.122748), 7);
			
			/*
			var objCopyright = new GCopyright(1, new GLatLngBounds(new GLatLng(52.091262,5.122748),new GLatLng(52.091262,5.122748) ), 7, "VROM");
			var objCopyrightCollection = new GCopyrightCollection('Map Data:');
			objCopyrightCollection.addCopyright(objCopyright);
			var tilelayers = [new GTileLayer(objCopyrightCollection, 7, 7)];
			tilelayers[0].getTileUrl = CustomGetTileUrl;
			var objCustommap = new GMapType(tilelayers, new GMercatorProjection(15), "Old OS");
			objGoogleMapSmall.addMapType(objCustommap);
			objGoogleMapSmall.setCenter(new GLatLng(52.091262, 5.122748), 7, objCustommap);
			*/
			
			// add mousemove event to the maps which sets the current location
			var objLastLocation;
			GEvent.addListener(objGoogleMapSmall, 'mousemove', function(objLocation)
			{
				objLastLocation = objLocation;
			});
			
			// add click event for getting the current location
			GEvent.addListener(objGoogleMapSmall, 'click', function()
			{
				var fltLatitude = objLastLocation.lat().toFixed(14);
				var fltLongitude = objLastLocation.lng().toFixed(14);
				
				$('latitude').value = fltLatitude;
				$('longitude').value = fltLongitude;
				$('oranjefonds').submit();
			});
		}
  	
		if(document.getElementById('googlemap'))
		{
			/*
			
		  	// create google map
		    objGoogleMap = new GMap2(document.getElementById('googlemap'));
		    
		    // add map control
		    objGoogleMap.addControl(new GSmallMapControl());
			
			// add type control
			objGoogleMap.addControl(new GMapTypeControl());
		    
		    // add user location
			var objSelf = objOwnLocation[0];
			
			var fltLongitude = objSelf.longitude;
			var fltLatitude = objSelf.latitude;
			
			// set location
			var objLocation = new GLatLng(fltLatitude, fltLongitude);
			objGoogleMap.setCenter(new GLatLng(fltLatitude, fltLongitude), intHeight);
			
			// create location
			var objLocation = new GLatLng(fltLatitude, fltLongitude);
			
			// add the marker & overlay
			var objIcon = getIcon('home');
			var objMarker = new GMarker(objLocation, { icon: objIcon });
			objGoogleMap.addOverlay(objMarker);
		    
			objData.each(function(objLocation)
			{
				// set some vars
				var strHtmlData = '<h3>' + objLocation.naam_project + '</h3><br />';
				strHtmlData += '<h2>' + objLocation.naam_organisatie + '</h2><br />';
				strHtmlData += '<strong>Gegevens</strong><br />';
				strHtmlData += objLocation.straatnaam + ' ';
				strHtmlData += objLocation.huisnummer + objLocation.toevoeging_huisnummer + '<br />';
				strHtmlData += objLocation.postcode + ' ' + objLocation.plaats + '<br />';
				strHtmlData += objLocation.provincie + '<br /><br />';
				strHtmlData += '<strong>Tel:</strong> ' + objLocation.telefoon_algemeen + '<br />';
				strHtmlData += '<strong>Website</strong> ' + objLocation.website_project + '<br />';
				strHtmlData += '<br />';
				strHtmlData += '<strong>Contactpersoon</strong><br />';
				strHtmlData += objLocation.voornaam_contactpersoon + ' ' + objLocation.achternaam_contactpersoon + '<br />';
				strHtmlData += objLocation.telefoon_contactpersoon + '<br />';
				strHtmlData += objLocation.email_contactpersoon + '<br />';
				strHtmlData += '<br />';
				strHtmlData += '<strong>Over het project</strong><br />';
				strHtmlData += objLocation.beschrijving_project + '<br />';
				strHtmlData += '<br />';
				strHtmlData += '<strong>Doelgroep</strong><br />' + objLocation.soort_doelgroep + '<br />';
				strHtmlData += '<br />';
				strHtmlData += '<strong>Projectduur</strong><br />' + objLocation.duur_project + '<br />';
				strHtmlData += '<br />';
				strHtmlData += '<strong>Werkgebied</strong><br />' + objLocation.werkgebied + '<br />';
				
				var strToData = objLocation.adres + ', ' + objLocation.postcode + ',' + objLocation.plaats;
				var fltLongitude = objLocation.longitude;
				var fltLatitude = objLocation.latitude;
				
				// set location
				var objLocation = new GLatLng(fltLatitude, fltLongitude);
				
				// create location
				var objLocation = new GLatLng(fltLatitude, fltLongitude);
				
				// add the marker & overlay
				var objIcon = getIcon('location');
				var objMarker = new GMarker(objLocation, { icon: objIcon });
				objGoogleMap.addOverlay(objMarker);
				
				// show some info on mouseover
				GEvent.addListener(objMarker, 'mouseover', function()
				{
					//$('locationdata').setHTML(strHtmlData);
				});
			});
			
			*/
	  	}
	}
}

// function for getting (or setting an icon)
function getIcon(strImage)
{
	var objIcon = null;
	if (strImage)
	{
		if (arrIcons[strImage])
		{
			objIcon = arrIcons[strImage];
		}
		else
		{
        	var objIcon = new GIcon();		
			objIcon.image = 'image/icon/icon_' + strImage + '.png';
			
			var intWidth				= 16;
			var intHeight				= 16;
			objIcon.iconSize			= new GSize(intWidth, intHeight);
			objIcon.iconAnchor			= new GPoint(intWidth >> 1, intHeight >> 1);
			objIcon.infoWindowAnchor	= new GPoint(10, 42);
			objIcon.shadowSize			= new GSize(intWidth, intHeight);
			
			arrIcons[strImage] = objIcon;
		}
	}
	return objIcon;
}

// handles errors returned from google maps
function handleErrors()
{
	if (objDirections.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: " + objDirections.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: " + objDirections.getStatus().code);
	}
	else if (objDirections.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: " + objDirections.getStatus().code);
	}
	else if (objDirections.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: " + objDirections.getStatus().code);
	}
	else if (objDirections.getStatus().code == G_GEO_BAD_REQUEST)
	{
		alert("A directions request could not be successfully parsed.\n Error code: " + objDirections.getStatus().code);
    }
	else
	{
		alert("An unknown error occurred.");  
	}
}