﻿function GxMarkerNamespace() {
var n4=(document.layers);
var n6=(document.getElementById&&!document.all);
var likesDXFilters = false;
var ie=(document.all);
if (ie) {
	var ua = navigator.userAgent;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null) {
		var rv = parseFloat( RegExp.$1 );
		if (rv >= 7) { 
			likesDXFilters = true;
		} else if ((rv >= 6) && ((new RegExp("SV1")).exec(ua) != null)) { 
			likesDXFilters = true;
		}
	}
}
var o6=(navigator.appName.indexOf("Opera") != -1);
var safari=(navigator.userAgent.indexOf("Safari") != -1);

function GxMarker( latlng, icon, tooltip, clickURL, target) {
    this.inheritFrom = GMarker;
    this.inheritFrom(latlng,icon);
    if ( typeof tooltip != "undefined" ) {
        this.setTooltip( tooltip );
		//alert("Tooltip set");
    }
	if(!target){this.target='_self';}else{this.target=target}
	this.clickURL = clickURL;
	this.imagesArray = '';
	this.oldImagePath = "";
	this.hoverIcon = null;//createSliceIcon(b.num,b.typeid,SLICE_STYLE_HIGHLIGHTED);
}

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.showHighlight = function() {
//	if (this.imagesArray.length == 0) {
//    	this.imagesArray = findImagesArray(this);
//	}

//	if (this.imagesArray.length != '') {
//		if (this.oldImagePath.length == 0) {
//			this.oldImagePath = this[this.imagesArray][0].src;
//		}
//		this[this.imagesArray][0].src = this.hoverIcon.image;
//		this[this.imagesArray][0].style.zIndex = 4999;
//		this.redraw(true);
//	}
}

GxMarker.prototype.removeHighlight = function() {
//	if (this.imagesArray.length == 0) {
//    	this.imagesArray = findImagesArray(this);
//	}

//	if (this.imagesArray.length > 0) {
//		this[this.imagesArray][0].src = this.oldImagePath;
//		this.oldImagePath = "";
//		this[this.imagesArray][0].style.zIndex = 4998;
//		this.redraw(true);
//	}
}

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);
        try { document.body.removeChild(this.tooltipObject); }
        catch (e) { ; }
}

GxMarker.prototype.onInfoWindowOpen = function() {
    this.hideMouseOver();
    GMarker.prototype.onInfoWindowOpen.call(this);
}

GxMarker.prototype.onClick = function() {
	if (this.clickURL)
	{
		saveMapPosition(arrGMapData); // save the map bounds in the cookie for later retrieval	
		window.open(this.clickURL, this.target);
	}
}

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>";

		// OPACITY START
			this.tooltip.opacity = 100;
            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;
		// OPACITY END
            //TODO, do fading?
            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";
    this.showHighlight();
	}
	
}

GxMarker.prototype.hideMouseOver = function() {
    if ( typeof this.tooltipObject != "undefined" ) {
        this.tooltipObject.style.display = "none";
    }
	this.removeHighlight();
}

function makeInterface(a) {
    var b = a || window;
    b.GxMarker = GxMarker;
}

makeInterface();
}

GxMarkerNamespace();
