// Copyright 2006 Kushal Dave (kushaldave.com)

// Portions of this code borrowed liberally from
// Mihai Parparita's overplot:
// http://persistent.info/overplot/classes.js

function ApartmentOverlay(buildings) {
  GOverlay.call(this);
  
  this.map = null;
  this.buildings = buildings;
  this.visibleApartments = {};
}

ApartmentOverlay.prototype.initialize = function(map) {
  this.map = map;
  this.parentNode = map.getPane(G_MAP_MARKER_PANE);
  
  // To handle clicks in the shadow, addinga simple handler to
  // G_MAP_MARKER_MOUSE_TARGET_PANE doesn't seem to work, and we don't want
  // to have to add DOM nodes there too. So instead we add a global click
  // handler to the map that looks for clicked markers if the regular
  // handler doesn't trigger
  
  GEvent.bindDom(this.parentNode, "click", this, this.handleDomClick);
  //GEvent.bind(map, "click", this, this.handleMapClick);  
  
  
  GEvent.bind(map, "dragstart", this, this.beginMapDrag);
  GEvent.bind(map, "dragend", this, this.endMapDrag);
}

ApartmentOverlay.prototype.beginMapDrag = function() {
  // Don't count short drags, so that they still trigger marker clicks
  var self = this;
  this.beginDragTimeout = window.setTimeout(function() {
    self.beginDragTimeout = null;
    self.inDrag = true;
  }, 250);
}

ApartmentOverlay.prototype.endMapDrag = function() {
  if (this.beginDragTimeout) {
    window.clearTimeout(this.beginDragTimeout);
  } else {
    // We reset the drag state in a timeout because we want the click event
    // (if any) to be processed first
    var self = this;
    window.setTimeout(function() {
      self.inDrag = false;
    }, 0);
  }
}

ApartmentOverlay.prototype.handleDomClick = function(event) {
  this.handledClick = true;
  
  if (this.inDrag) {
    return;
  }
  
  var t = event.target;
  if (t.apartment) {
    t.apartment.openPage();
  }
}

ApartmentOverlay.prototype.resetVisibleApartments = function() {
  var vis = this.visibleApartments;
  for (var name in vis) {
    this.parentNode.removeChild(vis[name].getIconNode());
  }
  this.visibleApartments = {};
}

ApartmentOverlay.prototype.updateVisibleApartments = function(newVisibleApartments) {
  var start = new Date().getTime();
  
  var zoom = this.map.getZoom();
  log(zoom, this.lastZoom);
  if (zoom != this.lastZoom) {
    this.lastZoom = zoom;
    this.resetVisibleApartments();
  }

  var apartmentToRemove = [];
  var apartmentToAdd = [];
  var mapBounds = this.map.getBounds();
  
  // Enlarge bounds a bit so points at the edges don't flicker in and out
  visibleCount = 0;
  for (var i = 0, apartment; apartment = this.buildings[i]; i++) {

    var alreadyVisible = this.visibleApartments[apartment.name];

    if (newVisibleApartments[apartment.name]) {
      visibleCount++;
      if (!alreadyVisible) {
        apartmentToAdd.push(apartment);
      } else {
        apartment.getIconNode(); // refresh color
      }
    } else if (alreadyVisible) {
      apartmentToRemove.push(apartment);
    }
  }
  
  log("adding", apartmentToAdd.length, "removing", apartmentToRemove.length);
  
  var removeStart = new Date().getTime();
  
  for (var i = 0, apartment; apartment = apartmentToRemove[i]; i++) {
    this.parentNode.removeChild(apartment.getIconNode());
  }
  
  this.visibleApartments = newVisibleApartments;
  
  var addStart = new Date().getTime();

  for (var i = 0, apartment; apartment = apartmentToAdd[i]; i++) {
    var iconNode = apartment.getIconNode();
    iconNode.apartment = apartment;
    var iconPosition = this.map.fromLatLngToDivPixel(apartment.getPoint());
    
    iconNode.style.left = iconPosition.x + "px";
    iconNode.style.top = iconPosition.y + "px";
    
    this.parentNode.appendChild(iconNode);
  }
  
  var end = new Date().getTime()
  

}