// Map speciffic globals
var InteliusVEMap = new _InteliusVEMap();

function VECallbackFunction()
{
    alert ( 'CB' );
}

function _addressLoactedCallback (ShapeLayer,FindResult,Place,HasMore)
{
    InteliusVEMap._virtualEarthFoundLatLongForAddress (ShapeLayer,FindResult,Place,HasMore);
}

function _locatedDirectionsCallback( route )
{
    InteliusVEMap._populateDrivingDirections ( route );
}

function _InteliusVEMap()
{
    this._map               = null;
    this._addresses         = null;
    this._locations         = null;
    
    this._count             = 0;
    this._currentIndex      = 0;
    this._idCount           = 0;
    
    this._dashboardSize     = null;
    this._dashboardVisible  = null;
    this._initialZoomLevel  = null;
    this._initialStyle      = null;
    
    this._needToLoadDriving = false;
    this._distanceUnit      = null;
    this._divNameForDirections = null;
    this._mapHasLoaded      = false;
    
    
    this.MapHasBeenLoaded = function()
    {
        return this._mapHasLoaded;
    }
    
    this.SetDivNameForDrivingDirections = function( divName )
    {
        this._divNameForDirections = divName;
        this._needToLoadDriving = true;
    }
    
    this.InitMap = function( divName )
    {
        if ( typeof divName != 'string' )
        {
            divName = divName.toString();
        }
        
        this._map           = new VEMap(divName);
        this._addresses     = new Array();
        this._locations     = new Array();
        this._currentIndex  = 0;
        this._count         = 0;
        
        this._dashboardSize     = VEDashboardSize.Normal;
        this._dashboardVisible  = true;
        this._initialZoomLevel  = 3;
        this._initialStyle      = VEMapStyle.Road;
    }
    
    this.LoadMap = function( dashboardSize, dashboardVisible, initialZoomLevel, initialStyle ) 
    {
        if ( dashboardSize )    this._dashboardSize     = dashboardSize;
        if ( dashboardVisible ) this._dashboardVisible  = dashboardVisible;
        if ( initialZoomLevel ) this._initialZoomLevel  = initialZoomLevel;
        if ( initialStyle )     this._initialStyle      = initialStyle;
        
        if ( this._map )
        {
            this._map.SetDashboardSize( dashboardSize );
            this._map.LoadMap( null, initialZoomLevel, initialStyle );
            if ( dashboardVisible == false )
            {
                this._map.HideDashboard();
            }
            /*
            if ( this._count > 0 )
            {
                for ( i = 1; i <= this._count; i++ )
                {
                    if ( this._addresses[i].Latitude && this._addresses[i].Longitude )
                    {
                       this.AddPushpin( i, 
                                        this._addresses[i].Latitude, 
                                        this._addresses[i].Longitude,
                                        this._addresses[i].Icon,
                                        this._addresses[i].Name,
                                        this._addresses[i].Description );
                    }
                    else
                    {
                        this._currentIndex = i;
                        
                        // alert ( this._addresses[i].Address );
                        this._map.Find( "", 
                                        this._addresses[i].Address,  
                                        VEFindType.Businesses, 
                                        null, 
                                        0, 
                                        1, 
                                        false, 
                                        false, 
                                        false, 
                                        false, 
                                        LocatedAddressCallback);
                        
                    }
                }
            }*/
            
            this._startAddingPushpinsForAddresses();
        }
        
        this._mapHasLoaded = true;
    }
    
    
    this._startAddingPushpinsForAddresses = function()
    {
        if ( this._count > 0 )
        {
            this._currentIndex = 1;
            this._addNextPushpin();
        }
    }
    
    
    this._addNextPushpin = function()
    {   
        index = this._currentIndex;
        if ( this._addresses[index].Latitude && this._addresses[index].Longitude )
        {
            this._addPushpinDirectlyFromLatLong();
        }
        else
        {
            this._addPushpinFromStreetAddress()
        }
    }
    
    this._finishedAddingPushpin = function()
    {
        this._currentIndex++
        
        if ( this._currentIndex <= this._count )
        {
            this._addNextPushpin();
        }
        else
        {
            if ( this._count > 1 )
            {
                if ( this._needToLoadDriving )
                {
                    this._loadDrivingDirections()
                }
            }
        }
    }
    
    
    this._addPushpinDirectlyFromLatLong = function()
    {
        this._idCount++;
        
        var index       = this._currentIndex;
        var pinID       = this._idCount; 
        var latitude    = this._addresses[index].Latitude;
        var longitude   = this._addresses[index].Longitude;
        var icon        = this._addresses[index].Icon;
        var name        = this._addresses[index].Name;
        var description = this._addresses[index].Description;
        
        var loc     = new VELatLong( latitude, longitude );
        
	    var pin     = new VEPushpin(
	       pinID, 
	       loc, 
	       icon, 
	       name, 
	       description
	    );
    	
	    VEPushpin.ShowDetailOnMouseOver = false;            
	    VEPushpin.OnMouseOverCallback =  function(x, y, name, details)
	    {	
		    onMouseover=ddrivetip(details, '200', '', 3);
	    }
	    
	    // Show all pushpins on the map
	    
	        this._map.AddPushpin(pin);
	        this._map.SetMapView(new Array( loc ));
	        this._map.SetZoomLevel(this._initialZoomLevel);
	    
	    
	    
	    //this._locations[index] = loc;
	    
	    //alert ( this._locations );
	    
	    //this._map.AddPushpin(pin);
	    //this._map.SetMapView(this._locations);
	    
	    //if ( index == 1 )
	    //{
	    //    this._map.SetZoomLevel(this._initialZoomLevel);
	    //}
	    
	    //this._map.AddPushpin(pin);
	    //this._map.SetMapView(new Array( loc ));
	    //this._map.SetZoomLevel(this._initialZoomLevel);
	    
	    this._finishedAddingPushpin();
	}
	
	this._showAllPushpins = function()
	{
	    var locs = new Array();
        var i = 1;
       
        for (i=1; i<=(this._count); i++ )
        {   
            locs[i] = new VELatLong(    this._addresses[i].Latitude, 
                                        this._addresses[i].Longitude );
        }
        
        this._map.SetMapView(locs);
         
	}
	
	this._addPushpinFromStreetAddress = function()
	{
	    var index       = this._currentIndex;
        var address     = this._addresses[index].Address;
        
        this._map.Find( "", 
                        address,  
                        VEFindType.Businesses, 
                        null, 
                        0, 
                        1, 
                        false, 
                        false, 
                        false, 
                        false, 
                        _addressLoactedCallback);
    }
    
    this._virtualEarthFoundLatLongForAddress = function(ShapeLayer,FindResult,Place,HasMore)
    {
        var index       = this._currentIndex;
        
        this._addresses[index].Latitude     = Place[0].LatLong.Latitude;
        this._addresses[index].Longitude    = Place[0].LatLong.Longitude;
        
        this._addPushpinDirectlyFromLatLong();
    }
    
    
    this._loadDrivingDirections = function()
    {
        divName  = this._divNameForDirections;
        
        if ( this._count == 2 )
        {
            var locations = new Array(
                new VELatLong(  this._addresses[1].Latitude, 
                                this._addresses[1].Longitude ),
                new VELatLong(  this._addresses[2].Latitude, 
                                this._addresses[2].Longitude ));
            
            
            var options = new VERouteOptions();
            options.SetBestMapView = true; // Change this... don't we want the map to refresh?
            options.RouteCallback  = _locatedDirectionsCallback;
            options.DistanceUnit   = VERouteDistanceUnit.Mile;
                        
            this._map.GetDirections(locations, options);
        }
    }
    
    
    this._populateDrivingDirections = function( route )
    {
        var turns = "<h3>Turn-by-Turn Directions</h3>(rounding errors are possible)";
        turns += "<p><b>Total Distance:</b> " + route.Distance.toFixed(1) + " miles";

        turns += "<br/><b>Total Time:</b> " + GetTime(route.Time) + "</p>";

        if (1)
        {
           // Unroll route and populate DIV
           var legs          = route.RouteLegs;
           var leg           = null;
           var turnNum       = 0;  // The turn #
           
           turns += '<table width="100%" cellspacing="0" cellpadding="3" border="0">';
            turns += '<tr background="/images/gr_blue.gif"><td background="/images/gr_blue.gif"><b>Step</b></td><td background="/images/gr_blue.gif"><b>Directions</b></td><td background="/images/gr_blue.gif"><b>Distance</b></td><td background="/images/gr_blue.gif"><b>Time</b></td></tr>';


           // Get intermediate legs
           for(var i = 0; i < legs.length; i++)
           {
              // Get this leg so we don't have to derefernce multiple times
              leg = legs[i];  // Leg is a VERouteLeg object

              var legNum = i + 1;
              //turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
              //         "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

              // Unroll each intermediate leg
              var turn        = null;  // The itinerary leg
              var legDistance = null;  // The distance for this leg
              
              for(var j = 0; j < leg.Itinerary.Items.length; j ++)
              {
                 turns += '<tr bgcolor="#ffffff">';
                 turnNum++;
                 
                 turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object

                 //turns += "<b>" + turnNum + "</b>\t" + turn.Text;
                   turns += '<td style="border-bottom:solid 1px #cccccc; border-left:solid 1px #cccccc; border-right:solid 1px #cccccc;">' + turnNum + '</td>';
                   turns += '<td style="border-bottom:solid 1px #cccccc; border-right:solid 1px #cccccc;">' + turn.Text + '</td>';
                    
                 legDistance    = turn.Distance;

                 // So we don't show 0.0 for the arrival
                 if(legDistance > 0)
                 {
                    // Round distances to 1/10ths
                    //turns += " (" + legDistance.toFixed(1) + " miles";
                    turns += '<td style="border-bottom:solid 1px #cccccc; border-right:solid 1px #cccccc;">' + legDistance.toFixed(1) + ' miles</td>';
                    
                    // Append time if found
                    if(turn.Time != null)
                    {
                       //turns += "; " + GetTime(turn.Time);
                       turns += '<td style="border-bottom:solid 1px #cccccc; border-right:solid 1px #cccccc;">' + GetTime(turn.Time) + '</td>';
                    }

                    //turns += "</tr>";
                 }
                 else
                 {
                    turns += '<td style="border-bottom:solid 1px #cccccc; border-right:solid 1px #cccccc;"> &nbsp; </td><td style="border-bottom:solid 1px #cccccc; border-right:solid 1px #cccccc;"> &nbsp; </td>';
                 }
              }

              //turns += "<br/>";
              turns += "</tr>";
           }
           
           turns += "</table>";
           
           
            // Populate DIV with directions
            if ( this._divNameForDirections )
            {
                var d = document.getElementById( this._divNameForDirections );
                d.innerHTML = turns;
            } 
        }
    }
    
    
    
    
    
    
    
    
    
    this.LoactedAddress= function( ShapeLayer,FindResult,Place,HasMore)
    {
        if ( this._currentIndex )
        {
            //alert ( this._addresses[ this._currentIndex ].Address );
            
            //alert ( Place.Name );
            
            
            this._addresses[ this._currentIndex ].Latitude = Place[0].LatLong.Latitude;
            this._addresses[ this._currentIndex ].Longitude = Place[0].LatLong.Longitude;
            
            
            this.AddPushpin(this._currentIndex, 
                            this._addresses[ this._currentIndex ].Latitude, 
                            this._addresses[ this._currentIndex ].Longitude,
                            this._addresses[ this._currentIndex ].Icon,
                            this._addresses[ this._currentIndex ].Name,
                            this._addresses[ this._currentIndex ].Description );
                            

        }
	    
	}
	
	
	this.AddPushpin = function( pinID, latitude, longitude, icon, name, description )
    {
        loc = new VELatLong( latitude, longitude );
        
	    var pin = new VEPushpin(
	       pinID, 
	       loc, 
	       icon, 
	       name, 
	       description
	    );
    	
	    VEPushpin.ShowDetailOnMouseOver = false;            
	    VEPushpin.OnMouseOverCallback =  function(x, y, name, details)
	    {	
		    onMouseover=ddrivetip(details, '200', '', 3);
	    }		
	    this._map.AddPushpin(pin);
	    this._map.SetMapView(new Array( loc ));
	    this._map.SetZoomLevel(this._initialZoomLevel);
	}
	
	this.AddAddress = function( address, description, name, icon )
    {
        this.AddAddressWithLatLong( address, null, null, description, name, icon );
    }
    
    
    this.AddAddressWithLatLong = function( address, latitude, longitude, description, name, icon )
    {
        if ( this._addresses == null )
        {
            this._addresses = new Array();
        }
        
        this._count++;
        
        this._addresses [ this._count ] = new _MapAddress( address, description, name, icon, latitude, longitude );
        
        /*
        
        this._addresses [ this._count ] = {
            Address:        address,
            Description:    description,
            Name:           name,
            Icon:           icon,
            Latitude:       latitude,
            Longitude:      longitude
        
        }
        */
    }
    
    
    this.LoadDrivingDirections = function( divName ) 
    {
        var locations = new Array();
        
        this._divNameForDirections = divName;
        
        
        if ( this._count == 2 )
        {
            var locations = new Array(
                new VELatLong(  this._addresses[1].Latitude, 
                                this._addresses[1].Longitude ),
                new VELatLong(  this._addresses[2].Latitude, 
                                this._addresses[2].Longitude ));
            
            alert('Setting driving directions...');
            alert(_locatedDirectionsCallback);
            
            var options = new VERouteOptions();
            options.SetBestMapView = false; // Change this... don't we want the map to refresh?
            options.RouteCallback  = _addressLoactedCallback;
            options.DistanceUnit   = VERouteDistanceUnit.Mile;
            options.ShowDisambiguation = true;  //wha?
            
            alert ( options );
            
            this._map.GetDirections(locations, options);
        }
        
    }

    
    this.PopulateDrivingDirections = function( route )
    {
        var turns = "<h3>Turn-by-Turn Directions</h3>(rounding errors are possible)";

        turns += "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

        turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";

        if (1)
        {
           // Unroll route and populate DIV
           var legs          = route.RouteLegs;
           var leg           = null;
           var turnNum       = 0;  // The turn #

           // Get intermediate legs
           for(var i = 0; i < legs.length; i++)
           {
              // Get this leg so we don't have to derefernce multiple times
              leg = legs[i];  // Leg is a VERouteLeg object

              var legNum = i + 1;
              turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
                       "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

              // Unroll each intermediate leg
              var turn        = null;  // The itinerary leg
              var legDistance = null;  // The distance for this leg
              
              for(var j = 0; j < leg.Itinerary.Items.length; j ++)
              {
                 turnNum++;
                 
                 turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object

                 turns += "<b>" + turnNum + "</b>\t" + turn.Text;

                 legDistance    = turn.Distance;

                 // So we don't show 0.0 for the arrival
                 if(legDistance > 0)
                 {
                    // Round distances to 1/10ths
                    turns += " (" + legDistance.toFixed(1) + " miles";

                    // Append time if found
                    if(turn.Time != null)
                    {
                       turns += "; " + GetTime(turn.Time);
                    }

                    turns += ")<br/>";
                 }
              }

              turns += "<br/>";
           }

           // Populate DIV with directions
           this.PopulateTurnByTurn( turns );
        }
    }
    
    this.PopulateTurnByTurn = function( directions )
    {
        if ( this._divNameForDirections )
        {
            var d = document.getElementById( this._divNameForDirections );
            d.innerHTML = directions;
        }
    }
    
}

