﻿this.directions = null;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var currentlyOpenedInfoWindow = null;
var map;
var routeBounds;
var gdir;
 
function init_map(map_canvas_id, lat, lng, zoom, markers, infoWindowContents) {
    var myLatLng = new google.maps.LatLng(lat, lng);
    
    var options = {
        zoom: zoom,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var map_canvas = document.getElementById(map_canvas_id);

    map = new google.maps.Map(map_canvas, options);

    if (markers && markers.length > 0) {
        var bounds = new google.maps.LatLngBounds();
    
        for (var i = 0; i < markers.length; i++) {
            var marker = new google.maps.Marker(markers[i]);
            marker.setMap(map);

            bounds.extend(marker.getPosition());

            if (infoWindowContents && infoWindowContents.length > i)
                createInfoWindow(map, marker, infoWindowContents[i]);
        }

        map.fitBounds(bounds);
        map.setCenter(bounds.getCenter());
    }
}

function createInfoWindow(map, marker, infoWindowProperties) {
    var info = new google.maps.InfoWindow(infoWindowProperties);

    google.maps.event.addListener(marker, 'click', function() {
        if (currentlyOpenedInfoWindow != null)
            currentlyOpenedInfoWindow.close();

        info.open(map, marker);
        currentlyOpenedInfoWindow = info;
    });
}

function initialize(map_canvas_id, fromlat, fromlng, tolat, tolng) {
    var myLatLng = new google.maps.LatLng(fromlat, fromlng);
    directionsDisplay = new google.maps.DirectionsRenderer();
    var myOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: myLatLng
    }


    map = new google.maps.Map(document.getElementById(map_canvas_id), myOptions);

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions'));   

    google.maps.event.addListener(map, 'resize', function () {
        if (routeBounds) {
            window.setTimeout(fitBounds, 0);
        }
    });

    var start = new google.maps.LatLng(fromlat, fromlng);
    var end = new google.maps.LatLng(tolat, tolng);
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            var path = response.routes[0].overview_path;
            routeBounds = new google.maps.LatLngBounds();
            for (var i = 0; i < path.length; i++) {
                routeBounds.extend(path[i]);
            }
            directionsDisplay.setDirections(response);
        }
    });

}


function fitBounds() {
    map.fitBounds(routeBounds);
}


function resizeMap() {
    var mapCanvas = document.getElementById("map_canvas");
    if (mapCanvas.style.width == "600px") {
        mapCanvas.style.width = "300px";
        mapCanvas.style.height = "300px";
    } else {
        mapCanvas.style.width = "600px";
        mapCanvas.style.height = "600px";
    }
    google.maps.event.trigger(map, 'resize');
}

function calcRoute(fromlat, fromlng, tolat, tolng) {
}

