var LIGHTS = 20;

var navLight = new Array();

var lightTimerID;
var lightI = 0;

var lightSelected = new Array(true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true);

var lightLat = new Array(
  49.2765846, // 0
  49.2767839,
  49.2770459,
  49.2772823,
  49.2774980,
  49.2776271,
  49.2777927,
  49.2779959,
  49.2782278,
  49.2783239,
  49.2796586,
  49.2794890,
  49.2793618,
  49.2791951,
  49.2790669,
  49.2788236,
  49.2785494,
  49.2783980,
  49.2782037,
  49.2779755);

var lightLng = new Array(
  -123.140596,
  -123.140713,
  -123.140889,
  -123.141054,
  -123.141225, 
  -123.141473,
  -123.141786,
  -123.142070,
  -123.142354,
  -123.142705,
  -123.138784,
  -123.138566,
  -123.138260,
  -123.137973,
  -123.137624,
  -123.137370,
  -123.137114,
  -123.136899,
  -123.136721,
  -123.136476);

var lightAlt = new Array(
  5.54275,
  5.48125,
  5.88333,
  5.838,
  5.66575,
  5.81475,
  6.3075,
  6.59225,
  6.90825,
  7.64325,             // 10
  6.2195,
  6.583,
  6.80675,
  6.81625,
  6.8305,
  6.9745,
  6.9285,
  6.95375,
  7.074,
  7.1975);
  
function correctedLightI(i) {
  return i;
  
//  if (i < 10) return (LIGHTS - 1 - i)
//  else return (i - 10);


  if (i < 10) return (i+10);
  else return (LIGHTS - 1 - i);

}

function checkLightsForWallIntersects() {
  for (i = 0; i < LIGHTS; i++) {
    navLight[i].checkForWallIntersects();
  }
}

