//***************************************************************************
// popup.js
// A set of javascript functions for opening and closing text popup windows.
//
// Created: 07-Dec-2004
// Author:  D.Niemeyer
// Copyright ©2004, Xionetic Technologies, Inc.  All rights reserved.
//***************************************************************************

//===========================================================
// BPopup class : used for help popups or other layers
//---------------------------------------------------------------------------
// c = id of the control associated with the popup
// t = title text
// b = body text
// etype = the event type that will be attached to the control specified in c
//			  H : Hover, attaches to c.onmouseover,c.onmouseout
//			  F : Focus, attaches to c.onfocus,c.onblur
//			  C : Click, attaches to c.onclick, acts as a toggle
// rtype = relative positioning type
//			  A : absolute (document)
//			  C : control relative
//			  M : mouse relative
//
// halign = type of horizontal positioning
//        LL: align source left edge with the destination left edge
//        LR: align source left edge with the destination right edge
//        RL: align source right edge with the destination left edge
//        RR: align source right edge with the destination right edge
//        C: center source horizontally with the destination
//
// valign = type of vertical positioning
//			 TT: align source top edge with the destination top edge
//			 TB: align source top edge with the destination bottom edge
//			 BT: align source bottom edge with the destination top edge
//			 BB: align source bottom edge with the destination bottom edge
//        C: center source vertically with the destination
//
// dx = additional horizontal offset
// dy = additional vertical offset 
// anim = AnimType structure describing popup animation
//===========================================================
function BPopup(c,t,b,etype,rtype,halign,valign,dx,dy,anim) 
{
	var p = this;
	
	//setTimeout functions need to have a global 'this' reference,
	//generate a unique global 'this' identifier for the instance
	this.og = "D." + c + "_g";
	
	p.obj	= get(c + "__d");
	p.tobj = get(c + "__t");
	p.bobj = get(c + "__b");
	p.ctrl = get(c);
	p.trigger = etype;
	p.mouse = N;
	p.rtype = rtype;
	p.halign = halign;
	p.valign = valign;
	p.dx = dx;
	p.dy = dy;
	p.anim = anim;
	show(p.obj,F);
	p.tobj.innerHTML = t;
	p.bobj.innerHTML = b;
	p.count = 0;

	//-----------------------------------------------
	// Event methods
	
	//===========================================================
	// hideEvent
	//-----------------------------------------------------------
	// Used for mouse relative display of the popup.  This action
	// hides the popup and changes the onclick event of the
	// control to showEvent.
	//-----------------------------------------------------------
	// e - event object.
	//===========================================================
	p.hideEvent = function(e)
	{
		var f = "bpShow(F," + p.og + ")";
		e = fixE(e);
		p.mouse = getMouseXY(e);
		window.setTimeout(f,p.anim.hidedelay);
		if(p.trigger == 'C')
			p.ctrl.onclick = p.showEvent;
		return false;
	}	
	 
	//===========================================================
	// showEvent
	//-----------------------------------------------------------
	// Used for mouse relative display of the popup.  This action
	// displays the popup and changes the onclick event of the
	// control to hideEvent.
	//-----------------------------------------------------------
	// e - event object.
	//===========================================================
	p.showEvent = function(e)
	{
		var f = "bpShow(T," + p.og + ")";
		e = fixE(e);
		p.mouse = getMouseXY(e);
		window.setTimeout(f,p.anim.showdelay);
		if(p.trigger == 'C')
			p.ctrl.onclick = p.hideEvent;
		return false;
	}

	if(p.ctrl!=N)
	{
		switch(p.trigger)
		{
			case 'H':
				p.ctrl.onmouseover = p.showEvent;
				p.ctrl.onmouseout = p.hideEvent;
				break;
			case 'F':
				p.ctrl.onfocus = p.showEvent;
				p.ctrl.onblur = p.hideEvent;
				break;
			case 'C':
				p.ctrl.onclick = p.showEvent;
				break;
		}
	}
	else
		p.trigger=N;

	//-----------------------------------------------
	// Function methods

	//===========================================================
	// show
	//-----------------------------------------------------------
	// Displays the popup using the settings associated with the
	// default control.
	//-----------------------------------------------------------
	// t - The title text to display
	// b - The main body text.
	// h - Timeout duration before a hide will be performed.
	//===========================================================
	p.show = function(t,b,h)
	{
		if(t!=N)	p.tobj.innerHTML=t;
		if(b!=N) p.bobj.innerHTML=b;
		if(p.ctrl!=N)
		{
			if((t=='')&&(b==''))
			{
				show(p.ctrl,F);
				return;
			}
			else
				show(p.ctrl,T);
		}
		else if((t=='')&&(b==''))
			return;
		
		var f = "bpShow(T," + p.og + ")";
		if(p.rtype=='M')
		{
			p.mouse = new Point();
			p.mouse.x = getX(c);
			p.mouse.y = getY(c);
		}
		if(p.trigger == 'C')
			p.ctrl.onclick = p.hideEvent;
		window.setTimeout(f,p.anim.showdelay);

		if(h!=N)
		{
			p.count++;
			f=p.og+".hide(" + p.count + ")";
			window.setTimeout(f,h);
		}
		return false;
	}

	//===========================================================
	// hide
	//-----------------------------------------------------------
	// Hides the popup window.
	//===========================================================
	p.hide = function(c)
	{
		if(c!=p.count) return;
		var f = "bpShow(F," + p.og + ")";
		window.setTimeout(f,p.anim.hidedelay);
		if(p.trigger == 'C')
			p.ctrl.onclick = p.showEvent;
		return false;
	}
} // class BPopup


//===========================================================
//	bpShow
//-----------------------------------------------------------
// Called by the BPopup class to display or hide the popup
// window.
//-----------------------------------------------------------
// v - Visibility flag (hidden or visible)
// p - BPopup class object with configuration settings.
//===========================================================
function bpShow(v,p)
{
	if(v)
	{
		show(p.obj,v);
		setPos(p.obj,p.ctrl,p.mouse,p.rtype,p.halign,p.valign,p.dx,p.dy);

		if(p.anim.wiperate != 0)
			animOpen(p,p.obj,p.anim);
	}
	else
	{
		if(p.anim.wiperate != 0)
			animClose(p,p.obj);
		else
			show(p.obj,F);
	}
} // bpShow

