/**
 * @author marian.negru
 * @version 1.0.0
 */
(function($){
$.widget('ui.checkbox', {
	_init: function(){
		// XXX: UI widget will not actually fail...
		if (!this.element.is(":radio,:checkbox")) {
			return false;
		}

		if (this.element.is(":radio")) {
			this._radio = $(this.element[0].form).find("input:radio")
				.filter('[name="'+this.element[0].name+'"]');
		} else {
			this._radio = false;
		}

		var self = this, o = this.options; // closures for callbacks.
		// Set the ARIA properties on the native input
		this.element
			.attr({
				role: (this._radio ? "radio" : "checkbox"),
				"aria-checked": !!this.element[0].checked
			});
		
		var scope = this.options.scope ? ' class="' + this.options.scope + '" ' : "";
		this.element.wrap('<span' + scope + '></span>').css({position:'absolute',margin:'0px',padding:'0px',top:'1px',left:'1px',width:'1px',hright:'1px',zIndex:-1}).parent().css({position:'relative',display:'inline-block'});
		
		this.wrap = this.element.wrap('<span></span>').parent();
		this.wrap.addClass((this._radio ? "ui-radio" : "ui-checkbox") +
				" ui-state-default");
		
		this.checkbox = $('<div></div>').prependTo(this.wrap).css({position:'absolute', display:'block', top:'0px', left:'0px', zIndex:200}).addClass("ui-icon " + this._icon(false))
		.click(function(event) {
				self.element[0].click();
				event.preventDefault();
				event.stopImmediatePropagation();
				return false;
			});;
		
		this.checkbox.width(this.wrap.outerWidth())
			.height(this.wrap.outerHeight());
			
		// bind events
		this.wrap.hover(function(event) {
				if (!o.disabled) {
					$(this).addClass("ui-state-hover");
				}
			}, function(event) {
				if (!o.disabled) {
					$(this).removeClass("ui-state-hover");
				}
			})
			.bind("mousedown", function(event) {
				if (!o.disabled) {
					$(this).addClass("ui-state-active");
				}
			})
			.bind("mouseup", function(event) {
				if (!o.disabled) {
					$(this).removeClass("ui-state-active");
				}
			})
			.bind(this.widgetEventPrefix + "focus", function(event) {
				if (!o.disabled) {
					if (self._radio) {
						self._radio.not(self.element)
							.removeClass("ui-state-focus");
					}
					$(this).addClass("ui-state-focus");
				}
			})
			.bind(this.widgetEventPrefix + "blur", function(event) {
				if (!o.disabled) {
					$(this).removeClass("ui-state-focus");
				}
			})
			.bind(this.widgetEventPrefix + "click", function(event) {
				if (!o.disabled) {
					if (self._radio) {
						self._radio.not(self.element).checkbox("uncheck");
						self.check();
					} else {
						self.toggle();
					}
				}
			});
		this.element
			.bind("focus." + this.widgetName, function(event) {
				self._trigger("focus", event); // Actually checkboxfocus
			})
			.bind("blur." + this.widgetName, function(event) {
				self._trigger("blur", event); // Actually checkboxblur
			})
			.bind("click." + this.widgetName, function(event) {
				self._trigger("click", event); // Actually checkboxclick
			});

		// Capture the initial value
		this._setData("checked", !!this.element[0].checked);
		
		if ( window.getSelection )
				this.wrap.parent().css('MozUserSelect', 'none');
	},
	destroy: function() {
		this.wrap.replaceWith(this.element);

		this.checkbox.remove();

		$.widget.prototype.destroy.apply(this, arguments);

	},
	_setData: function(key, value) {
		$.widget.prototype._setData.apply(this, arguments);

		if (key == "disabled") {
			if (value) {
				this.element.attr("disabled","disabled");
				this.wrap.removeClass("ui-state-focus ui-state-hover ui-state-active");
			} else {
				this.element.removeAttr("disabled");
			}
			this.wrap
				[value ? "addClass" : "removeClass"](
					this.widgetName + "-disabled " +
					this.namespace + "-state-disabled");
		} else if (key == "checked") {
			this.element[0].checked = !!value;
			this.element.attr("aria-checked", !!value);
			
			this.checkbox.addClass(this._icon(!!value))
				.removeClass(this._icon(!value));
		}
	},
	check: function() {
		this._setData("checked", true);
	},
	uncheck: function() {
		this._setData("checked", false);
	},
	toggle: function() {
		this._setData("checked", !this._getData("checked"));
	},
	_icon: function(state) {
		if (this._radio) {
			return "ui-icon-"
				+ this.options[state?"radioChecked":"radioUnchecked"];
		} else {
			return "ui-icon-"
				+ this.options[state?"checkboxChecked":"checkboxUnchecked"];
		}
	}
});
$.extend($.ui.checkbox, {
	version: "1.0.0",
	defaults: {
		scope : '',
		checkboxChecked: "check",
		checkboxUnchecked: "empty",
		radioChecked: "bullet",
		radioUnchecked: "empty"
	}
});
    
})(jQuery);
