/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
*
* NOTE THAT there must be a var called balloonPointerImageName set up before this .js file 
* is included. That will be used as the 'pointer' part of the tooltip balloon.
* 
***********************************************/

var offsetfromcursorX=12 //Customize x offset of the tooltip
var offsetfromcursorY=10 //Customize y offset of the tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

var maxColumnEntries=35 // Maximum number of strings that can be in one column of a table, if we are using a table.

document.write('<div id="custom_tooltip"></div>') //write out tooltip DIV
/* The 'shimmy' is needed so that the iframe covers up any 'select' objects - problem with ie6 */
document.write('<iframe src="' + spacerImageName + '" id="custom_tooltip_shimmy" style="position:absolute; left:0; top:0; visibility:hidden"></iframe>');
/* Note that the balloonPointerImageName must be set up prior to including this .js file */
document.write('<img id="custom_tooltip_pointer" src="' + balloonPointerImageName + '" />'); //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
var showpointer=true
var customTipObj=null;
var tooltipDataIsArray=false;
var tooltipArrayOfStrings=null;
var tableColumnWidth=0;
var suggestedNumberOfColumns=0
var displayingSuggestedTooltipTable=true
var customTipShimmyObj=null;
var customPointerObj=null;
var defaultTooltipWidth=250;

if (ie||ns6) {
	customTipObj=document.all? document.all["custom_tooltip"] : document.getElementById? document.getElementById("custom_tooltip") : ""
	customTipShimmyObj=document.all? document.all["custom_tooltip_shimmy"] : document.getElementById? document.getElementById("custom_tooltip_shimmy") : ""
	customPointerObj=document.all? document.all["custom_tooltip_pointer"] : document.getElementById? document.getElementById("custom_tooltip_pointer") : ""
}

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showCustomToolTip(theData, theWidth, theClass, theBackColor){
	if (ns6||ie){
		/*
		 * These variables are used later if we use a table to display the 
		 * tooltip and we have to resize the table to accomodate the page size
		 * and the location of the mouse pointer.
		 */
		tooltipDataIsArray=isArray(theData)
		tooltipDataArray = theData
		tableColumnWidth=theWidth
		displayingSuggestedTooltipTable = true
		if (customTipObj == null) {
			customTipObj=document.all? document.all["custom_tooltip"] : document.getElementById? document.getElementById("custom_tooltip") : ""
		}
		if (customTipShimmyObj == null) {
			customTipShimmyObj=document.all? document.all["custom_tooltip_shimmy"] : document.getElementById? document.getElementById("custom_tooltip_shimmy") : ""
		}
		if(customPointerObj == null) {
			customPointerObj=document.all? document.all["custom_tooltip_pointer"] : document.getElementById? document.getElementById("custom_tooltip_pointer") : ""
		}
		if (typeof theWidth!="undefined") {
			customTipObj.style.width=theWidth+"px"
		} else {
			customTipObj.style.width=defaultTooltipWidth+"px";
		}
		if (typeof theClass!="undefined" && theClass != "") {
			customTipObj.className=theClass
		} else {
			customTipObj.className='customTooltipDefault'
		}
		
		if (typeof theBackColor!="undefined" && theBackColor != "") {
			customTipObj.style.backgroundColor=theBackColor
			/*
				It doesn't look good to use the pointer with a tooltip that has a different background
				color because the pointer is an image and always has a white background.
			 */
			showpointer=false
		}
		/*
		 * If the data is an array of strings and there are more strings than we
		 * specify as a maximum, the tooltip will scroll off the bottom of the page
		 * so try to make it a table with multiple columns.
		 */
		if(tooltipDataIsArray) {
			if(tooltipDataArray.length > maxColumnEntries) {
				makeTooltipFromArray()
			} else {
				customTipObj.innerHTML = tooltipDataArray.join('<br>')
			}
		} else {
			customTipObj.innerHTML=theData
		}
		enabletip=true
		return false
	}
}
/*
 * Create a tooltip from an array of strings
 */
function makeTooltipFromArray() {
	/*
	 * Calculate the number of columns based on the maximum number of entries
	 * per column. 
	 */
	suggestedNumberOfColumns = Math.ceil(tooltipDataArray.length / maxColumnEntries)
	createArrayTooltipTable(suggestedNumberOfColumns);
}

/*
 * Create a table to display strings in a variable number of rows and columns
 */
