DISABLE_PANELEVENT = false;

// OK, here's the logic.

// onMove / onClick simply store the event into a global.
// Those are then caught by the handlers, and on tick are processed (move first, then click)
// - this way only the LAST move is handled.  (Although they ALL require a copy)

// mouseIn/Out is also pretty simple.  Only one object can have the in - so in stores the object that has last seen an in.
// and out just clears it.

var PanelEvent_PanelIn = null;
var PanelEvent_ClickyEvent = false;
// Copying the event does not appear to WORK for some stupid reason
var PanelEvent_MovingEvent = null;
// So we'll take it's x and y instead.
var PanelEvent_MovingEventClientX = 0;
var PanelEvent_MovingEventClientY = 0;

var PanelEvent_MovesRegistered = false;
var PanelEvent_ClicksRegistered = false;
var PanelEvent_ActivePanel = null;
var PanelEvent_ClickyButton = 0;

// TODO: Make this user configurable
var CFG_PANEL_xmouse_bringup = true;

function PanelEvent_StaticCreate_InOut(panel_name)
{
	return "onMouseOver=\"PanelEvent_PanelIn=\'"+panel_name+"\';\" onMouseOut=\"PanelEvent_PanelIn=null;\" ";
}

// On start drag / stretch:
function PanelEvent_Attach()
{
      //AppendLog ( "PanelEvent_Attach()" );
	PanelEvent_MovesRegistered  = true;
	PanelEvent_ClicksRegistered = true;
}

// On stop drag / stretch
function PanelEvent_Detach()
{
      //AppendLog ( "PanelEvent_Detach()" );
	PanelEvent_MovesRegistered  = false;
	PanelEvent_ClicksRegistered = false;
	document.onlick = null;
}

function PanelEvent_NotifyClick()
{
	PanelEvent_ClickyEvent = true;
}

// This is called by the event timer, regularly.
function PanelEvent_HandleThings()
{
	if ( PanelEvent_PanelIn != PanelEvent_ActivePanel && !PanelEvent_MovesRegistered && !PanelEvent_ClicksRegistered )
	{

	      Debug ( "Cur="+PanelEvent_ActivePanel+" In="+PanelEvent_PanelIn );

		if ( PanelEvent_ActivePanel ) 
		{
			// TODO: Find a way to allow user to choose to leave last active panel
			// to stay unfaded until a new panel becomes active
			Div_SetOpacity(PanelEvent_ActivePanel,CFG_Panel_MinOpacity);
		}
		if ( PanelEvent_PanelIn )
		{
			Div_SetOpacity(PanelEvent_PanelIn,CFG_Panel_MaxOpacity);
			if ( CFG_PANEL_xmouse_bringup ) Panel_BringUp(PanelEvent_PanelIn);
		}
		PanelEvent_ActivePanel = PanelEvent_PanelIn;
	}

	if ( PanelEvent_MovesRegistered && PanelEvent_MovingEvent != null )
	{
	    //AppendLog ( "Handle PanelEvent_MovesRegistered whilst " + Panel_CurrentAction + " at " + PanelEvent_MovingEventClientX+","+PanelEvent_MovingEventClientY);
	    if ( Panel_CurrentAction == "Moving" )
		    Panel_ToMoveWhilstMoving(PanelEvent_MovingEvent,Panel_CurrentTarget,PanelEvent_MovingEventClientX,PanelEvent_MovingEventClientY );
	    else if ( Panel_CurrentAction == "Sizing" )
		    Panel_ToMoveWhilstResizing(PanelEvent_MovingEvent,Panel_CurrentTarget,PanelEvent_MovingEventClientX,PanelEvent_MovingEventClientY );
	    else
		    Debug ( "PanelEvent_MovesRegistered whilst Panel_CurrentAction="+Panel_CurrentAction );
	}

	// User doesn't click often enough to need this, but we need to do it in order to keep things in the right order

	if ( PanelEvent_ClicksRegistered && PanelEvent_ClickyEvent )
	{
        PanelEvent_ClickyEvent = false;

	    //AppendLog ( "Handle PanelEvent_ClicksRegistered whilst " + Panel_CurrentAction);
	    if ( Panel_CurrentAction == "Moving" )
		    Panel_StopDraggingPanel();
	    else if ( Panel_CurrentAction == "Sizing" )
		{
			//AppendLog("handle-stop");
		    Panel_StopStretching();
		}
	    else
		    Debug ( "PanelEvent_ClicksRegistered whilst Panel_CurrentAction="+Panel_CurrentAction );
	}
}

function EventPanel_Init()
{
	PanelEvent_Detach();
	Event_Add ( "PanelEvent_HandleThings();", CFG_Event_Interval, CFG_Event_Interval );
}
