/**
 * jQuery Toolbar plugin 1.1.2
 *
 * Copyright (c) 2008 Ruslan V. Us
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
jQuery.fn.extend
({
	addToolbar: function (id, params)
	{
		if (jQuery ("#" + id).size () > 0) return;

		params = jQuery.extend ({
			buttonWidth:		24,
			buttonHeight:		24,
			tooltipId:			"___toolbar-hint",
			toolbarClass:		"___toolbar",
			buttonClass: 		"___toolbar-button",
			buttonOverClass:	"___toolbar-button-over",
			buttonPressClass:	"___toolbar-button-press",
			buttonDisableClass:	"___toolbar-button-disable",
			buttons:			[]
		}, params);

		var toolbar = jQuery ("<div></div>")
			.attr ("id", id)
			.css ({
				height: (params.buttonHeight + 6) + "px",
				padding: "1px"
			})
			.addClass (params.toolbarClass)
			.appendTo (jQuery (this));
		if (params.width) toolbar.css ("width", params.width);

		for (i = 0; i < params.buttons.length; i ++)
		{
			params.buttons [i] = jQuery.extend ({
				id: "button" + i,
				caption: "Button " + i,
				disabled: false,
				click: function () {}
			}, params.buttons [i]);

			var button = jQuery ("<div></div>")
				.attr ({
					id: id + "_" + params.buttons [i].id,
					tooltip: params.tooltipId,
					caption: params.buttons [i].caption,
					overclass: params.buttonOverClass,
					pressclass: params.buttonPressClass,
					disableclass: params.buttonDisableClass
				})
				.css ("float", "left")
				.addClass (params.buttonClass)
				.append (
					jQuery ("<img />")
						.attr ({
							src: params.buttons [i].image,
							width: params.buttonWidth,
							height: params.buttonHeight
						})
						.css ({
							margin: "0px",
							padding: "0px"
						})
				)
				.appendTo (toolbar)
				.bind ("mouseenter mouseleave", function ()
				{
					var btn = jQuery (this);
					if (btn.attr ("disabled")) return;
					btn.toggleClass (btn.attr ("overclass"));
					btn.removeClass (btn.attr ("pressclass"));
				})
				.bind ("mousedown mouseup", function ()
				{
                	var btn = jQuery (this);
					if (!btn.attr ("disabled"))
						btn.toggleClass (btn.attr ("pressclass"));
				})
				.click (params.buttons [i].click);
			if (params.buttons [i].disabled) jQuery.disableToolButton (id, params.buttons [i].id);
		}
		if (jQuery.bindTooltip) jQuery.bindTooltip (params.tooltipId);
	   	return toolbar;
	}
});

jQuery.extend
({
	disableToolButton: function (toolbarId, buttonId)
	{
		var button = jQuery ("#" + toolbarId + "_" + buttonId);
		if (button.attr ("disabled")) return;
		button
			.attr ("disabled", true)
			.addClass (button.attr ("disableclass"));
	},

	enableToolButton: function (toolbarId, buttonId)
	{
		var button = jQuery ("#" + toolbarId + "_" + buttonId);
		if (!button.attr ("disabled")) return;
		button
			.removeAttr ("disabled")
			.removeClass (button.attr ("disableclass"));
	}
});