function LocatedAddressCallback( ShapeLayer,FindResult,Place,HasMore )
{
    alert ( 'LocatedAddressCallback' );
    InteliusVEMap.InteliusVEMap( ShapeLayer,FindResult,Place,HasMore);
}

function LocatedDrivingDirectionsCallback( route )
{
    alert ( 'LocatedDrivingDirectionsCallback' );
    InteliusVEMap.PopulateDrivingDirections( route );
}
    
function _MapAddress( address, description, name, icon, latitude, longitude )
{
    this.Address = address;
    this.Description = description;
    this.Name = name;
    this.Icon = icon;
    this.Latitude = latitude;
    this.Longitude = longitude;
    
    /*
    this.Address = address;
    this.Description = description;
    this.Name = name;
    this.Icon = icon;
    this.Latitude = latitude;
    this.Longitude = longitude;
    */
    
}

function GetTime (time)
{
    if(time == null)
    {
       return("");
    }

    if(time > 60)
    {                                 // if time == 100
       var seconds = time % 60;       // seconds == 40
       var minutes = time - seconds;  // minutes == 60
       minutes     = minutes / 60;    // minutes == 1


       if(minutes > 60)
       {                                     // if minutes == 100
          var minLeft = minutes % 60;        // minLeft    == 40
          var hours   = minutes - minLeft;   // hours      == 60
          hours       = hours / 60;          // hours      == 1

          return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
       }
       else
       {
          return(minutes + " minutes, " + seconds + " seconds");
       }
    }
    else
    {
       return(time + " seconds");
    }
}
    

	
	
    
    
    

