var checkBox = new Array();

function addCheckBoxesToMap() {
  for (var i=0; i < LIGHTS; i++) {
    checkBox[i] = new CheckBox();
    checkBox[i].addToGM(i);
  }
}

function CheckBox() {
  this.placeMark = null;
  this.tag = 0;
//  this.checked = true;
  this.gmarker = null;

  this.addToGM = 
    function(itag) {
      this.tag = itag;
      var icon = new GIcon();
      icon.image = "images/checkbox_on.png";
      icon.iconSize = new GSize(14.0, 14.0);
      icon.iconAnchor = new GPoint(7.0, 7.0);
      icon.infoWindowAnchor = new GPoint(7.0, 7.0);
      
      var i = correctedLightI(this.tag);
  
      var base = new GLatLng(lightLat[i],lightLng[i]);
      
      this.gmarker = new GMarker(base, {icon:icon});
      
      GEvent.addListener(this.gmarker, 'click', checkBoxClicked); 
  
      map.addOverlay(this.gmarker);
    };
    
   this.setChecked = 
     function(checked) {
       if (checked) this.gmarker.setImage('images/checkbox_on.png');
       else this.gmarker.setImage('images/checkbox_off.png');
     }; 
}

function checkBoxClicked(event) {
  var i = checkBoxIFromLat(event.lat());
  
  lightSelected[i] = !lightSelected[i];
  checkBox[i].setChecked(lightSelected[i]);
  syncLightsCombo();
}  

function checkBoxIFromLat(lat) {
  var result = 0;
  var i;
  for (result=0; result < LIGHTS; result++) {
    i = correctedLightI(result);
    if (lat == lightLat[i]) return result;
  }
  return 0; // ???
}  

function syncCheckBoxes() {
  for (var i=0; i < LIGHTS; i++) {
    checkBox[i].setChecked(lightSelected[i]);
  }    
}

function listenForCheckBoxClicks(ge) {

// mouse down
// listen for mousedown on the window (look specifically for point placemarks)
  google.earth.addEventListener(ge.getWindow(), 'mousedown', 
    function(event) {
      if (checkBoxClicked(event.getTarget())) {
        event.preventDefault();
      }  
    }
  );

// mouse move
  google.earth.addEventListener(ge.getGlobe(), 'mousemove',
    function(event) {
      var lat = event.getLatitude();
      var lng = event.getLongitude();
   //   log('lat = ' + lat + ' lng = ' + lng);
      for (var i=0; i < LIGHTS; i++) {
        light[i].pointAt(lat,lng, targetAlt);
      }
    }
  );
}