Trr.Map = function( wrapperId, defaultLatitude, defaultLongitude, defaultZoom )
{
    if ( !GBrowserIsCompatible() )
    {
        return;
    }
    
    this.DefaultLongitude = defaultLongitude;
    this.DefaultLatitude = defaultLatitude;
    this.DefaultZoom = defaultZoom;
    this.GoogleMap = new GMap2( document.getElementById( wrapperId ) );
    this.GoogleMap.setUIToDefault();
    
    this.CurrentlyLoadedMarkerPoints = Array();
    
    // unloads the map
    $(window).unload( function () {
        GUnload();
    });
}

Trr.Map.prototype.ShowDefault = function()
{
    this.GoogleMap.setCenter(new GLatLng(map.DefaultLatitude, map.DefaultLongitude ), map.DefaultZoom );
}

Trr.Map.prototype.AddMarker = function( latitude, longitude, title, clickFunctionReference, isHK )
{
    var point = new GLatLng( latitude, longitude );
    var icon = isHK ? Trr.Map.GetHKIcon() : Trr.Map.GetIcon();
    var marker = new GMarker( point, {icon:icon, title:title } );
    GEvent.bind( marker, "click", this, clickFunctionReference );
    this.GoogleMap.addOverlay( marker );
    this.CurrentlyLoadedMarkerPoints[ this.CurrentlyLoadedMarkerPoints.length ] = point;
}

Trr.Map.GetHKIcon = function()
{
    var icon = new GIcon();
    icon.iconSize = new GSize(13, 13);
    // specifies where on the map the icon should be attached, relative to the image size
    // In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
    icon.iconAnchor = new GPoint(13, 13);
    icon.image = "/UI/Img/SearchOffice/MapIconHK.png";
    return icon;
}

Trr.Map.GetIcon = function()
{
    var icon = new GIcon();
    
    // specifies where on the map the icon should be attached, relative to the image size
    // In this case we want the icon image to attach at the actual point, 5px from left and 14px from top
    icon.iconAnchor = new GPoint(5, 14);
    
    icon.image = "/UI/Img/SearchOffice/MapIconWithoutShadow.png";
    icon.iconSize = new GSize(11, 15);
    
    icon.shadow = "/UI/Img/SearchOffice/MapIconShadow.png";
    icon.shadowSize = new GSize(20,15);
    
    return icon;
}

Trr.Map.prototype.ShowClosestOffices = function( query )
{
    var thisObj = this;
    new GClientGeocoder().getLatLng( query + ", Sverige",
        function( point )
        {
            if( point != null )
            {
                thisObj.GoogleMap.setCenter( point, 9 );
                thisObj.ZoomOutUntilFirstMarkerIsVisible();
            }
            else
            {
                thisObj.ShowDefault();
            }
        }
    );
}

/**
 * Keep zooming out unit at least one marker is visible
 */
Trr.Map.prototype.ZoomOutUntilFirstMarkerIsVisible = function()
{
    while(true)
    {
        //Get current bounds
        var bounds = this.GoogleMap.getBounds();
    
        //Check if any markers exists within current bounds
        for( i = 0; i < this.CurrentlyLoadedMarkerPoints.length; i++ )
        {   
            if( bounds.containsLatLng( this.CurrentlyLoadedMarkerPoints[ i ] ) )
            {
                return;
            }
        }
        
        this.GoogleMap.zoomOut();
        
        //Stop zooming out when the minimum zoom level has been reached
        if( this.GoogleMap.getZoom() == 2 )
        {
            return;
        }
    }
}