function AtlasblokLabel(name, lat, lon) {
    this.name_ = name;
    this.latLng = new google.maps.LatLng(lat, lon);
}

AtlasblokLabel.prototype = new google.maps.OverlayView();

AtlasblokLabel.prototype.onRemove = function() {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
};

AtlasblokLabel.prototype.onAdd = function() {
    var coords = this.getProjection().fromLatLngToDivPixel(this.latLng);
    this.div_ = document.createElement("div");
    this.div_.style.position = "absolute";
    this.div_.style.top = (coords.y + 3) + "px";
    this.div_.style.left = (coords.x + 3) + "px";
    this.div_.style.fontSize = "9px";
    this.div_.style.color = "#ff0000";
    this.div_.style.fontFamily = "Verdana";
    this.div_.style.width = "100px";
    this.div_.appendChild(document.createTextNode(this.name_));

    var panes = this.getPanes();
    panes.overlayImage.appendChild(this.div_);
};

AtlasblokLabel.prototype.draw = function() {
    this.onRemove();
    this.onAdd();
};

var currentMarker = null;
var currentPolygon = null;
var currentCross1, currentCross2 = null;
var currentAtlasblokLabel = null;
var map = null;

function loadAddReport(lat, lon) {
    $("#species").autocomplete({
        source: "/search-species.action",
        minLength: 3
    });

    if (document.getElementById("google_map") == null) {
        return;
    }
    map = new google.maps.Map(document.getElementById("google_map"), {
        center: new google.maps.LatLng(52.25, 5.3),
        zoom: 7,
        scrollwheel: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: true,
        navigationControl: true
    });

    google.maps.event.addListener(map, "zoom_changed", function() {
        var zoom = map.getZoom();
        if (currentAtlasblokLabel != null) {
            currentAtlasblokLabel.setMap(zoom > 13 ? map : null);
        }

        if (currentPolygon) {
            currentPolygon.setMap(zoom > 11 ? map : null);
        }

        if (currentCross1) {
            currentCross1.setMap(zoom > 11 ? map : null);
        }

        if (currentCross2) {
            currentCross2.setMap(zoom > 11 ? map : null);
        }
    });

    google.maps.event.addListener(map, "click", function(event) {
        var zoom = map.getZoom();
        if (zoom < 8) {
            zoom += 5;
        } else if (zoom < 11) {
            zoom += 2;
        } else if (zoom < 14) {
            zoom += 1;
        }

        if (currentMarker != null) {
            currentMarker.setMap(null);
        }
        if (currentAtlasblokLabel != null) {
            currentAtlasblokLabel.setMap(null);
        }
        if (currentPolygon) {
            currentPolygon.setMap(null);
        }
        if (currentCross1) {
            currentCross1.setMap(null);
        }
        if (currentCross2) {
            currentCross2.setMap(null);
        }

        updateAtlasblok(event.latLng, zoom);
    });
    if (lat && lon) {
        updateAtlasblok(new google.maps.LatLng(lat, lon), 12);
    }
}

function updateAtlasblok(latLng, zoom) {
    currentMarker = new google.maps.Marker({
        position: latLng,
        draggable: false,
        //bouncy: true,
        clickable: false,
        map: map
    });

    google.maps.event.addListener(currentMarker, "dragend", function() {
        updateAtlasblok(currentMarker.getPosition());
    });

    $.getJSON("/atlasblok.action", { lat: latLng.lat(), lon: latLng.lng() }, function(json) {
        if (json.error) {
            alert(json.error);
            currentMarker.setMap(null);
            $('#atlasblok').html('<span id="atlasblok">-</span>');
            $('#rd').html('<span id="rd">-</span>');
            $('#rdx').val(0);
            $('#rdy').val(0);
            $("#abroad_container").show();
        } else {
            var rdx = parseFloat(json.rd.x);
            var rdy = parseFloat(json.rd.y);

            var qx = rdx - Math.floor(rdx);
            var qy = rdy - Math.floor(rdy);
            var q = 0;
            if (qx < 0.5) {
                q = qy < 0.5 ? 3 : 1;
            } else {
                q = qy < 0.5 ? 4 : 2;
            }
            $('#plekinfo').val(q);

            var quadrant = "";
            switch (q) {
                case 1:
                    quadrant = " linksboven";
                    break;
                case 2:
                    quadrant = " rechtsboven";
                    break;
                case 3:
                    quadrant = " linksonder";
                    break;
                case 4:
                    quadrant = " rechtsonder";
                    break;
            }

            $('#atlasblok').html('<span id="atlasblok">' + json.atlasblok + quadrant + '</span>');
            $('#rd').html('<span id="rd">' + rdx.toFixed(3) + ", " + rdy.toFixed(3) + '</span>');
            $('#rdx').val(rdx.toFixed(3));
            $('#rdy').val(rdy.toFixed(3));

            /* Border */
            currentPolygon = new google.maps.Polyline({
                path: [
                    new google.maps.LatLng(json.box[0].lat, json.box[0].lon),
                    new google.maps.LatLng(json.box[1].lat, json.box[1].lon),
                    new google.maps.LatLng(json.box[2].lat, json.box[2].lon),
                    new google.maps.LatLng(json.box[3].lat, json.box[3].lon),
                    new google.maps.LatLng(json.box[0].lat, json.box[0].lon)
                ],
                strokeColor: '#ff0000',
                strokeWidth: 2,
                strokeOpacity: 0.7,
                clickable: false
            });

            /* Cross */
            currentCross1 = new google.maps.Polyline({
                path: [
                    new google.maps.LatLng(json.cross[0].lat, json.cross[0].lon),
                    new google.maps.LatLng(json.cross[1].lat, json.cross[1].lon)
                ],
                strokeColor: '#ff0000',
                strokeWidth: 1,
                strokeOpacity: 0.7,
                clickable: false
            });
            currentCross2 = new google.maps.Polyline({
                path: [
                    new google.maps.LatLng(json.cross[2].lat, json.cross[2].lon),
                    new google.maps.LatLng(json.cross[3].lat, json.cross[3].lon)
                ],
                strokeColor: '#ff0000',
                strokeWidth: 1,
                strokeOpacity: 0.7,
                clickable: false
            });

            currentAtlasblokLabel = new AtlasblokLabel(json.atlasblok, json.box[0].lat, json.box[0].lon);

            currentPolygon.setMap(zoom > 11 ? map : null);
            currentCross1.setMap(zoom > 11 ? map : null);
            currentCross2.setMap(zoom > 11 ? map : null);
            currentAtlasblokLabel.setMap(zoom > 13 ? map : null);

            map.setCenter(latLng);
            map.setZoom(zoom);

            $("#abroad_container").hide();
        }
    });
}

