/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 * @url http://onemarco.com
 * @version 1.0
 * This code is public domain
 */

/**
 * The GTooltip class is an addon designed for the Google Maps GMarker class. 
 * @constructor
 * @param {GMarker} marker
 * @param {String} text
 * @param {Number} padding
 */
function GTooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

GTooltip.prototype = new GOverlay();

GTooltip.prototype.initialize = function(map){
	
	var innerDiv = document.createElement("div");
		innerDiv.appendChild(document.createTextNode(this.text_));
	
	var div = document.createElement("div");
		div.appendChild(innerDiv);
		div.className = 'tooltip';
		div.style.position = 'absolute';
		div.style.visibility = 'hidden';
		
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	
	this.map_ = map;
	this.div_ = div;
}

GTooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

GTooltip.prototype.copy = function(){
	return new GTooltip(this.marker_,this.text_,this.padding_);
}

GTooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = 5 + xPos + 'px';
}

GTooltip.prototype.show = function(){
	this.redraw(true);
	this.div_.style.visibility = 'visible';
}

GTooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}
