﻿
var gmap = null;
var geocoder = null;
var directions = null;
var addressMarker = null;
var bedMarker = null;
var startAddress = null;

var bedIcon = new GIcon();
bedIcon.image = "/images/marker-bed.png";
bedIcon.shadow = "/images/marker-shadow.png";
bedIcon.iconSize = new GSize(24, 32);
bedIcon.shadowSize = new GSize(42, 32);
bedIcon.iconAnchor = new GPoint(12, 32);
bedIcon.infoWindowAnchor = new GPoint(10, 2);

function loadGMap(id_mapbody, id_directions) {
    if (!GBrowserIsCompatible()) return;
    if (!geocoder) geocoder = new GClientGeocoder();
    if (!gmap) {
        gmap = new GMap2(document.getElementById(id_mapbody));
        gmap.enableScrollWheelZoom();
        gmap.addControl(new GLargeMapControl3D());
        gmap.addControl(new GScaleControl());
        gmap.addControl(new GMapTypeControl());
        gmap.addControl(new GOverviewMapControl(new GSize(160, 120)));
        directions = new GDirections(gmap, document.getElementById(id_directions));
        GEvent.addListener(directions, "load", onDirectionsLoad);
        GEvent.addListener(directions, "error", onDirectionsError);
        window.onunload = GUnload;
    }
}

function enableQueryAddressButton() {

    document.getElementById("btn_query_address").disabled = (document.getElementById("txt_query_address").value.length == 0);
}

function pressedQueryAddressButton() {

    if (!document.getElementById("btn_query_address").disabled) {
        if (directions) directions.clear();
        if ((geocoder) && (gmap)) {
            geocoder.getLocations(
                document.getElementById("txt_query_address").value + ", ITALY",
                function(result) {
                    if (result.Status.code == G_GEO_SUCCESS) {
                        var point = new GLatLng(result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]);
                        if ((point) && (gmap)) {
                            onAddressLoad();
                            gmap.setCenter(point);
                            gmap.setZoom(13);
                            if (addressMarker) {
                                addressMarker.closeInfoWindow();
                                addressMarker.hide();
                            }
                            if (!addressMarker) {
                                addressMarker = new GMarker(point);
                                gmap.addOverlay(addressMarker);
                            }
                            else {
                                addressMarker.setLatLng(point);
                            }
                            addressMarker.show();

                            GEvent.addListener(addressMarker, "click", function() {
                                addressMarker.openInfoWindowHtml(document.getElementById("txt_query_address").value);
                            });

                            GEvent.trigger(addressMarker, "click");
                        }
                    }
                    else {
                        onAddressError(result.Status.code);
                    }
                }
            );
        }
    }
}

function enableQueryDirectionsButton() {

    document.getElementById("btn_query_directions").disabled = (document.getElementById("txt_query_from").value.length == 0) || (document.getElementById("txt_query_to").value.length == 0);
}

function pressedQueryDirectionsButton() {

    if (!document.getElementById("btn_query_directions").disabled) {
        if (addressMarker) {
            addressMarker.closeInfoWindow();
            addressMarker.hide();
        }
        if (directions) directions.load("from: " + document.getElementById("txt_query_from").value + " to: " + document.getElementById("txt_query_to").value);
    }
}

function enableQueryDirections2Button() {

    document.getElementById("btn_query_directions2").disabled = (document.getElementById("txt_query_from2").value.length == 0);
}

function pressedQueryDirections2Button() {

    if (!document.getElementById("btn_query_directions2").disabled) {
        if (addressMarker) {
            addressMarker.closeInfoWindow();
            addressMarker.hide();
        }
        if (directions) directions.load("from: " + document.getElementById("txt_query_from2").value + " to: " + startAddress);
    }
}

