var map = null;
var geocoder = null;
var to_marker = null;
var from_marker = null;
var point = null;
var to_address = null;
var from_address = null;
var from_city = null;
var null_value = null;
var directionsService = null;
var directionsDisplay = null;
function MAPinit(lat,long) {
	if (GBrowserIsCompatible()) {
		var myLatlng = new google.maps.LatLng(lat,long);
    var myOptions = {
      zoom: 13,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		to_marker = new google.maps.Marker({
			map: map,
			position: myLatlng
		});
		document.getElementById("map_canvas").style.display = '';
		document.getElementById("get_there_from").style.display = '';
	}
}
function MAPinitWithAddress(lat,long,address) {
	if (GBrowserIsCompatible()) {
		var myLatlng = new google.maps.LatLng(lat,long);
    var myOptions = {
      zoom: 13,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		to_marker = new google.maps.Marker({
			map: map,
			position: myLatlng,
			title:address
		});
		document.getElementById("map_canvas").style.display = '';
		document.getElementById("get_there_from").style.display = '';
	}
}
function MAPFromAddressinit() {
	if (GBrowserIsCompatible()) {
    var myOptions = {
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
		document.getElementById("map_canvas").style.display = '';
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	}
}
function showAddress(address,show_get_here_from) {
	if (address.indexOf("item_") == -1) {
		if (geocoder == null_value) {
			geocoder = new google.maps.Geocoder();
		}
		if (geocoder) {
			if (map == null_value) {
				MAPFromAddressinit();
			}
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
						map.setCenter(results[0].geometry.location);
						to_marker = new google.maps.Marker({
							map: map,
							position: results[0].geometry.location,
							title:address
						});
						to_address = results[0].geometry.location;
						var lat = to_address.lat();
						var lng = to_address.lng();
						if (document.getElementById("latitude")) {
							var lat_field = document.getElementById("latitude").value;
							var lng_field = document.getElementById("longitude").value;
							document.getElementById(lat_field).value = lat;
							document.getElementById(lng_field).value = lng;
						}
						if (show_get_here_from) {
							document.getElementById("get_there_from").style.display = '';
						}
				} else {
					// alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
	}
}
function ShowRoute() {
	if (geocoder == null_value) {
		geocoder = new google.maps.Geocoder();
	}
	if (from_marker != null_value) {
		from_marker.setMap();
	}
	if (to_marker != null_value) {
		to_marker.setMap();
	}
	from_address = document.getElementById("from_address").value;
	from_city = document.getElementById("from_city").value;
	if (from_address != '') {
		location_from = from_address;
	}
	if (from_city != '') {
		location_from = from_city;
	}
	if (from_address != '' && from_city != '') {
		location_from = from_address + ", " + from_city;
	}
	geocoder.geocode( { 'address': location_from}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			from_address = results[0].geometry.location;
			if (to_address==null_value) {
				var lat = document.getElementById("latitude").value;
				var long = document.getElementById("longitude").value;
				to_address = new google.maps.LatLng(lat,long);
			}
			var ReURL = "index.php?option=com_jigcompidx&task=popup_route&location_a=" + from_address + "&location_b=" + to_address;
			var left = parseInt((screen.availWidth/2) - (600/2));
			var top = parseInt((screen.availHeight/2) - (600/2));
			if (window.showModalDialog) {
				window.showModalDialog(ReURL,'MKB-bedrijvengids - Route planner','dialogWidth:600px;dialogHeight:600px;dialogLeft:'+left+'px;dialogTop:'+top+'px');
			} else {
				window.open(ReURL,'MKB-bedrijvengids - Route planner', 'height=600,width=600,left='+left+',top='+top+',toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no ,modal=yes');
			}
		} else {
			alert('De route kon niet bepaald worden.');
		}
	});
}
function getLatLong(city,getter,reurl) {
	if (geocoder == null_value) {
		geocoder = new google.maps.Geocoder();
	}
	var found_address = false;
	city += ', Nederland';
	geocoder.geocode( { 'address': city}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			from_address = results[0].geometry.location;
			found_address = from_address.lat() + "," + from_address.lng();
			if (reurl) {
				reurl += "&" + getter + "=" + found_address;
				document.location.href = reurl;
			}
		} else {
			// alert(city + " not found.");
		}
	});
}
function DistanceBetween(point_a, point_b) {
	var location_a = null;
	var location_b = null;
	ageocoder = new google.maps.Geocoder();
	bgeocoder = new google.maps.Geocoder();
	// We are pretty sure these are addresses, so go from that...
	ageocoder.geocode( { 'address': point_a}, function(aresults, astatus) {
		if (astatus == google.maps.GeocoderStatus.OK) {
			location_a = aresults[0].geometry.location;
			bgeocoder.geocode( { 'address': point_b}, function(bresults, bstatus) {
				if (bstatus == google.maps.GeocoderStatus.OK) {
					location_b = bresults[0].geometry.location;
					if (location_a != null_value && location_b != null_value) {
						if (directionsService == null_value) {
							directionsService = new google.maps.DirectionsService();
						}
						var request = {
								origin:location_a, 
								destination:location_b,
								travelMode: google.maps.DirectionsTravelMode.DRIVING
						};
						directionsService.route(request, function(response, status) {
							if (status == google.maps.DirectionsStatus.OK) {
								return response.trips[0].routes[0].distance.value;
							} else {
								return false;
							}
						});
					}
				} else {
					return false;					
				}
			});
		} else {
			return false;
		}
	});
}
function MarkAddress(id,company,address,latitude,longitude) {
	if (map == null_value) {
		if (latitude && longitude) {
			var myLatlng = new google.maps.LatLng(latitude,longitude);
		}
    var myOptions = {
      zoom: 13,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_companies"), myOptions);		
	}
	if (GBrowserIsCompatible()) {
		blnMarkIt = false;
		if (latitude && longitude) {
			var myLatlng = new google.maps.LatLng(latitude,longitude);
			blnMarkIt = true;
		}
		if (blnMarkIt) {
			to_marker = new google.maps.Marker({
				map: map,
				position: myLatlng,
				title: company + " (" + address + ")"
			});
			google.maps.event.addListener(to_marker, 'click', function() {
				document.location.href = "index.php?option=com_jigcompidx&task=details&record_id=" + id;
			});
			if (document.getElementById("map_companies").style.display != '') {
				document.getElementById("map_companies").style.display = '';
			}
		}
	}
}
