﻿// JScript File

PolyStyle =  function(opt_opts){
    this.opt_opts = opt_opts||{};
    this.color    = this.opt_opts.color  || '#4477ff';
    this.selColor = this.opt_opts.selColor||'#ff0000';
    this.weight   = this.opt_opts.weight ||7;
    this.opacity  = this.opt_opts.opacity||0.7;
}
PolyStyle.prototype.randPolyColor = function(){
}




POPLYSTYLE1 = new PolyStyle();


GPolyline.prototype.clearListeners = function()
{
    GEvent.clearInstanceListeners(this);
}



//główna klasa służąca do wyswietlania tras
RouteDirection = function(gmap,opt_opts){
   
     opt_opts = opt_opts || {};
     this.opts = opt_opts;
     
     this._polyline   = null;
     this._bounds     = new GLatLngBounds();;
     this._arrOfStops = new Array();
     this._arrOfIds   = new Array();
     this._style      = this.opts.style || POPLYSTYLE1; 

     this._stopStyle  = this.opts.stopStyle || STOPSTYLE2;   
     this._colorEqualNr = typeof(Global)!='undefined'?((typeof(Global.VT)!='undefined'?!Global.VT.enabled:true)):true;
    
     this._map = gmap; 
     this._isdrawed = true;
     this._isLoaded = true;
     this._gpolys   = []; //tablica fragmentow polylini (od przystanku do przystanku)
    
     //semafor ładowania
     this.isLoading = false;
     this._trackCreated = false;//informacja czy trasa została utworzona
     this._isAdded = false;//informacja że przystanki i linia zostały usunięcte z trasy
    
     
     car = new GIcon();
     car.image="http://maps.google.com/mapfiles/kml/pal4/icon49.png";
     car.iconSize=new GSize(32,18);
     car.iconAnchor=new GPoint(16,9);
    
     this._carmarker = new GMarker(gmap.getCenter());
      
      //animation variables
     this._timerID = null;
     this._isAnimation = true;
     this._currDistance = 0;
     this._step = 300;  
     
     
     this.isStopsDrawed = true;
     this.isPolylineDrawed = true;
     this.isStopsLabelDrawed = false;
}

RouteDirection.isStopsDrawed = true;
RouteDirection.isPolylineDrawed = true;
RouteDirection.isStopsLabelDrawed = false;


RouteDirection.isEditable = false;
RouteDirection.isLoading = false;//zmiena pomocna przy synchronizacji wczytywania trasy - semafor
RouteDirection.arrOfStops = [];//tablica wszystkich przystanków
RouteDirection.arrOfStopsIds=[];//tablica id wszystkich przystnków


RouteDirection.prototype.setRespCont=function(divCont)//kontener w którym wywietlane będą możliwe komunikaty, np. "zakończenie aktualizacji trasy"
{
    this._infoResp = new InfoContainer(divCont);    
}
RouteDirection.prototype.clearRespCont = function()
{
    this._infoResp.clear();
}


 RouteDirection.prototype.hidePolyline = function()
{ 
   this.isPolylineDrawed = false;       
    if(this._polyline!=null){
        this._polyline.hide();
    
}
}

RouteDirection.prototype.showPolyline = function()
{
    if(this._polyline!=null){
      this.isPolylineDrawed = true;
      this._polyline.show();
    }
}
 
RouteDirection.prototype.show = function()
{
    this.showStops();
    this.showPolyline();
 
}
RouteDirection.prototype.hide = function()
{
    this.hideStops();
    this.hidePolyline();
    this._map.removeOverlay(this._carmarker);
    this.isAnimation =false;
    this.isdrawed = false;
}

