/* JavaScript Library to Travelmaps on Virtual Earth
   using VE Version 6.2
*/

function GetVEMap(cityIndex, zoomFactor, countryID, homeLat, homeLong) 
{
    // If the browser is Firefox get the version number
    var ffv = 0;
    var ffn = "Firefox/"
    var ffp = navigator.userAgent.indexOf(ffn);
    if (ffp != -1) {
        ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
    }
    
    if (countryID == null)
    {
      countryID = 0;
    }

    if (homeLat == null)
    {
      homeLat = '0';
    }
    
    if (homeLong == null)
    {
      homeLong = '0';
    }
    
    // If we're using Firefox 1.5 or above override 
    // the Virtual Earth drawing functions to use SVG
    if (ffv >= 1.5) {
        Msn.Drawing.Graphic.CreateGraphic = function(f, b) {
            return new Msn.Drawing.SVGGraphic(f, b)
        }
    }

    map = new VEMap('myMap');
    var numberOfCities = MapLocations.length - 1;

    var centerPoint = Data2Point(numberOfCities);

    if (cityIndex != -1)
    {
        centerPoint = Data2Point(cityIndex);
    }

    if (homeLat != '0')
    {
       // use the provided home point
       centerPoint = new VELatLong(homeLat,homeLong);                
    }
    
   

    map.LoadMap(centerPoint, zoomFactor, 'r', false);

    var info = '';
    var CI90 = new VECustomIconSpecification();
    CI90.TextContent = '  ';
    CI90.ImageOffset = new VEPixel(6,-8);

    // put the points into a new array        
    var points = new Array(numberOfCities);
    for (i = 1; i <= numberOfCities; i++) {
              
        points[i - 1] = Data2Point(i);

        var flag = parseInt(MapLocations[i][_Flags]);
        var vehicle = parseInt(MapLocations[i][_Vehicle]);

        if ((countryID == parseInt(MapLocations[i][_Country])) || (countryID == 0))
        {

        // remember to set the center to the last one
        centerPoint = Data2Point(i);

          // Flags = 1 means we stayed overnight
          if (flag == 1) {

              info = MapLocations[i][_Title] + '<br />' + MapLocations[i][_Date] + '<br />'
                      + MapLocations[i][_Days];

              if (parseInt(MapLocations[i][_Days]) == 1) {
                  info += ' night';
              }
              else {
                  info += ' nights';
              }

              var pin = new VEShape(VEShapeType.Pushpin, Data2Point(i));
                                    
              CI90.Image = MarkerImage(i);            
              
              pin.SetCustomIcon(CI90);
              pin.SetTitle(info);
              map.AddShape(pin);
          }

          // Draw the lines
          if (i > 1) {
              var travelPoints = new Array(2);
              travelPoints[0] = Data2Point(i - 1);
              travelPoints[1] = Data2Point(i)

              var linecolor;
              var _color = "";

              // we arrived here by plane
              if (vehicle == 10) {
                  // red
                  linecolor = new VEColor(255, 0, 0, 1.0)
                  _color = "red";
              }
              else if (vehicle == 3) {
                  // road, blue
              linecolor = new VEColor(0, 0, 255, 1.0)
              _color = "blue";
              }
              else if (vehicle == 2) {
                  // train, green
              linecolor = new VEColor(28, 127, 0, 1.0)
              _color = "green";
              }
              else if (vehicle == 4) {
                  // boat, black
              linecolor = new VEColor(0, 0, 0, 1.0)
              _color = "black";
              }
              else if (vehicle == 21) {
                  // foot, brown #B73819
              linecolor = new VEColor(183, 56, 25, 1.0)
              _color = "brown";
              }
              else if (vehicle == 0) {
                  // white
              linecolor = new VEColor(255, 255, 255, 0)
              _color = "white";
              }            
              else {
                  // blue, the default
                  linecolor = new VEColor(0, 0, 255, 1.0)
                  _color = "blue";
              }

              if (linecolor == null) {
                  linecolor = new VEColor(100,100, 100, 1.0)
              }
              
              // create a polyline
              var polyline = new VEShape(VEShapeType.Polyline, travelPoints);
              polyline.SetLineColor(linecolor);
              polyline.SetLineWidth(1);      
              polyline.HideIcon(); 
              map.AddShape(polyline);   
           } // end country         
        }
    }

}

function Data2Point(index) {

    if (MapLocations[index][_Type] == 'dm') 
    {
        return new VELatLong(DM2DD(MapLocations[index][_Lat]),
                    DM2DD(MapLocations[index][_Long]));
    }
    else 
    {
        return new VELatLong(MapLocations[index][_Lat],
                    MapLocations[index][_Long]);
    }
}
