/* This notice must be untouched at all times.

tip_centerinwindow.js  v. 1.0

The latest version is available at
http://www.walterzorn.com
or http://www.devira.com
or http://www.walterzorn.de

Copyright (c) 2007 Walter Zorn. All rights reserved.
Last modified: 31.5.2007

Extension for the tooltip library wz_tooltip.js.
Centers a sticky tooltip in the window's visible clientarea,
optionally even if the window is scrolled or resized.

LICENSE: LGPL (see http://www.gnu.org/copyleft/lesser.html)
*/

// Here we define new global configuration variable(s) (as members of the
// predefined "config." class).
// From each of these config variables, wz_tooltip.js will automatically derive
// a command which can be passed to Tip() or Tag2Tip() in order to customize
// tooltips individually. These command names are just the config variable
// name(s) translated to uppercase,
// e.g. from config. CenterInWnd a command CENTERINWND will automatically be
// created.

//===================  GLOBAL TOOPTIP CONFIGURATION  =========================//
config. CenterInWnd = false		// true or false - default is false, as we don't want this to be the default behaviour
config. CenterAlways = false;	// true or false - recenter if window is resized or scrolled
//=======  END OF TOOLTIP CONFIG, DO NOT CHANGE ANYTHING BELOW  ==============//


// Create a new tt_Extension object (make sure that the name of that object,
// here ctrinwnd, is unique amongst the extensions available for
// wz_tooltips.js):
var ctrinwnd = new tt_Extension();

function DoCenterInWnd()
{
	if(tt_aV[CENTERINWND])
	{
		var t_x, t_y;

		// Here we use some functions and variables (tt_w, tt_h) which the
		// extension API of wz_tooltip.js provides for us
		t_x = (tt_GetClientW() - tt_w) / 2 + tt_GetScrollX();
		t_y = (tt_GetClientH() - tt_h) / 2 + tt_GetScrollY();
		tt_SetTipPos(t_x, t_y);
		return true;
	}
	return false;
}

// Implement extension eventhandlers on which our extension should react
ctrinwnd.OnConfig = function()
{
	if(tt_aV[CENTERINWND])
	{
		// Permit CENTERINWND only if the tooltip is sticky
		if(tt_aV[STICKY])
		{
			if(tt_aV[CENTERALWAYS])
			{
				tt_AddEvtFnc(window, "resize", DoCenterInWnd);
				tt_AddEvtFnc(window, "scroll", DoCenterInWnd);
			}
			return true;
		}
		tt_aV[CENTERINWND] = false;
	}
	return false;
}

// We react on the first OnMouseMove event to center the tip on that occasion
ctrinwnd.OnMouseMove = DoCenterInWnd;

ctrinwnd.OnKill = function()
{
	if(tt_aV[CENTERINWND] && tt_aV[CENTERALWAYS])
	{
		tt_RemEvtFnc(window, "resize", DoCenterInWnd);
		tt_RemEvtFnc(window, "scroll", DoCenterInWnd);
	}
}
