/********************************************
(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
********************************************/
/*jslint browser: true, onevar: true, undef: true, eqeqeq: true, bitwise: true, regexp: true, immed: true, strict: true, evil: true */

tooltip_fadeout_timer = null;

function PlacePopupInViewport( mouse_x, mouse_y ) {
	"use strict";
	var offX, offY, obj, window_size_x, window_size_y, dimensions, tooltip_size_x, tooltip_size_y, x_delta, y_delta, x, y;
	
	offX = 15;
	offY = 15;
	
	obj = $( 'tooltip_popup' );
	if( obj === null ) {
		return;
	}
	
	// Move the tooltip inside the visible window if it seems to be outside of it
	window_size_x = document.viewport.getWidth();
	window_size_y = document.viewport.getHeight();

	dimensions = obj.getDimensions();
	tooltip_size_x = dimensions.width;
	tooltip_size_y = dimensions.height;
	
	x_delta = ( window_size_x - ( mouse_x + tooltip_size_x + offX ) );
	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;
	}
		
	x = mouse_x + offX;
	y = mouse_y + offY;
	
	obj.style.left = x + 'px';
	obj.style.top = y + 'px';
	obj.style.visibility = 'visible';
}

function SetTooltipContents( contents ) {
	"use strict";
	var popup, popup_contents, popup_topborder, popup_bottomborder, old_x, old_y;
	
	popup = $('tooltip_popup');

	popup_contents = $( 'tooltip_contents' );
	if( popup_contents === null ) {
		return;
	}
	
	popup_topborder = $( 'tooltip_topborder' );
	if( popup_topborder === null ) {
		return;
	}

	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
	old_x = parseInt( popup.style.left, 10 );
	old_y = parseInt( popup.style.top, 10 );
	if( isNaN( old_x ) === true ) {
		old_x = 0;
	}
	if( isNaN( old_y ) === true ) {
		old_y = 0;
	}
	
	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 );
	}
}

// GetMouseX, GetMouseY, and FollowMouse based on script found at http://javascript.about.com/library/blfollow2.htm
function GetMouseX( evt ) {
	"use strict";
	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 ) {
	"use strict";
	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 ) {
	"use strict";
	var mouse_x, mouse_y;
	
	mouse_x = parseInt( GetMouseX( evt ), 10 );
	mouse_y = parseInt( GetMouseY( evt ), 10 );

	PlacePopupInViewport( mouse_x, mouse_y );
}

function ShowTooltip( element ) {
	"use strict";
	var contents, remote_contents, popup;
	
	contents = element.getAttribute( 'tooltip' );
	remote_contents = element.getAttribute( 'remote_tooltip' );
	if( ( contents === null || contents === "" ) &&
		( remote_contents === null || remote_contents === "" ) ) {
		return;
	}
	
	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() {
	"use strict";
	var popup;
	
	popup = document.getElementById( 'tooltip_popup' );
	if( popup === null ) {
		return;
	}
	
	clearTimeout( tooltip_fadeout_timer );
	tooltip_fadeout_timer = setTimeout( "FadeTooltip();", 100 );
}

function FadeTooltip() {
	"use strict";
	var popup, opacity;
	
	popup = document.getElementById( 'tooltip_popup' );
	if( popup === null ) {
		return;
	}
	
	opacity = parseInt( popup.getAttribute( "opacity" ), 10 );
	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 );
}

function EventShowTooltip() {
	"use strict";
	ShowTooltip( this );
}

function EventClearTooltip() {
	"use strict";
	ClearTooltip( this );
}

function newTooltip( element, content ) {
	"use strict";
	element.setAttribute( 'tooltip', content );
	Event.observe( element, 'mouseover', EventShowTooltip );
	Event.observe( element, 'mouseout', EventClearTooltip );
}

function NewRemoteTooltip( element, command ) {
	"use strict";
	element.setAttribute( 'remote_tooltip', command );
	Event.observe( element, 'mouseover', EventShowTooltip );
	Event.observe( element, 'mouseout', EventClearTooltip );
}
