<!--

var nPause    =  20;
var nRePause  =  20;
var nMxPause  = 125;

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}


function Route_Points( ndx ){

// ndx is Zero Based while MX_Ndx is the Number of Point Object
// For example, 50 point objects, ndx = 0,1,2,...49 and MX_Ndx=50

    if( ndx >= MX_Ndx  ){
        map.removeOverlay( standby_overlay );
        return;
    }

    var prv_latlng = null;

    var bb_Avoid_Hwys   = 0;
    var bb_Walking      = 0;
    var b_Follow_Roads  = 0;

    var poly_pts = new Array(2);

    var xlat   = Route_MX[ndx].mlat;
    var xlng   = Route_MX[ndx].mlng;
    var latlng = new GLatLng( xlat, xlng, true ) ;
    var marker = new GMarker( latlng );

    if( ndx != 0 ){
        xlat = Route_MX[ndx-1].mlat;
        xlng = Route_MX[ndx-1].mlng;
        prv_latlng = new GLatLng( xlat, xlng, true ) ;
    }

    nPoints = ndx + 1;
    display_route_npoints();
    
    obj_Map_UI = new Map_Data_UI( nPoints, marker, 0, null, 1, 1, prv_latlng );
        
    if( ndx == 0 ){
        remove_jump_marker();
        map.addOverlay( obj_Map_UI.mmarker );
        obj_Map_UI.mmarker.value = nPoints;
        Map_UI[ndx] = obj_Map_UI;
        Route_Points( ndx+1 );
    }else{
        iOpts         = Route_MX[ndx].mopts;
        bb_Avoid_Hwys = Route_MX[ndx].mHwy;
        bb_Walking    = Route_MX[ndx].mWalk;

        if( iOpts == 1 ){// iOpts == 1 => Follow Roads is TRUE
          //Follow_Roads......
            prv_latlng = obj_Map_UI.mLxL_Prv;
                latlng = obj_Map_UI.mLxL;

            if( bb_Walking == 1 )tmode = G_TRAVEL_MODE_WALKING;
            else                 tmode = G_TRAVEL_MODE_DRIVING;

            var dir_Panel = document.getElementById("walking_id");
            var dir = new google.maps.Directions( null, dir_Panel );
            //  dir = new google.maps.Directions( );

            dir.obj_Map_UI = obj_Map_UI;
            dir.latlng     = latlng;
            dir.ndx        = ndx;

            dir.loadFromWaypoints( [ prv_latlng, latlng ],
            {preserveViewport:true,
             getPolyline:true,
             getSteps:true,
             travelMode:tmode,
             avoidHighways:bb_Avoid_Hwys } );

            google.maps.Event.addListener( dir, "load", function() {
                    map.addOverlay( dir.getPolyline() );
                    dir.obj_Map_UI.mpoly = dir.getPolyline();
                    dir.obj_Map_UI.mopts = 1;
                    plength += dir.getPolyline().getLength();
                    
                    Map_UI[dir.ndx] = dir.obj_Map_UI;
                    
                    display_distance( plength );
    
                    pausecomp(nPause);
                    
                    nndx = dir.ndx;                                        
                    Route_Points( nndx+1 );
                    
                    if( b_Markers_ON == 1 ){
                        Dist_Marks( 0, ndx, dir.getPolyline(), 0 );
                        Dist_Marks( 1, ndx, dir.getPolyline(), 0 );
                    }
            });

            google.maps.Event.addListener( dir, "error", function() {
                var code   = dir.getStatus().code;

                //var reason = ''
                //    reason+= 'Code '   + code;
                //    reason+= '... LxL:'+ dir.latlng;
                //    reason+= ' (ndx='  + dir.ndx;
                //    reason+= ')';

                //document.getElementById("walking_id").innerHTML = reason;
                //alert('Error: Code = '+reason );

                nPause += 5;
                if( nPause > 125 )nPause = nRePause;
                Route_Points( dir.ndx );

            });
        }else{
          //Follow_Points......
            poly_pts[0] = obj_Map_UI.mLxL_Prv;
            poly_pts[1] = obj_Map_UI.mLxL;
            var polyline = new google.maps.Polyline( poly_pts );
            obj_Map_UI.mpoly = polyline;
            obj_Map_UI.mopts = 0;
            obj_Map_UI.mHwy  = 0;
            map.addOverlay( polyline );
            plength += polyline.getLength();
            display_distance( plength );
            Map_UI[ndx] = obj_Map_UI;
            Route_Points( ndx+1 );
            
            if( b_Markers_ON == 1 ){
                Dist_Marks( 0, ndx, polyline, 0 );
                Dist_Marks( 1, ndx, polyline, 0 );
            }
        }
    }
    
    display_start_button( nPoints );
}
//-----------------------------------------------------------------------------
function Re_Route_Points( ndx ){

// ndx is Zero Based while Map_UI.length is the Number of Point Objects
// For example for 50 point objects, ndx = 0,1,2,...49 and MX_Ndx=50
    
    if( ndx >= Map_UI.length  ){
        map.removeOverlay( standby_overlay );
        return;
    }
            
    var prv_latlng     = null;
    var bb_Avoid_Hwys  = 0;
    var b_Follow_Roads = 0;

    var poly_pts = new Array(2);

    var latlng = Map_UI[ndx].mLxL;
    var marker = Map_UI[ndx].mmarker;

    if( ndx != 0 ){
        prv_latlng = Map_UI[ndx].mLxL_Prv;
    }

    nPoints = ndx + 1;
    display_route_npoints();
          
    if( ndx == 0 ){    
        Reset_Direction_Start( latlng );
        remove_jump_marker();
        map.addOverlay( Map_UI[ndx].mmarker );
        Re_Route_Points( ndx+1 );
    }else{
        bb_Avoid_Hwys = Map_UI[ndx].mHwy;
        iOpts         = Map_UI[ndx].mopts;

        if( iOpts == 1 ){// iOpts == 1 => Follow Roads is TRUE
          //Follow_Roads......
          
            prv_latlng = Map_UI[ndx].mLxL_Prv;
                latlng = Map_UI[ndx].mLxL;

            bb_Walking = false;

            if( bb_Walking )tmode = G_TRAVEL_MODE_WALKING;
            else            tmode = G_TRAVEL_MODE_DRIVING;
            
            tmode = G_TRAVEL_MODE_WALKING;

            var dir_Panel = document.getElementById("walking_id");
            var dir = new google.maps.Directions( null, dir_Panel );
            //  dir = new google.maps.Directions( );


            dir.latlng     = latlng;
            dir.ndx        = ndx;

            dir.loadFromWaypoints( [ prv_latlng, latlng ],
            {preserveViewport:true,
             getPolyline:true,
             getSteps:true,
             travelMode:tmode,
             avoidHighways:bb_Avoid_Hwys } );

            google.maps.Event.addListener( dir, "load", function() {
                    map.addOverlay( dir.getPolyline() );                   
                    Map_UI[dir.ndx].mpoly = dir.getPolyline();
                    Map_UI[dir.ndx].mopts = 1;
                    plength += dir.getPolyline().getLength(); 
                    
                    display_distance( plength );
    
                    pausecomp(nPause);
                    
                    nndx = dir.ndx;                      
                    Sx_Route_Steps( nndx, dir );                                      
                    Re_Route_Points( nndx+1 );
                    
                    if( b_Markers_ON == 1 ){
                        Dist_Marks( 0, ndx, dir.getPolyline(), 0 );
                        Dist_Marks( 1, ndx, dir.getPolyline(), 0 );
                    }
            });

            google.maps.Event.addListener( dir, "error", function() {
                var code   = dir.getStatus().code;

                //var reason = ''
                //    reason+= 'Code '   + code;
                //    reason+= '... LxL:'+ dir.latlng;
                //    reason+= ' (ndx='  + dir.ndx;
                //    reason+= ')';

                //document.getElementById("walking_id").innerHTML = reason;
                //alert('Error: Code = '+reason );

                nPause += 5;
                if( nPause > 125 )nPause = nRePause;
                Re_Route_Points( dir.ndx );

            });
        }else{
          //Follow_Points......
            poly_pts[0] = Map_UI[ndx].mLxL_Prv;
            poly_pts[1] = Map_UI[ndx].mLxL;
            var polyline = new google.maps.Polyline( poly_pts );
            Map_UI[ndx].mpoly = polyline;
            Map_UI[ndx].mopts = 0;
            Map_UI[ndx].mHwy  = 0;
            map.addOverlay( polyline );
            plength += polyline.getLength();
            display_distance( plength );
            Re_Route_Points( ndx+1 );
            
            if( b_Markers_ON == 1 ){
                Dist_Marks( 0, ndx, polyline, 0 );
                Dist_Marks( 1, ndx, polyline, 0 );
            }
        }
    } 
    
}
//-----------------------------------------------------------------------------
// -->
