﻿function setupGoogleMap(address, homeLoc) {
    var map = new google.maps.Map2(document.getElementById("map"));
        var geocoder = new GClientGeocoder();

        map.setUIToDefault();
       // map.setMapType(G_SATELLITE_MAP); 
       // map.enableInfoWindow();

//        var home = new GPoint(6.5767772, 53.2175765);
//        map.setCenter(new google.maps.LatLng(53.2175765, 6.5767772), 8);

        var to = address;
        showAddress(to);
        /*
        var new_icon = new GIcon(G_DEFAULT_ICON); 
        new_icon.size = new GSize(70, 70);
        new_icon.iconAnchor = new GPoint(8, 9);
        new_icon.infoWindowAnchor = new GPoint(7, 7);

        var opt = {};
        opt.icon = new_icon;
        opt.draggable = false;
        opt.clickable = false;
        opt.dragCrossMove = true;

        var new_marker = new GMarker(home, opt)
        map.addOverlay(new_marker);
*/
       

        //getDirections
        $('#calculate').click(function() {
            var src = $('#address').get(0).value + " " + $('#city').get(0).value+" nederland";
            getDirections(src);

        });
        function showAddress(address) {            
              geocoder.getLatLng(
                address,
                function(point) {
                  if (!point) {
                    alert(address + " not found");
                  } else {
                    map.setCenter(point, 13);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                  }
                }
              );
            }
        var directions = false;
        function getDirections(from) {
            if (!directions) {
                directions = new google.maps.Directions(map);
                google.maps.Event.addListener(directions, "error", function() {
                    console.dir(directions.getStatus());
                    //$('#fromAddress').val('Onbekend adres');
                    alert('error: ' + 'Onbekend adres');
                });

            var panel = $('#route').get(0); 
            google.maps.Event.addListener(directions, "load", function() { 
                var html = '<table class="directionsTbl">';
                var cnt = directions.getNumRoutes();
                for (var routeNr = 0; routeNr < cnt; routeNr++) {
                    var rte = directions.getRoute(routeNr);
                    html += '<tr class="directionsHeader"><td colspan="2"><br /><br />Routebeschrijving naar ' + rte.getEndGeocode().address
                                         + '<br />' + rte.getDistance().html + ' - ca. ' + rte.getDuration().html
                                         + '<br />' + from
                                         + '</td></tr>';


                    for (var stepNr = 0; stepNr < rte.getNumSteps(); stepNr++) {
                        var step = rte.getStep(stepNr);
                        html += '<tr class="rte">';
                        //dirapiShowMapBlowup(this.route, this.step)
                        html += '<td>' + (stepNr + 1) + '.</td>';
                        html += '<td>';
                        html += step.getDescriptionHtml();
                        //html += '</td>';
                        //html += '<td>';
                        html += ' ';
                        html += step.getDistance().html;
                        //html += '</td>';
                        //html += '<td>';
                        html += ' ';
                        html += step.getDuration().html;
                        html += '</td>';
                        html += '</tr>';

                    }
                }

                html += '</table>';
                
                $(panel).hide();
                
                panel.innerHTML = html;
                $(panel).show('slow'); 
            });


}

             directions.clear(); 
             directions.load("from: " + from + " to: " + to ,
                 { travelMode: G_TRAVEL_MODE_DRIVING, getSteps: true,
                            locale: 'nl'
                        });
        } 
}



