/*****************************************************************************
 *
 * File     : ButtonEventFilter.js   
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 * 
 * 
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*	
*
*************************************************************************/

/************************************************************************
*
*	function:	ButtonEventFilter
*
*	purpose:	Constructor
*
*************************************************************************/
function ButtonEventFilter(uiElement, button)
{
	// method assignments
	this.init = ButtonEventFilter.init;
	this.isEnabled = ButtonEventFilter.isEnabled;
	this.setEnabled = ButtonEventFilter.setEnabled;
	this.setClassName = ButtonEventFilter.setClassName;
	
	this.click = ButtonEventFilter.click;
	this.mouseDown = ButtonEventFilter.mouseDown;
	this.mouseUp = ButtonEventFilter.mouseUp;
	this.mouseEnter = ButtonEventFilter.mouseEnter;
	this.mouseLeave = ButtonEventFilter.mouseLeave;
	this.dragEnd = ButtonEventFilter.dragEnd;
	
	this.clickAdapter = ButtonEventFilter.clickAdapter;
	this.mouseDownAdapter = ButtonEventFilter.mouseDownAdapter;
	this.mouseUpAdapter = ButtonEventFilter.mouseUpAdapter;
	this.mouseEnterAdapter = ButtonEventFilter.mouseEnterAdapter;
	this.mouseLeaveAdapter = ButtonEventFilter.mouseLeaveAdapter;
	this.dragEndAdapter = ButtonEventFilter.dragEndAdapter;
	
	// constants
	this.INNER_STATE_NORMAL			= 0;
	this.INNER_STATE_INFLATED		= 1;
	this.INNER_STATE_ENGAGED		= 2;
	this.INNER_STATE_ONHOLD			= 3;

	// instance field declarations
	this._button;
	this._uiElement;
	this._innerState;
	this._buttonEnabled;
	this._dataTransferBuffer;

	// initialization
	this.init(uiElement, button);
}

/************************************************************************
*
*	function:	init
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.init(uiElement, button)
{
	this._uiElement = uiElement;
	this._button = button;
	this._uiElement.className = "button";
	this._innerState = this.INNER_STATE_NORMAL;
	this._buttonEnabled = true;

	this._uiElement.setAttribute("dataTransfer", this);
	this._uiElement.attachEvent("onclick", ButtonEventFilter.clickAdapter);
	this._uiElement.attachEvent("onmousedown", ButtonEventFilter.mouseDownAdapter);
	this._uiElement.attachEvent("onmouseup", ButtonEventFilter.mouseUpAdapter);
	this._uiElement.attachEvent("onmouseenter", ButtonEventFilter.mouseEnterAdapter);
	this._uiElement.attachEvent("onmouseleave", ButtonEventFilter.mouseLeaveAdapter);
}

/************************************************************************
*
*	function:	setClassName
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.setClassName(className)
{
	this._uiElement.className = className;
}

/************************************************************************
*
*	function:	setEnabled
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.setEnabled(enable)
{
	this._buttonEnabled = enable;
}

/************************************************************************
*
*	function:	mouseDown
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.mouseDown()
{
	if (this._buttonEnabled == false) return;
	if (this._innerState == this.INNER_STATE_INFLATED) 
	{
		this._innerState = this.INNER_STATE_ENGAGED;
		this._button.onEngaged();
	}
}

/************************************************************************
*
*	function:	mouseUp
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.mouseUp()
{
	if (this._buttonEnabled == false) return;
	if (this._innerState == this.INNER_STATE_ENGAGED) 
	{
		this._innerState = this.INNER_STATE_INFLATED;
		this._button.onClicked();
		this._button.onInflated();
	}
}

/************************************************************************
*
*	function:	mouseEnter
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.mouseEnter()
{
	if (this._buttonEnabled == false) return;
	if (this._innerState == this.INNER_STATE_NORMAL) 
	{
		this._innerState = this.INNER_STATE_INFLATED;
		this._button.onInflated();
	}
	if (this._innerState == this.INNER_STATE_ONHOLD) 
	{
		this._innerState = this.INNER_STATE_ENGAGED;
		this._button.onEngaged();
	}
}

/************************************************************************
*
*	function:	mouseLeave
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.mouseLeave()
{
	if (this._buttonEnabled == false) return;
	if (this._innerState == this.INNER_STATE_INFLATED) 
	{
		this._innerState = this.INNER_STATE_NORMAL;
		this._button.onNormal();
	}
	else if (this._innerState == this.INNER_STATE_ENGAGED) 
	{
		this._innerState = this.INNER_STATE_ONHOLD;
		this._button.onOnHold();
		this._dataTransferBuffer = document.body.dataTransfer;
		document.body.dataTransfer = this;
		document.body.attachEvent("onclick", ButtonEventFilter.dragEndAdapter);
	}
}

/************************************************************************
*
*	function:	dragEnd
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.dragEnd()
{
	if (this._buttonEnabled == false) return;
	if (this._innerState != this.INNER_STATE_ONHOLD) return;

	this._innerState = this.INNER_STATE_NORMAL;
	this._button.onNormal();
	document.body.dataTransfer = this._dataTransferBuffer;
	document.body.detachEvent("onclick", ButtonEventFilter.dragEndAdapter);
}

/************************************************************************
*
*	function:	click
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.click(event)
{
	if (this._buttonEnabled == false) event.cancelBubble = true;
}

/************************************************************************
*
*	function:	Event handler adapters
*
*	purpose:	
*
*************************************************************************/
function ButtonEventFilter.dragEndAdapter(){document.body.dataTransfer.dragEnd()}
function ButtonEventFilter.clickAdapter(event){target(event, ButtonEventFilter).click(event);}
function ButtonEventFilter.mouseDownAdapter(event){target(event, ButtonEventFilter).mouseDown(event);}
function ButtonEventFilter.mouseUpAdapter(){target(event, ButtonEventFilter).mouseUp(event);}
function ButtonEventFilter.mouseEnterAdapter(){target(event, ButtonEventFilter).mouseEnter(event);}
function ButtonEventFilter.mouseLeaveAdapter(){target(event, ButtonEventFilter).mouseLeave(event);}

