function CToolbar()
{
	this.m_aButtons = new Array();  // Associative array.
	this.m_iButtons = new Array();  // Indexed array.
	this.currentTool = null;
	this.doc = null;
	this.parent = null;  // This is the CMapFuncs class that controls this toolbar.
	
	this.addButton =
	function m_addButton(button)
	{
		this.m_aButtons[button.name] = button;
		this.m_iButtons[this.m_iButtons.length] = button;
	}

	// Factory methods -->
	this.newButton =
	function m_newButton(name, actionCallback)
	{
		var btn = new CButton();
		btn.setButtonProps(name, actionCallback, this);
		return btn;
	}

	this.newTool =
	function m_newTool(name)
	{
		var tool = new CTool();
		tool.setButtonProps(name, null, this);
		return tool;
	}
	
	this.newToggle =
	function m_newToggle(name, actionCallback)
	{
		var tgl = new CToggle();
		tgl.setButtonProps(name, actionCallback, this);
		return tgl;
	}
	// <-- Factory methods

	this.getButton =
	function m_getButton(name)
	{
		return this.m_aButtons[name];
	}

	this.setCurrentTool =
	function m_setCurrentTool(button)
	{
		if (button != null && this.isTool(button))
		{
			if (this.currentTool != null)
				this.currentTool.deactivate();
			this.currentTool = button;
			this.currentTool.activate();

			// Set the value of the hidden text box.
			if (this.parent != null && this.parent.m_form != null)
				this.parent.m_form.cmd.value = button.name;
		}
	}

	this.setCurrentToolByName =
	function m_setCurrentToolByName(toolName)
	{
		var tool = this.m_aButtons[toolName];
		if (tool != null)
			this.setCurrentTool(tool);
	}

	this.defaultCurrentTool =
	function m_defaultCurrentTool()
	{
		for (var i = 0; i < this.m_iButtons.length; i++)
		{
			var btn = this.m_iButtons[i];
			if (btn.type == "tool")
			{
				this.setCurrentTool(btn);
				break;
			}
		}
	}

	this.clickButton =
	function m_clickButton(name)
	{
		var btn = this.getButton(name);
		if (btn != null)
		{
			this.setCurrentTool(btn);
			btn.click();
		}
	}

	this.isTool =
	function m_isTool(button)
	{
		return (button.type == "tool");
	}
	
	this.isToggle =
	function m_Toggle(button)
	{
		return (button.type == "toggle");
	}

	// Toggle the named toggle button.
	this.setToggle =
	function m_setToggle(name)
	{
		var btn = this.getButton(name);
		if (btn != null && this.isToggle(btn))
			btn.setToggled(true);
	}
}

function CButton()
{
	this.type = "button";
	this.name = "";
	this.parent = null;
	this.action = null;

	this.click =
	function m_click()
	{
		if (this.action != null)
			this.action(this);
	}

	this.setButtonProps =
	function m_setButtonProps(name, actionCallback, toolbar)
	{
		this.name = name;
		this.action = actionCallback;
		this.parent = toolbar;
	}
}

function CTool()
{
	// Inherit.
	this.inheritFrom = CButton;
	this.inheritFrom();

	this.type = "tool";
	this.activatedClass = "active";

	this.click =
	function m_click()
	{
		// The tool *may* have an action, but probably doesn't.
		if (this.action != null)
			this.action(this);
	}

	// Change to the active version of the tool.
	this.activate =
	function m_activate()
	{
		this.changeClass(this.activatedClass);
	}

	// Change to the inactive version of the tool.
	this.deactivate =
	function m_deactivate()
	{
		this.changeClass("");
	}

	this.changeClass =
	function m_changeClass(className)
	{
		var lis = this.parent.doc.getElementsByName(this.name);
		var li = lis[0];
		li.className = className;
	}
}

function CToggle()
{
	this.inheritFrom = CTool;
	this.inheritFrom();

	this.type = "toggle";
	this.activatedClass = "toggled";
	this.toggled = false;

	this.click =
	function m_click()
	{
		// Toggle only if it's action returns true.
		if (this.action(this))
			this.setToggled(!this.toggled);
	}

	this.setToggled =
	function m_setToggled(toggled)
	{
		this.toggled = toggled;
		if (toggled)
			this.activate();
		else
			this.deactivate();
	}
}