// ELabel.js
//
//   This Javascript is provided by Mike Williams
//   Blackpool Community Church Javascript Team
//   http://www.commchurch.freeserve.co.uk/
//   http://econym.googlepages.com/index.htm
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.2      Works with GMarkerManager in v2.72, v2.73, v2.74 and v2.75
// version 1.3      add .isHidden()
// version 1.4      permit .hide and .show to be used before addOverlay()
// version 1.5      fix positioning bug while label is hidden
// version 1.6      added .supportsHide()
// version 1.7      fix .supportsHide()

function ELabel(point, html, color, pixelOffset, percentOpacity, overlap, hidden) {
	this.map_ = map;
	this.point = point;
	this.html = '<nobr>'+html+'</nobr>';
	this.color = color ? color : {color:'#FFFFFF',fill_color:'#7687DE',line_color:'#5A67A8'};
	this.pixelOffset = pixelOffset ? pixelOffset : new GSize(0,0);
	if (percentOpacity) {
		if(percentOpacity<0){percentOpacity=0;}
		if(percentOpacity>100){percentOpacity=100;}
	}
	this.percentOpacity = percentOpacity;
	this.overlap = overlap||false;
	this.hidden = hidden||false;
}
ELabel.prototype = new GOverlay();

ELabel.prototype.initialize = function() {
	var me = this;
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.style.cursor = 'pointer';
	div.className = 'map_elabel';
	if(this.color.color) div.style.color = this.color.color;
	if(this.color.fill_color) div.style.backgroundColor = this.color.fill_color;
	if(this.color.line_color) {
		div.style.borderColor = this.color.line_color;
		div.style.borderWidth = '1px';
		div.style.borderStyle = 'solid';
	}
	div.innerHTML = this.html;
	this.map_.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
	this.div_ = div;
	if (this.percentOpacity) {
		if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
		if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
		if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
		if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
	}
	if (this.overlap) {
		var z = GOverlay.getZIndex(this.point.lat());
		this.div_.style.zIndex = z;
	}
	if (this.hidden) {
		this.hide();
	}
	GEvent.addDomListener(div, "click", function(event) {
		GEvent.trigger(me, "click");
	});
	GEvent.addDomListener(div, "mouseover", function() {
		//GEvent.trigger(me.overlay.div_,'mouseover');
		me.bringToFront_();
	});
	GEvent.addDomListener(div, "mouseout", function() {
		//GEvent.trigger(me.overlay.div_,'mouseout');
		me.sendBack_();
	});
}
ELabel.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}
ELabel.prototype.copy = function() {
	return new ELabel(this.point, this.html, this.color, this.pixelOffset, this.percentOpacity, this.overlap);
}
ELabel.prototype.redraw = function(force) {
	var p = this.map_.fromLatLngToDivPixel(this.point);
	var h = parseInt(this.div_.clientHeight);
	this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
	this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
}
ELabel.prototype.show = function() {
	if (this.div_) {
		this.div_.style.display="";
		this.redraw();
	}
	this.hidden = false;
}
ELabel.prototype.hide = function() {
	if (this.div_) {
		this.div_.style.display="none";
	}
	this.hidden = true;
}
ELabel.prototype.isHidden = function() {
	return this.hidden;
}
ELabel.prototype.supportsHide = function() {
	return true;
}
ELabel.prototype.setContents = function(html) {
	this.html = '<nobr>'+html+'</nobr>';
	this.div_.innerHTML = this.html;//'<div class="' + this.color + '">' + this.html + '</div>' ;
	this.redraw(true);
}
ELabel.prototype.getContents = function() {
	return this.html.replace('<nobr>','').replace('<NOBR>','').replace('</nobr>','').replace('</NOBR>','');
}
ELabel.prototype.setStyle = function(color) {
	if(color.color) this.div_.style.color = color.color;
	if(color.fill_color) this.div_.style.backgroundColor = color.fill_color;
	if(color.line_color) this.div_.style.borderColor = color.line_color;
}
ELabel.prototype.setPoint = function(point) {
	this.point = point;
	if (this.overlap) {
		var z = GOverlay.getZIndex(this.point.lat());
		this.div_.style.zIndex = z;
	}
	this.redraw(true);
}
ELabel.prototype.setLatLng = function(point) {
	this.setPoint(point);
}
ELabel.prototype.getPoint = function() {
	return this.point;
}
ELabel.prototype.getLatLng = function() {
	return this.point;
}
ELabel.prototype.bringToFront_ = function() {
  var z = GOverlay.getZIndex(this.point.lat());
  this.div_.style.zIndex = 1e9;
  if(this.overlay) this.overlay.div_.style.zIndex = 1e9;
}

ELabel.prototype.sendBack_ = function() {
  var z = GOverlay.getZIndex(this.point.lat());
  this.div_.style.zIndex = z;
  if(this.overlay) this.overlay.div_.style.zIndex = z;
}