//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
var scrOfX = 0, scrOfY = 0;
var IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? true : false;//defect 843

//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
	if (IE7) {//defect 843
		this.top = y//event.scrollY//y - document.documentElement.scrollTop
	}
	else{
	    this.top = y - scrollY
	}	
	this.right = windowWidth - this.left
	this.bottom = windowHeight - this.top
}

 //Phase II Defect # 1738 Added by Devender Tokas on 5-Oct-07-->
//show tooltip
function showTTSearch(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()
		positionTTSearch(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.style.noWrap = true
		tooltip.innerHTML = ttText

		if (tooltip.offsetWidth > ttMaxWidth) {
			tooltip.style.noWrap = false
			tooltip.style.width = ttMaxWidth + "px"
		} else {
			tooltip.style.width = tooltip.offsetWidth
		}

		positionTTSearch(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
	}
}

//show tooltip
function showTT(evt, include) {
	
	if (typeof(tooltip) == "undefined") return

	if (include == "") return
	//Web-983
	include = include.replace("~", "'")
	
	//var x = evt.clientX;  //? evt.clientX : evt.screenX
	//var y = evt.clientY-20; //? evt.clientY : evt.screenY

	if (evt.x){
	    //IE
	    var x = evt.clientX
	    var y = evt.clientY-20
	}
	else{
	    //FF
	    var x = evt.pageX
	    var y = evt.pageY-20
	}

	var ttText =  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.style.noWrap = true
		tooltip.innerHTML = ttText

		if (tooltip.offsetWidth > ttMaxWidth) {
			tooltip.style.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 = ""
	}
}

 //Phase II Defect # 1738 Added by Devender Tokas on 5-Oct-07-->
//position tooltip
function positionTTSearch(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
			//Rami - 04/07/07 - DefectId: 829 - DH-RR: Instant Confirmation pop-up on mouse hover not aligned correctly
			x = Number(x) + Number(document.body.scrollLeft)
			y = Number(y) + Number(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
}


//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
			//Rami - 04/07/07 - DefectId: 829 - DH-RR: Instant Confirmation pop-up on mouse hover not aligned correctly
			x += document.documentElement.scrollLeft
			y += document.documentElement.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
		if (IE7) {
			posTop = y + mouseOffset
		}
	}

	//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
}

initTT()
