var map = null;
var gdir = null;
var geocoder = null;
var addressMarker;
var _address = '';

$(function(){
    $("a.gmap_trigger").fancybox({});
});

function setDirections(fromAddress){
    if (_address && gdir) {
        if (_address.length > 1) {
            gdir.load("from: " + fromAddress + " to: " + _address, {
                "locale": "de"
            });
        }
    }
}

function loadGoogleMap(id, lang){
    _address = $("#GMapAddress_" + id).val();
    _zoom = parseInt($("#GMapZoom_" + id).val());
    _width = parseInt($("#GMapWidth_" + id).val());
    _height = parseInt($("#GMapHeight_" + id).val());
    _description = $("#GMapDesc_" + id).html();

    geocoder = new GClientGeocoder();
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("GMap_" + id), {
            size: new GSize(_width, _height)
        });
        map.setUIToDefault();
        gdir = new GDirections(map, document.getElementById("GDir_" + id));
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "addoverlay", resizeFancy);
        if (geocoder) {
            geocoder.getLatLng(_address, function(point){
                if (!point) {
                    alert(address + " not found");
                }
                else {
                    map.setCenter(point, _zoom);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(_description);
                }
            });
        }
    }
}



function handleErrors(){
	log(gdir);
	/*
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        $("#" + gdir.Ua.id).html("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    }
    else
        if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
            $("#" + gdir.Ua.id).html("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
        }
        else
            if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
                $("#" + gdir.Ua.id).html("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
            }
            else
                if (gdir.getStatus().code == G_GEO_BAD_KEY) {
                    $("#" + gdir.Ua.id).html("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
                }
                else
                    if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
                        $("#" + gdir.Ua.id).html("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
                    }
                    else {
                        $("#" + gdir.Ua.id).html("An unknown error occurred.");
                    }
    */
}

function onGDirectionsLoad(){
}

function resizeFancy(){
    setTimeout($.fancybox.resize, 1000);
}

function log(){
    if (window && window.console && window.console.log)
        for (var i = 0, len = arguments.length; i < len; i++)
            console.log(arguments[i]);
}

