/*
Copyright 2005-2007 James Tolley - http://www.bitperfect.com | http://www.gmaptools.com
All rights reserved.
This software is released under the LGPL. (http://www.opensource.org/licenses/lgpl-license.php)

	BpTooltip - a bare-bones tooltip class

  create it (set hide to , add it to the map (hidden), add it to all markers with marker.setTooltip(tt, html);
  and that's it.

TODO:
  rework this to inherit from BpOverlay
  when the marker flips from one side of the world to the other, the tooltip should flip with it.
  when the map is dragged so the tooltip is off the edge, it doesn't move to the other side of the marker
  the tooltip doesn't show all of itself when the marker is near the top or bottom of the map

*/

function BpTooltip() {
	
function BpTooltip(latlng,html,hide) {
	this._latlng = latlng || new GLatLng(0,0);
	this._html   = html   || '';

  if (arguments.length >= 3)
  	this._hide = hide;
  else
    this._hide = true;
}

BpTooltip.prototype = new GOverlay();

var onZoomListener;
BpTooltip.prototype.initialize = function(map) {
	this._map = map;

  if (! this._latlng)
    this._latlng = map.getCenter();

	this._div = document.createElement('div');
	this._div.innerHTML = this._html;

	this._div.style.position				= 'absolute';
  this._div.style.border          = '1px solid black';
  this._div.style.backgroundColor = 'white';
  this._div.style.filter          = 'alpha(opacity:80)';
  this._div.style.KHTMLOpacity    = .8;
  this._div.style.MozOpacity      = .8;
  this._div.style.opacity         = .8;
  this._div.style.fontWeight      = 'bold';
  this._div.style.whiteSpace      = 'nowrap';
  this._div.style.paddingRight    = '3px';
  this._div.style.paddingLeft     = '3px';
	this._div.style.display 				= 'none';

	// put it off-map for now
	this._div.style.top  = - screen.height;
	this._div.style.left = - screen.width;

	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(this._div);

	// get the height and width
	this._height = this._div.offsetHeight;
	this._width  = this._div.offsetWidth;

	if(!this._hide)
		this._div.style.display	= '';

	// setup the code to reposition the tooltip on zoom
  if (!onZoomListener)
  	onZoomListener = GEvent.addListener(map,'zoomend',GEvent.callback(this,this.onZoomEnd));

	return this._div;
};

BpTooltip.prototype.onZoomEnd = function() {
	if(this._marker)
		this.setPoint(this._marker.getTooltipPoint());
};

BpTooltip.prototype.setPoint = function(ll) {
	this._latlng = ll;
	this.redraw(true);
};

BpTooltip.prototype.getPoint = function() {
	return this._latlng;
};

BpTooltip.prototype.copy = function() {
	return new BpTooltip(this._latlng,this._str);
};

BpTooltip.prototype.show = function() {
	if(this._div && this._div.style)
		this._div.style.display = '';
};

BpTooltip.prototype.hide = function() {
	if(this._div && this._div.style)
		this._div.style.display = 'none';
};

BpTooltip.prototype.getWidth = function() {
	if(this._div.style.display != 'none')
		return this._div.offsetWidth || this._div.style.pixelWidth || 0;
	else
		return this._width;
};

BpTooltip.prototype.getHeight = function() {
	if(this._div.style.display != 'none')
		return this._div.offsetHeight || this._div.style.pixelHeight || 0;
	else
		return this._height;
};

BpTooltip.prototype.setCurrentMarker = function(marker) {
	this._marker = marker;
	if(!marker) {
		this.hide();
		this.setHtml('');
	}
};

BpTooltip.prototype.getCurrentMarker = function() {
	return this._marker;
};

BpTooltip.prototype.getHtml = function() {
	return this._html;
};

BpTooltip.prototype.setHtml = function(html) {
	this._html = html;
	this._div.innerHTML = html;

	// return if not mapped
	if(!(this._div && this._div.parentNode && this._div.parentNode === this._map.getPane(G_MAP_FLOAT_SHADOW_PANE)))
		return;

	// store the size
	if(this._div.style.display != 'none') {
		this._height = this.getHeight();
		this._width  = this.getWidth();
	} else { // move the thing somewhere else, check the size, return to current location
		// move it
		var top  = this._div.style.top;
		var left = this._div.style.left;
		this._div.style.top  = - screen.height;
		this._div.style.left = - screen.width;
		
		// check it
		this.show();
		this._height = this.getHeight();
		this._width  = this.getWidth();
		this.hide();

		// remove it
		this._div.style.top  = top;
		this._div.style.left = left;
	}
};

BpTooltip.prototype.redraw = function(really) {
  var point = this._map.fromLatLngToDivPixel(this._latlng);
  this._div.style.top  = point.y + 'px';
	this._div.style.left = point.x + 'px';
};

BpTooltip.prototype.remove = function() {
	if(!this._div || !this._div.parentNode)
		return;

	this._div.parentNode.removeChild(this._div);
	delete this._div;
};
window.BpTooltip = BpTooltip;
}
BpTooltip();