var GoGoogle = function () {
    this.search.type = 'complete';
    this.addressString = this.search.street+', '+this.search.zip+''+' '+this.search.city;
    this.geocode();
}
GoGoogle.prototype.geocode = function () {
    var geocoder = new GClientGeocoder();
    if (geocoder) {
        geocoder.setBaseCountryCode('DE');
        geocoder.getLatLng(this.addressString, this.check.bindTo(this));
    }
}
GoGoogle.prototype.check = function (point) {
    if (!point && this.search.type == 'complete') {
        this.search.type = 'simple';
        this.addressString = this.search.zip+' '+this.search.city;
        this.geocode();
    } else if (!point && this.search.type == 'simple') {
        window.goMap24 = new GoMap24(this.search);
    } else {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(point, 9);
        map.setMapType(G_NORMAL_MAP);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var marker = new GMarker(point);
        map.addOverlay(marker);
    }
}
var GoMap24 = function (search) {
    if (search) {
        this.search = search;
        this.search.type = 'complete';
        this.addressString = this.search.street+', '+this.search.zip+' '+this.search.city;
    }
    Map24.loadApi( ["core_api", "wrapper_api"] , this.map24ApiLoaded.bindTo(this) );
}
GoMap24.prototype.map24ApiLoaded = function (){
    Map24.MapApplication.init( { NodeName: "map", MapType: "Static" } );
    if (typeof this.search != 'undefined')
        this.geocode(this.addressString);
    else {
        console.log(this.Longitude, this.Latitude)
        this.centerResult([new Map24.Location({
                        'Longitude':this.Longitude,
                        'Latitude':this.Latitude
                    }
                )
            ]
        );
    }
}
GoMap24.prototype.geocode = function ( searchText ){
    var geocoder = new Map24.GeocoderServiceStub();
    geocoder.geocode( { SearchText: Map24.trim( searchText ), MaxNoOfAlternatives: 10, CallbackFunction: this.centerResult.bindTo(this) } );
}
GoMap24.prototype.centerResult = function (locs) {
    var myLoc = new Map24.Location({
        Longitude: locs[0].getLongitude(),
        Latitude: locs[0].getLatitude(),
        Description: this.searchText,
        LogoURL: "http://maptpzone.navteq.com/AJAXAPI/images/pin_maparea_home.png",
        TooltipOpen: "OnMouseOver",
        TooltipClose: "OnMouseOut"
    });
    
    myLoc.commit();
    myLoc.center( { MinimumWidth: 120000 } );
}
eventHandler.addLoadEvent(function () {
    if (document.getElementById("mapBorder")) {
        eventHandler.addEvent(document.getElementById('toggleMapButton'), 'click', function () {
            window.toggleMap();
        })
        window.toggleMap = function () {
            with (document.getElementById("mapBorder").style) {
                if (GBrowserIsCompatible()) {
                    if (display=='none')  {
                        if (typeof goGoogle == 'undefined' && typeof goMap24 == 'undefined')
                            if (typeof GoMap24.prototype.Longitude == 'undefined'
                                && typeof GoMap24.prototype.Latitude == 'undefined')
                                window.goGoogle = new GoGoogle();
                            else
                                window.goMap24 = new GoMap24();
                        display = 'block';
                    } else {
                        display = 'none';
                    }
                }
            }
        }
    }
})
eventHandler.addEvent(window, 'unload', function () {
    if (typeof GUnload != 'undefined')
        GUnload();
})