//initialise tooltip variables
var ttMaxWidth = 200	//maximum width 
var ttFont = "tahoma"	//font 
var ttFontSize = 8		//font size
var ttBgColor = "#ffffe7"	//background colour
var mouseOffset = 20	//distance from mouse pointer
var edgeOffset = 5		//distance from window edge


//create tooltip container
if (document.layers) {
	document.write('<layer id=tt visibility=hide bgcolor=' + ttBgColor + ' width=' + ttMaxWidth + '></layer>')
} else if (document.all || document.getElementById) {
	document.write('<div id=tt style="padding:2; position:absolute; visibility:hidden; background-color:' + ttBgColor + '; border:1px solid; font-size:' + ttFontSize + 'pt; font-family:' + ttFont + '"></div>')
}


//initialise tooltip
function initTT() {
	if (document.layers) {
		tooltip = document.tt
	} else if (document.all) {
		tooltip = document.all.tt
	} else if (document.getElementById) {
		tooltip = document.getElementById("tt")
	}
}


//full tooltip size (including offsets) object constructor
function objSize(ttWidth, ttHeight, mouseOffset, edgeOffset) {
	this.width = ttWidth + mouseOffset + edgeOffset
	this.height = ttHeight + mouseOffset + edgeOffset
}


//"space available for tooltip" object constructor
function objMargin(x, y, scrollX, scrollY, windowWidth, windowHeight) {
	this.scrollX = scrollX
	this.scrollY = scrollY
	this.left = x - scrollX
	this.top = y - scrollY
	this.right = windowWidth - this.left
	this.bottom = windowHeight - this.top
}


//show tooltip
function showTT(evt, include) {

	if (typeof(tooltip) == "undefined") return

	var x = evt.x ? evt.x : evt.pageX
	var y = evt.y ? evt.y : evt.pageY

	var ttText = typeof(include) == "string" ? include : incArray[include]

	if (document.layers) {
		tooltip.document.write('<span style="border:1px solid; font-size:' + ttFontSize + 'pt; font-family:' + ttFont + '">' + ttText + '</span>')
		tooltip.document.close()
		positionTT(tooltip, x, y, tooltip.clip.width, tooltip.clip.height)
		tooltip.visibility = "show"
	} else if (document.all || document.getElementById) {
		tooltip.style.left = ""
		tooltip.style.top = ""
		tooltip.noWrap = true
		tooltip.innerHTML = ttText

		if (tooltip.offsetWidth > ttMaxWidth) {
			tooltip.noWrap = false
			tooltip.style.width = ttMaxWidth + "px"
		} else {
			tooltip.style.width = tooltip.offsetWidth
		}

		positionTT(tooltip.style, x, y, tooltip.offsetWidth, tooltip.offsetHeight)
		tooltip.style.display = "none"  //this removes blinking in NN6
		tooltip.style.visibility = "visible"
		tooltip.style.display = "block"
		tooltip.style.zIndex = 100
	}
}


//hide tooltip
function hideTT() {
	if (typeof(tooltip) == "undefined") return

	if (document.layers) {
		tooltip.visibility = "hide"
	} else if (document.all || document.getElementById) {
		tooltip.style.visibility = "hidden"
		tooltip.style.width = ""
	}
}


//position tooltip
function positionTT(posObj, x, y, ttWidth, ttHeight) {
	//fix width problem in ie4
	if (ttWidth > ttMaxWidth) ttWidth = ttMaxWidth

	var ttMargin, posLeft, posTop
	var ttSize = new objSize(ttWidth, ttHeight, mouseOffset, edgeOffset)

	if (document.all) {
		//fix event coordinates for explorer 5 and later
		if (newMSIE()) {
			x += document.body.scrollLeft
			y += document.body.scrollTop
		}

		ttMargin = new objMargin(x, y, document.body.scrollLeft, document.body.scrollTop, document.body.clientWidth, document.body.clientHeight)
	} else if (document.layers || document.getElementById) {
		ttMargin = new objMargin(x, y, window.pageXOffset, window.pageYOffset, window.innerWidth, window.innerHeight)
	}

	if (ttMargin.right >= ttSize.width) {
		posLeft = x + mouseOffset
	} else {
		posLeft = ttMargin.left + ttMargin.right >= ttSize.width ? x + ttMargin.right - ttSize.width : ttMargin.scrollX + edgeOffset
	}

	if (ttMargin.bottom >= ttSize.height) {
		posTop = y + mouseOffset
	} else {
		posTop = ttMargin.top + ttMargin.bottom >= ttSize.height ? y + ttMargin.bottom - ttSize.height : ttMargin.scrollY + edgeOffset
	}

	//make sure tooltip does not appear over the mouse pointer
	if (x >= posLeft && x <= posLeft + ttSize.width && y >= posTop && y <= posTop + ttSize.height) {
		if (ttMargin.top >= ttSize.height) {
			posTop = y - ttSize.height
		} else if (ttMargin.left >= ttSize.width) {
			posLeft = x - ttSize.width
		} else {
			posLeft = x + mouseOffset
			posTop = y + mouseOffset
		}
	}

	if (document.all || document.getElementById) {
		posLeft += "px"
		posTop += "px"
	}

	posObj.left = posLeft
	posObj.top = posTop
}


//returnes true if user agent is explorer 5 or later
function newMSIE() {
	var ua = navigator.userAgent
	var msie = ua.indexOf("MSIE")

	if (msie > 0) {
		if (parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))) >= 5) return true
	}

	return false
}
