/********************************************
(c) 2005-present, uTime Games
Despite the fact that you can see this, 
you may NOT use or copy this code for any reason.
If you'd like to use this code, please contact:
	Geoffrey Benson at
	sales@utimegames.com

Licensing terms are generally reasonable -
ranging from free to cheap - but you MUST
ask and obtain permission first.

Thank you for not being a jerk.
  --The Management
********************************************/

var tooltip_fadeout_timer = null;

function ShowTooltip( element )
{
	var contents = element.getAttribute( 'tooltip' );
	var remote_contents = element.getAttribute( 'remote_tooltip' );
	if( ( contents == null || contents == "" ) &&
		( remote_contents == null || remote_contents == "" ) )
		return;
	
	var popup = $( 'tooltip_popup' );
	if( popup == null )
		return;
	
	popup.style.zIndex = Windows.maxZIndex + 1;
	
	// Clear the fading out/timer and reset to full visibility
	if( tooltip_fadeout_timer != null )
	{
		clearTimeout( tooltip_fadeout_timer );
		popup.setAttribute( "opacity", 100 );
		popup.style.opacity = "1";
		if( g_is_ie6 || g_is_ie7 )
			popup.style.filter = "alpha(opacity=100)";
	}
	
	// Set up the window

	// Order of operations: First, see if there's a remote_contents attribute.  If so, use that.
	// else, use the contents attribute.
	if( remote_contents != null && remote_contents != "" )
	{
		// Set this to loading for now, then retrieve the tooltip contents
		SetTooltipContents( "Loading..." );
		SendRequest( "load_tooltip", { "command": remote_contents } );
	}
	else if( contents != null && contents != "" )
	{
		// No, I can't believe it requires this mess either.
		// The problem is that browsers automatically use the html entities encoding when the data is set into
		// an attribute.  Nothing says "fun" like your data being changed under you for no reason.
		contents = contents.replace( /&lt;/g, "<" ).replace( /&gt;/g, ">" );

		SetTooltipContents( contents );
	}
	
	// Turn on the mousemove event to track the popup
	document.onmousemove = FollowMouse;
}

function ClearTooltip()
{
	var popup = document.getElementById( 'tooltip_popup' );
	if( popup == null )
		return;
	
	clearTimeout( tooltip_fadeout_timer );
	tooltip_fadeout_timer = setTimeout( "FadeTooltip();", 100 );
}

function FadeTooltip()
{
	var popup = document.getElementById( 'tooltip_popup' );
	if( popup == null )
		return;
	
	var opacity = parseInt( popup.getAttribute( "opacity" ) );
	if( !opacity )
		opacity = 0;
	
	opacity -= 20;
	if( opacity < 1 )
	{
		// Final cleanup of the popup
		popup.style.visibility = "hidden";
		popup.style.top = 0;
		popup.style.left = 0;
		document.onmousemove = null;
		return;
	}
	
	popup.setAttribute( "opacity", opacity );
	popup.style.opacity = opacity / 100;
	if( g_is_ie6 || g_is_ie7 )
		popup.style.filter = "alpha(opacity=" + opacity + ")";
	
	tooltip_fadeout_timer = setTimeout( "FadeTooltip();", 50 );
}

// GetMouseX, GetMouseY, and FollowMouse based on script found at http://javascript.about.com/library/blfollow2.htm
function GetMouseX( evt )
{
	if( !evt )
		evt = window.event;
	
	if( evt.pageX )
		return evt.pageX;
	else if( evt.clientX )
		return evt.clientX + ( document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	else
		return 0;
}

function GetMouseY( evt )
{
	if( !evt )
		evt = window.event;
	
	if( evt.pageY )
		return evt.pageY;
	else if( evt.clientY )
		return evt.clientY + ( document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else
		return 0;
}

function FollowMouse( evt )
{
	var mouse_x = parseInt( GetMouseX( evt ) );
	var mouse_y = parseInt( GetMouseY( evt ) );

	PlacePopupInViewport( mouse_x, mouse_y );
}

function PlacePopupInViewport( mouse_x, mouse_y )
{
	var offX = 15;
	var offY = 15;
	
	var obj = $( 'tooltip_popup' );
	if( obj == null )
		return;
	
	// Move the tooltip inside the visible window if it seems to be outside of it
	var window_size_x = document.viewport.getWidth();
	var window_size_y = document.viewport.getHeight();

	var dimensions = obj.getDimensions();
	var tooltip_size_x = dimensions.width;
	var tooltip_size_y = dimensions.height;
	
	var x_delta = ( window_size_x - ( mouse_x + tooltip_size_x + offX ) );
	var y_delta = ( window_size_y - ( mouse_y + tooltip_size_y + offY ) );
	
	if( x_delta < 0 )
		offX += x_delta;

	if( y_delta < 0 )
		offY += y_delta;
		
	var x = mouse_x + offX;
	var y = mouse_y + offY;
	
	obj.style.left = x + 'px';
	obj.style.top = y + 'px';
	obj.style.visibility = 'visible';
}

function SetTooltipContents( contents )
{
	var popup = $('tooltip_popup');

	var popup_contents = $( 'tooltip_contents' );
	if( popup_contents == null )
		return;
	
	var popup_topborder = $( 'tooltip_topborder' );
	if( popup_topborder == null )
		return;

	var popup_bottomborder = $( 'tooltip_bottomborder' );
	if( popup_bottomborder == null )
		return;

	// Temporarily move the tooltip to 0,0 to allow it to fit correctly, then move it back
	var old_x = parseInt( popup.style.left );
	var old_y = parseInt( popup.style.top );
	
	popup.style.visibility = "hidden";
	popup.style.left = "0px";
	popup.style.top = "0px";
			
/*	if( g_is_ff == true || g_is_ie6 == true || g_is_ie7 == true )
	{
		// No, I can't believe it requires this mess either.
		// The problem is that these browsers automatically use the html entities encoding when the data is set into
		// an attribute, but Opera does not.  Nothing says "fun" like your data being changed under you for
		// no reason.
		contents = contents.replace( /&lt;/g, "<" ).replace( /&gt;/g, ">" );
	}
*/
	popup_contents.update( contents );

	// Scale the top and bottom so that they match the contents
	popup_topborder.style.width = popup_contents.offsetWidth + "px";
	popup_bottomborder.style.width = popup_contents.offsetWidth + "px";

	// Restore the tooltip to its old location, moving it within the viewport if needed
	if( old_x != 0 && old_y != 0 )
		PlacePopupInViewport( old_x - 15, old_y - 15 );
}