var dblGoogleMapMarkerLat = 0;
var dblGoogleMapMarkerLng = 0;

    var map = null;
    var geocoder = null;

    function Left(str, n){
        if (n <= 0) {
            return "";
        } else if (n > String(str).length) {
            return str;
        } else {
            return String(str).substring(0,n);
        }
    }
    function Right(str, n){
        if (n <= 0) {
           return "";
        } else if (n > String(str).length) {
           return str;
        } else {
           var iLen = String(str).length;
           return String(str).substring(iLen, iLen - n);
        }
    }
    function GoogleMap()
    {
        if (GBrowserIsCompatible())
        {
            map = new GMap2(document.getElementById("map"));
            map.setUIToDefault();
        }
    }
    function GoogleMapSpecific(strID)
    {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById(strID));
            map.setUIToDefault();
        }
    }
    function GoogleMapMarker(Lat, Lng, strIcon)
    {
        var icon = new GIcon();
        if (strIcon) {
            icon.image = strIcon;
        } else {
            icon.image = "http://www.google.com/mapfiles/marker.png";
            icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        }
        icon.iconSize = new GSize(12, 20);
        icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
        
        var point = new GLatLng(Lat, Lng);
        
        map.addOverlay(new GMarker(point, icon));
    }
    function GoogleMapMarkerX(obj)
    {
        var icon = new GIcon();
        icon.image = (obj.icon.image || "http://www.google.com/mapfiles/marker.png");
        icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        icon.iconSize = new GSize((obj.icon.width || 12), (obj.icon.height || 20));
        icon.shadowSize = new GSize((obj.icon.width || 12), (obj.icon.height || 20));
        icon.iconAnchor = new GPoint(((obj.icon.width / 2) || 20), (obj.icon.height || 20));
        icon.infoWindowAnchor = new GPoint(5, 1);
        
        var point = new GLatLng(obj.lat, obj.lon);
        
        map.addOverlay(new GMarker(point, icon));
    }
    function GoogleMapCenter(Lat, Lng, Zoom)
    {
        map.setCenter(new GLatLng(Lat, Lng), Zoom);
    }
    
    function GoogleMapAddress(address)
    {
        geocoder = new GClientGeocoder();
        geocoder.getLatLng( address, function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                $('#points').val(point);
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml(address);
            }
        });
    }
    function GoogleMapType(strType)
    {
        switch (strType)
        {
            case 'Map':
                map.setMapType(G_NORMAL_MAP);
                break;
            case 'Satellite':
                map.setMapType(G_SATELLITE_MAP);
                break;
            case 'Hybrid':
                map.setMapType(G_HYBRID_MAP);
                break;
            case 'Terrain':
                map.setMapType(G_PHYSICAL_MAP);
                break;
        }
    }
    function GoogleMapZoom(zoom)
    {
        map.setZoom(zoom);
    }

    function GoogleMapLocation(address)
    {
        geocoder = new GClientGeocoder();
        geocoder.getLocations(address, GoogleMapLocationShow);
    }
    function addUK(strAddress) {
        var boolAddUK = true;
        if (Right(strAddress.toLowerCase(), 2) === 'uk') {
            boolAddUK = false;
        }
        if (Right(strAddress.toLowerCase(), 14) === 'united kingdom') {
            boolAddUK = false;
        }
        if (boolAddUK === true) {
            strAddress = strAddress + ', uk';
        }
        return strAddress;
    }
    function GoogleMapLookup(obj) {
        geocoder = new GClientGeocoder();
        geocoder.getLocations(addUK(obj.address), function (response) {
            if (!response || response.Status.code != 200)
            {
                if (obj.error) {
                    obj.error({
                        error: response
                    });
                }
            } else {
                place = response.Placemark[0];
                if (obj.success) {
                    obj.success({
                        lat: place.Point.coordinates[1],
                        lon: place.Point.coordinates[0],
                    });
                }
            }            
        });        
    }

    function GoogleMapLocationShow(response)
    {
        map.clearOverlays();
        if (!response || response.Status.code != 200)
        {
            alert("\"" + response + "\" not found");
        } else {
            place = response.Placemark[0];
            dblGoogleMapMarkerLat = place.Point.coordinates[1];
            dblGoogleMapMarkerLng = place.Point.coordinates[0];
            point = new GLatLng(dblGoogleMapMarkerLat, dblGoogleMapMarkerLng);
            marker = new GMarker(point);
            map.addOverlay(marker);
            map.setCenter(new GLatLng(dblGoogleMapMarkerLat, dblGoogleMapMarkerLng), map.getZoom());
          
            var p;
            p = p + place.Point.coordinates[1];
            p = p + ",";
            p = p + place.Point.coordinates[0];
            $('#points').val(point);
        }
    }


