////////////////////////////////////////////////////////////////////////////////////////////
// Toolbar client-side script
////////////////////////////////////////////////////////////////////////////////////////////

addNamespace("Toolbar");

Toolbars = new Core.Collection();

Toolbar.Select = function(buttonId)
{
	for (i = 0; i < Toolbars.items.length; i++)
		Toolbars.items[i].select(buttonId);
};

Toolbar.MenuSelect = function(buttonId, arg)
{
	var button = eval(buttonId);
	
	if (button && button.onmenuselect)
		button.onmenuselect(arg);
};

////////////////////////////////////////////////////////////////////////////////////////////

Toolbar.Toolbar = Class.create();
Toolbar.Toolbar.prototype =
{
	buttons: new Core.Collection(),
	
	initialize: function(id, enabled, target)
	{
		this.id = id;
		this.target = null;
		this.enabled = true;
		
		if (Toolbars.contains(this.id))
			Toolbars.remove(this.id);
		
		Toolbars.add(this);
		
		this.control = $(this.id);
		
		if (target)
			this.target = target;
		
		if (enabled != null)
			this.enabled = enabled;
	},
	
	select: function(buttonId)
	{
		var button = this.buttons.get(buttonId);
		
		if (!button || !this.enabled)
			return;
		
		if (button.selected)
		{
			button.deselect(button);
			return;
		}
		
		for (i = 0; i < this.buttons.items.length; i++)
		{
			if (!this.buttons.items[i].selected)
				continue;
			
			if (button.exclusive && this.buttons.items[i].exclusive)
				this.buttons.items[i].deselect(button);
			else if (this.buttons.items[i].onmenuselect && this.buttons.items[i].id != buttonId)
				this.buttons.items[i].deselect(button);
		}
		
		button.select();
	},
	
	deselect: function(buttonId)
	{
		var button = this.buttons.get(buttonId);
		
		if (!button || !this.enabled)
			return;
		
		if (button.selected)
			button.deselect(null);
			return;
	},
	
	show: function()
	{
		this.control.style.display = "";
	},
	
	hide: function()
	{
		this.control.style.display = "none";
	},
	
	showButton: function(buttonId)
	{
		for (i = 0; i < this.buttons.items.length; i++)
		{
			var button = this.buttons.items[i];
			var id = button.id.replace(this.id + "_", "");
			
			if (id == buttonId)
				button.show();
		}
	},
	
	hideButton: function(buttonId)
	{
		for (i = 0; i < this.buttons.items.length; i++)
		{
			var button = this.buttons.items[i];
			var id = button.id.replace(this.id + "_", "");
			
			if (id == buttonId)
				button.hide();
		}
	},
	
	getButton: function(buttonId)
	{
		for (i = 0; i < this.buttons.items.length; i++)
		{
			var button = this.buttons.items[i];
			
			if (button.id == buttonId || button.id.replace(this.id + "_", "") == buttonId)
				return button;
		}
		
		return null;
	},
	
	showMenu: function(fullMenuId, buttonId)
	{
		var control = $(fullMenuId);
		
		if (control)
		{
			var button = $(buttonId);
			if (!button)
				return;
			
			control.style.left = Core.Utility.toSize(Core.Utility.getElementX(button, this.control));
			control.style.top = Core.Utility.toSize(Core.Utility.getElementY(button, this.control) + Core.Utility.getElementHeight(button) - 1);
			control.style.display = "";
		}
	},
	
	hideMenu: function(fullMenuId)
	{
		var control = $(fullMenuId);
		
		if (control)
			control.style.display = "none";
	}
};

////////////////////////////////////////////////////////////////////////////////////////////

Toolbar.Button = Class.create();
Toolbar.Button.prototype =
{
	initialize: function(id, toolbar, exclusive, onselect, ondeselect)
	{
		this.id = id;
		this.toolbar = null;
		this.onselect = null;
		this.ondeselect = null;
		this.onshow = null;
		this.onhide = null;
		this.onrefresh = null;
		this.onmenuselect = null;
		this.exclusive = true;
		this.selected = false;
		this._timer = new Core.Timer(1000);
		
		if (toolbar)
			this.addToToolbar(toolbar);
		if (exclusive != null)
			this.exclusive = exclusive;
		if (onselect)
			this.onselect = onselect;
		if (ondeselect)
			this.ondeselect = ondeselect;
		
		this.control = $(this.id);
	},
	
	setRefresh: function(handler, delay)
	{
		var _this = this;
		this.onrefresh = handler;
		this._timer.ontimer = function()
		{
			return _this.onrefresh();
		}
		
		if (delay)
			this._timer.delay = delay;
		
		this._timer.start();
	},
	
	addToToolbar: function(toolbar)
	{
		if (toolbar.buttons.contains(this.id))
			toolbar.buttons.remove(this.id);
		
		toolbar.buttons.add(this);
		
		this.toolbar = toolbar;
	},
	
	select: function()
	{
		var update = true;
		
		if (this.onselect)
			update = this.onselect();
		
		if (update)
		{
			this.selected = true;
			this.update();
			this.toolbar.showMenu(this.id + "_Menu", this.id);
		}
		
		return update;
	},
	
	deselect: function(newbutton)
	{
		var update = true;
		
		if (this.ondeselect)
			update = this.ondeselect(newbutton);
		
		if (update)
		{
			this.selected = false;
			this.update();
			this.toolbar.hideMenu(this.id + "_Menu");
		}
		
		return update && newbutton != this;
	},
	
	update: function()
	{
		if (!this.control)
			return;
		
		if (this.selected)
		{
			this.control.style.backgroundColor = this.control.getAttribute("c2");
			this.control.style.borderStyle = "inset";
		}
		else
		{
			this.control.style.backgroundColor = this.control.getAttribute("c1");
			this.control.style.borderStyle = "outset";
		}
	},
	
	show: function()
	{
		var update = true;
		
		if (this.onshow != null)
			update = this.onshow();
		
		if (update && this.control)
			this.control.style.display = "inline";
	},
	
	hide: function()
	{
		var update = true;
		
		if (this.onhide != null)
			update = this.onhide();
		
		if (update)
		{
			if (this.control)
				this.control.style.display = "none";
			
			this.toolbar.hideMenu(this.id + "_Menu");
		}
	}
};

