/* Google Maps Control for Pharos Reizen van ANWB

Marker
Id        (int)      Id
Type      (string)   Type of Marker (can be used for icons)
Title     (string)   Popup Title
Text      (string)   More information shown on click (HTML)
Point     (LatLng)   Coordinate 

Options
Center    (LatLng)   Center coordinate
ZoomLevel (int)      Zoomlevel (0-global, higher=zoom-in)
*/

function GoogleMaps(mapId, legendId, directionsId, options, markers) {
	this.mapEl = document.getElementById(mapId);
	this.legendEl = document.getElementById(legendId);
	this.directionsEl = document.getElementById(directionsId);

	//Map type control
	options.mapTypeControlOptions = { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, position: google.maps.ControlPosition.TOP_RIGHT };

	//Zoom control
	if (options.ShowLargeMapControl) {
		options.navigationControlOptions = { style: google.maps.NavigationControlStyle.ZOOM_PAN };
	}

	this.options = options;
	this.markers = markers;
}

GoogleMaps.prototype.initialize = function() {
	this.map = new google.maps.Map(this.mapEl, this.options);
}

GoogleMaps.prototype.showLegend = function() {
	if (this.legendEl != null) {
		var legend = "";
		for (var i = 0; i < this.markers.length; i++) {
			var marker = this.markers[i];
			legend += "<div class=\"legendRow\"><b>" + String.fromCharCode("A".charCodeAt(0) + i) + "</b> " + marker.Title + "</div>";
		}
		this.legendEl.innerHTML = legend;
	}
}

GoogleMaps.prototype.showMarkers = function() {
	var infowindow = new google.maps.InfoWindow();
	var bounds = new google.maps.LatLngBounds();
	for (var i = 0; i < this.markers.length; i++) {
		var marker = this.markers[i];
		var point = new google.maps.LatLng(this.markers[i].Point.Latitude, this.markers[i].Point.Longitude);
		if (i == 0) {
			this.map.setCenter(point, 0);
		}
		bounds.extend(point);

		var title = marker.Title;
		if (marker.Type != null && marker.Type != "ACCO")
			title += " (" + marker.Type + ")";

		var gmarker = new google.maps.Marker({ position: point, map: this.map, title: title, icon: getMarkerIcon(marker.Type) });
		setMarker(gmarker, infowindow, marker.Text);
	}

	this.map.setCenter(bounds.getCenter());
	if (this.options.ZoomLevel > 0) {
		this.map.setZoom(this.options.ZoomLevel);
	}
	else {
		this.map.fitBounds(bounds);
	}
}

function getMarkerIcon(type) {
	var icon;
	switch (type) {
		case "ACCO":
			icon = null;
			break;
		case "Hotel": case "Hotel/Vakantiewoning":
			icon = "http://google-maps-icons.googlecode.com/files/hotel.png";
			break;
		case "Camping":
			icon = "http://google-maps-icons.googlecode.com/files/campingsite.png";
			break;
		case "Vakantiepark": case "Vakantiewoning": case "Vakantiepark/hotel":
			icon = "http://google-maps-icons.googlecode.com/files/hostel.png";
			break;
		case "Appartementencomplex": case "Hotel/Appartement":
			icon = "http://google-maps-icons.googlecode.com/files/apartment.png";
			break;
		default:
			icon = null;
			break;
	}
	return icon;
}

function setMarker(marker, infowindow, content) {
	if (content != null) {
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.setContent(content);
			infowindow.open(marker.map, marker);
		});
	}
}

GoogleMaps.prototype.showDirections = function(fromAddress) {
	if (this.directionsEl != null) {
		this.directionsEl.innerText = '';
	}

	var rendererOptions = { draggable: true };
	var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	google.maps.event.addListener(directionsDisplay, 'directions_changed', function() { });

	var directionsService = new google.maps.DirectionsService();

	directionsDisplay.setMap(this.map);
	directionsDisplay.setPanel(this.directionsEl);

	var locations = new Array();
	for (var i = 0; i < this.markers.length; i++) {
		var point = this.markers[i].Point;
		point.stopover = true;

		locations[i] = point;
	}

	var dest; var waypts = [];
	if (locations.length == 1) {
		var point = this.markers[0].Point;
		dest = new google.maps.LatLng(point.Latitude, point.Longitude);
	}
	else if (locations.length > 1) {
		//round-trip; destination is starting point
		dest = fromAddress;
		for (var i = 0; i < this.markers.length; i++) {
			var point = this.markers[i].Point;
			var loc = new google.maps.LatLng(point.Latitude, point.Longitude);

			waypts.push({
				location: loc,
				stopover: true
			});
		}
	}

	var request = {
		origin: fromAddress,
		destination: dest,
		waypoints: waypts,
		travelMode: google.maps.DirectionsTravelMode.DRIVING,
		provideRouteAlternatives: true
	};

	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		} else {
			showDirectionsFailedMessage(status);
		}
	});
}

function showDirectionsFailedMessage(status) {
	switch (status) {
		case "INVALID_REQUEST":
		case "MAX_WAYPOINTS_EXCEEDED":
		case "ZERO_RESULTS":
		case "NOT_FOUND":
			GM.showMessage('Uw zoekopdracht heeft geen resultaten opgeleverd. Probeer een andere locatie als vertrekpunt, bijvoorbeeld een [postcode] of [straatnaam, plaatsnaam, land].');
			break;
		case "OVER_QUERY_LIMIT":
		case "UNKNOWN_ERROR":
			GM.showMessage('Routeberekening is op het ogenblik niet beschikbaar. Probeer het later nog eens.');
			break;
	}
}

GoogleMaps.prototype.showMessage = function(msg) {
	this.directionsEl.innerHTML = "<div>" + msg + "</div>";
}

var GM;
function GoogleMaps_Initialize(mapId, legendId, directionsId, options, markers, onLoad) {
	GM = new GoogleMaps(mapId, legendId, directionsId, options, markers);

	if (onLoad == "true") {
		window.onload = function() {
			GM.initialize();
			GM.showLegend();
			GM.showMarkers();
		}
	}
	else {
		GM.initialize();
		GM.showLegend();
		GM.showMarkers();

		////jQuery style entry point
		//$(document).ready(function() {

		//});
	}
}
