var GM = {

    map:null,
    geocoder: null,

    init: function()
    {
        geocoder = new google.maps.Geocoder();

        var latlng = new google.maps.LatLng(55.67835873246176, 12.553939819335938, 12);
        var myOptions = {
            zoom: 12,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var redTop, redBig, outerZone, transitLine;

        this.map = new google.maps.Map(document.getElementById("map"), myOptions);

        var button = document.getElementById('search_button');
        button.onclick = GM.showAddress;

        var form = document.getElementById('myform');
        form.onsubmit = GM.showAddress;

        var print = document.getElementById('print_button');
        print.onclick = GM.print;

        outerZone = new google.maps.Polygon({
            paths: outerzone,
            strokeColor: "#6d9d31",
            strokeOpacity: .5,
            strokeWeight: 3,
            fillColor: "#6d9d31",
            fillOpacity: .3
        });

        var transitLine = new google.maps.Polyline({
            path: transitline,
            strokeColor: "#65544a",
            strokeOpacity: .7,
            strokeWeight: 8
        });

        outerZone.setMap(map);
        transitLine.setMap(map);
    },

    print: function()
    {
        window.print();
        return false;
    },

    showAddress: function()
    {
        address = document.getElementById('address').value;
        if (geocoder) {
            geocoder.geocode(
                {
                    'address': address + ', Copenhagen',
                    region:'DK'
                },
                function(results, status)
                {
                    if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);
                        map.setZoom(16);

                        var text = results[0].address_components[0].long_name;

                        var latLng = new google.maps.LatLng(
                            results[0].geometry.location.Ra,
                            results[0].geometry.location.Sa
                        );

                        var marker = new google.maps.Marker({
                            position: latLng,
                            draggable: false,
                            visible: false,
                            map: map
                        });

                    } else {
                        alert('Kan ikke findes!');
                    }
                }
            );
        }

        return false;
    }
}

window.onload = GM.init;
