﻿var myMap = null;
var pin2 = null;
var pin1 = null;
var pins = null;
var dragShape = null;
var layer = new VEShapeLayer();



function LoadMap()
{
try{
    myMap=new VEMap('map');
    myMap.LoadMap();
    document.getElementById('banner').innerHTML = document.title;

    
    myMap.AttachEvent("onmousedown",MouseHandler);
    myMap.AttachEvent("onmouseup",MouseHandler);
    myMap.AttachEvent("onmousemove",MouseHandler);
    var ex = new VEException;
}
catch(ex){handleE(ex,"Main");}
}
function SetPins(s,e)
{
   if(s&&e !=null)
   {
    pin1 = document.getElementById('start1').value;
    pin2 = document.getElementById('end1').value;
    pins = new Array(pin1,pin2);
    Drive();
   }
   else
   {
    alert("You need to supply a Start and End Address for this to work");
    
   }
}
function Drive()
{
try{
    var options = new VERouteOptions;
    options.DrawRoute= true;
    options.SetBestMapView = true;
    options.RouteCallback= ShowTurns;
    myMap.GetDirections(pins, options);
   
    }
catch(ex){handleE(ex, "drive")}
}
function ShowTurns(route)
{
var turns = "<h3>Turn-by-Turn Directions</h3>(rounding errors are possible)";
//myMap.SetMapView(route.RouteLegs.Start, route.RouteLegs.End);
turns += "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";

   var legs          = route.RouteLegs;
   var leg           = null;
   var turnNum       = 0;  // The turn #

   // Get intermediate legs
   try{
   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/>";
      }

    }
  }
catch(ex){handleE(ex, "rout info");}

   // Populate DIV with directions
   SetDirections(turns);   
}

function SetDirections(s)
{
document.getElementById("results").innerHTML = s;

}

// time is an integer representing seconds
// returns a formatted string
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");
}
}

function MouseHandler(e)
{
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = myMap.PixelToLatLong(pixel);
   

    try{
   
    var msg;

   
    if (e.eventName == "onmousedown" && e.elementID != null)
    {
        if(myMap.GetShapeByID(e.elementID).GetTitle().match("Start")||myMap.GetShapeByID(e.elementID).GetTitle().match("End"))
        {
        dragShape = myMap.GetShapeByID(e.elementID);
        }
        return true; // prevent the default action
        
    }
    else if (e.eventName == "onmouseup" && myMap.GetShapeByID(e.elementID)!=null)
    {
        if(myMap.GetShapeByID(e.elementID).GetTitle().match("Start")||myMap.GetShapeByID(e.elementID).GetTitle().match("End"))
        {
            LL = dragShape.GetPoints();
            var lat = LL[0].Latitude;
            var lon = LL[0].Longitude;
            if(myMap.GetShapeByID(e.elementID).GetTitle().match("Start"))
            {
               pin1 = new VELatLong(lat,lon);
            }
            else
            {
               pin2 = new VELatLong(lat,lon);
            }
            var new_pins = new Array(pin1,pin2);
            pins = new_pins;
            Drive();
        }
      
       dragShape = null;     
      
    }
    else if (e.eventName == "onmousemove" && dragShape != null)
    {

        dragShape.SetPoints(LL);
        
        return true; // prevent the default action
    }
   
    }
    catch(ex){handleE(ex, "mouse Handler");}
    }

function handleE(ex, from)
{
    alert("Error : "+ex.message+" | occured From Source : "+ex.source+" | By the name of : "+ex.name+" | From Method : "+from);
}

