var map = null;
var geocoder = new GClientGeocoder();
var base_icon = new GIcon(G_DEFAULT_ICON);
base_icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
base_icon.iconSize = new GSize(20, 34);
base_icon.shadowSize = new GSize(37, 34);
base_icon.iconAnchor = new GPoint(9, 34);
base_icon.infoWindowAnchor = new GPoint(9, 2);
var init_point = new GLatLng(44.5, -30.0);
var icon1 = new GIcon(base_icon);
icon1.image = "http://www.google.com/mapfiles/markerS.png";
var icon2 = new GIcon(base_icon);
icon2.image = "http://www.google.com/mapfiles/markerD.png";
var marker1 = new GMarker(new GLatLng(48.1391265, 11.5801863),
    {draggable:true, icon:icon1});
var marker2 = new GMarker(new GLatLng(40.756054, -73.986951),
    {draggable:true, icon:icon2});
var search_marker = new Object();
var polyline = new GPolyline([marker1.getLatLng(), marker2.getLatLng()],
    "#ff0000", 10);

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setUIToDefault();
        map.setCenter(init_point, 3);
        map.addOverlay(marker1);
        map.addOverlay(marker2);
        map.setMapType(G_PHYSICAL_MAP);
        refresh();
        GEvent.addListener(marker1, "dragend", refresh);
        GEvent.addListener(marker2, "dragend", refresh);
		map.addOverlay(polyline);
    }
}
function use_search(point) {
    if (point == 'startpoint') {
        marker1.setLatLng(search_marker.getLatLng());
    }
    else if (point == 'destination') {
        marker2.setLatLng(search_marker.getLatLng());
    }
    map.removeOverlay(search_marker);
    refresh();
}
function refresh() {
    var point1 = marker1.getLatLng();
    var point2 = marker2.getLatLng();
    map.removeOverlay(polyline);
    polyline = new GPolyline([point1, point2], "#ff0000", 10);
    map.addOverlay(polyline);
}
function get_point(address) {
    geocoder.getLatLng(address, function(point) {
        if (!point) {
            // do sth.
        }
        else {
            map.setCenter(point);
            map.removeOverlay(search_marker);
            search_marker = new GMarker(point);
            map.addOverlay(search_marker);
            search_marker.openInfoWindowHtml('Use this location as:<br /><a href="javascript:use_search(\'startpoint\')">start point (S)</a><br /><a href="javascript:use_search(\'destination\')">destination (D)</a>');
        }
    });
}
function calculate() {
    var http = null;
    if (window.XMLHttpRequest) {
       http = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
       http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (http != null) {
        var point1 = marker1.getLatLng();
        var point2 = marker2.getLatLng();
        var params = 'lat1='+point1.lat()+'&lng1='+point1.lng()+'&lat2='+point2.lat()+'&lng2='+point2.lng();
        http.open('GET', '/calculate?'+params, true);
        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                response = http.responseText.split(';')
                document.getElementById('result').innerHTML = '<a href="javascript:document.getElementById(\'result\').style.display = \'none\';" style="float:right">Close</a>Flight distance: '+response[0]+'km<br />Ground distance: '+response[1]+'km<br />Flight time: <strong>'+response[2]+'h '+response[3]+'min</strong>';
                document.getElementById('result').style.display = 'block';
            }
        };
        http.send(null);
    }
}