// based on gxmarker.1.js

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);

function GxMarker( a, b, tooltip, clickURL) {
    this.inheritFrom = GMarker;
    this.inheritFrom(a,b);
    if ( typeof tooltip != "undefined" ) {
        this.setTooltip( tooltip );
    }
	this.clickURL = clickURL;
}

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

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

GxMarker.prototype.initialize = function( a ) {
    try {
        GMarker.prototype.initialize.call(this, a);
        // Setup the mouse over/out events
		GEvent.bindDom(this, "mouseover", this, this.onMouseOver);
		GEvent.bindDom(this, "mouseout",  this, this.onMouseOut);
		GEvent.bindDom(this, "click",  this, this.onClick);
		this.map = a;
    } catch(e) {
    	console.error(e);
    }
}

GxMarker.prototype.remove = function( a ) {
    GMarker.prototype.remove.call(this);
    if ( this.tooltipObject )
        //this.map.div.removeChild(this.tooltipObject);
        document.body.removeChild(this.tooltipObject);
}

GxMarker.prototype.onInfoWindowOpen = function() {
    this.hideMouseOver();
    GMarker.prototype.onInfoWindowOpen.call(this);
}

GxMarker.prototype.onClick = function() {
	if (this.clickURL)
	{
		window.open(this.clickURL);
	}
}

GxMarker.prototype.onMouseOver = function() {
    this.showMouseOver();
};

GxMarker.prototype.onMouseOut = function() {
    this.hideMouseOver();
};

GxMarker.prototype.showMouseOver = function() {
    if ( this.tooltip ) {
        if ( typeof this.tooltipObject == "undefined" ) {
            this.tooltipObject = document.createElement("div");
            this.tooltipObject.style.display    = "none";
            this.tooltipObject.style.position   = "absolute";
            this.tooltipObject.style.background = "#fff";
            this.tooltipObject.style.padding    = "0";
            this.tooltipObject.style.margin     = "0";
            this.tooltipObject.style.zIndex     = 50000;
            this.tooltipObject.innerHTML        = "<div class=\"markerTooltip\">" + this.tooltip.contents + "</div>";

/*
            var opacity = this.tooltip.opacity / 100;
            this.tooltipObject.style.MozOpacity = opacity;
            this.tooltipObject.style.filter     = "alpha(opacity=" + this.tooltip.opacity + ")";
            this.tooltipObject.style.opacity    = opacity;
*/

            document.body.appendChild(this.tooltipObject);
        }

		function getElementPosition(elem) {
		    var offsetTrail = elem;
		    var offsetLeft = 0;
		    var offsetTop = 0;
		    while (offsetTrail) {
		        offsetLeft += offsetTrail.offsetLeft;
		        offsetTop += offsetTrail.offsetTop;
		        offsetTrail = offsetTrail.offsetParent;
		    }
		    if (navigator.userAgent.indexOf("Mac") != -1 && 
		        typeof document.body.leftMargin != "undefined") {
		        offsetLeft += document.body.leftMargin;
		        offsetTop += document.body.topMargin;
		    }
		    return {left:offsetLeft, top:offsetTop};
		}

		
		var tlcLatLng = this.map.fromContainerPixelToLatLng(new GPoint(0,0), true);
		var tlcDivPixel = this.map.fromLatLngToDivPixel(tlcLatLng);
		var pointDivPixel = this.map.fromLatLngToDivPixel(this.getPoint());
		var c = new GPoint(pointDivPixel.x-tlcDivPixel.x, pointDivPixel.y-tlcDivPixel.y);
		var mapPos = getElementPosition(this.map.getContainer());

		this.tooltipObject.style.left = "-1000px";
        this.tooltipObject.style.top = "-1000px";
        this.tooltipObject.style.display = "block";

        var tipLeft = c.x - this.getIcon().iconAnchor.x + mapPos.left;
        if (tipLeft > (document.body.offsetWidth/2)) {
        	tipLeft -= this.tooltipObject.offsetWidth;
        } else {
        	tipLeft += this.getIcon().iconSize.width;
        }
        this.tooltipObject.style.left = tipLeft + "px";

        var tipTop = c.y - this.getIcon().iconAnchor.y + mapPos.top;
        if ((c.y + this.map.getContainer().offsetTop) > (this.map.getContainer().offsetHeight/2)) {
        	tipTop -= this.tooltipObject.offsetHeight;
        } else {
        	tipTop += this.getIcon().iconSize.height;
        }
        this.tooltipObject.style.top = tipTop + "px";
    }
}

GxMarker.prototype.hideMouseOver = function() {
    if ( typeof this.tooltipObject != "undefined" ) {
        this.tooltipObject.style.display = "none";
    }
}

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

makeInterface();
}

GxMarkerNamespace();
