// Define the overlay, derived from google.maps.OverlayView
function BookAreaLabel(opt_options) {
 // Initialization
 this.setValues(opt_options);

 // BookAreaLabel specific
 var span = this.span_ = document.createElement('span');
span.style.cssText = 'z-index:1000;font-size:10pt;color:black;font-weight:bold;';
// could do with circularity, fixed size/variable font and semi-transparency here

this.span_.title = "Total number of books indexed for this area.";

 var div = this.div_ = document.createElement('div');
 div.appendChild(span);
 div.style.cssText = 'position: absolute; display: none';
 
 var poibookid = this.poibookid_;
 var lng = this.lat_;
 var lng = this.lng_;
};

BookAreaLabel.prototype = new google.maps.OverlayView;



// Implement onAdd
BookAreaLabel.prototype.onAdd = function() {
	this.div_.style.visibility = "hidden";	// REMmed this out because PoI show wasn't making labels visible (where there were no listeners) - maybe restore if markers too ugly at start
	var pane = this.getPanes().overlayMouseTarget;		// put labels on overlayMouseTarget (layer 5 of 6) because directions were obscuring them on overlayLayer, making them unclickable
 pane.appendChild(this.div_);

 // Ensures the label is redrawn if the text or position is changed.
 var me = this;
 this.listeners_ = [
   google.maps.event.addListener(this, 'position_changed',
       function() { me.draw(); }),
   google.maps.event.addListener(this, 'text_changed',
       function() { me.draw(); })
 ];
};

// Implement onRemove
BookAreaLabel.prototype.onRemove = function() {
 this.div_.parentNode.removeChild(this.div_);

 // BookAreaLabel is removed from the map, stop updating its position/text.
 for (var i = 0, I = this.listeners_.length; i < I; ++i) {
   maps.google.event.removeListener(this.listeners_[i]);
 }
};

// Implement draw
BookAreaLabel.prototype.draw = function() {
 var projection = this.getProjection();
 var position = projection.fromLatLngToDivPixel(this.get('position'));

 var div = this.div_;
 div.style.left = position.x - 5 + 'px';		// random displacement +/- 25 pixels
 div.style.top = position.y - 35 + this.get('offsetVertical') + 'px';
 div.style.display = 'block';
 
 div.title = this.get('title');
 div.style.visibility='visible';
 this.span_.innerHTML = this.get('text').toString();
};

BookAreaLabel.prototype.hide = function() {  if (this.div_) {    this.div_.style.visibility = "hidden";  }}
BookAreaLabel.prototype.show = function() {  if (this.div_) {    this.div_.style.visibility = "visible";  }}
BookAreaLabel.prototype.toggle = function() {  if (this.div_) {    if (this.div_.style.visibility == "hidden") {      this.show();    } else {      this.hide();    }  }}

BookAreaLabel.prototype.getDiv = function() {  if (this.div_) {    return this.div_;  }}
BookAreaLabel.prototype.setWhite = function() {  if (this.div_) { if (this.span_.style.backgroundColor != 'black') {this.span_.style.color = 'white';} else {this.span_.style.color = 'red';}  }}	// set to show up
BookAreaLabel.prototype.setBlack = function() {  if (this.div_) { if (this.span_.style.backgroundColor != 'black') {this.span_.style.color = 'black';} else {this.span_.style.color = 'white';} }}	// back to normal

// div.style may be more persistent 

BookAreaLabel.prototype.setPoibookid = function(myPoibookid) { this.poibookid_ = myPoibookid; }	// allow, but don't force, poibookid to be set, stored & gotten
BookAreaLabel.prototype.getPoibookid = function() { return this.poibookid_; }
BookAreaLabel.prototype.setDescription = function(Description) { this.div_.title = Description + '. ' + this.div_.title; }

BookAreaLabel.prototype.setLat = function(lat) { this.lat_ = lat; }	// allow, but don't force, poibookid to be set, stored & gotten
BookAreaLabel.prototype.setLng = function(lng) { this.lng_ = lng; }	// allow, but don't force, poibookid to be set, stored & gotten
BookAreaLabel.prototype.getLat = function() { return this.lat_; }	// allow, but don't force, poibookid to be set, stored & gotten
BookAreaLabel.prototype.getLng = function() { return this.lng_; }	// allow, but don't force, poibookid to be set, stored & gotten

BookAreaLabel.prototype.setTextPos = function() {this.div_.style.top = '-50px';}