RouteDirection.prototype.showStops = function()
{ 
    if(!this.isStopsDrawed){
          this.isStopsDrawed = true;
          
          for(var i=0; i<this._arrOfStops.length;++i){
            this._arrOfStops[i].show();
            if(this.isStopsLabelDrawed) this._arrOfStops[i].showLabel();
          }
          
         if(this._arrOfStops.length > 1){
            this._arrOfStops[0].showLabel();
            this._arrOfStops[this._arrOfStops.length-1].showLabel();
         } 
    }
}
RouteDirection.prototype.showStopsLabel = function()
{
   
if(!this.isStopsDrawed){this.isStopsLabelDrawed=true;  return;}
    if(!this.isStopsLabelDrawed){
        this.isStopsLabelDrawed = true;
        for(var i=0; i<this._arrOfStops.length;++i)
             this._arrOfStops[i].showLabel();
    }
}

RouteDirection.prototype.hideStopsLabel = function()
{

if(!this.isStopsDrawed)  {this.isStopsLabelDrawed = false;return;};
    
if(this.isStopsLabelDrawed ){
  this.isStopsLabelDrawed = false;
  for(var i=1; i<this._arrOfStops.length-1;++i)
    this._arrOfStops[i].hideLabel();
}
}

RouteDirection.prototype.hideStops = function()
{
if(this.isStopsDrawed ){
  this.isStopsDrawed = false;
  for(var i=0; i<this._arrOfStops.length;++i)
    this._arrOfStops[i].hide();
}
}

RouteDirection.prototype.startAnimation = function()
{ 
    if(this._polyline==null) return;
      this.isAnimation = true;
      this._currDistance = 0;
      this._map.addOverlay(this._carmarker);
      this.animate();
 
}


RouteDirection.prototype.compare = function(routeNr,trackT) //check the current track
{   
    if(this._routeNr ==null || this._trackT == null) return false;
    return (this._routeNr == routeNr) && (trackT == this._trackT);
}

RouteDirection.prototype.animate = function()
{
      var p = this._polyline.GetPointAtDistance(this._currDistance);
      this._carmarker.setPoint(p);
 
      if(this._polyline.Distance() > (this._currDistance + this._step))
          this._currDistance+=this._step;
      else{
        this.isAnimation = false;this._map.removeOverlay(this._carmarker);return;
        }
      
      var me= this;  
      
      if(this.isAnimation){
        this.timerID=window.setTimeout(function(){me.animate();},100);    
    }
}
RouteDirection.prototype.getPointBetwenStops = function(idp,wsp)
{
    var idx = this._arrOfIds[idp];
    
    //alert(idp + " " + idx);

    if(idx==null){
        throw "Nie znaleziono przystanku RouteDirection.prototype.getPointBetwenStops";
        return this._polyline.getVertex(this._polyline.getVertexCount()-1);
    }
    
    if(idx != this._arrOfStops.length - 1)
    {
        var poly = this._gpolys[idx];
        
        if(this._gpolys[idx]==null){
            return this._polyline.getVertex(this._polyline.getVertexCount()-1);
         }
        //alert(idx + " " + this._arrOfWayPoints.length + " " + this._arrOfStops.length +" " + this._gpolys.length);
    
        return poly.GetPointAtDistance(wsp * poly.getLength());
    }
    else 
        return this._polyline.getVertex(this._polyline.getVertexCount()-1);//jest na ostatnim przystanku
  
  
  //if(this._polyline != null)
  
}


RouteDirection.prototype.loaded = function(self){}

RouteDirection.prototype.getBounds = function()
{
    if(this._polyline != null)   return this._polyline.getBounds()
    else  return this._map.getBounds();
}
RouteDirection.prototype.centreMap = function() {
    if (this._polyline != null) {
        this._map.setCenter(this._polyline.getBounds().getCenter(), this._map.getBoundsZoomLevel(this._polyline.getBounds()));
    }
}

RouteDirection.prototype.newPolyline=function(points,style)
{
    if(style==null){
      var poly =  new GPolyline(points,POPLYSTYLE1.color,POPLYSTYLE1.weight,POPLYSTYLE1.opacity);
      poly.style = POPLYSTYLE1;
      return poly;
    }
    else{
      var poly =  new GPolyline(points,style.color,style.weight,style.opacity);
      poly.style = style;
      return poly;
    }
}


