/*
 * Gx.Widget
 */

Gx.Widget = new Class({
	Implements: [Options],
	initialize: function(container, options) {
		this.setOptions(options);
		this.container = $(container);

		// The 'disabled' property determines if the widget is disabled or not
		this.container.disabled = false;

		// 
		this.container.addEvents({
			mousedown:	this.mousedown.bindWithEvent(this),
			mouseup: 	this.mouseup.bindWithEvent(this),
			mouseenter: this.mouseenter.bindWithEvent(this),
			mouseleave: this.mouseleave.bindWithEvent(this)
		});
	
		// Add the widget how
		Gx.add(this.container);
	},
	mousedown: function(event) {
		if (!this.isFocused() || event.rightClick == true)
			return;
	
		this.container.addClass('active');
		this.highlight(event);
	},
	mouseup: function(event) {
		this.container.removeClass('active');
	},	
	mouseenter: $empty,
	mouseleave: function(event) {
		this.container.removeClass('active');//.removeClass('hover');
	},
	/*
	 * Highlight function repace focus event
	 */
	highlight: function(event) {
		Gx.blur();
		this.container.addClass('focus');
		Gx.current = this.container;
		
		// Update tabindex with focus on the current element
		Gx.available().each(function(element, index) {
			if (element == Gx.current)
				Gx.tabindex = index;
		});
	},
	blur: function(event) {
		this.container.removeClass('focus');
	},
	/*
	 * Temporary soon replace Gx.Box
	 */
	setRangeVThree: function(width) {
		var available, elements = this.container.getElements([
			'span.left', 'span.middle', 'span.right'
		].join(',')).associate([
			'left', 'middle', 'right'
		]);

		available = width.toInt() - elements.left.getSizeWithMargin().x - elements.right.getSizeWithMargin().x;
		elements.middle.setStyle('width', available);
		return available;
	},
	setDisabled: function(value) {
		if ($type(value) != 'boolean')
			return;
	
		(value) ? this.container.addClass('disabled') : this.container.removeClass('disabled');
		this.container.disabled = value;
		return this;
	},
	/*
	 * This function is called to see if the widget can receive focus,
	 * by default if the item contains the class 'disabled', disabled 
	 * owned and updated by the function 'setDisabled()', it returns 
	 * false otherwise true.
	 */
	isFocused: function() {
		return !this.container.disabled;
	},
	hasFocus: function() {
		//console.log(this.container.hasClass('focus'));
	}
});
