﻿// JScript File
RouteDirectionManager = function(map)
{
    this.Routes = [];
    this._map = map;
    
    this._currentLoading = [];
  
  // GEvent.bind(this,"load",this,function(obj,fromCache){}) 
   
    
   // $(this).bind("load",function(obj){});
   this.isStopsDrawed = true;
   this.isPolylineDrawed = true
   this.isStopsLabelDrawed = false;
}

RouteDirectionManager.prototype.addRoute= function(rdItem)
{
        this.Routes.push(rdItem);
}
RouteDirectionManager.prototype.indexOf = function(strRoute,strTrack)
{   
    for(var i = 0;i<this.Routes.length;++i){
        if(this.Routes[i]._routeNr == strRoute && this.Routes[i]._trackT == strTrack)
            return i;
    }
    return -1; 
}

RouteDirectionManager.prototype.getRoute = function(strRoute,strTrack)
{
    for(var i = 0;i<this.Routes.length;++i){
        if(this.Routes[i]._routeNr == strRoute && this.Routes[i]._trackT == strTrack)
            return this.Routes[i];
    }
    return i;
}
//usunięcie elementu RouteDirection
RouteDirectionManager.prototype.removeRoute=function(strRoute,strTrack)
{
    var idx = -1;
    for(var i = 0;i<this.Routes.length;++i){
        if(this.Routes[i]._routeNr == strRoute && this.Routes[i]._trackT == strTrack)
        {
            idx = i;
            break;
        }
    }
    if(idx!=-1)
        this.Routes.splice(idx,1);
}

RouteDirectionManager.prototype.indexOfCurLoading = function(strRoute,strTrack){
      for(var i = 0;i<this._currentLoading.length;++i){
        if(this._currentLoading[i].routeNr == strRoute && this._currentLoading[i].trackType == strTrack)
            return i;
    }
    return -1; 
}


RouteDirectionManager.prototype.loadRouteInNMode = function(strRoute,strTrack,opts){
 
        var me = this;
        //trasa istnieje
        var existIdx = this.indexOf(strRoute,strTrack);
        var options = opts || {};
        
        if(existIdx !=-1)
        {
             GEvent.trigger(me,"load",this.Routes[existIdx],true);
             return;
        }

        //trasa aktualnie się ładuje
        if(this.indexOfCurLoading(strRoute,strTrack)!=-1)
            return;
            
            
            
       var rt = new RouteDirection(TaStops.map,
            {
               style:new PolyStyle({color:options.color}),
               stopStyle: new StopStyle(
                                options.stopIcon || STOPSTYLE2.bluredIcon,
                                options.stopShineIcon|| STOPSTYLE2.focusedIcon,
                                options.stopClickedIcon|| STOPSTYLE2.clickedIcon,
                                STOPSTYLE2.iconSize,
                                STOPSTYLE2.iconAnchor,
                                STOPSTYLE2.labelClass,
                                STOPSTYLE2.labelOffset
                                )
            }
        );
        rt.isPolylineDrawed     = this.isPolylineDrawed;
        rt.isStopsDrawed        = this.isStopsDrawed;
        rt.isStopsLabelDrawed   = this.isStopsLabelDrawed;
         
         
        this._currentLoading.push({routeNr:strRoute,trackType:strTrack,vehicles:[]});
        var idx = this._currentLoading.length - 1;

        rt.dataLoaded = function(){
            me._currentLoading.splice(idx,1);
            GEvent.trigger(me,"load",rt,false);
            me.Routes.push(rt);
        }
        rt.loadInNMode(strRoute,strTrack);
}

//wczytuje trase dla pojazdu
RouteDirectionManager.prototype.loadRouteInNModeForVeh = function(veh){

        var me = this;
        //trasa istnieje
        var existIdx = this.indexOf(veh.CnrInfo.routeNr, veh.CnrInfo.trackT);
      
        if(existIdx !=-1)
        {
             GEvent.trigger(veh,"load",this.Routes[existIdx],true);
             return;
        }

        //trasa aktualnie się ładuje
        var curIdx = this.indexOfCurLoading(veh.CnrInfo.routeNr, veh.CnrInfo.trackT);
         if(curIdx != -1){
            this._currentLoading[curIdx].vehicles.push(veh);
            return;
        }
        var rt = new RouteDirection(TaStops.map,
            {
               style:new PolyStyle({color:veh.vehicleType_.routeColor}),
               stopStyle: new StopStyle(
                            veh.vehicleType_.stopIcon,
                            veh.vehicleType_.stopShineIcon,
                            veh.vehicleType_.stopClickedIcon,
                            STOPSTYLE2.iconSize,
                            STOPSTYLE2.iconAnchor,
                            STOPSTYLE2.labelClass,
                            STOPSTYLE2.labelOffset
                        )
            }
        );
      
        
        rt.isPolylineDrawed     = this.isPolylineDrawed;
        rt.isStopsDrawed        = this.isStopsDrawed;
        rt.isStopsLabelDrawed   = this.isStopsLabelDrawed;
         
        this._currentLoading.push({routeNr:veh.CnrInfo.routeNr,trackType:veh.CnrInfo.trackT,vehicles:[veh],route:rt});
        rt.Index = this._currentLoading.length - 1;
       
        rt.dataLoaded = function(){
            var idx = this.Index;
            for(var i=0;i<  me._currentLoading[idx].vehicles.length;++i){
                GEvent.trigger(me._currentLoading[idx].vehicles[i],"load",rt,false);
            }
            
            me._currentLoading.splice(idx,1);
            for(var i = idx;i<me._currentLoading.length;++i)
                me._currentLoading[i].route.Index = i;
            me.Routes.push(rt);
        }
        rt.loadInNMode(veh.CnrInfo.routeNr, veh.CnrInfo.trackT);
}



RouteDirectionManager.prototype.hideRoute = function(strRoute,strTrack)
{
    var r = this.getRoute(strRoute,strTrack);
    if(r!=null)
        r.removeFromMap();

}

RouteDirectionManager.prototype.clear = function(){
    //wyczyszczenie lini
    for(var i =0;i<this.Routes.length;++i)
        this.Routes[i].clear();

    this.Routes = [];
    this._currentLoading = [];    
    GEvent.clearInstanceListeners(this);
}


RouteDirectionManager.prototype.hideStops = function()
{
    if(this.isStopsDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].hideStops();

        this.isStopsDrawed = false;
    }

}

RouteDirectionManager.prototype.showStops = function()
{
    if(!this.isStopsDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].showStops();
        this.isStopsDrawed = true;
    }
}
RouteDirectionManager.prototype.showPolyline = function()
{
    if(!this.isPolylineDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].showPolyline();
        this.isPolylineDrawed = true;
    }
}
RouteDirectionManager.prototype.hidePolyline = function()
{
    if(this.isPolylineDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].hidePolyline();
        this.isPolylineDrawed = false;
    }
}
RouteDirectionManager.prototype.hideLabels = function()
{
    if(this.isStopsLabelDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].hideStopsLabel();
        this.isStopsLabelDrawed = false;
    }
}
RouteDirectionManager.prototype.showLabels = function()
{
    if(!this.isStopsLabelDrawed){
        for(var i=0;i<this.Routes.length;++i)
            this.Routes[i].showStopsLabel();
        this.isStopsLabelDrawed = true;
    }
}