function Light() {

  var placeMark;
  var coords;
  
  var targetLat;
  var targetLng;
  var targetAlt;
  
  this.tag = 0;
  
  this.lat = 0;
  this.lng = 0;
  this.alt = 0;
  
  this.beam = new Beam();
  
  this.endPoint = new Point3D();
  
  this.wallEndPoint = new Point3D();
  
  var orientation;
  
  this.base = new Point3D();
  this.target = new Point3D();

  this.addToGE = 
    function(ge,itag) {
      this.tag = itag;
     
// create a placemark
      this.placeMark = ge.createPlacemark('');
      
// create and init a location      
      var loc = ge.createLocation('');
      
      var i = correctedLightI(this.tag);
      
      this.lat = lightLat[i];
      this.lng = lightLng[i];
      this.alt = lightAlt[i];
      
      loc.setLatitude(this.lat);
      loc.setLongitude(this.lng);
      loc.setAltitude(this.alt);

// create a model and give it the location
      var model = ge.createModel('');
      model.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);

      model.setLocation(loc);
      
      this.orientation = ge.createOrientation('');
      this.orientation.set(0, 0, 0);
      model.setOrientation(this.orientation);
          
      var scale = ge.createScale('');
      scale.set(1, 1, 1);
      model.setScale(scale);
      
// give it the dae file      
      var link = ge.createLink('');
//      link.setHref('http://lozano-hemmer.com/vancouver/models/dome.dae');
      link.setHref('http://www.vectorialvancouver.net/models/dome.dae');
      
      model.setLink(link);
      
// give the model to the placemark
      this.placeMark.setGeometry(model);      
      
// add the placemark to google earth      
      ge.getFeatures().appendChild(this.placeMark); 
      
// set the base
      this.base.setFromLatLngAlt(this.lat, this.lng, this.alt);
      
//this.base.show('Light #'+tag);      
      
// add the beam
      this.beam.addToGE(ge, this.lat, this.lng, this.alt);
   
    };
  
  this.pointAt = 
    function(lat, lng, alt) {
    
// find the 3D point corresponding to the given lat, lng, alt    
      this.target.setFromLatLngAlt(lat, lng, alt);
      
// find the distance to the base
      var dx = this.target.x - this.base.x;
      var dy = this.target.y - this.base.y;
      var dz = this.target.z - this.base.z;
      
      var d = Math.sqrt(dx*dx + dy*dy + dz*dz);
      
// find the orientation to point at this target
      var heading = Math.PI - Math.atan2(dy, dx);
      heading = radToDeg(heading);
      this.beam.orientation.setHeading(heading);

// find the projected distance and the tilt
      var dp = Math.sqrt(dx*dx + dy*dy);
      var ry = Math.PI/2 - Math.atan2(dz, dp);  
      ry = radToDeg(ry);
      this.beam.orientation.setRoll(ry);
    };
    
 this.syncXYZ = 
   function(x, y, z) {
   
     switch (designMode) {
       
       case DesignMode.pyramid :
         this.pointAtXYZ(x, y, z);
         break;
         
       case DesignMode.parallel :
         this.pointAtXYZ(this.base.x + x, this.base.y + y, 500);
         break;
      
        case DesignMode.grid :
          var i = LIGHTS - 1 - this.tag; 
          
          var tx =navLight[i].base.x + x;
          var ty =navLight[i].base.y + y;
          
          var tz = minZAtXY(tx, ty) + (z - MinSliderZ);
          
          this.pointAtXYZ(tx, ty, tz);

          break;              
      }    
   };  
    
 this.pointAtXYZ = 
    function(x, y, z) {
      this.target.x = x;
      this.target.y = y;
      this.target.z = z;
        
// find the distance to the base
      var dx = this.target.x - this.base.x;
      var dy = this.target.y - this.base.y;
      var dz = this.target.z - this.base.z;
      
      var d = Math.sqrt(dx*dx + dy*dy + dz*dz);
      
// find the orientation to point at this target
      var heading = Math.atan2(dx, dy);
      heading = radToDeg(heading);
      this.beam.orientation.setHeading(heading);

// find the projected distance and the tilt
      var dp = Math.sqrt(dx*dx + dy*dy);
      var ry = Math.PI/2 - Math.atan2(dz, dp);  
      ry = radToDeg(ry);
      this.beam.orientation.setRoll(ry);
      
      this.findEndPoint();
    }; 
   
    
  this.setHeadingAndTilt = 
    function(heading, tilt) {
      this.beam.setHeadingAndTilt(heading, tilt);
    }
    
  this.setHeadingAndRoll = 
    function(heading, roll) {
      this.beam.setHeadingAndRoll(heading, roll);
    }
    
  this.findEndPoint = 
    function() {
      this.findEndPointFromTarget(this.target);
    }  
    
  this.findEndPointFromTarget = 
    function(tgt) {
      this.endPoint.extendFromBaseToTarget(this.base,tgt,BeamLength);
    }  
    
  this.findWallEndPointFromTarget = 
    function(tgt) {
      this.wallEndPoint.extendFromBaseToTarget(this.base,tgt,BeamLength*10);
    }  
    
  this.turnOff = 
    function() {
      this.beam.placeMark.setVisibility(false);
    }  
    
  this.turnOn = 
    function() {
      this.beam.placeMark.setVisibility(true);
    }     
    
  this.checkForWallIntersects = 
    function() {
      var w=0;
      var hitWall = false;
      
      do {
        if (wall[w].plane.intersectedByLineBetweenPoints(this.base,this.endPoint)) {
          hitWall = true;
        }
        else w++;
      }  
      while ((w < WALLS) && (!hitWall));
      
      if (hitWall) this.turnOff()
      else this.turnOn();
    }  
    
  this.dataString = 
    function() {
      var result = 'L' + this.tag + 
                   'X:' + this.target.x.toFixed(2) + 
                   'Y:' + this.target.y.toFixed(2) + 
                   'Z:' + this.target.z.toFixed(2); 
      return result;              
    }   
}

function flashLights() {
  lightI = 0;
  lightTimerID = setInterval(updateLightFlash,500);
}

function updateLightFlash() {
  navLight[lightI].pointAt(originLatitude,originLongitude,100);
  lightI++;
  if (lightI == LIGHTS) {
    clearInterval(lightTimerID);
    syncBeams();
  } 
}