// directions.js: Google Map with travel directions

// locations
var locations = [
	null,
	[52.018566,4.6608],
	[52.20019,4.429379],
	[52.416727,4.826764],
	[52.018566,4.6608],
	[51.711094,5.309209]
];

$(document).ready(function() {GoogleMap.initialize();});
var GoogleMap = {
	initialize: function(el)
	{
		// Google map
		if ($('gmap'))
			this.loadMap();
	},
	// load Google map
	loadMap: function()
	{
        if (GBrowserIsCompatible()) {
			$('form#routeform').bind('submit', function(e)
            {
                var elm = e.target;
				if (elm.route_to.value * 1 == 0)
				{
					alert("Geef een bestemming");
					e.preventDefault();
					e.stopPropagation();
					return;
				}
				$("#mapresult").css("display", "block");
				$(document).unload(GUnload);

				// display map
				var map = new GMap2($('#gmap').get(0));
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
				map.enableScrollWheelZoom();
				// markeringen toevoegen
				var myIcon = new GIcon();
				myIcon.image = "/js/mapicon.png";
				myIcon.iconSize = new GSize(53, 34);
				myIcon.iconAnchor = new GPoint(26, 17);
				var iconpoint;
				var tmppos = [];
				for (var n = 0; n < locations.length; n++)
				{
					if (locations[n] != null) // geen lege locatie
					{
						if (jQuery.inArray(locations[n].join(","), tmppos) < 0) // geen bestaande locatie
						{
							iconpoint = new GLatLng(locations[n][0], locations[n][1]);
							map.addOverlay(new GMarker(iconpoint, myIcon));
							tmppos.push(locations[n].join(","));
						}
					}
				}
				tmppos = null;

				// bepalen positie
				var i = elm.route_to.value * 1;
				if (locations[i - 1])
					tocoords = locations[i - 1];
				// toevoegen richting
				if (elm.route_from.value != "")
				{
					var directions = new GDirections(map, document.getElementById('gdirections'));
					directions.load('from: ' 
						+ elm.route_from.value.replace(/(\d{4})\W+([A-Za-z]{2})/, "$1$2")
						+ ' to: ' + tocoords.join(","), {locale: 'nl_NL'});
				}
				else
				{
					var point = new GLatLng(tocoords[0], tocoords[1]);
					map.setCenter(point, 15);
				}
				e.preventDefault();
				e.stopPropagation();
            });
        }
    }
};




