var wall = new Array();

var WALLS = 6;

//var WallX0 = new Array(-750, -800, -750, -950, -413, -636);
//var WallY0 = new Array(100, -400, 100, -425, -540, -141);
//var WallX1 = new Array(570, -560, -350, 350, 735, 263);
//var WallY1 = new Array(800, 470, -600, -450, 290, 448);

var WallX0 = new Array(-636, -800, -750, -950, -413, -187);
var WallY0 = new Array(-141, -400, 100, -425, -540, 104);
var WallX1 = new Array(263, -560, -350, 350, 735, -196);
var WallY1 = new Array(448, 470, -600, -450, 290, 56);

// each corner is the same height
var WallZ = new Array(125, 200, 150, 200, 300, 40);

function createWalls() {
  for (var i = 0; i < WALLS; i++) {
    wall[i] = new Wall();
    wall[i].init(i);
  };
};  

function addWallsToGE(ge) {
return;
  for (var w = 0; w < WALLS; w++) {
    wall[w].addToGE(ge);
  }
}

function Wall() {
  this.lat = new Array(0,0);
  this.lng = new Array(0,0);
  this.alt = 0;
  
  this.placeMark = null;
  this.polygon = null;
  
  this.plane = new Plane();
  
  this.corner = new Array();
  
  this.tag = 0;
  this.height = 0;
  
  this.init =
    function(itag) {
      this.tag = itag;
      this.height = WallZ[itag];
      this.plane.init(WallX0[itag], WallY0[itag], WallX1[itag], WallY1[itag], WallZ[itag]);
    }  
  
  this.addToGE = 
    function(ge) {

// find the first corner    
      var corner = new Point3D();
      corner.x = WallX0[this.tag];
      corner.y = WallY0[this.tag];
      corner.z = WallZ[this.tag];
      var gCorner0 = corner.toGLatLng();

// find the second corner      
      corner.x = WallX1[this.tag];
      corner.y = WallY1[this.tag];
      var gCorner1 = corner.toGLatLng();
      
// store the coordinates      
      var ring = ge.createLinearRing('');
      var coords = ring.getCoordinates();

      coords.pushLatLngAlt(gCorner0.lat(), gCorner0.lng(), corner.z);
      coords.pushLatLngAlt(gCorner1.lat(), gCorner1.lng(), corner.z);
      coords.pushLatLngAlt(gCorner1.lat(), gCorner1.lng(), 0);
      coords.pushLatLngAlt(gCorner0.lat(), gCorner0.lng(), 0);
       
// create the polygon and give it the ring's coordinates      
      this.polygon = ge.createPolygon('');
      this.polygon.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
      this.polygon.setOuterBoundary(ring);
      
// create the place mark and give it the polygon      
      this.placeMark = ge.createPlacemark('');
      this.placeMark.setGeometry(this.polygon);
      
// add it to the earth                  
      ge.getFeatures().appendChild(this.placeMark);
    }
    
  this.intersected = 
    function() {
//   return (this.plane.intersectedByLineBetweenPoints(navLight[0].base,navLight[0].endPoint));
      for (var i = 0; i < LIGHTS; i++) {
        if (lightSelected[i]) {
          if (this.plane.intersectedByLineBetweenPoints(navLight[i].base,navLight[i].wallEndPoint)) {
            return true;
          }  
        }    
      }
      return false;
    }  
   
  this.show = 
    function() {
      this.plane.show();
    }
}

function anyWallIntersects(target) {

// find the end points for this target
  for (var i = 0; i < LIGHTS; i++) {
    if (lightSelected[i]) {
      navLight[i].findWallEndPointFromTarget(target); 
    }
  }  

// see if we have any intersects  
  for (var w = 0; w < WALLS; w++) {
    if (wall[w].intersected()) return true;
  }
  return false;
}

//        |z2   z2 = min Z
//        |
//        |
//    z1  | z1 = wall intersection point z
//     | /
//     |/    /
//     |    /
//    /   a2
//   /a1  /
//  /    /
function minZAtXY(x,y) {
  var a1,a2,z1,z2;
  
  var iPt = new Point3D();
  
  var target = new Point3D();
  target.x = x;
  target.y = y;
  target.z = MinSliderZ;
  
  findEndPoints(target);  
  
// loop through each of the walls  
  for (var w = 0; w < WALLS; w++) {
  
// and each of the lights
    for (var i = 0; i < LIGHTS; i++) {
      if (wall[w].plane.intersected(navLight[i].base, navLight[i].wallEndPoint, iPt)) {
        a1 = navLight[i].base.distanceTo(iPt);
        a2 = navLight[i].base.distanceTo(target);
        z1 = wall[w].height - iPt.z;
        z2 = z1 * (a2 / a1);
        target.z += z2;
        
// find new end points based on the adjusted target        
        findEndPoints(target);
      }
    }    
  }    
  return target.z;
}

function findEndPoints(target) {
  for (var i = 0; i < LIGHTS; i++) {
    if (lightSelected[i]) {
      navLight[i].findWallEndPointFromTarget(target); 
    }
  }
}

function minZAtXYZ(x, y, z) {
  var target = new Point3D();
  target.x = x;
  target.y = y;
  target.z = z;
 
// keep going up until we're safe 
  while (anyWallIntersects(target)) {
    target.z += 50.0;
  }
  
// maybe the min is ok  
  if (target.z == z) return target.z;
  
// go back down again by a smaller amount until we're hitting a wall again
  do {
    target.z -= 10.0;
  }
  while (!anyWallIntersects(target));
  
// go back up again by an even smaller amount until we're safe again
  do {
    target.z += 1.0;
  }
  while (anyWallIntersects(target));
  
  return target.z;
}


