try
{
    wedebug.addLogger("prox", "ProximityController");
}
catch(e)
{
    alert(e.message);
}

var ProximityController = Class.create();
ProximityController.RSSUrl = '/tolk/Page/Map/ResultFeeder.aspx?skey=map_proximity_search_results&';
ProximityController.prototype = {
    m_guiController : null,    
    m_mapController : null,
    m_menuController : null,
    m_proximityCollectionId : "Proximity",    
    m_origin : null,
    m_upperLevel : null,
    
    setOrigin : function(wgs)
    {
        this.m_origin = wgs;
    },
    
    initialize : function(guiController, mapController, menuController)
    {
        this.m_upperLevel = mapController.getZoomLevels().last();
    
        this.m_guiController = guiController;
        this.m_mapController = mapController;    
        this.m_menuController = menuController;
        
        mapController.addEventListener("onchangeview", this._cb_onChangeView.bind(this));
        mapController.addEventListener("onmapstylechange", this._cb_onChangeStyle.bind(this));        
        
        if( mapController.getZoomLevel() <= this.m_upperLevel &&
            mapController.getMapStyle() != MapController.Styles.BIRDS_EYE )
        {
            var menu = this.m_menuController.getMenu();
            if( menu != null && menu.collapsible)
            {
                menu.collapsible.showPane(
                    menu.Collapsible.PANELS.SearchProximity, false, false
                ); 
            }
        }   
    },
    
    loadRSS : function()
    {
        this.m_guiController.loadGeoRSS( 
            ProximityController.RSSUrl + 'lat='+ String(this.m_origin.lat) +'&lon='+ String(this.m_origin.lon), 
            this.m_proximityCollectionId, null, null, this.m_upperLevel, true, -11, -28 );
    },
    
    clear : function()
    {
        this.m_mapController.removeHotspots(this.m_proximityCollectionId);
    },
    
    _hideMenu : function(t_menu)
    {
        t_menu.$attemptInvoke(
            function()
            {
                t_menu.collapsible.hidePane(
                    t_menu.Collapsible.PANELS.SearchProximity
                );   
            }
        ); 
    },
    
    _showMenu : function(t_menu)
    {
        t_menu.$attemptInvoke(
            function()
            {
                t_menu.collapsible.showPane(
                    t_menu.Collapsible.PANELS.SearchProximity, false, false
                );   
            }
        );
    },
    
    _cb_onChangeStyle : function(t_style)
    {
        this._cb_onChangeView(null, t_style, this.m_mapController.getZoomLevel(), null, this.m_mapController.getMapProvider());
    },
    
    _cb_onChangeView : function(t_center, t_style, t_level, t_viewport, t_mapProvider)
    {
        var t_menu = this.m_menuController.getMenu();
        
        if( t_menu != null )
        {
            if( t_level <= this.m_upperLevel && 
                t_style != MapController.Styles.BIRDS_EYE && 
                t_mapProvider == "GLMap")
            {        
                 this._showMenu(t_menu);
            }
            else
            {
                this._hideMenu(t_menu);
            }
        }
    }
}
