/*
 GxMarker version 1.3

 SYNOPSIS
    This version is compatible with Google Maps API Version 2
	
    A more full-featured marker that supports tooltips and hover events.  The
    first iteration just supports triggering of mouse over events, and tooltips.
   
    To setup a tooltip, pass in a third parameter (after the icon) to the
    GxMarker class:
        var marker = new GxMarker( new GLatLng(lat,lng), icon, "My Tooltip" );
        map.addOverlay(marker);

    Or:
        var marker = new GxMarker( new GLatLng(lat,lng) );
        marker.setTooltip("My Tooltip");
        map.addOverlay(marker);

    As of 1.1, changes to setTooltip() should work after the initial invocation

    Please refer to http://code.toeat.com/package/gxmarker for additional
    documentation.
    
    TESTED PLATFORMS:
        Linux: Firefox
        Windows: Firefox, IE6
        Mac OS X (Panther): Safari

    There is no warranty of functionality of this code, if you wish to use it
    and it does not work for you, I recommend you submit a patch.  This software
    is licensed under the GNU Lesser General Public License (LGPL):
    the full text at: http://opensource.org/licenses/lgpl-license.php
	
	Update: 04/07/06 - modified to load with API v2.44+ of the Google Maps API
	Modified by Robert Aspinall - raspinall (AT) gmail (dot) com
	
	Update: 04/13/06 - modified to add ability for relatively positioned static (persistent) labels
	Modified by Robert Aspinall - raspinall (AT) gmail (dot) com
	
	New constructor:
		var marker = new GxMarker(new GLatLng(lat, lng), icon, "My Tooltip", "static");
	Or:
		var marker = new GxMarker(new GLatLng(lat, lng), icon, "My Tooltip", "static", x_offset, y_offset);
	
	If x and y offsets are specified, the label will be positioned relative to the standard location, modified by those offsets.
	To position a label to addthe left, a negative x offset is required.
	To position a label below the marker, a negative y offset is required.
	
*/

