/**
 * @author alexander.farkas
 */
(function($){
    $.widget('ui.checkBox', {
        init: function(){
            var that = this, opts = this.options;
            this.labels = $('label[for=' + this.element.attr('id') + ']');
            this.checkedStatus = false;
            
            this.radio = opts.radioElements || 
				(this.element.is(':radio')) ? 
				$(document.getElementsByName(this.element.attr('name'))) : 
				false;
            
            var usermode = ($.userMode && $.userMode.get) ? 
				$.userMode.get() : 
				false;
            
            if (!usermode && opts.hideInput) {
                var css =  {
                    position: 'absolute',
                    width: '1px'
                };
				if($('html').attr('dir') == 'rtl'){
					css.right = '-999em';
				} else {
					css.left = '-999em';
				}
                this.element.css(css).bind('usermode', function(e, o){
                    if (o.usermode) {
                        that.destroy.call(that, true);
                    }
                });
            }
            this.reflectUI({
                type: 'initialReflect'
            });
            this.element
				.bind('click.checkBox', $.bind(this, this.reflectUI))
				.bind('focus.checkBox blur.checkBox', function(e){
               		that.labels[(e.type == 'focus') ? 'addClass' : 'removeClass'](opts.focusClass);
            });
        },
        destroy: function(onlyCss){
            this.element.css({
                position: '',
                left: '',
                right: '',
                width: ''
            });
            if (!onlyCss) {
                var o = this.options;
                this.element.unbind('.checkBox');
                this.labels.removeClass(o.disabledClass + ' ' + o.checkedClass + ' ' + o.focusClass);
            }
        },
        disable: function(){
            this.element[0].disabled = true;
            this.reflectUI({
                type: 'manuallyDisabled'
            });
        },
        enable: function(){
            this.element[0].disabled = false;
            this.reflectUI({
                type: 'manuallyenabled'
            });
        },
        toggle: function(){
            this.changeCheckStatus((this.element.is(':checked')) ? false : true);
        },
        changeCheckStatus: function(status){
            this.element.attr({
                'checked': status
            });
            this.reflectUI({
                type: 'changeCheckStatus'
            });
        },
        propagate: function(n, e){
            if (this.radio) {
                this.radio.checkBox('reflectUI', e);
            }
            this.element.triggerHandler("checkbox" + n, [e, {
                instance: this,
                options: this.options,
                checked: this.checkedStatus,
                labels: this.labels
            }]);
        },
        reflectUI: function(elm, e){
            var checked = this.element.is(':checked'), oldChecked = this.checkedStatus, o = this.options;
            e = e ||
            elm;
            this.labels[(this.element.is(':disabled'))? 
				'addClass' : 
				'removeClass'](o.disabledClass);
            
            this.checkedStatus = checked;
            
            if (checked !== oldChecked) {
                this.labels.toggleClass(o.checkedClass);
                this.propagate('change', e);
            }
        }
    });
    $.ui.checkBox.defaults = {
        focusClass: 'focus',
        checkedClass: 'checked',
        disabledClass: 'disabled',
        hideInput: true,
		radioElements: false
    };
    
})(jQuery);
