//<![CDATA[
/*
Versie: 1.0

v1.0: 	Eerste bruikbare versie.
v1.1: 	div#tooltip_bg.
		oTooltipBg
		Is een background die achter
		de marker verschijnt als je
		hovered. Aansluitend aan onze
		tooltip.

*/



// Function used by getPosition
function subGPoints(a,b) {
	return new GPoint(a.x-b.x, a.y-b.y);
}

// Get position for the tooltip. Checks of right side is out of bounds. If so, display left.
// return value: object with x and y ( object.x, object.y )
function getPosition(map, point, elTooltipDiv){
	elMap = document.getElementById('map');

	var mapOffsetTop = elMap.offsetTop;
	var mapOffsetLeft = elMap.offsetLeft;

	var TlcLatLng = map.fromContainerPixelToLatLng(new GPoint(0,0),true);
	var TlcDivPixel = map.fromLatLngToDivPixel(TlcLatLng);
	var pointDivPixel = map.fromLatLngToDivPixel(point);
	var pointContainerPixel = subGPoints(pointDivPixel, TlcDivPixel);

	// Get bounds information for the map
	var bounds = map.getBounds();
	var boundsSpan	= bounds.toSpan();
	var longSpan = boundsSpan.lng();
	var mapWidth = map.getSize().width;

	var pointContainerPixelNew = new Object();

	// Convert pixel width to degrees ( map width )
	var tooltipWidthInDeg = (elTooltipDiv.offsetWidth + iIconWidth + 6 ) / mapWidth * longSpan;

	if ( point.lng() + tooltipWidthInDeg < bounds.getNorthEast().lng() ) {
		// Display right side of marker.
		pointContainerPixel.x = pointContainerPixel.x + iTTXMargin + mapOffsetLeft;
		pointContainerPixel.y = pointContainerPixel.y - iIconHeight + mapOffsetTop;

		// Change tooltipbg to facilitate right side
		/* Test case
		oTooltipBg.style.borderLeftWidth = "1px";
		elTooltipDiv.style.borderRightWidth = "1px";
		oTooltipBg.style.borderRightWidth = "0px";
		elTooltipDiv.style.borderLeftWidth = "0px";
		*/
	} else {
		// display left side of marker
		pointContainerPixel.x = pointContainerPixel.x - elTooltipDiv.offsetWidth - iIconWidth + iTTXMargin + mapOffsetLeft;
		pointContainerPixel.y = pointContainerPixel.y - iIconHeight + mapOffsetTop;


		// Change tooltipbg to facilitate right side
		/* Testcase
		oTooltipBg.style.borderLeftWidth = "0px";
		elTooltipDiv.style.borderRightWidth = "0px";
		oTooltipBg.style.borderRightWidth = "1px";
		elTooltipDiv.style.borderLeftWidth = "1px";
		*/
	}
	// return object
	return pointContainerPixel;
}


// check if tooltip is onscreen
function markerOnScreen(point) {
	var bounds = map.getBounds();
	// check for onscreen.
	if ( point.lng() < bounds.getNorthEast().lng() && point.lat() < bounds.getNorthEast().lat() && point.lng() > bounds.getSouthWest().lng() && point.lat() > bounds.getSouthWest().lat() ) {
		return true;
	} else {
		return false;
	}
}


// Show the tooltip div at coordinates with sText inside.
function showTooltip( map, point, elTooltipDiv, sText ) {

		elTooltipDiv.innerHTML = unescape(sText);

		oCoordinates = getPosition(map, point, oTooltip);

		elTooltipDiv.style.top = oCoordinates.y + 'px';
		elTooltipDiv.style.left = oCoordinates.x + 'px';

		/*
		Testcase hover behind marker

		map.getPane(3).appendChild(oTooltipBg);

		test = map.fromLatLngToDivPixel(point);

		oTooltipBg.style.left = ( test.x - 14 )  + "px";
		oTooltipBg.style.top = ( test.y - 14 ) + "px";
		*/
}

// Show tooltip by calling this function with eg. an anchor.
function showTooltipExternal (map, marker, oTooltip ) {
	if ( markerOnScreen(marker.getPoint()) ) {
		showTooltip(map, marker.getPoint(), oTooltip, marker.tooltip);
	}
}

// Hides the tooltip div.
function hideTooltip () {
	oTooltip.style.left = '-500px';oTooltip.style.top = '0px';
	// oTooltipBg.style.left = '-500px'; TEstcase hover behind marker

}

/*

function load() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(52.011504,4.359399), 10);
	var point = new GLatLng(52.011504,4.359399);
	marker	= new GMarker(point)
	map.addOverlay(marker);

	// Important: information to be shown in tooltip.
	marker.tooltip = "Im the markers tooltip.";
	// Important: reference to tooltip element.
	oTooltip = document.getElementById('tooltip');

	GEvent.addListener(marker, "mouseover", function() {
		if ( markerOnScreen(marker.getPoint()) ) {
			oCoordinates = getPosition(map, marker.getPoint(), oTooltip);
			showTooltip(map, marker.getPoint(), oTooltip, marker.tooltip);
		}
	});

	GEvent.addListener(marker, "mouseout", function() {
		oTooltip.style.left = "-500px";
		oTooltip.style.top = "0px";
	});
  }
}

// Define heigth and width of icon used.
var iIconHeight = 11;
var iIconWidth = 23;

// define the x-axis margin to right
var iTTXMarginR = 13;

// define the x-axes margin to left
var iTTXMarginL = -3;


// Global variables needed
var oTooltip;
var elMap;

*/



//]]>// JavaScript Document