 
function Tooltip(gmarker, markup, padding, width){
	this.marker = gmarker;
	this.text = markup;
	this.padding_ = padding;
	this.dragMode = false;
	this.divX = 0;
	this.divY = 0;
	this.offsetX = 0;
	this.offsetY = 0; 
	this.mapCenter = new GLatLng(0, 0);
	this.div = null;
	this.map = null;
	this.width = width;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(objMap){
	this.map = objMap;
    this.mapCenter = this.map.getCenter();
	
	this.div = document.createElement("div");
	this.div.innerHTML = this.text;
	this.div.className = 'tooltip';
	this.div.style.position = 'absolute';
	this.div.style.visibility = 'hidden';
	this.div.style.textAlign = 'center';
	if(this.width === undefined || this.width == 0)
	    this.width = calculateWidth(extractElementText(this.div.childNodes[0])) ;
	    
	this.div.style.width = this.width + 'px';
	
	var pane = this.map.getPane(G_MAP_MAP_PANE).parentNode.parentNode.parentNode;
	pane.appendChild(this.div);  
	
	if(this.map.TooltipsCollections == null) {
	    this.map.TooltipsCollections = new Array();
	}
	this.map.TooltipsCollections[this.map.TooltipsCollections.lenght] = this;
	
	var t = this;
	var f = function(){
        t.reposition();
	};
	
	GEvent.addListener(this.map, "zoomend", f);
	GEvent.addListener(this.map, "dragend", f);
	GEvent.addListener(this.map, "dblclick", f);
	GEvent.addListener(this.map, "click", f);
	
	$(this.div).bgiframe();
}

Tooltip.prototype.remove = function(){
	this.div.parentNode.removeChild(this.div);
}

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker,this.text,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
     
    this.mapCenter = this.map.getCenter();
	
	var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
	var iconAnchor = this.marker.getIcon().iconAnchor;
	
	this.divX = Math.round(markerPos.x - this.div.clientWidth / 2);
	this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;
	 
    this.setPosition(new GSize(this.divX, this.divY));
}

Tooltip.prototype.reposition = function(){
        var oldPos = this.map.fromLatLngToDivPixel(this.mapCenter);
        var newPos = this.map.fromLatLngToDivPixel(this.map.getCenter());

        this.offsetX += oldPos.x - newPos.x;
        this.offsetY += oldPos.y - newPos.y;
        
        this.mapCenter = this.map.getCenter();
}

Tooltip.prototype.show = function(){
    this.reposition(); 
    
    var sz = new GSize(this.offsetX, this.offsetY);
    this.setPosition(sz);
	this.div.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div.style.visibility = 'hidden';
}


Tooltip.prototype.setPosition = function(newOffset){
    var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
	var iconAnchor = this.marker.getIcon().iconAnchor;
	
	this.divX = Math.round(markerPos.x - this.div.clientWidth / 2);
	this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;

    var offset = new GSize(this.divX + newOffset.width, this.divY + newOffset.height);

    //decide where to put the tooltip 
    if ((this.div.clientWidth)  > (this.map.getContainer().clientWidth)) { //stretched throuhout whole map - on wide side - use default
	    this.divX = Math.round(markerPos.x - this.div.clientWidth / 2);
	    this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;
    }
    else if(offset.height < 0 && offset.width < 0){ //top left
	    this.divX = markerPos.x + this.padding_;
	    this.divY = markerPos.y + this.padding_;
    }
    else if (offset.height >= 0 && (offset.width + this.div.clientWidth)  > (this.map.getContainer().clientWidth)) { //middle right
	    this.divX = Math.round(markerPos.x - this.div.clientWidth);
	    this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;
    }
    else if (offset.height < 0 && (offset.width + this.div.clientWidth)  >= (this.map.getContainer().clientWidth)) { //top right
	    this.divX = Math.round(markerPos.x - this.div.clientWidth);
	    this.divY = markerPos.y + this.padding_;
    }
    else if (offset.height >= 0 && offset.width < 0) { //middle left
	    this.divX = markerPos.x + this.padding_;
	    this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;
    }
    else if (offset.height < 0 && offset.width >= 0) { //middle top
	    this.divX = Math.round(markerPos.x - this.div.clientWidth / 2);
	    this.divY = markerPos.y + this.padding_;
    }
    else{ //in the middle
	    this.divX = Math.round(markerPos.x - this.div.clientWidth / 2);
	    this.divY = markerPos.y - iconAnchor.y - this.div.clientHeight - this.padding_;
    }

    offset = new GSize(this.divX + newOffset.width, this.divY + newOffset.height);
	
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, offset); 
	pos.apply(this.div); 
}

function calculateWidth(text){
    return text.length * 7 + ( (text.length < 35) ? 20 : -20);
}
function extractElementText( element ) { 
    var text = '';
    if( !element || element.nodeType == 8 ) // ignore html comments 
        return ""; 
    var tagName = element.tagName ? element.tagName.toLowerCase() : ""; 
    if( tagName == "input" || tagName == "textarea" )    
        return ""; 

    var text = element.nodeValue || ""; 
    for( var i = 0; i < element.childNodes.length; i++ ) 
        text += extractElementText( element.childNodes[ i ] ); 

    return text; 
}

//create marker with html info
function createMarkerWithInfo(point, htmlInfo, title, tooltipWidth) {
    var marker = new GMarker(point, { title: title, icon: icon });

    map.addOverlay(marker);

    marker.bindInfoWindowHtml(htmlInfo, { maxWidth: tooltipWidth });

    return marker;
}


//create marker with html info
function createMarker(point, htmlInfo, url, tooltipWidth) {  
    var marker = new GMarker(point, icon);  
	if(!url)url = '#';

    GEvent.addListener(marker, 'click', function() { 
        document.location = url;  
    }); 
     
    var tooltip = new Tooltip(marker, htmlInfo, 4, tooltipWidth);
    marker.tooltip = tooltip;
    map.addOverlay(marker);
    map.addOverlay(tooltip);

    GEvent.addListener(marker,'mouseover',function(){
        this.tooltip.show();
    });
    GEvent.addListener(marker,'mouseout',function(){
        this.tooltip.hide();
    });
    map.addOverlay(marker);
    return marker;
  }