// Copyright © 2005-2006 Water Street USA, LLC.  All rights reserved. --
// ----------------------------------------------------------------------------------
// Water Street USA, LLC (WSUSA) software and code [both binary and source 
// (if released)] (Software) is intellectual property owned by WSUSA and is copyright 
// of WSUSA in all countries in the world, and ownership remains with WSWS.
//
// You (Licensee) may use the WSUSA Software and install it only in accordance with 
// use and licensing agreements you have signed with WSUSA.  Licenses are distributed 
// only by WSWS. Please send an email to info@waterstreetusa.com for more information.
//
// If you do not have a valid signed license agreement, you may not use, copy or
// distribute the Software for any purpose.
//
// This copyright notice may not be removed or altered or it will constitute a
// violation of any license agreements.
// ----------------------------------------------------------------------------------

/* ------------------------------------------------------------------
	Copyright c 2006, Water Street USA, LLC.
	http://www.waterstreetusa.net
	steve@waterstreetusa.net
	Core javascript variables and methods.
	
	USAGE: 
		* include scripts in HTML page at the top of the body section
		<script language=javascript src=wsws_core.js></script>
		<script language=javascript src=wsws_tooltip.js></script>
		* on any html element
		<a href="http://www.waterstreetusa.net" 
			onMouseover="tooltip('<b>WaterStreet website.</b>')";
			onMouseout="hidetooltip()">Yahoo</a>
			
		
--------------------------------------------------------------------- */

/* ------------------------------------------------------------------
	User Variables
   ------------------------------------------------------------------ */

	var tooltip_width = "400px";
	var tooltip_backgroundColor = "maroon";
	var tooltip_borderWidth = "2px";
	var tooltip_borderColor = "#AEAEAE";
	var tooltip_borderStyle = "solid";
	var tooltip_padding = "0px";
	var tooltip_shadowWidth = "3px";
	var tooltip_shadowColor = "#888888";
//	var tooltip_shadowAngle = 135; /* angle with respect to up */
	// x distance of tooltip from cursor, only positive numbers
	var tooltip_xoffset = 10;
	// y distance of tooltip from cursor, only positive numbers
	var tooltip_yoffset = 10; 


/* ------------------------------------------------------------------
	Do not edit below this line
   ------------------------------------------------------------------ */

if (toInt(tooltip_xoffset) < 0) tooltip_xoffset = toInt(tooltip_xoffset)*(-1);
if (toInt(tooltip_yoffset) < 0) tooltip_yoffset = toInt(tooltip_yoffset)*(-1);
tooltip_shadowWidth = toInt(tooltip_shadowWidth);

// insert the initial default tooltip style
document.write(
	'<style type="text/css">'
	+ '.ws_tooltip'
	+ '{'
	+ '	position: absolute;'
	+ '	left: -2800;'
	+ '	width: ' + tooltip_width + ';'
	+ '	border: ' + tooltip_borderWidth + ' ' + tooltip_borderStyle + ' ' + tooltip_borderColor + ';'
	+ '	padding: ' + tooltip_padding + ';'
	+ '	background-color: ' + tooltip_backgroundColor + ';'
//	+ ' visibility: hidden;'
//	+ '	z-index: 100;'
	/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
//	+ '	filter: progid:DXImageTransform.Microsoft.Shadow(color=' + tooltip_shadowColor + ',direction=' + tooltip_shadowAngle + ');'
	+ '}'
	+ '</style>');

// insert the tooltip div and shim (to cover up background controls)
document.write('<div id="tooltipDiv" class="ws_tooltip"></div>');
document.write('<iframe id="tooltipShim" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; display:none; filter:Alpha(opacity=0);"></iframe>');
if (tooltip_shadowWidth > 0)
{
	document.write('<div id="tooltipShadow" style="'
		+ 'position:absolute;'
		+ 'left:-2800;'
		+ 'top:10px;'
//		+ 'width:200px;'
//		+ 'height:200px;'
		+ 'background-color:' + tooltip_shadowColor + ';'
		+ 'display:none;'
		+ '"></div>');
}