RouteDirection.prototype.setPolyStyle = function(poly,style)
{
    if(style==null){
      poly.setStrokeStyle({color:POPLYSTYLE1.color,weight:POPLYSTYLE1.weight,opacity:POPLYSTYLE1.opacity});
      poly.style = POPLYSTYLE1;
    }
    else{
      poly.setStrokeStyle({color:style.color,weight:style.weight,opacity:style.opacity});
      poly.style = style;
    }
}

//---- NORMAL MODE
//opts{color:kolor lini
RouteDirection.prototype.loadInNMode = function(strRoute,strTrack)
{

    if(this.isLoading) return;//ładuje się inna trasa więc należy poczekać aż proces się zakończy

    this.isLoading  = true;//zapisz info że rozpoczeło się ładowanie trasy

    this.beginLoad();//odpal funkcję informującą o rozpoczęciu ładowania trasy
    this.clearInNMode();//wyczyść starą trasę
    
    
    this._routeNr = strRoute;
    this._trackT = strTrack;
    
    var me = this;
    GDownloadUrl((typeof(PATH)!='undefined'?PATH:'')+ 'cache/graph/gr_'+strRoute+'_' + strTrack +  '.xml',function(data,kode){
    
        if(kode!=200) {me.isLoading = false;return;}

        var graph = GXml.parse(data).getElementsByTagName("Route");
        if(graph.length>0){
 
            var rcolor;
            if(me._colorEqualNr)//kolor odpowiada numerowi lini
                rcolor = graph[0].getAttribute("col");
            
            if(rcolor!=null)
                 me._style.color = rcolor; 
            
            for(var i=0;i<graph[0].childNodes.length;++i){
                var node = graph[0].childNodes[i];
                if(node.nodeType !=1) continue;
                
                var stopId = parseInt(node.getAttribute("id1"));
                var eMode = 0;
                //dodanie przystanku
                me.addMarkerInNMode(stopId,eMode);
                
                
                //dodawanie punktów pośrednich
                for(var j=1;j<node.childNodes.length;++j)//pomijam pierwszy  punkt, gdyż są to współrzędne przystanków, a te czytam z tabeli przystan
                {
                   var mnode = node.childNodes[j];
                   if(mnode.nodeType !=1) continue;
                      me.addWayPoint(parseFloat(mnode.getAttribute("y")),parseFloat(mnode.getAttribute("x")),mnode.getAttribute("gd")=="T"?0:1,false,me._arrOfStops.length - 1)
                }
                
            }
        }    
          
        me.isLoading = false;//potwiedz że dane zostały wczytane
        me.dataLoaded();//callback
    });
}     


