var mapBeam = new Array();

var BeamLength = 750.0; // metres
var BeamAlt = 100.0;
 
function createMapBeams() {
  for (var i=0; i < LIGHTS; i++) {
    mapBeam[i] = new MapBeam();
    mapBeam[i].addToGM(i);  
  }
}

function syncMapBeams(x, y, z) {
  for (var i=0; i < LIGHTS; i++) {
    if (lightSelected[i]) mapBeam[i].sync(x, y, z);
  }
}

function syncMapBeamsWithLights() {
  for (var i=0; i < LIGHTS; i++) {
    if (lightSelected[i]) mapBeam[i].syncWithLight();
  }
}


function finishAddingMapBeams() {
  for (var i=0; i < LIGHTS; i++) {
    mapBeam[i].finishAdd();
  }
}

function MapBeam() {
  this.tag = 0;
  
  this.base = null;    
  this.target = null;
  this.polyline = null;
  
  this.basePt = new Point3D();
  this.targetPt = new Point3D();
  this.endPt = new Point3D();
  
  this.addToGM = 
    function(iTag) {
      this.tag = iTag;
    
      this.base = new GLatLng(lightLat[this.tag],lightLng[this.tag]);
      this.basePt.setFromGLatLngAndAlt(this.base, lightAlt[this.tag]);
      
      this.target = new GLatLng(originLatitude, originLongitude);
      this.targetPt.setFromGLatLngAndAlt(this.target, MarkerAlt);
      
// find the end point
      this.endPt.extendFromBaseToTarget(this.basePt, this.targetPt, BeamLength);    
      
      
// extend the target to find the end point
      this.polyline = new GPolyline([this.base, this.endPt], "#FFFFFF", 3, 0.75);
      
      this.sync(0, 0, MarkerAlt);
      map.addOverlay(this.polyline);
    }
    
  this.finishAdd = 
    function() {
      map.addOverlay(this.polyline);
    }
    
  this.sync = 
    function(x,y,z) {
      this.targetPt.x = x;
      this.targetPt.y = y;
      this.targetPt.z = z;
      
      this.endPt.extendFromBaseToTarget(this.basePt, this.targetPt, BeamLength);      

      var endLatLng = this.endPt.toGLatLng();
      
      this.polyline.insertVertex(1, endLatLng);
      this.polyline.deleteVertex(2);
    }  
    
  this.syncWithLight = 
    function() {
      var tgt = navLight[this.tag].target;
      this.sync(tgt.x, tgt.y, tgt.z);
    }      
}
