if (typeof MGI == "undefined") {
    var MGI = {};
}


MGI.google_map = null;
MGI.google_geocoder = null;

/*
 * taken from the googlemap example
 */
MGI.googlemapload = function() {
    if (GBrowserIsCompatible()) {
        var mapdiv = document.getElementById("googlemapdiv");
        var address = $(mapdiv).attr('address');
        var itemname = $(mapdiv).attr('itemname');
        var zoom = $(mapdiv).attr('zoom');
        var lat = Number($(mapdiv).attr('lat'));
        var lon = Number($(mapdiv).attr('lon'));
        var smallcontrols = $(mapdiv).attr('smallcontrols') == "yes";
        zoom = Number(zoom ? zoom : 13);
        
        $.log.info("Zoom: " + zoom);
        $.log.info("Address: " + address);
        
        MGI.google_map = new GMap2(mapdiv);
        MGI.google_map.setCenter(new GLatLng(0,0));
        if(smallcontrols){
            MGI.google_map.addControl(new GSmallMapControl());
        }else{
            MGI.google_map.addControl(new GLargeMapControl3D());
        }
        
        
        function showItem(point){
            if (point){
                $.log.info("Centered");
                MGI.google_map.setCenter(point, zoom);
                if(itemname){
                    var marker = new GMarker(point);
                    MGI.google_map.addOverlay(marker);
                    marker.openInfoWindowHtml("<b>"+itemname+"</b>"+"<br/>"+
                                                address);
                }
            }
        }

        if(lat && lon){
            $.log.info("Used LatLong Lat:" + lat + " Lon:" + lon);
            var point = new GLatLng(lat, lon, 0);
            showItem(point);
        }else
        {
            MGI.google_geocoder = new GClientGeocoder();
            MGI.google_geocoder.getLatLng(address, showItem);
            
        }
        
        
    }
    else{
        //well there is nothing to do really, you're fucked :)
    }
};

MGI.googlemapunload = function() {
    GUnload();
    MGI.google_map = null;
    MGI.google_geocoder = null;
};