RouteDirection.prototype.refreshInNMode = function()
{
    //gdy trasa zostałą wczytana to nie przliczaj jej ponownie
    if(this._trackCreated) return;
    
    
    var result =[];
    var tmp = [];
    var j = 0;

    for(var i=0; i< this._arrOfWayPoints.length;++i){//wybór punktów dla  części obiektów typu GDirection
        var p = this._arrOfWayPoints[i];
        
        
    if(j > 23 ){ //gdy przekroczono zakres GDirection lub jest to ostatni punkt, ale nie jedyny
                tmp.push(p.getPoint());
                result.push(tmp);
                tmp = [p.getPoint()];
                j=0;
            }
            else
                tmp.push(p.getPoint());
         ++j;
    }
  
    
     if(tmp.length>1)//wcześniej był GDirection 
     {
         result.push(tmp);
         tmp=[];
         j = 0;
     }
    
    var gDir = new GDirections();
    var _me = this;
    var isStop = false;
    var idx = 0;
    var polyPoints = [];
  
     //narazie tylko dla testów
    GEvent.addListener(gDir, "error", function() {
      throw "błąd GDirection";
      //  _me._infoResp.showText("BŁĄD, należy potwórzyć wczytywanie trasy");
    });

      
  
     GEvent.addListener(gDir, "load", function() {
     var dpoly = gDir.getPolyline();
     var points  =[];
    
     
     for(var k=0;k<gDir.getNumRoutes()-1;++k)//dziele długą polylinię na mniejsze segmenty
     {
        var r1 = gDir.getRoute(k).getStep(0).getPolylineIndex();
        var r2 = gDir.getRoute(k+1).getStep(0).getPolylineIndex();
    
        for(var c = r1;c<=r2;++c){
            points.push(dpoly.getVertex(c));
        }
        polyPoints.push(points);
        points =[];
     }
     
     //uzupełnienie
     var r1=gDir.getRoute(gDir.getNumRoutes()-1).getStep(0).getPolylineIndex();
      
      //czy ten odcinek rozpoczynany jest przez przystanek, czy przez punkt pośredni
     if(_me._arrOfWayPoints[gDir.getNumRoutes()-1].isStop) isStop = true;
     else isStop = false;

     
     
     for(var c = r1;c<dpoly.getVertexCount();++c)
        points.push(dpoly.getVertex(c));
 
        polyPoints.push(points);
        points =[];
 
     if(idx > result.length-1){//skończyło wczytywać gdirections, czas na wypełnienie brakujących polylini definiowanych ręcznie
        _me._finishLoadingInNMode(polyPoints);
       
       return;
     }
    
      //uzupełnienie    
      if(idx < result.length){
           gDir.loadFromWaypoints(result[idx]  ,{getPolyline:true,getSteps:true});
           ++idx;
      }
    
    
    });

      if(idx < result.length){
           gDir.loadFromWaypoints(result[idx]  ,{getPolyline:true,getSteps:true});
           ++idx;
      }
      
}

RouteDirection.prototype.routePartLoaded = function(polyline)
{
    
}


//ładuje odcinek pomędzy przystankami idp1 i idp2, po zakończeniu odpala metodę routePartLoaded, argumentem jest utworzona polylina
RouteDirection.prototype.loadRoutePart = function(idp1,idp2)
{
     
     var idx2 = 0;
     if(this._arrOfStops[this._arrOfIds[idp2]]== null){
        //this.routePartLoaded(new GPolyline([this._arrOfWayPoints[this._arrOfWayPoints.length-1].getPoint(),this._arrOfWayPoints[this._arrOfWayPoints.length-1].getPoint()]))
        GEvent.trigger(this,"partLoaded",new GPolyline([this._arrOfWayPoints[this._arrOfWayPoints.length-1].getPoint(),this._arrOfWayPoints[this._arrOfWayPoints.length-1].getPoint()]));
        
        
        return;
     }
     else
        var idx2 = this._arrOfStops[this._arrOfIds[idp2]].WayPointIndex;
     
     if(this._arrOfIds[idp1]==null) return;
     
     var idx1 = this._arrOfStops[this._arrOfIds[idp1]].WayPointIndex;
     
     
     if(this._gpolys[idx1]!=null){//jeśli linia jest zapisana w cache to ją załaduj
        //this.routePartLoaded(this._gpolys[idx1]);
         GEvent.trigger(this,"partLoaded",this._gpolys[idx1]);
        
        return;
     }
        
     
     
     var result =[];
     var tmp = [];
     var j = 0;

     for(var i=idx1;  i<= idx2 ;++i){//wybór punktów dla  części obiektów typu GDirection
         var p = this._arrOfWayPoints[i];
        
        
         if(j > 23 ){ //gdy przekroczono zakres GDirection lub jest to ostatni punkt, ale nie jedyny
                    tmp.push(p.getPoint());
                    result.push(tmp);
                    tmp = [p.getPoint()];
                    j=0;
                }
                else
                    tmp.push(p.getPoint());
             ++j;
        }
      
        
         if(tmp.length>1)//wcześniej był GDirection 
         {
             result.push(tmp);
             tmp=[];
             j = 0;
         }
        
        var gDir = new GDirections();
        var _me = this;
        var isStop = false;
        var idx = 0;
        var polyPoints = [];

        //narazie tylko dla testów
        GEvent.addListener(gDir, "error", function() {
            throw "błąd GDirection";
            //_me._infoResp.showText("BŁĄD, należy potwórzyć wczytywanie trasy");
        });

         GEvent.addListener(gDir, "load", function() {
         var dpoly = gDir.getPolyline();
         var points  =[];
        
         
         for(var k=0;k<gDir.getNumRoutes()-1;++k)//dziele długą polylinię na mniejsze segmenty
         {
            var r1 = gDir.getRoute(k).getStep(0).getPolylineIndex();
            var r2 = gDir.getRoute(k+1).getStep(0).getPolylineIndex();
        
            for(var c = r1;c<=r2;++c){
                points.push(dpoly.getVertex(c));
            }
            polyPoints.push(points);
            points =[];
         }
         
         //uzupełnienie
         var r1=gDir.getRoute(gDir.getNumRoutes()-1).getStep(0).getPolylineIndex();
         
         for(var c = r1;c<dpoly.getVertexCount();++c)
            points.push(dpoly.getVertex(c));
     
            polyPoints.push(points);
            points =[];
     
       
         if(idx > result.length-1){//skończyło wczytywać gdirections, czas na wypełnienie brakujących polylini definiowanych ręcznie
           _me._finishLoadingRoutePart(polyPoints,idx1);
           return;
         }
         
          //uzupełnienie    
          if(idx < result.length){
               gDir.loadFromWaypoints(result[idx]  ,{getPolyline:true,getSteps:true});
               ++idx;
          }
       });

       if(idx < result.length){
               gDir.loadFromWaypoints(result[idx]  ,{getPolyline:true,getSteps:true});
               ++idx;
       }
    

}