var showTooltip = false;
var tooltipObject = getObject('tooltipDiv');
var shimObject = getObject('tooltipShim');
var shadowObject = getObject('tooltipShadow');

/* ------------------------------------------------------------------
	Functions
   ------------------------------------------------------------------ */
function getTooltipHeight()
{
	// get the tooltip height
	var height;
	if (bf_netscape4)
		height = tooltipObject.clip.height;
	else
	{
		if (tooltipObject.style.pixelHeight)
			height = tooltipObject.style.pixelHeight;
		else
			height = tooltipObject.offsetHeight;
	}

	// ensure we have a valid height
	height = toInt(height);
	if (height)
		return height;
	else
		return 0;

}
function getTooltipWidth()
{
	// get the tooltip width
	var width;
	if (bf_netscape4)
		width = tooltipObject.clip.width;
	else
	{
		if (tooltipObject.style.pixelWidth)
			width = tooltipObject.style.pixelWidth;
		else
			width = tooltipObject.offsetWidth;
	}

	// ensure we have a valid height
	width = toInt(width);
	if (width)
		return width;
	else
		return 0;

}
function toggleTooltip( visible )
{
	// this method turns the tooltip containers visibility on or off
	
	if (!tooltipObject || !dhtml)
		return;

	showTooltip = visible;
	// if we're hiding the tooltip, move far away from the viewable area
	if (!showTooltip)
	{
		tooltipObject.left = -2000;
		shimObject.left = -2000;
		shadowObject.left = -2000;
	}
		
	showTooltip ? tooltipObject.style.display = "block" 
			: tooltipObject.style.display = "none";

	// show/hide the tooltip div	
	if (bf_netscape4) 
		tooltipObject.visibility = showTooltip ? 'show' : 'hide';
	else 
		tooltipObject.style.visibility = showTooltip ? 'visible' : 'hidden';
	
	tooltipObject.style.zIndex = 100;
	
	// show/hide the iframe shim	
	if ( bf_ieVersion >= 5.5 && shimObject)
	{
		if ( showTooltip )
		{
			shimObject.style.display = "block";
			shimObject.style.width = getTooltipWidth() + toInt(tooltip_shadowWidth);
			shimObject.style.height = getTooltipHeight() + toInt(tooltip_shadowWidth);
			shimObject.style.zIndex = tooltipObject.style.zIndex - 2;
		}
		else 
			shimObject.style.display = "none";
	}
	
	// show/hide the shadow object
	if (shadowObject && showTooltip)
	{
		shadowObject.style.display = "block";
		shadowObject.style.width = getTooltipWidth();
		shadowObject.style.height = getTooltipHeight();
		shadowObject.style.zIndex = tooltipObject.style.zIndex - 1;
	}
	else
		shadowObject.style.display = "none";
		
}
function tooltip(html)
{
	// call this function from the onmouseover of an html element
	// this is the main entry point into the code
	if (!tooltipObject || !dhtml)
		return;
	tooltipObject.innerHTML = html;
	toggleTooltip(true);
	return false;
}
function setTooltipXPosition(e)
{
	// get everything in html document coords rather than window or screen coords
	var xCursor = mouseX(e);
	var windowWidth = getBrowserScreenWidth();
	var xDsoc = getScrollOffsetX();
	var xVisibleLeft = xDsoc;
	var xVisibleRight = xDsoc + windowWidth;
	
	// set the initial tooltip position
	var tooltipStyle = tooltipObject;		
	if (tooltipObject.style)
		tooltipStyle = tooltipObject.style;
	tooltipStyle.left = xCursor + toInt(tooltip_xoffset);

	var padLeft = 10;
	var padRight = 10;
	var ttwidth = getTooltipWidth();
	var roomLeft = xCursor - xVisibleLeft;
	var roomRight = xVisibleRight - xCursor;
	var neededLeft = ttwidth + padLeft + toInt(tooltip_xoffset);
	var neededRight = ttwidth + padRight + toInt(tooltip_xoffset);

	// put the tooltip right if there's room
	if (neededRight <= roomRight) 
		tooltipStyle.left = xCursor + tooltip_xoffset;
	// if there's not room right, but plenty left, put it there
	else if (neededLeft <= roomLeft) 
		tooltipStyle.left = xCursor - tooltip_xoffset - ttwidth;
	// if the window has enough width for the tooltip, then just make it flush right
	else if (windowWidth - padRight - padLeft >= ttwidth) 
		tooltipStyle.left = xVisibleRight - padRight - ttwidth; 
	// otherwise, we need to overflow, force tooltip to left
	else 
		tooltipStyle.left = xVisibleLeft + padLeft;

}
function setTooltipYPosition(e)
{
	// get everything in html document coords rather than window or screen coords
	var xCursor = mouseX(e);
	var yCursor = mouseY(e);
	var windowHeight = getBrowserScreenHeight();
	var yDsoc = getScrollOffsetY();
	var yVisibleTop = yDsoc;
	var yVisibleBottom = yDsoc + windowHeight;
	
	// set the initial tooltip position
	var tooltipStyle = tooltipObject;		
	if (tooltipObject.style)
		tooltipStyle = tooltipObject.style;
	tooltipStyle.top = yCursor + toInt(tooltip_yoffset);

	var padTop = 0;
	var padBottom = 10;
	var ttwidth = getTooltipWidth();
	var ttheight = getTooltipHeight();
	var roomTop = yCursor - yVisibleTop;
	var roomBottom = yVisibleBottom - yCursor;
	var neededTop = ttheight + padTop + toInt(tooltip_yoffset);
	var neededBottom = ttheight + padBottom + toInt(tooltip_yoffset);

	// put the tooltip below if there's room
	if (neededBottom <= roomBottom) 
		tooltipStyle.top = yCursor + tooltip_yoffset;
	// if there's not room below, but plenty above, put it there
	else if (neededTop <= roomTop) 
		tooltipStyle.top = yCursor - tooltip_yoffset - ttheight;
	// the tooltip does not overlap the cursor in x
	else if (xCursor < toInt(tooltipObject.style.left) 
          || xCursor > toInt(tooltipObject.style.left) + toInt(ttwidth)) 
 {
			// if there's enough room in the window to display the tooltip
			// make it flush bottom
			if (windowHeight - padTop - padBottom >= ttheight)
				 tooltipStyle.top = yVisibleBottom - padBottom - ttheight;
		 // if the tooltip is bigger than the page, make it flush top
		 else
				 tooltipStyle.top = yVisibleTop + padTop;
	}
	// otherwise, we just force the tooltip below because there's no way to
	// position it without overlapping the cursor
	else 
		tooltipStyle.top = yCursor + tooltip_yoffset;

}
function updateTooltip(e)
{
	if (!tooltipObject || !dhtml)
		return;
	
	if (showTooltip)
	{	
		setTooltipXPosition(e);
		setTooltipYPosition(e);
		// setup shim
		shimObject.style.left = tooltipObject.style.left;
		shimObject.style.top = tooltipObject.style.top;	
		// setup shadow
		if (shadowObject)
		{
			shadowObject.style.left = toInt(tooltipObject.style.left) + toInt(tooltip_shadowWidth);
			shadowObject.style.top = toInt(tooltipObject.style.top) + toInt(tooltip_shadowWidth);
		}
	}	
}
function hidetooltip()
{
	toggleTooltip(false);
	tooltipObject.style.left = -2000;
	shimObject.style.left = -2000;
	if (shadowObject)
		shadowObject.style.left = -2000;
}

document.onmousemove=updateTooltip;
