/*****************************************************************************
 *
 * File     : Toolbar.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : This class implements the toolbar behaviour. 
 *            It is constructed with the html toolbar element parameter. This element
 *            satisfies the following constraints:
 *            1. It is a container element, usually, but not necessarily <div> element
 *            2. It must have an ID
 *            3. It has a number of <a> elements representing buttons.
 *            4. Each button is a child of the container (first-level descendant)
 *            5. Container can contain other elements, however only <a> anchors will
 *               be considered buttons. (See Button.js)
 *            
 *            An example of the toolbar html element:
 * 
 * <div id="requestToolbar" class="toolbar">
 *   <a id="selectButton" class="buttonPressed" type="plain" href="javascript:" onclick="activateSelect(this)"><img id="selectButtonImage" class="buttonImage" alt="Select" src="images/selectActive.gif"/></a>
 *   <span class="buttonText">&nbsp;&nbsp;(Select a feature)</span>
 *   <p></p>
 *   <a id="panButton" class="button" type="instantToggle" href="javascript:" onclick="activatePan(this)"><img id="panButtonImage" class="buttonImage" alt="Pan" src="images/panActive.gif"/></a>
 * </div>

 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*	
*
*************************************************************************/

/************************************************************************
*
*	function:	Toolbar
*
*	purpose:	Constructor
*
*************************************************************************/
function Toolbar(uiElement)
{
	// method assignments
	this.init = Toolbar.init;
	this.setEnabled = Toolbar.setEnabled;
	this.isEnabled = Toolbar.isEnabled;
	this.getButtons = Toolbar.getButtons;
	this.toggleButtonSelected = Toolbar.toggleButtonSelected;
	this.getButton = Toolbar.getButton;
	this.getSelectedButton = Toolbar.getSelectedButton;
	this.setSize = Toolbar.setSize;

	this.onDrag = Toolbar.onDrag;
	this.onDragAdapter = Toolbar.onDragAdapter;
	
	// instance field declarations
	this._uiElement;
	this._buttons;
	this._enabled;

	// initialization
	this.init(uiElement);
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initializes the globals
*
*************************************************************************/
function Toolbar.init(uiElement)
{
	this._uiElement = uiElement;
	this._uiElement.className = "toolbar";
	this._enabled = true;
	var buttonUIs = selectNodes(this._uiElement, "a");

	this._buttons = new Collection();
	for (var index = 0; index < buttonUIs.getSize(); index++)
	{
		var buttonUI = buttonUIs.get(index);
		var type = buttonUI.getAttribute("type");
		var button;
		if (type && type == "toggle") button = new ToggleButton(buttonUI, this);
		else if (type && type == "tab") button = new TabButton(buttonUI, this);
		else button = new Button(buttonUI, this);
		this._buttons.add(button);
	}

	// ondrag is used only to prevent the "not-allowed" cursor from appearing
	// this happens when user engages the button (mousedown) and then moves away
	// e.g. changed his mind
	this._uiElement.setAttribute("dataTransfer", this);
	this._uiElement.attachEvent("ondrag", Toolbar.onDragAdapter);
}

/************************************************************************
*
*	function:	setSize
*
*	purpose:	
*
*************************************************************************/
function Toolbar.setSize(width, height)
{
	this._uiElement.width = width;
	this._uiElement.height = height;
}

/************************************************************************
*
*	function:	getButtons
*
*	purpose:	
*
*************************************************************************/
function Toolbar.getButtons()
{
	return this._buttons;
}

/************************************************************************
*
*	function:	getButton
*
*	purpose:	
*
*************************************************************************/
function Toolbar.getButton(id)
{
	var size = this._buttons.getSize();
	for (var index = 0; index < size; index++)
	{
		var button = this._buttons.get(index);
		if (button.getId() == id) return button;
	}
	return null;
}

/************************************************************************
*
*	function:	setEnabled
*
*	purpose:	
*
*************************************************************************/
function Toolbar.setEnabled(enable)
{
	if (this._enabled == enable) return;

	var size = this._buttons.getSize();
	for (var index = 0; index < size; index++)
	{
		this._buttons.get(index).setEnabled(enable);
	}
	this._enabled = enable;
}

/************************************************************************
*
*	function:	isEnabled
*
*	purpose:	
*
*************************************************************************/
function Toolbar.isEnabled()
{
	return this._enabled;
}

/************************************************************************
*
*	function:	toggleButtonSelected
*
*	purpose:	
*
*************************************************************************/
function Toolbar.toggleButtonSelected(theButton)
{
	var size = this._buttons.getSize();
	for (var index = 0; index < size; index++)
	{
		var button = this._buttons.get(index);
		if (button == theButton) continue;
		button.setSelected(false);
	}
}

/************************************************************************
*
*	function:	getSelectedButton
*
*	purpose:	
*
*************************************************************************/
function Toolbar.getSelectedButton()
{
	for (var index = 0; index < this._buttons.getSize(); index++)
	{
		var button = this._buttons.get(index);
		if (button.isSelected()) return button;
	}
	return null;
}

/************************************************************************
*
*	function:	onDrag
*
*	purpose:	
*
*************************************************************************/
function Toolbar.onDrag(event)
{
	event.returnValue = false;
}

/************************************************************************
*
*	function:	Event handler adapters
*
*	purpose:	
*
*************************************************************************/
function Toolbar.onDragAdapter(event){target(event, Toolbar).onDrag(event);}