function onAddressLoad() {

    document.getElementById("address_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
    document.getElementById("address_error_OTHER").style.display = "none";
}

function onAddressError(code) {

    if (code == G_GEO_SUCCESS) {
        document.getElementById("address_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
        document.getElementById("address_error_OTHER").style.display = "none";
    }
    else if (code == G_GEO_UNKNOWN_ADDRESS) {
        document.getElementById("address_error_G_GEO_UNKNOWN_ADDRESS").style.display = "block";
        document.getElementById("address_error_OTHER").style.display = "none";
    }
    else {
        document.getElementById("address_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
        document.getElementById("address_error_OTHER").style.display = "block";
    }
}

function onDirectionsLoad() {

    document.getElementById("directions_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
    document.getElementById("directions_error_OTHER").style.display = "none";
}

function onDirectionsError() {

    var code = directions.getStatus().code;

    if (code == G_GEO_SUCCESS) {
        document.getElementById("directions_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
        document.getElementById("directions_error_OTHER").style.display = "none";
    }
    else if (code == G_GEO_UNKNOWN_ADDRESS) {
        document.getElementById("directions_error_G_GEO_UNKNOWN_ADDRESS").style.display = "block";
        document.getElementById("directions_error_OTHER").style.display = "none";
    }
    else {
        document.getElementById("directions_error_G_GEO_UNKNOWN_ADDRESS").style.display = "none";
        document.getElementById("directions_error_OTHER").style.display = "block";
    }
}

function setStartAddress(address, zoom) {

    if (geocoder) {
        geocoder.getLocations(
            address,
            function(result) {
                if (result.Status.code == G_GEO_SUCCESS) {
                    var point = new GLatLng(result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]);
                    setStartPoint(point, zoom);
                    startAddress = address;
                }
                else {
                    onAddressError(result.Status.code);
                }
            }
        );
    }
}

function setStartPoint(point, zoom) {

    if (gmap) gmap.setCenter(point, zoom);
    startAddress = point;
}

function setStartHotelByLatLng(hotelName, hotelLocation, hotelAddress1, hotelAddress2, hotelPhone, hotelEMail, hotelPoint, hotelZoom) {

    if (gmap) {

        gmap.setCenter(hotelPoint);
        gmap.setZoom(hotelZoom);

        if (bedMarker) {
            bedMarker.closeInfoWindow();
            bedMarker.hide();
        }
        if (!bedMarker) {
            bedMarker = new GMarker(hotelPoint, { icon: bedIcon });
            gmap.addOverlay(bedMarker);
        }
        else {
            bedMarker.setLatLng(hotelPoint);
        }
        bedMarker.show();

        var bedHtml = '';
        bedHtml += '<div class="marker">';
        bedHtml += '<h3><img class="photo left" src="/images/marker-logo.gif" alt="' + hotelName + ' - ' + hotelLocation + '" />' + hotelName + '</h3>';
        if (hotelLocation.length > 0) bedHtml += '<h4>' + hotelLocation + '</h4>';
        bedHtml += '<p>';
        if (hotelAddress1.length > 0) bedHtml += hotelAddress1 + '<br />';
        if (hotelAddress2.length > 0) bedHtml += hotelAddress2 + '<br />';
        if (hotelPhone.length > 0) bedHtml += hotelPhone + '<br />';
        if (hotelEMail.length > 0) bedHtml += 'e-mail: <a href="mailto:' + hotelEMail + '">' + hotelEMail + '</a>';
        bedHtml += '</p>';
        bedHtml += '</div>';

        GEvent.addListener(bedMarker, "click", function() {
            bedMarker.openInfoWindowHtml(bedHtml);
        });

        GEvent.trigger(bedMarker, "click");

        startAddress = hotelPoint;
    }
}

function setStartHotelByAddress(hotelName, hotelLocation, hotelAddress1, hotelAddress2, hotelPhone, hotelEMail, hotelGoogleAddress, hotelZoom) {

    if (geocoder) {
        geocoder.getLocations(
            hotelGoogleAddress,
            function(result) {
                if (result.Status.code == G_GEO_SUCCESS) {
                    var hotelPoint = new GLatLng(result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]);
                    setStartHotelByLatLng(hotelName, hotelLocation, hotelAddress1, hotelAddress2, hotelPhone, hotelEMail, hotelPoint, hotelZoom);
                    startAddress = hotelGoogleAddress;
                }
                else {
                    onAddressError(result.Status.code);
                }
            }
        );
    }
}
