var GoogleMap =
{
    my_map:null,
    my_icon:null,
    my_bounds:null,
    
    getMap:function()
    {
        if(this.my_map==null) 
        {
            this.my_map = new GMap2(document.getElementById("mymap"));
        }
        
        return this.my_map;
    },
    
    getIcon:function()
    {
        if(this.my_icon==null)
        {
            this.my_icon = new GIcon();
            this.my_icon.image = "http://devhost/reisonaut/website/img/icon/gmap_pin.png";
            this.my_icon.shadow = "http://devhost/reisonaut/website/img/icon/gmap_shadow.png";
            this.my_icon.iconSize = new GSize(12, 20);
            this.my_icon.shadowSize = new GSize(22, 20);
            this.my_icon.iconAnchor = new GPoint(6, 20);
            this.my_icon.infoWindowAnchor = new GPoint(5, 1);
        }
        
        return this.my_icon;
    },
    
    getBounds:function()
    {
        if(this.my_bounds==null)
        {
            this.my_bounds = new GLatLngBounds();
        }
        
        return this.my_bounds;
    },
    
    init:function(size)
    {
        var mt = this.getMap().getMapTypes();
        for (var i=0; i<mt.length; i++) 
        {
            mt[i].getMinimumResolution = function() {return 5;}
        }
        
        if(size!='small')
        {
            this.getMap().addControl(new GLargeMapControl());
            this.getMap().addControl(new GMapTypeControl());
        }
        else
        {
            this.getMap().addControl(new GSmallZoomControl());
        }
        
        var center = new GLatLng(0,0);
        this.getMap().setCenter(center);
    },
    
    loadXml:function(xml)
    {
        var request = GXmlHttp.create();
        request.open("GET", xml, true);
        
        request.onreadystatechange = function() 
        {
            if (request.readyState == 4) 
            {
                var xmlDoc = request.responseXML;
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                
                for (var i = 0; i < markers.length; i++) 
                {
                    var lat = parseFloat(markers[i].getAttribute("lat"));
                    var lng = parseFloat(markers[i].getAttribute("lng"));
                    var html = markers[i].getAttribute("html");
                    var label = markers[i].getAttribute("label");
                    var url = markers[i].getAttribute("url");

                    var point = new GLatLng(lat,lng);
                    
//                    var marker = this.createInfoMarker(point,label,html);
                    var marker = this.createLinkMarker(point,label,html,url);

                    this.getMap().addOverlay(marker);
                }
            }
        }
        request.send(null);
    },
    
    createInfoMarker:function(lat,lng,label,title1,html1,title2,html2)
    {
        var point = new GLatLng(lat,lng);
        
        var marker = new GxMarker(point,this.getIcon(),label);
        
        var infoTabs = [
            new GInfoWindowTab(title1, html1),
            new GInfoWindowTab(title2, html2)
        ];
        
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowTabsHtml(infoTabs); });
        
        this.getMap().addOverlay(marker);
        
        this.getBounds().extend(point);
    },
    
    createLinkMarker:function(lat,lng,label,html,url)
    {
        var point = new GLatLng(lat,lng);
        
        var marker = new GxMarker(point,this.getIcon(),html);
        
        GEvent.addListener(marker, "click", function() { window.location.href = url; });
        
        this.getMap().addOverlay(marker);
        
        this.getBounds().extend(point);
    },

    createSimpleMarker:function(lat,lng,label,html,url)
    {
        var point = new GLatLng(lat,lng);
        
        var marker = new GxMarker(point,this.getIcon());
        
//        GEvent.addListener(marker, "click", function() { window.location.href = url; });
        
        this.getMap().addOverlay(marker);
        
        this.getBounds().extend(point);
    },

    centerAndZoom:function()
    {
        var center=this.getBounds().getCenter();
        
        this.getMap().setCenter(center);
        var zoom=Math.min(this.getMap().getBoundsZoomLevel(this.getBounds()),15);
        this.getMap().setZoom(zoom);
    },
    
    searchForAddress:function(address)
    {
        var geocoder = new GClientGeocoder();
        
        geocoder.getLatLng(
            address,
            function(point) 
            {
                if (!point) 
                {
                    alert(address + " not found");
                } else {
                    this.getMap().setCenter(point, 13);
                    var marker = new GMarker(point);
                    this.getMap().addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                }
            }
        );
    },
    
    clear:function()
    {
        this.my_map.clearOverlays();
        this.my_icon=null;
        this.my_bounds=null;
    }
    
}


function loadMap(inputfile) {
    if (GBrowserIsCompatible()) { 

      
      return map;
    }
    
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}