/*
$Archive: $
$Revision: $
$Author: $
$Date: $

Description:  This file contains functions for the client tool tip component.
$History: $

*/
//State variables
var keepAlive = false; 		//toolTip state on/off
var isTranslated = false;	//Identifies if the toolTip has already been positioned for the current term
var openTimer = 0;			//Time to keep toolTip visible (ms)
var lastUniqueId = 0;		//Identifies the last element passed into the showToolTip function
var allSupport = true;		//Identifies the browser viewing the page (true: all browsers but Netscape, false: Netscape)

//Object variables
var containerEl = "";		//Identifies the container element used to store the content element
var contentEl = "";			//Identifies the content element that holds content

//Location variables
var mousex = 0;				//Identifies the current mouse position along the x axis
var mousey = 0;				//Identifies the current mouse position along the y axis

//Detect browser window size 
if (parseInt(navigator.appVersion)>3) {
	if (navigator.appName=="Netscape") {
		//Capture events for mouse coordinates for Netscape browsers
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = setMouseCoord;
		allSupport = false;
	 }else{
		allSupport = true;
	 }
}
	
function setMouseCoord(e){
	//For Netscape only, stores current mouse x, y position
	mousex = e.pageX;
	mousey = e.pageY;
}

function showToolTip(content, uId){
	// Get container element per browser
	if (allSupport){ //Is most browsers
	  containerEl = document.all.toolTip;
	  contentEl = document.all.toolTipContent;
	}else{ //Is netscape
	  containerEl = document.getElementById("toolTip");
	  contentEl = document.getElementById("toolTipContent");
	}
	
	 containerEl.width="100%";
	 containerEl.height="100%";
	 
	//Clear openTimer
	window.clearTimeout(openTimer);
	//If this element is different then the last, allow new translation
	
	if(uId != lastUniqueId){
		isTranslated = false;
	}
	//Assign new lastUniqueId
	lastUniqueId = uId;
	//Validate that content is present
	if(content.length > 0){
		//Populate content div
		contentEl.innerHTML = content;
	}else{
		//Display error message if no content is passed in.
		contentEl.innerHTML = "<b>The following error(s) has occured:</b><br /><br />No definition has been found for this term.";
	}
	//Position toolTip
	translateToolTip();
	//Set mouseOnTerm to true
	toolTipKeepAlive(true);
	//Set the timer
	setTimer();
}

function getScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
} 

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

function translateToolTip(){
	var xOffSet = 15; 				//Extra translation value for toolTip on x axis
	var yOffSet = 15; 				//Extra translation value for toolTip on y axis
	var windowX = 0; 				//Width of the browser window
	var windowY = 0; 				//Height of the browser window
	var isYAutoCorrected = false; 	//Specifies if the system made changes to the x coordinate
	var isXAutoCorrected = false; 	//Specifies if the system made changes to the y coordinate

	//Translate position each time the menu is initially displayed
	if(!isTranslated){
		//Configure variables for translation
		if (allSupport){ 
		 	//Store mouse x, y coordinates
		  	mousex = window.event.clientX + getScrollWidth();
		  	mousey = window.event.clientY + getScrollHeight();
			/*NOTE: Mouse coordinates are decided by the getMouseCoord function for Netscape browsers*/
		}
			
		//Store current browser window size and scroll offset, if any
		windowX = document.body.clientWidth + getScrollWidth();
		windowY = document.body.clientHeight + getScrollHeight();

		//Check for right screen boundry
		if(mousex + xOffSet + containerEl.width > windowX){
			containerEl.style.left = windowX - containerEl.width + "px";
			isXAutoCorrected = true;
		}
		
		//Check for left screen boundry
		if(mousex + xOffSet + containerEl.width < 0){
			containerEl.style.left = 0 + "px";
			isXAutoCorrected = true;
		}
		
		//Check for top screen boundry
		if(mousey + yOffSet + containerEl.height < 0){
			containerEl.style.top = 0 + "px";
			isYAutoCorrected = true;
		}
		
		//Check for bottom screen boundry
		if(mousey + yOffSet + containerEl.height > windowY){
			containerEl.style.top = windowY - containerEl.height + "px";
			isYAutoCorrected = true;
		}
		
		//if no autocorrection done, use initial inputs
		if(!isXAutoCorrected){
			containerEl.style.left = mousex + xOffSet + "px";
		}
		
		if(!isYAutoCorrected){
			containerEl.style.top = mousey + yOffSet + "px";
		}
		 
		//Mark as translated for this element
		isTranslated = true;
		
		//Show container element
		toggleToolTip(true);
	}
}

function toggleToolTip(bool){
	//Turns toolTip visibility on/off
	if(bool){
		//Show toolTip
		containerEl.style.visibility = "visible";
	}else{
		//Hide toolTip
		containerEl.style.visibility = "hidden";
	}
}

function toolTipKeepAlive(bool){
	//Update keepAlive variable for open state
	keepAlive = bool;
}

function checkToolTipOpenStatus(){
	//Determine if the toolTip is still in use at the end of the timeout.
	if(!keepAlive){
		//Reset for next translation
		isTranslated = false;
		//Hide toolTip
		toggleToolTip(false);
	}else{
		//Continue to show toolTip
		toggleToolTip(true);
	}
}

function setTimer(){
	//Set the timer 
	openTimer = setTimeout("setTimer()", 1500);
	//Evaluate keepAlive status
	checkToolTipOpenStatus();
}
