/*
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)

  first, the tt needs to be created and added to the map

    setup tooltip methods of GMarkers:
    
    marker.setTooltip(BpTooltip_object, html);
    var tt = marker.getTooltip();
    var html = marker.getTooltipHtml();
    marker.setTooltipHtml(html);
    var latlng = marker.getTooltipPoint();
    marker.setMaintainTooltip(bool);

0.1  - initial release
0.11 - requires manual calling
       optional constructor argument
       better vertical placement in relation to marker: centered

*/

function setupSetTooltip(constructor) {
  if (typeof(constructor) != 'function')
    constructor = GMarker;

	constructor.prototype.setTooltip = function(tt,html) {
		if(!tt && this._ttOverListener) {
			GEvent.removeListener(this._ttOverListener);
			delete this._ttOverListener;
			GEvent.removeListener(this._ttOutListener);
			delete this._ttOutListener;
			if(this._tt.getCurrentMarker() === this)
				this._tt.setCurrentMarker();
			this._tt = null;
			return;
		}

		this._tt = tt;
		this._ttHtml = html;

    var overListener = function() { var ok = this.getEventTarget;
			var tt = this.getTooltip();
			if(tt) {
				tt.setCurrentMarker(this);
				tt.setHtml(this._ttHtml);
				tt.setPoint(this.getTooltipPoint());
				tt.show();
			}
		};

    if (typeof(constructor.prototype.getEventTarget) == 'function')
  		this._ttOverListener = GEvent.bindDom(this.getEventTarget(),'mouseover',this,overListener);
    else
      this._ttOverListener = GEvent.addListener(this,'mouseover',overListener);

    var outListener = function() {
			var tt = this.getTooltip();
			if(tt && !this._ttMaintain)
				tt.setCurrentMarker();
		};

    if (typeof(constructor.prototype.getEventTarget) == 'function')
  		this._ttOutListener = GEvent.bindDom(this.getEventTarget(),'mouseout',this,outListener);
    else
      this._ttOutListener = GEvent.addListener(this,'mouseout',outListener);
	};

	constructor.prototype.getTooltip = function() {
		return this._tt;
	};

	constructor.prototype.getTooltipHtml = function() {
		return this._ttHtml;
	};

	constructor.prototype.setTooltipHtml = function(html) {
		this._ttHtml = html;
	};

	constructor.prototype.getTooltipPoint = function() {
		var margin = 5; // px to the right (or left) of the icon

		var map = this._tt._map;
		var pixel = map.fromLatLngToDivPixel(this.getPoint());
  	var icon = this.getIcon();
		pixel.x += (icon.iconSize.width - icon.iconAnchor.x) + margin;
		// find where it would be vertically centered
		pixel.y = parseInt(pixel.y - icon.iconAnchor.y + 0.5 * icon.iconSize.height - 0.5 * this._tt.getHeight());

		// how many pixels are between the margin and the right side of the map?
		var nePixel = map.fromLatLngToDivPixel(map.getBounds().getNorthEast());
		var width = this._tt.getWidth();
		if(nePixel.x - width < pixel.x)
			pixel.x -= ((2 * margin) + icon.iconSize.width) + width;

		return map.fromDivPixelToLatLng(pixel);
	};

  constructor.prototype.showTooltip = function(bool) {
		var tt = this.getTooltip();
		if(!tt) return;
			
    tt.setCurrentMarker(this);
    tt.setHtml(this._ttHtml);
    tt.setPoint(this.getTooltipPoint());
    tt.show();
  };


	constructor.prototype.hideTooltip = function(bool) {
		var tt = this.getTooltip();
		if(!tt) return;
			
    tt.hide();
  };

  // this is not a good, tested version -- use the tested version instead
	constructor.prototype.setMaintainTooltip = function(bool) {
		var tt = this.getTooltip();
		if(!tt) return;
			
		this._ttMaintain = bool;
		if(bool)
      this.showTooltip();
		else
			this.hideTooltip();
	};
}
setupSetTooltip.version = 0.11;
