var __LR_GoogleMaps_UnloadSetup = false;

function __LR_GoogleMaps_Initialize(id, latitude, longitude, zoom, type, idnosupport, controls, infoWindow)
{
    try
    {
        __LR_GoogleMaps_CheckUnloadSetup();
        var obj = document.getElementById(id);
        if ((obj != null) && (GBrowserIsCompatible()))
        {
            var map = new GMap2(obj);
            map.setCenter(new GLatLng(latitude, longitude), zoom, type);
            map.currentInfoWindow = null;
            map.previousInfoWindow = null;
            map.isLRInfoWindow = false;
            obj.__LR_GoogleMaps_Obj = map;
            if ((controls != null) && (controls.length > 0))
            {
                for (var i = 0; i < controls.length; i++)
                {
                    __LR_GoogleMaps_AddControl(id, controls[i].Control, controls[i].Position)
                }
            }
            if (infoWindow != null)
            {
                __LR_GoogleMaps_OpenInfoWindow(id, infoWindow.lat, infoWindow.lng, infoWindow.id);
            }
            GEvent.addListener(map, "infowindowclose", function() {__LR_GoogleMaps_InfoWindowClosed(id)});
        }
        else
        {
            obj = document.getElementById(idnosupport);
            if (obj) obj.style.display = "";
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_InfoWindowClosed(id)
{
    var obj = __LR_GoogleMaps_GetMapObject(id);
    if ((obj != null) && (theForm != null))
    {
        if (!obj.isLRInfoWindow)
        {
            obj.previousInfoWindow = obj.currentInfoWindow;
            obj.currentInfoWindow = null;
        }
        if ((obj.previousInfoWindow != null) && (obj.previousInfoWindow != obj.currentInfoWindow))
        {
            obj.previousInfoWindow.style.display = "none";
        }
        obj.isLRInfoWindow = false;
        if (obj.previousInfoWindow != null)
        {
            theForm.appendChild(obj.previousInfoWindow);
        }
    }
}

function __LR_GoogleMaps_CheckUnloadSetup()
{
    try
    {
        if (!__LR_GoogleMaps_UnloadSetup)
        {
            var oldUnload = window.onunload;
            if (oldUnload != null)
            {
                window.onunload = function() { oldUnload(); try {GUnload();} catch (e){} };
            }
            else
            {
                window.onunload = function () { try {GUnload();} catch (e){} };
            }
            __LR_GoogleMaps_UnloadSetup = true;
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_GetMapObject(id)
{
    try
    {
        var obj = document.getElementById(id);
        if (obj != null) return obj.__LR_GoogleMaps_Obj;
    }
    catch(e)
    {
    }
    return null;
}

function __LR_GoogleMaps_PanTo(id, latitude, longitude)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            obj.panTo(new GLatLng(latitude, longitude));
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_AddControl(id, control, position)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if ((obj != null) && (control != null))
        {
            if (position != null)
            {
                obj.addControl(control, position);
            }
            else
            {
                obj.addControl(control);
            }
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_OpenInfoWindow(id, latitude, longitude, node, marker)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        node = document.getElementById(node);
        if (obj != null)
        {
            obj.previousInfoWindow = obj.currentInfoWindow;
            obj.currentInfoWindow = node;
            obj.isLRInfoWindow = true;
            if (node != null)
            {

                if (!node.length)
                {
                    node.style.display = "";
                    if (marker != null)
                    {
                        marker.openInfoWindow(node);
                    }
                    else
                    {
                        obj.openInfoWindow(new GLatLng(latitude, longitude), node);
                    }
                }
                else
                {
                    if (marker != null)
                    {
                        marker.openInfoWindowTabs(node);
                    }
                    else
                    {
                        obj.openInfoWindowTabs(new GLatLng(latitude, longitude), node);
                    }
                }
            }
            else
            {
                if (marker != null)
                {
                    marker.showMapBlowup();
                }
                else
                {
                    obj.showMapBlowup(new GLatLng(latitude, longitude));
                }
            }
            if (obj.getInfoWindow().isHidden()) obj.getInfoWindow().show();
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_CloseInfoWindow(id)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            obj.closeInfoWindow();
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_AddMarker(id, latitude, longitude, title, image, shadow, imageSize, shadowSize, imageAnchor, IWAnchor, infoWindow)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            var options = null;
            if ((title != null) || (image != null) || (shadow != null) || (imageSize != null) || (shadowSize != null) || (imageAnchor != null) || (IWAnchor != null))
            {
                options = new Object();
                if (title != null) options.title = title;
                if ((image != null) || (shadow != null) || (imageSize != null) || (shadowSize != null) || (imageAnchor != null) || (IWAnchor != null))
                {
                    var icon = new GIcon();
                    if (image != null) icon.image = image;
                    if (shadow != null) icon.shadow = shadow;
                    if (imageSize != null) icon.iconSize = imageSize;
                    if (shadowSize != null) icon.shadowSize = shadowSize;
                    if (imageAnchor != null) icon.iconAnchor = imageAnchor;
                    if (IWAnchor != null) icon.infoWindowAnchor = IWAnchor;
                    options.icon = icon;
                }
            }
            var point = new GLatLng(latitude, longitude);
            var marker = new GMarker(point, options);
            if (obj.markersBounds == null)
            {
                obj.markersBounds = new GLatLngBounds();
            }
            obj.markersBounds.extend(point);
            if (infoWindow != null)
            {
                GEvent.addListener(marker, "click", function() {__LR_GoogleMaps_OpenInfoWindow(id, null, null, infoWindow, marker);});
            }
            obj.addOverlay(marker);
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_FitToMarkers(id, doPan, points)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            var bounds = null;
            if ((points != null) && (points.length))
            {
                bounds = new GLatLngBounds();
                for (var i = 0; i < points.length; i++) bounds.extend(points[i]);
            }
            else
            {
                bounds = obj.markersBounds;
            }
            if (bounds != null)
            {
                obj.setZoom(obj.getBoundsZoomLevel(bounds));
                if (doPan)
                {
                    obj.panTo(bounds.getCenter());
                }
                else
                {
                    obj.setCenter(bounds.getCenter());
                }
            }
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_SetType(id, type)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            obj.setMapType(type);
        }
    }
    catch(e)
    {
    }
}

function __LR_GoogleMaps_SetZoom(id, zoom)
{
    try
    {
        var obj = __LR_GoogleMaps_GetMapObject(id);
        if (obj != null)
        {
            obj.setZoom(zoom);
        }
    }
    catch(e)
    {
    }
}