// ----- CUSTOM INFO WINDOW overlay ----------------------
function RegistryInfoWindow(anchor, html_content, html_header){
  this.anchor_ = anchor; // GLatLng
  this.background_ = new Image(388,217); 
  this.tail_ = new Image(388,30);

  this.content_ = html_content;
  this.header_ = html_header;

  this.first_time = true;

    // default template
  var template = '<div id="overlay_tab" class="inside">';
      template += '<div class="header"></div>';
      template += ' <div id="info_summary" class="content"></div>';
      template += '</div>';
      template += '<div class="bottom"></div>'; 
    this.template_ = template; 
}

RegistryInfoWindow.prototype = new GOverlay();


RegistryInfoWindow.prototype.initialize = function(map) {
  var self = this;
  var overlay_div = new Element('div', {id:'iw_overlay'}).update(self.template_);// $('iw_overlay');

  var content_div = overlay_div.down('.content');
  var header_div =  overlay_div.down('.header');
  
  content_div.update(self.content_);
  header_div.update(self.header_);

  var close_span = new Element("span", {'class':'close'});
  var close_link = new Element("a", {'href': "#"});
  close_link.onclick = function(){map.removeOverlay(self)};// self.closeEvent; // function(){self.closeEvent(); return false; };
  
  close_span.insert(close_link);
  overlay_div.insert(close_span);

  map.getPane(G_MAP_FLOAT_PANE).appendChild(overlay_div); 
  
  // prevents map click event from triggering when overlay is clicked
  GEvent.bindDom(overlay_div, 'mousedown', this, this.onClick_);
  
  this.overlay_div_ = overlay_div;
  this.header_div_ = header_div;
  this.content_div_ = content_div;
	this.map_ = map;
};

// ML: onClick_ function is from the ExtInfoWindow library; comments as follows
/**
 * Private function to steal mouse click events to prevent it from returning to the map.
 * Without this links in the ExtInfoWindow would not work, and you could click to zoom or drag 
 * the map behind it.
 * @private
 * @param {MouseEvent} e The mouse event caught by this function
 */
RegistryInfoWindow.prototype.onClick_ = function(e) {
  if(navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    e.preventDefault();
    e.stopPropagation();
  }
};

RegistryInfoWindow.prototype.remove = function() {
  this.overlay_div_.parentNode.removeChild(this.overlay_div_);
};

RegistryInfoWindow.prototype.copy = function() {
  return new RegistryInfoWindow(this.anchor_, this.content_, this.header_);
};

RegistryInfoWindow.prototype.redraw = function(force) {
  if (!force){ return; } // Only redraw on zoom level change
  if ($('iw_overlay')){
    var x_offset = 176; // 168 for default icon
    var y_offset = 10; // 30 for default icon

    var map_pt = this.map_.fromLatLngToDivPixel(this.anchor_);
    var y_loc = parseInt(map_pt.y) - this.overlay_div_.offsetHeight - y_offset;
    var x_loc = parseInt(map_pt.x) - x_offset;

    this.overlay_div_.style.top = y_loc  + "px";
    this.overlay_div_.style.left = x_loc + "px";

    if (this.first_time){   
      var new_pt = new GPoint(parseInt(map_pt.x), parseInt(map_pt.y) - 15);
      this.map_.panTo(this.map_.fromDivPixelToLatLng(new_pt));
      this.first_time = false;
    }
  }
};
// -------- END REGISTRYINFOWINDOW ----------------------------