// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap

function ELabel(point, html, classname, pixelOffset, percentOpacity, overlap) {
	// Mandatory parameters
	this.point = point;
	this.html = html;

	// Optional parameters
	this.classname = classname||"";
	this.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;
} 
      
ELabel.prototype = new GOverlay();

ELabel.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>';
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);
	this.map_ = map;
	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;
	}
}

ELabel.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}

ELabel.prototype.copy = function() {
	return new ELabel(this.point, this.html, this.classname, 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() {
	this.div_.style.display="";
}
      
ELabel.prototype.hide = function() {
	this.div_.style.display="none";
}

ELabel.prototype.setContents = function(html) {
	this.html = html;
	this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
	this.redraw(true);
}

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.setOpacity = function(percentOpacity) {
	if (percentOpacity) {
		if(percentOpacity<0){percentOpacity=0;}
		if(percentOpacity>100){percentOpacity=100;}
	}        
	this.percentOpacity = percentOpacity;
	if (this.percentOpacity) {        
		if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
		if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
		if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
		if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
	}
}

// A function to create the marker and set us up the event window
function createMarker(point, icon, index, name, html, url) {
	var marker = new GMarker(point, icon);
	
	// === store the name so that the tooltip function can use it ===
	marker.tooltip = '<div class="tooltip">'+name+'</div>';

	if (html != '') GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
	if (url != '') GEvent.addListener(marker, "click", function() { parent.location.href=url; });       

	gmarkers[i] = marker; 
	htmls[i] = html;
	i++;
	map.addOverlay(marker);

	//  ======  The new marker "mouseover" and "mouseout" listeners  ======
	GEvent.addListener(marker, "mouseover", function() { showTooltip(marker); });        
	GEvent.addListener(marker, "mouseout", function() { tooltip.style.visibility="hidden"; });        

	if (index != '') {
		var label = new ELabel(point, index, "mlcIconLabel", new GSize(-6,5), 80, true);
		map.addOverlay(label)            
	}
}

// ====== This function displays the tooltip ======
// it can be called from an icon mousover or a sidebar mouseover
function showTooltip(marker) {
	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(), map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}

// ===== This function is invoked when the mouse goes over an entry in the sidebar =====
// It launches the tooltip on the icon      
	function mymouseover(i) {
	showTooltip(gmarkers[i])
}

// ===== This function is invoked when the mouse leaves an entry in the sidebar =====
// It hides the tooltip      
function mymouseout() {
	tooltip.style.visibility="hidden";
}

// This function picks up the sidebar click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}