﻿// Maps initialiser for Information Systems (IS) like Friss and Traffic Scotland

//GLOBAL IS BASE
var _is_mapController= null;
var _is_layerController = null;

//GLOBAL IS AUX
var _global_is_ActiveLayersSession = null;
var _global_is_navigateToPi = null;
var _global_is_AjaxMapsSession = null;

//implementation of layercontroller listener 
function _is_LayerControllerListener()
{
    this.onMarkerMouseOver = function(layer, marker, pointitem)
    {
        //alert(pointitem.Description);
        try
        {
          toolTip(pointitem.Name, pointitem.Description, "black", "#7592AE", "#E3E4ED");        
        }
        catch(e)
        {
          alert(e);
        }
    }
        
    this.onMarkerMouseOut = function(layer, marker, pointitem)
    {
        toolTip();
    }
    
    this.onMarkerClick = function(layer, marker, pointitem)
    {
        var redirUrl = "mapdetails.aspx?id=" + pointitem.Id;
        document.location = redirUrl;
    }
    
    this.onLayerActivateComplete = function(layer)
    {
        _global_is_ActiveLayersSession.addActiveLayer(layer.layerId);
    }
    
    this.onLayerRemoveComplete = function(layer)
    {
        _global_is_ActiveLayersSession.removeActiveLayer(layer.layerId);       
    }
}


function _is_MapControllerListener()
{
    this.onZoom = function(zoomlevel)
    {
        try
        {
          _global_is_AjaxMapsSession.setZoomLevel(zoomlevel);
        }
        catch(e_setzoom)
        {
            alert(e_setzoom);
        }
    }
    
    this.onMove = function(centrex, centrey)
    {
        try
        {
          _global_is_AjaxMapsSession.setCoords(centrex, centrey);
        }
        catch(e_move)
        {
           alert(e_move);
        }
    }
}


//LOADER ENTRY POINT
function init_is_loadmap()
{
    initToolTips();
    
    _global_is_AjaxMapsSession = ibi.friss.web.ajaxnet.AjaxMapsSession;
    var gp_res = _global_is_AjaxMapsSession.getParameters();
    if ( gp_res.value == null)
        throw "can not obtain initial parameters";
        
    var initParams = gp_res.value;                                  

    var mapCtlEL = new _is_MapControllerListener();
    _is_mapController = new GoogleMapController("_is_mapController", mapCtlEL);    
    _is_mapController.recenterAndZoom(initParams.X, initParams.Y, initParams.ZoomLevel);
                
    _is_layerController = new LayerController
   (
        "_is_layerController",
        ibi.friss.web.ajaxnet.AjaxActiveLayers,
        _is_mapController,
        null, null
   );
   
   //Registering interest in events on this layer controller
   var listener = new _is_LayerControllerListener();
   _is_layerController.objLayerControllerEventListener = listener;   
   
   
   //Initialising progress bar 
   var pb = new IbiDivProgressBar("pb_inner", "pb_outer");
   _is_layerController.objProgressNotificator = pb;
   
   
   //Activating layers that were not activated before
   var restoreActivated = new _is_RestoreActivated();
   restoreActivated.restore();
}

//CLASSTOOL TO ACTIVATE ALL THE SAVED LAYERS
function _is_RestoreActivated()
{     
    this.SVarname = "_global_is_RestoreActivated";
    
    this._p_arr_activated = new Array();
    this._p_curr_idx = 0;
       
    this.objProgressListener = null;
    
    this._p_processLayer = function()
    {
         var currlayerid = this._p_arr_activated[this._p_curr_idx];
        _is_layerController.activateLayer(currlayerid);       
    }
        
    //ENTRY FUNCTION
    this.restore = function()
    {  
       _global_is_ActiveLayersSession = ibi.friss.web.ajaxnet.AjaxActiveLayersSession;
       var res = _global_is_ActiveLayersSession.getCurrentActiveLayers();
       if ( res.value == null || res.value.length == 0)
            return; //nothing to activate
            
       this._p_arr_activated = res.value;    
       this._p_curr_idx = 0;
       
       //changing up progress listener
       this.objProgressListener = _is_layerController.objProgressNotificator;
       _is_layerController.objProgressNotificator = this;
       this._p_processLayer();
    }                       
    
    //Progress 
    this.setSoFar = function(sofar)
    {
        if ( this.objProgressListener != null) 
            this.objProgressListener.setSoFar(sofar);
    }       
        
    this.setTotal = function(total)
    {
        if ( this.objProgressListener != null) 
            this.objProgressListener.setTotal(total);
    }
    
    this.setComplete = function()
    {
        if ( this.objProgressListener != null) 
          this.objProgressListener.setComplete();

        //giving directions to process another layer
            
        if ( this._p_curr_idx < this._p_arr_activated.length - 1)
        {
            this._p_curr_idx ++;
            this._p_processLayer();
        }
        else
        { //that's it     
           _is_layerController.objProgressNotificator =  this.objProgressListener;
           
           //Check if have to navigate to some special point
           if ( _global_is_navigateToPi != null)
           {
                //navigate there 
                 var res = ibi.friss.web.ajaxnet.AjaxPointItemFactory.getPointItem( _global_is_navigateToPi );                
                 if ( res.value != null)
                 {
                    var navigatePi = res.value;
                    _is_mapController.recenterAndZoom(navigatePi.X, navigatePi.Y, 11);
                 }
           }
        }
                       
    }
    
    this.isInProgress = function()
    {
        if ( this.objProgressListener != null) 
            return this.objProgressListener.isInProgress();
                                                    
    }
} //End Class RestoreActivated



//ACTIVATE LAYER EVENT HANDLER
function _is_ActivateLayer(layerId, ctl)
{        
    var isActive = ctl.checked;

    if ( isActive )
    {
        _is_layerController.activateLayer(layerId);
    }
    else
    {
        //_is_layerController.removeLayer(layerId);
        var layer = _is_layerController.getLayer(layerId);
        if ( layer != null)
            layer._blocking_remove();
    }
}
