showGoogleMaps = function(sAddress, iRadius, sDestination, iZoom, aOrigin, aRouteInfoWindowContent)
{
	var self = this;

	self.googleMap  = null;
	self.oDirectionsService = null;
	self.oDirectionsRenderer  = null;
	self.mapRef     = document.getElementById('googleMap');
	self.infoWindow = false;

	var oOptions = {
		mapTypeControlOptions: {
			 mapTypeIds: [
			 	google.maps.MapTypeId.ROADMAP,
			 	google.maps.MapTypeId.SATELLITE,
			 	google.maps.MapTypeId.HYBRID
			 ]

		},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var oMap     = self.mapRef;
	var geocoder = new google.maps.Geocoder();

	self.oDirectionsService = new google.maps.DirectionsService();
	self.oDirectionsRenderer  = new google.maps.DirectionsRenderer();


	if (sAddress == '')
	{
		geocoder.geocode(
			{
				'address': 'Nederland, NL'
			},
			function(latLong, status)
			{
				var oGeodata = latLong[0].geometry.location;

				oOptions.zoom   = 7;
				oOptions.center = oGeodata;

				self.googleMap = new google.maps.Map(oMap,oOptions);
				$.getJSON(
					'/json/googleMapMarkers.php',
					{
						address : 'all'
					},
					function(json)
					{
						if (json.length > 0)
						{
							self.addMarkers(json);
						}
					}
				);
			}
		);
	}
	else if(sAddress.match(/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/ ))
	{
		var latlng = new google.maps.LatLng(sAddress.split(', ')[0], sAddress.split(', ')[1]);

		geocoder.geocode(
			{
				'latLng': latlng
			},
			function(latLong, status)
			{
				var oGeodata = latLong[0].geometry.location;
			    oOptions.zoom = 12;
			    oOptions.center = oGeodata;

			    self.googleMap = new google.maps.Map(oMap, oOptions);

			    if(sDestination == '')
				{
					$.getJSON(
						'/json/googleMapMarkers.php',
						{
							lat: oGeodata.lat(),
							lng: oGeodata.lng(),
							radius:  ''

						},
						function(json)
						{
							if (json.length > 0)
							{
								if(aOrigin != '')
								{
									self.addMarkers(json,aOrigin);
								}
								else
								{
									self.addMarkers(json);
								}
							}
						}
					);
				}
				else
				{
					var $directions = $('.directions')[0];
					self.getDirections(sAddress + ' Nederland, NL', sDestination, $directions, aRouteInfoWindowContent);

					self.oDirectionsRenderer.setMap(self.googleMap);
				}
			}
		);
	}
	else
	{
		geocoder.geocode({ 'address': sAddress + ' nederland, NL'}, function(latLong, status)
		{
			if(status == 'OK')
			{
				var oGeodata = latLong[0].geometry.location;

				var oRadiusZoom = {
					1:	 16,
					5:	 15,
					10:  12,
					20:  10,
					50:  9,
					100: 8
				};

				oOptions.center = oGeodata;
				if(iZoom == '')
				{
					oOptions.zoom = oRadiusZoom[iRadius];
				}
				else
				{
					oOptions.zoom = 12;
				}

				self.googleMap = new google.maps.Map(oMap,oOptions);

				if(sDestination == '')
				{
					$.getJSON(
						'/json/googleMapMarkers.php',
						{
							lat: oGeodata.lat(),
							lng: oGeodata.lng(),
							radius:  ''

						},
						function(json)
						{
							if (json.length > 0)
							{
								if(aOrigin != '')
								{
									self.addMarkers(json,aOrigin);
								}
								else
								{
									self.addMarkers(json);
								}
							}
						}
					);
				}
				else
				{
					var $directions = $('.directions')[0];
					self.getDirections(sAddress + ' Nederland, NL', sDestination, $directions, aRouteInfoWindowContent);
				}
			}
		});
		self.oDirectionsRenderer.setMap(self.googleMap);
	}

	this.addMarkers = function(json, aOrigin)
	{
		if(aOrigin)
		{
			self.addMarker(aOrigin);
		}

		for (var i in json)
		{
			self.addMarker(json[i]);
		}
	}

	this.addMarker = function(data )
	{
		if(typeof(data.google_lat) == 'undefined' || data.google_lat == '' || typeof(data.google_long) == 'undefined' || data.google_long == '')
		{
			return false;
		}
		var markerLatLng = new google.maps.LatLng(data.google_lat, data.google_long);

		if(data.isOrigin == true)
		{
			var sMarkerImg = '/img/google_maps_origin_marker_icon.png';
		}
		else
		{
			var sMarkerImg = '/img/google_maps_marker_icon.png';
		}

		var markerImage = new google.maps.MarkerImage(
			sMarkerImg,
			new google.maps.Size(20, 34),
			new google.maps.Point(0,0),
   		   	new google.maps.Point(10, 34)
   		);

		var oMarker = new google.maps.Marker({
		      position:           markerLatLng,
		      map:                self.googleMap,
		      title:              data.title,
		      id:                 data.id,
		      google_coordinates: data.google_coordinates,
		      icon:               markerImage
		});


		if(data.isOrigin != true)
		{
			google.maps.event.addListener(oMarker, "click", function() {
				self.markerClick(oMarker);
			});
		}
	}
	this.markerClick = function(oMarker)
	{
		$.getJSON(
			'/json/getMarkerInfo.php',
			{
				id:			  oMarker.id
			},
			function(data)
			{
				if (typeof(self.infoWindow) == 'object')
				{
					self.infoWindow.close();
				}
				self.infoWindow = new google.maps.InfoWindow(
					{
						content: 	data.html,
						size: 		new google.maps.Size(50,50)
					}
				);
				self.infoWindow.open(self.googleMap, oMarker);
			}
		);
	}

	/* Directions */
	this.getDirections = function(sFrom, sTo, oDirectionElement, aContent)
	{
		self.oDirectionsRenderer.setMap(self.googleMap);
		self.oDirectionsRenderer.setPanel(oDirectionElement);

		var oRequest = {
			origin:      sFrom,
			destination: sTo,
			travelMode:  google.maps.DirectionsTravelMode.DRIVING
		};

		self.oDirectionsService.route(oRequest,function(oResult, status)
		{
		    if (status == google.maps.DirectionsStatus.OK) {
		    	if(aContent)
		    	{
			    	oResult.routes[0].legs[0].start_address = aContent['origin'];
			      	oResult.routes[0].legs[0].end_address = aContent['destination'];
			    }
		      	self.oDirectionsRenderer.setDirections(oResult);
		    }
		});
	}
}