RouteDirection.prototype._finishLoadingRoutePart = function(polysPoints,stopIndex){
   
 
    var gpolyIdx = 0;
    var _me = this;
    var polyCluster = [];
    var polyPartsPoints = [];
    
  //  alert(_me._arrOfWayPoints.length + " " + polysPoints.length);
    
    if(_me._arrOfWayPoints.length == 0)//gdyby w trakcie wczytywania GDirection obiekt został wyczyszczony
        return;
   
   
    for(var k =0;k<_me._arrOfWayPoints.length - 1;++k){
                if(_me._arrOfWayPoints[k].editMode == 1){//jeśli to tryb ręcznego przeciągania lini
                if(k>0)     
                    p1 = polysPoints[k-1][polysPoints[k-1].length-1];//ostatni punkt wcześniejszego fragmentu
                else
                    p1 = _me._arrOfWayPoints[k].getPoint(); //pierwszy punkt obecnego fragmentu(zazwyczaj przystanek)-> pierwszy punkt całej trasy
                
                if(k + 1< polysPoints.length && _me._arrOfWayPoints[k+1].editMode != 1){
                    p3 = polysPoints[k+1][0];
                    }
                else 
                    p3 = _me._arrOfWayPoints[k+1].getPoint();
                    
                    p2 = _me._arrOfWayPoints[k].getPoint();  
                
                polyCluster = polyCluster.concat([p1,p2,p3]);
            }
            else{
                polyCluster = polyCluster.concat(polysPoints[k]);
            }
        }             
   
    var poly = new GPolyline(polyCluster);
    //zachowaj info o polylini dla optymalizacji
    _me._gpolys[stopIndex] = poly;
    
    //odpal funkjce informującą o zakończeniu wczytywania
    //_me.routePartLoaded(poly);
    
    GEvent.trigger(this,"partLoaded",poly);
       
    
}





