function initializeMap() {
    var directionsService = new google.maps.DirectionsService;
    var directionsRenderer = new google.maps.DirectionsRenderer({
        suppressMarkers: true
    });

    var map = new google.maps.Map(document.getElementById('travel-map'), {
        center: { lat: 46.055333112, lng: 14.507831302 },
        zoom: 5,
        mapTypeId: 'roadmap'
    });

    directionsRenderer.setMap(map);
    calculateAndDisplayRoute(directionsService, directionsRenderer, map);
}

function calculateAndDisplayRoute(directionsService, directionsRenderer, map) {

    var start = $("#startLocationAddress").val();
    var end = $("#endLocationAddress").val();
    var addressArray = [];
    var waypts = [];

    $('.address').each(function () {
        addressArray.push($(this).attr('data-address'));
    });

    if (addressArray.length < 1) {
        return;
    }

    for (var i = 0; i < addressArray.length; i++) {
        waypts.push({
            location: addressArray[i]
        });
    }

    if (!waypts[0]) {
        return;
    }

    if (start === end) {
        end = '';
    }

    if (!end) {
        if (waypts.length > 1) {
            end = waypts[waypts.length - 1].location;
            waypts.pop();
        }
        else {
            end = waypts[0].location;
            waypts = [];
        }
    }

    directionsService.route({
        origin: start,
        destination: end,
        waypoints: waypts,
        optimizeWaypoints: false,
        travelMode: 'DRIVING'
    }, function (response, status) {
        if (status === 'OK') {
            directionsRenderer.setDirections(response);
            var route = response.routes[0];

            var startRoute = route.legs[0];
            addMarker(startRoute.start_location, startRoute.start_address);
            for (var i = 0; i < route.legs.length; i++) {
                var leg = route.legs[i];
                addMarker(leg.end_location, leg.end_address);
            }

        } else {
            console.log('Directions request failed');
        }
    });

    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var labelIndex = 0;
    function addMarker(position, positionText) {
        var marker = new google.maps.Marker({
            position: position,
            title: positionText,
            label: { text: labels[labelIndex++ % labels.length], color: "white" },
            map: map
        });

        var infowindow = new google.maps.InfoWindow({
            content: positionText
        });

        marker.addListener('click', function () {
            infowindow.open(map, marker);
        });

        return marker;
    }
}

function initializePlacesSearch(inputId) {
    var input = document.getElementById(inputId);
    var autocomplete = new google.maps.places.Autocomplete(input);

    google.maps.event.addDomListener(input, 'keydown', function (event) {
        if (event.keyCode === 13) {
            event.preventDefault();
        }
    });
}

function selectFirstOnEnter(input) {
    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
    function addEventListenerWrapper(type, listener) {
        // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected, and then trigger the original listener.
        if (type == "keydown") {
            var orig_listener = listener;
            listener = function (event) {
                var suggestion_selected = $(".pac-item-selected").length > 0;
                if (event.which == 13 && !suggestion_selected) {
                    var simulated_downarrow = $.Event("keydown", { keyCode: 40, which: 40 });
                    orig_listener.apply(input, [simulated_downarrow]);
                }
                orig_listener.apply(input, [event]);
            };
        }
        _addEventListener.apply(input, [type, listener]); // add the modified listener
    }
    if (input.addEventListener) {
        input.addEventListener = addEventListenerWrapper;
    } else if (input.attachEvent) {
        input.attachEvent = addEventListenerWrapper;
    }
}