//<![CDATA[

  //============================================================
  //
  var initialCenter = new GLatLng(49.0,8.5);
  var initialScale  = 7;
  var initialType   = G_PHYSICAL_MAP;
  var traceColor    = '#2020f0';

  //============================================================
  //
  function getMapWidth()
  {
    if (document.body.offsetWidth)
    {
      return document.body.offsetWidth;
    }
    else
    {
      return 800;
    }
  }

  //============================================================
  //
  function getMapHeight()
  {
    if(window.innerHeight)
    {
      return window.innerHeight - 50;
    }
    else
    {
      return 600;
    }
  }

  //============================================================
  //   A resize method will update the dimensions of the map
  window.onresize = function resize( event )
  {
    center = map.getCenter();

    document.getElementById( "map" ).style.width = getMapWidth() + "px";
    document.getElementById( "map" ).style.height = getMapHeight()  + "px";

    map.checkResize();
    map.setCenter( center );
  }

  //============================================================
  //   create a piece of HTML code to open the map
  document.write( "  <div id=\"map\" style=\"width: " + getMapWidth() + "px; height: " + getMapHeight() + "px\"><\/div>" );
  document.write( "  </div>" );

  document.write( "  Sensor Selection: " );
  document.write( "  <a HREF=\"ads.html\">ALL</a>" );
  document.write( "  <a HREF=\"ldb.html\">LDB</a>" );
  document.write( "  <a HREF=\"lic.html\">LIC</a>" );
  document.write( "  <a HREF=\"ofb.html\">OFB</a>" );
  document.write( "  <a HREF=\"lux.html\">LUX</a>" );
  document.write( "  <a HREF=\"bfl.html\">BFL</a>" );
  document.write( "  <a HREF=\"syd.html\">SYD</a>" );
  document.write( "  <a HREF=\"sof.html\">SOF</a>" );
  document.write( "  <a HREF=\"status.html\">STATUS</a>" );

  //============================================================
  //   let us create the map
  var head    = [];
  var markers = [];
  var labels  = [];
  var pps = [];

  var map = new GMap2( document.getElementById( "map" ) );
  // map.addMapType( G_SATELLITE_3D_MAP );
  map.addMapType( G_PHYSICAL_MAP );
  map.addControl( new GHierarchicalMapTypeControl() );
  map.addControl( new GLargeMapControl() );
  map.addControl( new GMapTypeControl() );
  map.enableScrollWheelZoom ();
  //map.addControl( new GOverviewMapControl() );
  map.setCenter( initialCenter, initialScale, initialType );

  //============================================================
  //   let us load ac-icons
  var ac_icon = [];
  var ac_width   = 32;
  var ac_height  = 32;
  for (i = 0; i < 360; i+=5)
  {
    imageName = "ac/ac" + i +".png";
    ac_icon[i] = new GIcon( G_DEFAULT_ICON, imageName );
    ac_icon[i].iconSize = new GSize( ac_width, ac_height );
    ac_icon[i].iconAnchor = new GPoint( ac_width/2, ac_height/2 );
    ac_icon[i].transparent = imageName;
    ac_icon[i].shadow = imageName;
    ac_icon[i].shadowSize = new GSize( ac_width, ac_height );
    ac_icon[i].shadowSize = new GSize( ac_width, ac_height );
  }

  //============================================================
  //   A function to create the image marker for a point
  function createImageMarker( name, heading, level, point )
  {
    var image;
    var marker;

    //============================================================
    //   history
    poly = pps[name];
    if( !poly )
    {
       poly = new GPolyline( [], traceColor, 2, 1 );
       map.addOverlay( poly );
       pps[name] = poly;
    }

    if (poly.getVertexCount() <= 25)
    {
       poly.insertVertex( 0, point );
    }
    else
    {
       poly.deleteVertex( poly.getVertexCount()-1 );
       poly.insertVertex( 0, point );
    }

    //============================================================
    //   target update
    heading= ((heading+5)-((heading+5)%10))%360;

    marker = labels[name];
    if (marker)
    {
       marker.setLatLng( point );
    }

    marker = markers[name];
    if ((marker) && (head[name] != heading))
    {
       map.removeOverlay( marker );
       delete markers[name];
    }

    marker = markers[name];
    if(marker)
    {
       marker.setLatLng( point );
       return marker;
    }

    //============================================================
    //   target icon
    head[name] = heading;
    icon = ac_icon[heading];

    marker = new GMarker( point, icon );
    map.addOverlay( marker );
    markers[name] = marker;

    //============================================================
    //   label icon
    if (labels[name])
    {
       return marker;
    }
    imageName = "label.php?cs=" + name;
    icon = new GIcon( G_DEFAULT_ICON, imageName );
    icon.iconSize = new GSize( 64, 16 );
    icon.iconAnchor = new GPoint( -15, 20 );
    icon.transparent = imageName;
    icon.shadow = imageName;
    icon.shadowSize = new GSize( 64, 16 );
    icon.shadowSize = new GSize( 64, 16 );
    icon.imageMap = [ 0, 0, 64, 0, 64, 16, 0, 16, 0, 0 ];

    marker = new GMarker( point, icon );
    map.addOverlay( marker );
    labels[name] = marker;

    return marker;
  }

  //============================================================
  //   create/update a track
  function track( callsign, lat, lon, heading, speed, level )
  {
    createImageMarker( callsign, heading, level, new GLatLng( lat, lon ) );
  }

  //============================================================
  //   delete a track
  function deltrack( name )
  {
     if (markers[name])
     {
        map.removeOverlay (markers[name]);
        delete markers[name];
     }

     if (labels[name])
     {
        map.removeOverlay (labels[name]);
        delete labels[name];
     }

     if (pps[name])
     {
        map.removeOverlay (pps[name]);
        delete pps[name];
     }
  }


  //============================================================
  //   refreshing
  function refreshResponse( urlContent, returnCode )
  {
    eval( urlContent );
    window.setTimeout( "refreshData()", 5000 );
  }


  //============================================================
  //   the periodic timer function
  var data_filename = "./data.js";
  function refreshData()
  {
    GDownloadUrl( data_filename, refreshResponse );
  }

  //============================================================
  //   load initial data
  function loadData(name)
  {
     data_filename = name;
     refreshData();
  }


//]]>