function GxMarkerNamespace() {

var n4=(document.layers);
var n6=(document.getElementById&&!document.all);
var ie=(document.all);
var o6=(navigator.appName.indexOf("Opera") != -1);
var safari=(navigator.userAgent.indexOf("Safari") != -1);
var currentSpan = new GBounds();
var xpos = 0;
var ypos = 0;

var staticlabel = 0;

function GxMarker( a, b, tooltip, staticlabel, xpos, ypos ) {
    this.inheritFrom = GMarker;
    this.inheritFrom(a,b);
    if ( !currentSpan.minX || a.x < currentSpan.minX ) currentSpan.minX = a.x;
    if ( !currentSpan.maxX || a.x > currentSpan.maxX ) currentSpan.maxX = a.x;
    if ( !currentSpan.minY || a.y < currentSpan.minY ) currentSpan.minY = a.y;
    if ( !currentSpan.maxY || a.y > currentSpan.maxY ) currentSpan.maxY = a.y;
    if ( typeof tooltip != "undefined" ) {
        this.setTooltip( tooltip );
    }
	//this.align = position;
	if (staticlabel == "static") {
		this.staticlabel = 1;
		if (typeof(xpos) != "undefined") {
			this.xpos = xpos;
		} else { this.xpos = 0; }
		if (typeof(ypos) != "undefined") {
			this.ypos = ypos;
		} else { this.ypos = 0; }
	}
}

GxMarker.redraw = function(e) {
	
}

GxMarker.prototype = new GMarker(new GLatLng(1, 1));

GxMarker.prototype.setTooltip = function( string ) {
    this.removeTooltip();
    this.tooltip = new Object();
    this.tooltip.opacity  = 80;
    this.tooltip.contents = string;
};

GxMarker.prototype.initialize = function( a ) {
    try {
        GMarker.prototype.initialize.call(this, a);
		
        // Setup the mouse over/out events
		GEvent.bind(this, "mouseover", this, this.onMouseOver);
		GEvent.bind(this, "mouseout", this, this.onMouseOut);
		//Show the tooltip all the time if the marker is static
		if (this.staticlabel) {
			this.showTooltip();
		}
    } catch(e) {
		alert(e);
    }
}

GxMarker.prototype.remove = function( a ) {
    GMarker.prototype.remove.call(this);
    this.removeTooltip();
}

GxMarker.prototype.removeTooltip = function() {
    if ( this.tooltipObject ) {
        this.map.div.removeChild(this.tooltipObject);
        this.tooltipObject = null;
    }
}

GxMarker.prototype.onInfoWindowOpen = function() {
    //If not a static marker, hide the tooltip
	if (!this.staticlabel) { 
		this.hideTooltip();
	}
    GMarker.prototype.onInfoWindowOpen.call(this);
}

GxMarker.prototype.onMouseOver = function() {
    //this.showTooltip();
    //GEvent.trigger(this, "mouseover");
};

GxMarker.prototype.onMouseOut = function() {
    //If not a static marker, trigger the mouseout function
	if (!this.staticlabel) {
		this.hideTooltip();
    	GEvent.trigger(this, "mouseout");
	}
};

GxMarker.prototype.showTooltip = function() {
    if ( this.tooltip ) {
        if ( !this.tooltipObject ) {
            var opacity = this.tooltip.opacity / 100;
            this.tooltipObject = document.createElement("div");
            this.tooltipObject.style.display    = "none";
            this.tooltipObject.style.position   = "absolute";
            this.tooltipObject.style.background = "#fff";
			this.tooltipObject.style.color = "#000000";
            this.tooltipObject.style.padding    = "0";
            this.tooltipObject.style.margin     = "0";
            this.tooltipObject.style.MozOpacity = opacity;
            this.tooltipObject.style.filter     = "alpha(opacity=" + this.tooltip.opacity + ")";
            this.tooltipObject.style.opacity    = opacity;
			//this.tooltipObject.style.zIndex = map.getPane(G_MAP_FLOAT_PANE).
            this.tooltipObject.style.zIndex     = 50000;
            this.tooltipObject.innerHTML        = "<div class=\"markerTooltip\">" + this.tooltip.contents + "</div>";
			map.getPane(G_MAP_MARKER_PANE).appendChild(this.tooltipObject);
        }
		var c = map.fromLatLngToDivPixel(new GLatLng(this.getPoint().y, this.getPoint().x));
		
		//If not in static label mode, position labels normally.
		if (!this.staticlabel) {
			this.tooltipObject.style.top = c.y - ( this.getIcon().iconAnchor.y + 5) + "px";
			this.tooltipObject.style.left = c.x + ( this.getIcon().iconSize.width - this.getIcon().iconAnchor.x + 5 ) + "px";
		} else {
			this.tooltipObject.style.top = c.y - ( this.getIcon().iconAnchor.y + 5) - parseInt(this.ypos) + "px";
			this.tooltipObject.style.left = c.x + ( this.getIcon().iconSize.width - this.getIcon().iconAnchor.x + 5 ) + parseInt(this.xpos) + "px";
		}
        this.tooltipObject.style.display = "block";
    }
}

GxMarker.prototype.hideTooltip = function() {
    if ( this.tooltipObject ) {
        this.tooltipObject.style.display = "none";
    }
}

GMap.prototype.flushOverlays = function() {
    currentSpan = new GBounds();
    this.clearOverlays();
}


GMap.prototype.zoomToMarkers = function() {
    var span = new GSize( currentSpan.maxX - currentSpan.minX, currentSpan.maxY - currentSpan.minY );
    for ( var zoom = 0; zoom < this.spec.numZoomLevels; zoom++ ) {
        var ppd = this.spec.getPixelsPerDegree(zoom);
        var pixelSpan = new GSize(
            Math.round(span.width * ppd.x), Math.round(span.height * ppd.y));
        if ( pixelSpan.width  <= this.viewSize.width &&
             pixelSpan.height <= this.viewSize.height )
        { break; }
    }
	this.centerAndZoom( new GPoint( currentSpan.minX + (span.width/2), currentSpan.minY + (span.height/2) ), zoom);
}

function makeInterface(a) {
    var b = a || window;
    b.GxMarker = GxMarker;
}

makeInterface();
}

GxMarkerNamespace();