/*



var InteliusVEMap = {

    _map:           null,
    _addresses:     null,
    _locations:     null,
    
    _count:         0,
    _currentIndex:  0,
    
    _dashboardSize:     null,
    _dashboardVisible:  null,
    _initialZoomLevel:  null,
    _initialStyle:      null,
    
    _distanceUnit:      null,
    
    _divNameForDirections: null,
    
    // --------------------------------------------------------------------------------------
    InitMap: function( divName )
    {
        alert('InitMap called');
        if ( typeof divName != 'string' )
        {
            divName = divName.toString();
        }
        
        this._map           = new VEMap(divName);
        this._addresses     = new Array();
        this._locations     = new Array();
        this._currentIndex  = 0;
        this._count         = 0;
        
        this._dashboardSize     = VEDashboardSize.Normal;
        this._dashboardVisible  = true;
        this._initialZoomLevel  = 3;
        this._initialStyle      = VEMapStyle.Road;
        
         
    
    },
    
    // --------------------------------------------------------------------------------------
    LoadMap: function( dashboardSize, dashboardVisible, initialZoomLevel, initialStyle ) 
    {
        alert('LoadMap called');
        if ( dashboardSize )    this._dashboardSize     = dashboardSize;
        if ( dashboardVisible ) this._dashboardVisible  = dashboardVisible;
        if ( initialZoomLevel ) this._initialZoomLevel  = initialZoomLevel;
        if ( initialStyle )     this._initialStyle      = initialStyle;
        
        if ( this._map )
        {
            this._map.SetDashboardSize( dashboardSize );
            this._map.LoadMap( null, initialZoomLevel, initialStyle );
            if ( dashboardVisible == false )
            {
                this._map.HideDashboard();
            }
            
            if ( this._count > 0 )
            {
                for ( i = 1; i <= this._count; i++ )
                {
                    if ( this._addresses[i].Latitude && this._addresses[i].Longitude )
                    {
                       this.AddPushpin( i, 
                                        this._addresses[i].Latitude, 
                                        this._addresses[i].Longitude,
                                        this._addresses[i].Icon,
                                        this._addresses[i].Name,
                                        this._addresses[i].Description );
                    }
                    else
                    {
                        
                        this._currentIndex = i;
                        this._map.Find( "", 
                                        this._addresses[i].Address,  
                                        VEFindType.Businesses, 
                                        null, 
                                        0, 
                                        1, 
                                        false, 
                                        false, 
                                        false, 
                                        false, 
                                        this.LoactedAddress);
                        
                    }
                }
            }
        }
    },
    
    
    
    // --------------------------------------------------------------------------------------
    LoactedAddress: function( ShapeLayer,FindResult,Place,HasMore)
    {
        
        if ( InteliusVEMap._currentIndex )
        {
            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Latitude = Place[0].LatLong.Latitude;
            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Longitude = Place[0].LatLong.Longitude;
            
            InteliusVEMap.AddPushpin(InteliusVEMap._currentIndex, 
                            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Latitude, 
                            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Longitude,
                            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Icon,
                            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Name,
                            InteliusVEMap._addresses[ InteliusVEMap._currentIndex ].Description );

        }
	    //AddPushpin(locCount++, Place[0].LatLong.Latitude, Place[0].LatLong.Longitude, Icon[(CurrIndex-1)], Name[(CurrIndex-1)], Description[(CurrIndex-1)], "", "", "");
	    
	},
	          
    // --------------------------------------------------------------------------------------
    AddPushpin: function( pinID, latitude, longitude, icon, name, description )
    {
        loc = new VELatLong( latitude, longitude );
        
	    var pin = new VEPushpin(
	       pinID, 
	       loc, 
	       icon, 
	       name, 
	       description
	    );
    	
	    VEPushpin.ShowDetailOnMouseOver = false;            
	    VEPushpin.OnMouseOverCallback =  function(x, y, name, details)
	    {	
		    onMouseover=ddrivetip(details, '200', '', 3);
	    }		
	    this._map.AddPushpin(pin);
	    this._map.SetMapView(new Array( loc ));
	    this._map.SetZoomLevel(this._initialZoomLevel);
	},
    
    // --------------------------------------------------------------------------------------  
    AddAddress: function( address, description, name, icon )
    {
        this.AddAddressWithLatLong( address, null, null, description, name, icon );
    },
    
    // --------------------------------------------------------------------------------------
    AddAddressWithLatLong: function( address, latitude, longitude, description, name, icon )
    {
        if ( this._addresses == null )
        {
            this._addresses = new Array();
        }
        
        this._count++;
        this._addresses [ this._count ] = {
            Address:        address,
            Description:    description,
            Name:           name,
            Icon:           icon,
            Latitude:       latitude,
            Longitude:      longitude
        }
    },
    
    // --------------------------------------------------------------------------------------
    LoadDrivingDirections: function( divName ) 
    {
        var locations = new Array();
        
        this._divNameForDirections = divName;
        
        if ( this._count > 0 )
        {
            for ( i = 1; i <= this._count; i++ )
            {
                if ( this._addresses[i].Latitude && this._addresses[i].Longitude )
                {
                    locations[i] = new VELatLong(   this._addresses[i].Latitude, 
                                                    this._addresses[i].Longitude );
                    
                }
                else
                {
                    locations[i] = this._addresses[i].Address;
                }
            }
            
            var options = new VERouteOptions();
            options.SetBestMapView = false; // Change this... don't we want the map to refresh?
            options.RouteCallback  = this.LocatedDirections;
            options.DistanceUnit   = VERouteDistanceUnit.Mile;
            options.ShowDisambiguation = true;  //wha?
            
            // Get directions
            this._map.GetDirections(locations, options);
        }
    },
    
    LocatedDirections: function( route )
    {
        var turns = "<h3>Turn-by-Turn Directions</h3>(rounding errors are possible)";

        turns += "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

        turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";

        if (1)
        {
           // Unroll route and populate DIV
           var legs          = route.RouteLegs;
           var leg           = null;
           var turnNum       = 0;  // The turn #

           // Get intermediate legs
           for(var i = 0; i < legs.length; i++)
           {
              // Get this leg so we don't have to derefernce multiple times
              leg = legs[i];  // Leg is a VERouteLeg object

              var legNum = i + 1;
              turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
                       "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

              // Unroll each intermediate leg
              var turn        = null;  // The itinerary leg
              var legDistance = null;  // The distance for this leg
              
              for(var j = 0; j < leg.Itinerary.Items.length; j ++)
              {
                 turnNum++;
                 
                 turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object

                 turns += "<b>" + turnNum + "</b>\t" + turn.Text;

                 legDistance    = turn.Distance;

                 // So we don't show 0.0 for the arrival
                 if(legDistance > 0)
                 {
                    // Round distances to 1/10ths
                    turns += " (" + legDistance.toFixed(1) + " miles";

                    // Append time if found
                    if(turn.Time != null)
                    {
                       turns += "; " + GetTime(turn.Time);
                    }

                    turns += ")<br/>";
                 }
              }

              turns += "<br/>";
           }

           // Populate DIV with directions
           this.PopulateDirections( turns );
        }
    },
    
    PopulateDirections: function( directions )
    {
        if ( this._divNameForDirections )
        {
            var d = document.getElementById( this._divNameForDirections );
            d.innerHTML = directions;
        }
    },
    
    // From MSDN
    GetTime: function(time)
    {
        if(time == null)
        {
           return("");
        }

        if(time > 60)
        {                                 // if time == 100
           var seconds = time % 60;       // seconds == 40
           var minutes = time - seconds;  // minutes == 60
           minutes     = minutes / 60;    // minutes == 1


           if(minutes > 60)
           {                                     // if minutes == 100
              var minLeft = minutes % 60;        // minLeft    == 40
              var hours   = minutes - minLeft;   // hours      == 60
              hours       = hours / 60;          // hours      == 1

              return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
           }
           else
           {
              return(minutes + " minutes, " + seconds + " seconds");
           }
        }
        else
        {
           return(time + " seconds");
        }
    }
}
    


*/
