$Button = new Class() ({
	__init__: function ($button) {
		this.initCache($button);

		if (! this.$button.hasClass('ui-button ui-widget')) {
			this.initText();
			this.initContainer(this.getButtonLabelType(this.getIconCount(), this.hasText()));
			this.initIcons();
		}
		
		return this.$button;
	},

	initCache: function ($button) {
		if (! ($button instanceof jQuery)) {
			$button = jQuery($button);
		}

		this.$button = $button;
		this.$text = $button.children('.ui-button-text');
		this.$alt = $button.children('.ui-button-alt');
		this.$icons = $button.children('[class|=ui-icon]');
	},

	initText: function () {
		if (! this.hasText()) {
			// .icon-only, set tooltip on <button>, then hide ui-button-text <span>
			this.$button.attr('title', this.$alt.text());
			this.$alt.addClass('ui-button-text');
		}
	},
	
	initContainer: function (buttonLabelType) {
		// initialize classes on <button>
		this.$button.addClass('ui-button ui-widget ui-state-default ' + buttonLabelType);
		
		if (this.$button.attr('selected') !== undefined) {
			this.$button.addClass('ui-state-active');
		}
	},

	initIcons: function () {
		// .icon-primary == left  // .icon-secondary == right
		this.$icons.filter(':eq(0)').addClass('ui-icon ui-button-icon-primary');
		this.$icons.filter(':eq(1)').addClass('ui-icon ui-button-icon-secondary');
	},
	
	hasText: function () { return this.$text.length !== 0; },
	getIconCount: function () { return this.$icons.length; },

	getButtonLabelType: function (iconCount, hasText) {
		if (hasText) {
			return iconCount ? (iconCount === 1 ? 'ui-button-text-icon' : 'ui-button-text-icons') : 'ui-button-text-only';
		}
		
		return 'ui-button-icon-only';
	}
});

$Button .Static({
	create: function (buttonType, label, primaryIcon, secondaryIcon, iconsOnly) {
		var $button = jQuery('<'+buttonType+'>');
		
		if (label) { jQuery('<span>').addClass('ui-button-' + (iconsOnly ? 'alt' : 'text')).html(label).appendTo($button); }
		if (primaryIcon) { jQuery('<span>').addClass(primaryIcon).appendTo($button); }
		if (secondaryIcon) { jQuery('<span>').addClass(secondaryIcon).appendTo($button); }
		
		return new $Button($button);
	},

	initializeAllButtons: function () {
		jQuery('button > .ui-button-text, a > .ui-button-text, button > [class|=ui-icon], a > [class|=ui-icon]').each(function (i, e) {
			// instantiating this $Button class will set everything up as it should be
			var $button = new $Button(jQuery(e).parent());
		});
	},

	initializeLiveButtonEvents: function () {
		jQuery('.ui-state-default:not(.ui-state-disabled)')
			.live('focus',		function () { jQuery(this).addClass('ui-state-focus'); })
			.live('blur',		function () { jQuery(this).removeClass('ui-state-focus'); })
			.live('mousedown',	function () { jQuery(this).addClass('ui-state-active'); })
			.live('mouseup',	function () { jQuery(this).removeClass('ui-state-active'); })
			.live('mouseover',	function () { jQuery(this).addClass('ui-state-hover'); })
			.live('mouseout',	function () { 
				if (jQuery(this).attr('selected') === undefined && ! jQuery(this).hasClass('ui-tabs-selected')) {
					jQuery(this).removeClass('ui-state-hover ui-state-active ui-state-focus');
				}
			});
	}
});
