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

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

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	var div = new Element('div', {
        'class'     : 'tooltip'
       ,'styles'    : {
            'position'  : 'absolute'
           ,'visibility': 'hidden'
           }
       });
    div.grab(new Element('span', {text: this.text_}));
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
    
    this.div_.dimensions = this.div_.getDimensions();
}

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

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

Tooltip.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 + 10);
    
    var endpoint = xPos + this.div_.dimensions.width;
    if (endpoint > mapsize.width)
    {
        var diff = endpoint - mapsize.width;
        xPos -= diff + 2;
    }

	var yPos = markerPos.y - iconAnchor.y + this.div_.clientHeight;

	this.div_.setStyle('top', yPos + 'px');
	this.div_.setStyle('left', xPos + 'px');
}

Tooltip.prototype.show = function(){
    //this.redraw(true);
	this.div_.setStyle('visibility', 'visible');
}

Tooltip.prototype.hide = function(){
	this.div_.setStyle('visibility', 'hidden');
}