function createArrayTooltipTable(numberOfColumns) {
	if(customTipObj == null || tooltipDataArray == null ||
		tooltipDataArray.length == 0 || numberOfColumns == null ||
		numberOfColumns == 0) return
	/*
	 * Just in case the last time there was something placed in innerHTML, remove
	 * it...otherwise the tooltip will contain both the innerHTML and the table.
	 */
	customTipObj.innerHTML = ""
	/* 
	 * Remove the last entry from the div (from the last time this was called)
	 */
	var childToRemove = customTipObj.firstChild
	if(childToRemove != null) {
		customTipObj.removeChild(childToRemove)
	}
	/*
	 * Evenly distribute the text strings into a set of rows.
	 */
	var numberOfRows = Math.ceil(tooltipDataArray.length / numberOfColumns)
	/*
	 * Adjust the width to be able to support the number of columns needed
	 */
	if (typeof tableColumnWidth!="undefined") {
		customTipObj.style.width=(tableColumnWidth*numberOfColumns)+"px"
	} else {
		customTipObj.style.width=(defaultTooltipWidth*numberOfColumns)+"px";
	}
	var table = document.createElement("table")
	table.setAttribute("cellpadding", "0")
	table.setAttribute("cellspacing", "0");
	table.setAttribute("border", "0");	
	var tbody = document.createElement("tbody")
	var tr = null
	var td = null
	var arrayIndex = 0
	for(var rownum = 0; rownum < numberOfRows; rownum++) {
		tr = document.createElement("TR")
		for(var colnum = 0; colnum < numberOfColumns; colnum++) {
			arrayIndex = rownum + (numberOfRows * colnum)
			if(arrayIndex < tooltipDataArray.length) {
				td = document.createElement("TD")
				td.style.paddingTop = "0"
				td.style.paddingBottom = "0"
				td.style.borderTop = "0"
				td.style.borderBottom = "0"
				/*
				 * Separate the columns a bit. It is assumed that there is enough
				 * space given in the width to support this.
				 */
				if(colnum != (numberOfColumns-1)) {
					td.style.paddingRight="5"
				}
				if(colnum != 0) {
					td.style.paddingLeft="5"
					td.style.borderLeft="dotted 1px #000"
				}
				
				td.appendChild(document.createTextNode(tooltipDataArray[arrayIndex]))
				tr.appendChild(td)
			}
		}
		tbody.appendChild(tr)
	}
	table.appendChild(tbody)
	customTipObj.appendChild(table)
}

function positionCustomToolTip(e){
	if (enabletip){
		if(tooltipDataIsArray && !displayingSuggestedTooltipTable) {
			createArrayTooltipTable(suggestedNumberOfColumns)
		}
		var nondefaultpos=false
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		var clientY = (ns6)?e.clientY : event.clientY;
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20
		
		var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
		var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY
		
		var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000
		/* 
		 * Just in case we have to position the tooltip to the left of the mouse
		 * pointer, move the horizontal position of the tooltip to the left by 
		 * its width. Add 10, otherwise we get mouseout events.
		 */
		var leftPosIfNeeded=curX-(customTipObj.offsetWidth+10)
		/**
		 * Some horizontal positioning logic might require additional spacing
		 * between the mouse pointer and the tooltip.
		 */
		var additionalYSpacing = 0
		/* if the horizontal distance isn't enough to accomodate the width of 
		 * tooltip and going all the way to the left will cause the left part of
		 * the tooltip to not be visible...
		 */
		if (rightedge < customTipObj.offsetWidth && leftPosIfNeeded >= 0){
			
			customTipObj.style.left=leftPosIfNeeded+"px"
			nondefaultpos=true
		}
		else if (leftPosIfNeeded < 0 && tooltipDataIsArray) {
			createArrayTooltipTable(suggestedNumberOfColumns + 1)
			leftPosIfNeeded=curX-(customTipObj.offsetWidth+10)
			customTipObj.style.left="0px"
			displayingSuggestedTooltipTable=false
			additionalYSpacing = 10
			nondefaultpos=true
		}
		else if (curX < leftedge)
			customTipObj.style.left="5px"
		else{
			// position the horizontal position of the menu where the mouse is positioned */
			customTipObj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
			customPointerObj.style.left=curX+offsetfromcursorX+"px"
		}
		
		//same concept with the vertical position
		if (bottomedge < customTipObj.offsetHeight){
			customTipObj.style.top=curY-(additionalYSpacing+customTipObj.offsetHeight)+"px"
			/*
			 * This condition means that the top of the tooltip will be hidden
			 * by the top of the browser, so we need to reduce the top so that
			 * it doesn't go 'above' the top or 'below' the bottom - if possible.
			 * Err to going off the bottom.
			 */
			if(clientY - customTipObj.offsetHeight < 0) {
				customTipObj.style.top = ietruebody().scrollTop;
			}
			nondefaultpos=true
		}
		else{
			customTipObj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
			customPointerObj.style.top=curY+offsetfromcursorY+"px"
		}
		customTipObj.style.visibility="visible"
		if(customTipShimmyObj != null) {
			customTipShimmyObj.style.left = customTipObj.style.left;
			customTipShimmyObj.style.width = customTipObj.style.width;
			customTipShimmyObj.style.top = customTipObj.style.top;
			customTipShimmyObj.style.height = customTipObj.offsetHeight -8;
			customTipShimmyObj.style.visibility = "visible";
			customTipShimmyObj.style.backgroundColor="#CCCCCC";
		}
		
		if (!nondefaultpos && showpointer)
			customPointerObj.style.visibility="visible"
		else
			customPointerObj.style.visibility="hidden"
	}
}

function hideCustomToolTip(){
	if (ns6||ie){
		enabletip=false
		showpointer=true
		if(customTipObj != null) {
			customTipObj.style.visibility="hidden"
			customPointerObj.style.visibility="hidden"
			customTipShimmyObj.style.visibility = "hidden";
			customTipObj.style.left="-1000px"
			customTipObj.style.backgroundColor=''
			customTipObj.style.width=''
		}
		if(customPointerObj != null) {
			customPointerObj.style.visibility="hidden"
		}
		if(customTipShimmyObj != null) {
			customTipShimmyObj.style.visibility = "hidden";
		}
	}
}
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

document.onmousemove=positionCustomToolTip