function CToolbar()
{
	this.buttons = new Array();
	this.currentTool = null;

	this.addButton = 
	function m_addButton(image, hoverImage, actionFunc, name, description)
	{
		var btn = new CButton();
		btn.setButtonProps(image, hoverImage, actionFunc, name, description);
		this.addToButtons(btn);
	};

	this.addTool =
	function m_addTool(image, hoverImage, actionFunc, name)
	{
		var tool = new CTool();
		tool.setToolProps(image, hoverImage, actionFunc, name);
		this.addToButtons(tool);

		// Default to this being the active tool if it's the first one.
		if (this.currentTool == null)
			this.setCurrentTool(tool.index);
	}

	this.addToggle =
	function m_addToggle(image, hoverImage, checkedImage, checkedHoverImage, actionFunc, name)
	{
		var toggle = new CToggle();
		toggle.setToggleProps(image, hoverImage, checkedImage, checkedHoverImage, actionFunc, name);
		this.addToButtons(toggle);
	}

	this.addToButtons =
	function m_addToButtons(button)
	{
		button.index = this.buttons.length;
		this.buttons[button.index] = button;
	};

	// Set current tool by index.
	this.setCurrentTool =
	function m_setCurrentTool(index)
	{
		// Change the previous tool's image back.
		if (this.currentTool != null)
			this.currentTool.leave();

		this.currentTool = null;
		if (this.inBounds(index))
		{
			var btn = this.buttons[index];
			if (this.isTool(btn))
			{
				// Set the current tool and it's image.
				this.currentTool = btn;
				btn.enter();
			}
		}
	};

	// Set current tool by its name.
	this.setCurrentToolByName =
	function m_setCurrentToolByName(name)
	{
		var btn = this.findButtonByName(name);
		if (btn != null)
		{
			if (this.isTool(btn))
				this.setCurrentTool(btn.index);
		}
	};

	// Check a toggle by its name.
	this.checkToggleByName =
	function m_checkToggleByName(name, value)
	{
		var btn = this.findButtonByName(name);
		if (btn != null)
		{
			if (this.isToggle(btn))
				btn.setChecked(value);
		}
	};

	this.findButtonByName =
	function m_findButtonByName(name)
	{
		for (var i = 0; i < this.buttons.length; i++)
		{
			var btn = this.buttons[i];
			if (btn.name == name)
				return btn;
		}
		return null;
	};

	this.findButtonByIndex =
	function m_findButtonByIndex(index)
	{
		if (this.inBounds(index))
			return this.buttons[index];
		else
			return null;
	};

	this.isTool =
	function m_isTool(button)
	{
		return (button.type == "tool");
	};
	
	this.isToggle =
	function m_isToggle(button)
	{
		return (button.type == "toggle");
	};
		
	this.clickButton =
	function m_clickButton(index)
	{
		if (this.inBounds(index))
		{
			var btn = this.buttons[index];
			if (this.isTool(btn))
				this.setCurrentTool(btn.index);
			btn.click();
		}
		return true;
	};

	// Change to the hover image.
	this.enter =
	function m_enter(index)
	{
		if (this.inBounds(index))
		{
			// The current tool is unaffected.
			if (index != this.currentTool.index)
				this.buttons[index].enter();
		}
	};

	// Change to the normal image.
	this.leave =
	function m_leave(index)
	{
		if (this.inBounds(index))
		{
			// The current tool is unaffected.
			if (index != this.currentTool.index)
				this.buttons[index].leave();
		}
	};

	// Is this a valid array index.
	this.inBounds =
	function m_inBounds(index)
	{
		return (index < this.buttons.length);
	};
}

function CButton()
{
	this.type = "button";
	this.image = null;
	this.hoverImage = null;
	this.action = null;
	this.index = -1;
	this.name = "";
	this.description = "";

	this.setButtonProps =
	function m_setButtonProps(image, hoverImage, actionFunc, name, description)
	{
		this.setImage(image);
		this.setHoverImage(hoverImage);
		this.action = actionFunc;
		this.name = name;
		this.description = description;
	}

	this.setImage =
	function m_setImage(src)
	{
		this.image = this.createImage(src);
	};

	this.setHoverImage =
	function m_setHoverImage(src)
	{
		this.hoverImage = this.createImage(src);
	};

	this.createImage =
	function m_createImage(src)
	{
		var img = new Image();
		img.src = src;
		return img;
	};

	this.click =
	function m_click()
	{
		if (this.action != null)
			this.action(this);
	};

	this.enter =
	function enter()
	{
		this.changeImage(this.hoverImage.src);
		//window.status = this.description;
		return true;
	};

	this.leave =
	function leave()
	{
		this.changeImage(this.image.src);
		//window.status = "";
		return true;
	};

	this.changeImage =
	function m_changeImage(newImage)
	{
		eval("document.images['button" + this.index + "'].src = newImage");
	};
}

function CTool()
{
	this.inheritFrom = CButton;
	this.inheritFrom();

	this.type = "tool";

	this.setToolProps =
	function m_setToolProps(image, hoverImage, actionFunc, name, description)
	{
		this.setButtonProps(image, hoverImage, actionFunc, name, description);
	};
}

function CToggle()
{
	this.inheritFrom = CButton;
	this.inheritFrom();
	
	this.type = "toggle";
	this.checkedImage = null;
	this.checkedHoverImage = null;
	this.checked = false;
	
	this.setToggleProps =
	function m_setToggleProps(image, hoverImage, checkedImage, checkedHoverImage, actionFunc, name, description)
	{
		this.setButtonProps(image, hoverImage, actionFunc, name, description);
		this.setCheckedImage(checkedImage);
		this.setCheckedHoverImage(checkedHoverImage);
	};
	
	this.setCheckedImage =
	function m_setCheckedImage(src)
	{
		this.checkedImage = this.createImage(src);
	};

	this.setCheckedHoverImage =
	function m_setCheckedHoverImage(src)
	{
		this.checkedHoverImage = this.createImage(src);
	};

	this.click =
	function m_click()
	{
		this.setChecked(!this.checked);
		if (this.action != null)
			this.action(this);
	};

	this.setChecked =
	function m_setChecked(value)
	{
		if (value)
			this.check();
		else
			this.uncheck();
	};

	this.check =
	function m_check()
	{
		this.checked = true;
		this.changeImage(this.checkedImage.src);
	};

	this.uncheck =
	function m_uncheck()
	{
		this.checked = false;
		this.changeImage(this.image.src);
	};

	this.enter =
	function m_enter()
	{
		if (this.checked)
			this.changeImage(this.checkedHoverImage.src);
		else
			this.changeImage(this.hoverImage.src);
	};

	this.leave =
	function m_leave()
	{
		if (this.checked)
			this.changeImage(this.checkedImage.src);
		else
			this.changeImage(this.image.src);
		//window.status = "";
	};
}
