//	mapTooltip 1.1
//	
//	Copyright (c) 2009 Gabriel Svennerberg (svennerberg.com)
//	Licensed under the MIT licence (http://www.opensource.org/licenses/mit-license.php)
// 
//	Subclassed GOverlay
//	
//	Author:		Gabriel Svennerberg
//	Email:		gabriel@svennerberg.com
//	Web:		  http://www.svennerberg.com
//	Date:  		Version 1.0: 2009-03-30
//            Version 1.1: 2010-01-03
//
//	Adds tooltip as an overlay to objects in Google maps. So far it's been tested with 
//	GMarkers, GPolyLines and GPolygons
//
//	To get it to work it assumes that you've loaded the Google Maps Api
//
//	The constructor takes two reguierd parameters and one optional
//
//		MapTooltip(reference:object, tooltip:string, opts?:object)
//
//	opts is an object literal which take the following parameters
//
//		width				      The width of the tooltip, ex: "100px"
//		padding			      the padding around the content of the tooltip
//						          	For a 3px padding: "3px"
//						          	For a 2px top/bottom padding and a 5px left/right padding: "2px 5px"
//
//		backgroundColor		Backgroundcolor of the tooltip, ex: "#ff9"
//		color				      The color of the text inside the tooltip, ex: "#000"
//		border				    Styling of the border of the tooltip, ex: "1px solid green"
//		fontSize			    The size of the text inside the tooltip, ex "1em"
//		offsetX			    	The horizontal distance in pixels from the pointer to the tooltip, ex "10"
//		offsetY			    	The vertical distance in piexels from the pointer to the tooltip, ex: "10"
//
//	For more information on how to use this extension se http://www.svennerberg.com/2009/03/announcing-maptooltip/
//
//	This extension is heavily inspired by the article "custom info window / popup for Google Maps, or loving the goverlay" 
//  at http://danmarvelo.us/older/2007/9/10/custom-info-window-for-google/
//	It's released under the MIT licence (http://www.opensource.org/licenses/mit-license.php) which means it's 
//	free for any use as long as this text is intact. If you use it for a project or alters it I'll be happy if
//	you send me an email (gabriel@svennerberg.com). It's just fun knowing what it's been used for. And if you made any
//	improvements I could add them as well.
//
//	Changes in Version 1.1
//
//  Functionality for preventing the tooltip to appear outside the map boundries was added.
//
var MapTooltip = function(obj, html, options) {
	this.obj = obj;
	this.html = html;
	this.options = options || {};
}

MapTooltip.prototype = new GOverlay();

MapTooltip.prototype.initialize = function(map) {
	var div = document.getElementById('MapTooltipContainer');
  var that = this;

	if (!div) {
		var div = document.createElement('div');
		div.setAttribute('id', 'MapTooltipContainer');
	}
	
	// Setting the apperance of the tooltip
	if (this.options.maxWidth || this.options.minWidth) {
		div.style.maxWidth = this.options.maxWidth || '150px';
		div.style.minWidth = this.options.minWidth || '150px';
	} else {
		div.style.width = this.options.width || '150px';
	}
	
	div.style.padding = this.options.padding || '3px 5px';
	div.style.backgroundColor = this.options.backgroundColor || '#ff9';
	div.style.border = this.options.border || '1px solid #fc0';
	div.style.fontSize = this.options.fontSize || '80%';
	div.style.color = this.options.color || '#000';
	
	// Positioning the tooltip
	div.innerHTML = this.html;
	div.style.position = 'absolute';
	div.style.zIndex = '1000';

	var offsetX = this.options.offsetX || 10;
	var offsetY = this.options.offsetY || 0;
  
  var bounds = map.getBounds();
  rightEdge = map.fromLatLngToDivPixel(bounds.getNorthEast()).x;
  bottomEdge = map.fromLatLngToDivPixel(bounds.getSouthWest()).y;
  
  
	// Because GPolyLine and GPolygon mousemove event doesn't return the mouseposition, we need to get it
	// in some other way. Since GMap2 returns the mouseposition on mousemove we use that.
	var mapev = GEvent.addListener(map, 'mousemove', function(latlng) {
		GEvent.removeListener(mapev);

		var pixelPosX = (map.fromLatLngToDivPixel(latlng)).x + offsetX;
		var pixelPosY = (map.fromLatLngToDivPixel(latlng)).y - offsetY;
		div.style.left = pixelPosX + 'px';
		div.style.top = pixelPosY + 'px';

  	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    
    // Adjusting tooltip position
    // This have to be done after the tooltip has been added to the map for the offsetWidth and offsetHeight to be able 
    // to calculate the correct dimensions of the div containing the tooltip
        
    // Check to see if the tooltip is outside right edge of the map
    if ( (pixelPosX + div.offsetWidth) > rightEdge ) {
      div.style.left = (rightEdge - div.offsetWidth - 10) + 'px';
    }

    // Check to see if the tooltip is outside the bottom edge of the map
    if ( (pixelPosY + div.offsetHeight) > bottomEdge ) {
      div.style.top = (bottomEdge - div.offsetHeight - 10) + 'px';
    }
    
  });

	this._map = map;
	this._div = div;
}

MapTooltip.prototype.remove = function() {
	if(this._div != null) {
		this._div.parentNode.removeChild(this._div);	
	}
}

MapTooltip.prototype.redraw = function(force) {
	// Not implemented
}
