// A Label is a simple overlay that outlines a lat/lng bounds on the
// map. It has a border of the given weight and color and can optionally
// have a semi-transparent background color.
function Label(text, latLng, opt_weight, opt_color) {
  this.text_ = text;
  this.latLng = latLng;
  this.weight_ = opt_weight || 2;
  this.minZoomLevel_ = 1;
  this.maxZoomLevel_ = 18;
  this.vis_ = false;
}
Label.prototype = new GOverlay();

// Creates the DIV representing this Label.
Label.prototype.initialize = function(map) {

   // Create the DIV representing our Label
  var div = document.createElement("div");
  div.className="ziptext";
  div.style.position = "absolute";
  div.innerHTML = this.text_
  div.style.display = "none";
  
  // Our Label is flat against the map, so we add our selves to the
  // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
  // below the marker shadows)
  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;

}

Label.prototype.setViewLevel = function(min, max) {
	this.minZoomLevel_ = min;
	this.maxZoomLevel_ = max;
	
	this.redraw(true);
}

Label.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
	this.div_ = null;
	this.text_ = null;
	this.lat_ = null;
	this.lng_ = null;
	this.weight_ = null;
}

// Copy our data to a new Label
Label.prototype.copy = function() {
  return new Label(this.bounds_, this.weight_, this.color_,
                       this.backgroundColor_, this.opacity_);
}

Label.prototype.setClassName = function(className) {
	this.div_.className = className;
}

/* Changes the vis_ variable based on user request, but only sets the style if
 * the label currently falls between the allowable zoom levels
 */
Label.prototype.setVisibility = function(vis) {
	this.vis_ = vis;
	
	if( this.div_ != undefined ) {
		var zl = map.getZoom();
		if( zl >= this.minZoomLevel_ && zl <= this.maxZoomLevel_ )		
			this.div_.style.display = (this.vis_ ? "block" : "none");
		else this.div_.style.display = "none";
	}
}

// Redraw the Label based on the current projection and zoom level
Label.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;
     
  if( this.div_ != undefined ) {
  	loc = this.map_.fromLatLngToDivPixel(this.latLng);
  	//this.div_.style.left = (loc.x - Math.floor(this.div_.clientWidth/2)) + "px";
  	this.div_.style.left = (loc.x - Math.floor((this.div_.innerHTML.length*6)/2)) + "px";
  	this.div_.style.top = loc.y + "px";
  	
  	// If the zoom level so directs this will hide the item
  	this.setVisibility(this.vis_);
  }
}