RouteDirection.prototype._finishLoadingInNMode = function(polysPoints){
   
 
    var gpolyIdx = 0;
    var _me = this;
    var polyCluster = [];
    var polyPartsPoints = [];
   
    for(var k =0;k<polysPoints.length;++k){
                
                if(_me._arrOfWayPoints[k].editMode == 1){//jeśli to tryb ręcznego przeciągania lini
                if(k>0)     
                    p1 = polysPoints[k-1][polysPoints[k-1].length-1];//ostatni punkt wcześniejszego fragmentu
                else
                    p1 = _me._arrOfWayPoints[k].getPoint(); //pierwszy punkt obecnego fragmentu(zazwyczaj przystanek)-> pierwszy punkt całej trasy
                
                if(k + 1< polysPoints.length && _me._arrOfWayPoints[k+1].editMode != 1){
                    p3 = polysPoints[k+1][0];
                    }
                else 
                    p3 = _me._arrOfWayPoints[k+1].getPoint();
                    
                    p2 = _me._arrOfWayPoints[k].getPoint();  
                
               
                //tworze duży fragment polylini potrzebny do wyświetlania
                polyCluster = polyCluster.concat([p1,p2,p3]);
            }
            else{
                 
                 /*//tworze mały fragment polylini, potrzebny do obliczania położen pojazdów, fragment ciągnie się od przytanku do przystanku
                 if(k > 0 && _me._arrOfWayPoints[k].isStop){
                        _me._gpolys[_me._arrOfWayPoints[k].stopIndex - 1] = new  GPolyline(polyPartsPoints);
                        polyPartsPoints= [];       
                   }
                */
                
                /*
                var points = [];
                for(var j =0;j<polysPoints[k].length;++j){
                    points.push(polysPoints[k][j].point);
                    polyPartsPoints.push(polysPoints[k][j].point);
                }
                */
                polyCluster = polyCluster.concat(polysPoints[k]);
            }
             
        }             
        
              //dodanie polylini dla przedostatniego przystanku
               // _me._gpolys[_me._arrOfStops.length - 2] = new  GPolyline(polyPartsPoints);
                //polyPartsPoints= [];       
                

    this._currDistance =0;
    if (this._arrOfStops.length>0)
    {   //zmiana początkowej i końcowej ikony markera
        this._arrOfStops[0].setStyle(STOPSTYLE3);
        var flabel = this._arrOfStops.length+' ' + this._arrOfStops[this._arrOfStops.length-1].getTitle();
        var llabel = '1 '+this._arrOfStops[0].getTitle();
        this._arrOfStops[this._arrOfStops.length-1].setLabelText(flabel.replace(/ /g,'&nbsp;'));
        this._arrOfStops[this._arrOfStops.length-1].setStyle(STOPSTYLE4);
        this._arrOfStops[0].setLabelText(llabel.replace(/ /g,"&nbsp;"));
        this._carmarker.setPoint(this._arrOfStops[0].getLatLng());
    }
   
      _me._polyline = _me.newPolyline(polyCluster,_me._style);

    /*
    for (var i=0;i<this._arrOfStops.length;++i)
    {
        this._map.addOverlay(this._arrOfStops[i]);
        if(!this.isStopsDrawed) this._arrOfStops[i].hide();
        else if(this.isStopsLabelDrawed)  this._arrOfStops[i].showLabel();
    }
           
    if(this.isStopsDrawed && this._arrOfStops.length > 1){
        this._arrOfStops[0].showLabel();
        this._arrOfStops[this._arrOfStops.length-1].showLabel();
    }
    
   
     _me._map.addOverlay(_me._polyline);
    
       
    if(!this.isPolylineDrawed)
        this._polyline.hide();
*/
    
    this._trackCreated = true;
    this.addToMap();
    //odpal funkjce informującą o zakończeniu wczytywania
    _me.trackLoaded();
 }


RouteDirection.prototype.addWayPoint = function(lat,lng,editMode,isstop,stopindex){
    this._arrOfWayPoints.push({getPoint:function (){return new GLatLng(lat,lng);},editMode :editMode,isStop:isstop,stopIndex:stopindex
                               });
}

