var infoboxID = "gMapInfobox"
var infoboxTimeoutID = null

function onMouseOverShowCustomInfobox(oMarker, obj) {
	var posX = 0
	var posY = 0
	// get object position
	var objPos = getObjectPosition(obj)
	posX = parseInt(objPos.split("|")[0])
	posY = parseInt(objPos.split("|")[1])
	// get object dimensions
	var oWidth = obj.offsetWidth
	var oHeight = obj.offsetHeight
	// show infobox
	showCustomInfobox(oMarker, (posX - 10), posY)
}

function delayedShowCustomInfobox(oMarker, posX, posY) {
	showCustomInfobox(oMarker, posX, posY, 1000)
}

function showCustomInfobox(oMarker, posX, posY) {
	// create container
	if ( !document.getElementById(infoboxID) ) {
		var _infoboxDiv = document.createElement("div")
		_infoboxDiv.id = infoboxID
		_infoboxDiv.className = "gMapInfobox"
		_infoboxDiv.style.display = "none"
		document.body.appendChild(_infoboxDiv)
	}
	// need to clear timeout
	if (infoboxTimeoutID != null) {
		clearTimeout(infoboxTimeoutID);
		infoboxTimeoutID = null;
	}
	// get container
	var _infoboxContainer = document.getElementById(infoboxID)
	// set contents
	_infoboxContainer.innerHTML = oMarker.description
	// position the div
	_infoboxContainer.style.position = "absolute"
	_infoboxContainer.style.zIndex = 1000
	_infoboxContainer.style.cssFloat = "left"
	_infoboxContainer.style.display = "inline"
	_infoboxContainer.style.visibility = "hidden"
	// set infobox events
	_infoboxContainer.onmouseover = function () { if(infoboxTimeoutID != null){clearTimeout(infoboxTimeoutID); infoboxTimeoutID = null} }
	_infoboxContainer.onmouseout = function () { delayedHideCustomInfobox() }
	// get container dimensions
	var oInfoboxWidth = _infoboxContainer.offsetWidth
	var oInfoboxHeight = _infoboxContainer.offsetHeight
	// position infobox
	_infoboxContainer.style.top = posY + "px"
	if ((posX - oInfoboxWidth) < 0) {
		_infoboxContainer.style.left = posX + "px"
	} else {
		_infoboxContainer.style.left = (posX - oInfoboxWidth) + "px"
	}
	// delayed show?
	if (arguments.length == 4) {
		infoboxTimeoutID = setTimeout("document.getElementById(infoboxID).style.visibility='visible'", arguments[3])
	} else {
		_infoboxContainer.style.visibility = "visible"
	}
}

function hideCustomInfobox(e) {
	if (!e) var e = window.event
	// container exists
	if ( document.getElementById(infoboxID) ) {
		// get container
		var _infoboxContainer = document.getElementById(infoboxID)
		_infoboxContainer.style.display = "none"
		_infoboxContainer.innerHTML = ""
		// reset timeout
		if (infoboxTimeoutID != null) infoboxTimeoutID = null
	}
}

function delayedHideCustomInfobox() {
	infoboxTimeoutID = setTimeout("hideCustomInfobox()", 1000)
}

function getObjectPosition(obj) {
	var posx = 0
	var posy = 0
	if (obj.offsetParent) {
		posx = obj.offsetLeft
		posy = obj.offsetTop
		while (obj = obj.offsetParent) {
			posx += obj.offsetLeft
			posy += obj.offsetTop
		}
	}
	// posx and posy contain the object position relative to the document
	return (posx + "|" + posy)
}

function getMousePosition(e) {
	var posx = 0
	var posy = 0
	if (!e) var e = window.event
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	return (posx + "|" + posy)
}