////////////////////////////////////////////////////////////////////////////////////////////

addNamespace("Toolbar.Buttons");
Toolbar.Buttons.FactoryClass = new Class.create();
Toolbar.Buttons.FactoryClass.prototype =
{
	buildColorMenu: function(fullMenuId)
	{
		var menu = $(fullMenuId);
		var buttonId = fullMenuId.replace("_Menu", "");
		var template = null;
		
		if (!menu || menu.childNodes.length == 0 || menu.childNodes[0].childNodes.length > 10)
			return;
		
		for (i = 0; i < menu.childNodes[0].childNodes.length; i++)
		{
			if (menu.childNodes[0].childNodes[i].nodeName == "DIV")
			{
				template = menu.childNodes[0].childNodes[i];
				break;
			}
		}
		
		for (c = 0; c < Drawing.WebSafeColors.length; c++)
		{
			var color = Drawing.WebSafeColors[c];
			var div = template.cloneNode(true);
			div.title = color;
			div.style.backgroundColor = "#" + color;
			
			var row = Math.floor(c / 18);
			
			div.style.position = "absolute";
			div.style.top = Core.Utility.toSize(row * 7);
			div.style.left = Core.Utility.toSize((c - (row * 18)) * 7);
			
			menu.childNodes[0].appendChild(div);
		}
		
		menu.childNodes[0].removeChild(template);
	}
};
Toolbar.Buttons.Factory = new Toolbar.Buttons.FactoryClass();

////////////////////////////////////////////////////////////////////////////////////////////


Toolbar.Buttons.UtilityClass = new Class.create();
Toolbar.Buttons.UtilityClass.prototype =
{
	_getFirstChild: function(element, type)
	{
		if (!element.childNodes)
			return null;
		
		for (i = 0; i < element.childNodes.length; i++)
		{
			if (element.childNodes[i].nodeName == type.toUpperCase())
				return element.childNodes[i];
		}
	},
	
	_updateColorButton: function(button, color)
	{
		color = color.toString();
		
		for (i = 0; i < button.control.childNodes.length; i++)
		{
			if (button.control.childNodes[i].className == "colorBar")
			{
				if (button.control.childNodes[i].style.backgroundColor != color)
				{
					button.control.childNodes[i].style.backgroundColor = color ? color : "transparent";
					$(button.id + "_Preview").style.backgroundColor = color ? color : "transparent";
				}
				
				break;
			}
		}
	},
	
	_updateSelectButton: function(button, value)
	{	
		var select = Toolbar.Buttons.Utility._getFirstChild(button.control, "select");
		
		if (select && select.value != value)
			select.value = value;
	},
	
	_updateToggleButton: function(button, value)
	{
		if (button.selected != Boolean(value))
		{
			button.selected = Boolean(value);
			button.update();
		}
	},
	
	_updateSplitMenuImage: function(childButton)
	{
		if (childButton.id.indexOf("MenuButton_") > 0)
		{
			var parentId = childButton.id.substring(0, childButton.id.indexOf("MenuButton_") + 10);
			var parent = eval(parentId);
			var image = Toolbar.Buttons.Utility._getFirstChild(childButton.control, "img");
			var image1 = Toolbar.Buttons.Utility._getFirstChild(parent.control, "img");
			
			if (image && image1 && image1.src != image.src)
				image1.src = image.src;
		}
	},
	
	_selectParent: function(button)
	{
		if (button.id.indexOf("MenuButton_") > 0)
		{
			var parentId = button.id.substring(0, button.id.indexOf("MenuButton_") + 10);
			var parent = eval(parentId);
			
			if (parent)
			{
				parent.selected = true;
				parent.update();
			}
		}
	}
};
Toolbar.Buttons.Utility = new Toolbar.Buttons.UtilityClass();