//dodanie do mapy
RouteDirection.prototype.addToMap = function()
{
    if(!this._isAdded){
    
        //alert(this.isStopsLabelDrawed);
    
        for (var i=0;i<this._arrOfStops.length;++i)
        {
            this._map.addOverlay(this._arrOfStops[i]);
            if(!this.isStopsDrawed) this._arrOfStops[i].hide();
            else if(this.isStopsLabelDrawed)  this._arrOfStops[i].showLabel();
        }
               
        if(this.isStopsDrawed && this._arrOfStops.length > 1){
            this._arrOfStops[0].showLabel();
            this._arrOfStops[this._arrOfStops.length-1].showLabel();
        }
        this._map.addOverlay(this._polyline);
        if(!this.isPolylineDrawed)
            this._polyline.hide();

       this._isAdded = true;
    }
}

//usunięcie z mapy
RouteDirection.prototype.removeFromMap = function()
{
     if(this._isAdded){
        if(this._polyline != null)
            this._map.removeOverlay(this._polyline);
        for(var i=0;i<this._arrOfStops.length;++i)
            this._map.removeOverlay(this._arrOfStops[i]);
        this._isAdded = false;
     }
     
}



RouteDirection.prototype.addMarkerInNMode=function(id,editMode)
{ 
     var marker = RouteDirection.arrOfStops[RouteDirection.arrOfStopsIds[id]];

     var p = new GLatLng(marker.getLatLng().lat()-0.000005,marker.getLatLng().lng());
     var nmarker = new Stop(p,{title: marker.getTitle(),labelText:this._arrOfStops.length+1,id: marker.getId(),stopStyle:this._stopStyle,consistRoute:true,hasLabel:true,draggable:false});
     
     nmarker.index = marker.index;
     nmarker.setRouteInfo(marker.getRouteInfo());
     nmarker.count_=marker.count_;
     nmarker.nr_  = marker.nr_;
     nmarker.nrs_ = marker.nrs_; 
     nmarker.initListeners();
     nmarker.onclick_ = marker.onclick_;
     
          
     var me = this; 
     //this._arrOfIds[marker.id_] = this._arrOfStops.length;//muszę wiedzeć który marker odpowiada id przystanku, oraz gdzie przystanek jest na lini
     //nmarker._polyIdx = this._arrOfPoints.length;
     this._arrOfStops.push(nmarker);
     this._arrOfIds[marker.id_] = this._arrOfStops.length - 1;//muszę wiedzeć który marker odpowiada id przystanku, oraz gdzie przystanek jest na lini
     nmarker.WayPointIndex = this._arrOfWayPoints.length;
     
      //dodanie WayPoints
      this.addWayPoint(nmarker.getLatLng().lat(),nmarker.getLatLng().lng(),editMode,true,this._arrOfStops.length - 1)
}


RouteDirection.prototype.clearInNMode = function(){
  
    if(this._polyline != null)
        this._map.removeOverlay(this._polyline);
    
    for(var i=0;i<this._arrOfStops.length;++i)
        this._map.removeOverlay(this._arrOfStops[i]);
        
    this._trackCreated = false;    
    
    this._arrOfStops = [];
    this._arrOfWayPoints = [];
    this._gpolys = [];
    this._arrOfIds = [];
   
    this._bounds = new GLatLngBounds();
}   

RouteDirection.prototype.lightClear = function(){
  
    if(this._polyline != null)
        this._map.removeOverlay(this._polyline);
    
    for(var i=0;i<this._arrOfStops.length;++i)
        this._map.removeOverlay(this._arrOfStops[i]);

    this._bounds = new GLatLngBounds();
}   

     
 
RouteDirection.prototype.beginLoad=function(){//zdarzenie odpalane gdy rozpocznie się ładowanie nowej trasy
} 


//zdarzenie zostaje odpalone gdy sciągnięte zostaną dane trasy
RouteDirection.prototype.dataLoaded = function(){
};
     
//zdarzenie zostaje odpalone gdy trasa została całkowicie zainicjowana
RouteDirection.prototype.trackLoaded = function(){
};
    