/*
 * Wrapper class for all Sweden.se Google map functionality
 */
function Setra()
{
}

Setra.Map = null;

/**
 * {Setra.View}
 */
Setra.View = null;

/*
 * Constants
 */
Setra.IsTestEnviroment = false;

/**
 * page id of the current page
 */
Setra.CurrentPageId = "";

/**
 * Defines the width for the info window
 * {int}
 */
Setra.InfoWinWidth = null;

/*
 * Contains the current active markers
 */
Setra.Markers = new Array();

Setra.AjaxPageURL = "";

Setra.LanguageCode = "";
                
Setra.LanguageCodeName = "";

/*
 * Contains the current search query
 */
Setra.SearchQuery = "";

/*
 * Should be called when the page has finished loading
 */
Setra.Load = function( id, view )
{
    Setra.View = view;
    Setra.Init( id );
    Setra.UpdateMarkers();
}

Setra.UpdateMarkers = function()
{
    Setra.Map.clearOverlays();
    
    for( i = 0; i < Setra.Markers.length; i++ )
    {    
        markerOptions = {icon:Setra.GetIcon( Setra.Markers[ i ].CategoryName ), title:Setra.Markers[ i ].Title };     
        Setra.Markers[ i ].CurrentMarker = new GMarker( Setra.Markers[ i ].Point, markerOptions );
        
        GEvent.bind( Setra.Markers[ i ].CurrentMarker, "click", Setra.Markers[ i ], Setra.Markers[ i ].Select );
        
        Setra.Map.addOverlay( Setra.Markers[ i ].CurrentMarker );
    }
}

/**
 * Instantiates the object's fundamental parts
 *
 * @param {string} id
 */
Setra.Init = function( id )
{
    if ( !GBrowserIsCompatible() )
    {
        return;
    }
    
    Setra.Map = new GMap2( document.getElementById( id ) );
    Setra.Map.setCenter( Setra.View.DefaultCoordinates, Setra.View.DefaultZoom );
    
    Setra.MapParts.Types.Init();
    Setra.MapParts.Zoom.Init();
    
    // init slider
    Setra.MapParts.Zoom.SetSliderBarFromLevel( Setra.Map.getZoom() );

    // update slider
    GEvent.addListener( Setra.Map, "zoomend", function( oldLevel, newLevel )
    {
        Setra.MapParts.Zoom.SetSliderBarFromLevel( newLevel );
        
        if( Setra.Marker.SelectedObj != null )
        {
            Setra.Marker.SelectedObj.Center();
        }
    });
    
    // unloads the map
    $(window).unload( function () {
        GUnload();
    });
}

/**
 * Adds an event to the list of active events
 *
 * @param {int} pageId
 * @param {String} categoryName
 * @param {String} title
 * @param {int} latitude
 * @param {int} longitude
 */
Setra.AddMarker = function( pageId, categoryName, title, latitude, longitude )
{
    Setra.Markers[ Setra.Markers.length ] = new Setra.Marker( pageId, categoryName, title, latitude, longitude );
}

/**
 * Depending of zoom level and given categoryName, the correct GIcon is created and returned
 *
 * @param {String} categoryName
 * @return {GIcon}
 */
Setra.GetLargeIcon = function( categoryName )
{
    var icon = new GIcon();
    icon.infoWindowAnchor = new GPoint(5, 1);

    if( jQuery.browser.msie && jQuery.browser.version < 7 )
    {
        icon.iconSize = new GSize(33, 38);
        // 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, in the middle of the bottom line
        icon.iconAnchor = new GPoint(16.5, 38);
        icon.image = "/Layout/Setra/img/map/icons/ie6/" + categoryName.toLowerCase() + "-big.gif";
    }
    else 
    {
        icon.iconSize = new GSize(37, 42);
        // 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, in the middle of the bottom line
        icon.iconAnchor = new GPoint(18, 33);
        icon.image = "/Layout/Setra/img/map/icons/" + categoryName.toLowerCase() + "-big.png";
    }
    
    return icon;
}

/**
 * Depending of zoom level and given categoryName, the correct GIcon is created and returned
 *
 * @param {String} categoryName
 * @return {GIcon}
 */
Setra.GetIcon = function( categoryName )
{
    var icon = new GIcon();
    icon.infoWindowAnchor = new GPoint(5, 1);

    if( jQuery.browser.msie && jQuery.browser.version < 7 )
    {
        icon.iconSize = new GSize(15, 17);
        // 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, in the middle of the bottom line
        icon.iconAnchor = new GPoint(7.5, 17);
        icon.image = "/Layout/Setra/img/map/icons/ie6/" + categoryName.toLowerCase() + "-small.gif";
    }
    else 
    {
        icon.iconSize = new GSize(19, 21);
        // 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, in the middle of the bottom line
        icon.iconAnchor = new GPoint(9, 15);
        icon.image = "/Layout/Setra/img/map/icons/" + categoryName.toLowerCase() + "-small.png";
    }
    
    return icon;
}

Setra.LoadAndSearch = function( id, query , defaultView )
{
    var geocoder = new GClientGeocoder();

    geocoder.getLatLng(query + ", Sverige", function( point ) {
        if(!point)
        {
            //Search query did not return a specific location, show default view
            Setra.Load( id, defaultView );
        }
        else
        {
            //Set center based on search hit
            Setra.Load( id, new Setra.View( 9, point ) );
            
            //Keep zooming out unit at least one marker is visible
            Setra.ZoomOutUntilMarkerIsVisible();
        }
    });
}

/**
 * Keep zooming out unit at least one marker is visible
 */
Setra.ZoomOutUntilMarkerIsVisible = function()
{
    while(true)
    {
        //Get current bounds
        var bounds = Setra.Map.getBounds();
    
        //Check if any markers exists within current bounds
        for( i = 0; i < Setra.Markers.length; i++ )
        {   
            if( bounds.containsLatLng( Setra.Markers[ i ].Point ) )
            {
                return;
            }
        }
        
        Setra.Map.zoomOut();
        
        //Stop zooming out when the minimum zoom level has been reached
        if( Setra.Map.getZoom() == 2 )
            return;
    }
}