/* Select */
(function(jQuery) {
    jQuery.widget('ui.checkBox', {
        _init: function() {
            var that = this, opts = this.options;

            this.labels = jQuery([]);

            this.checkedStatus = false;
            this.disabledStatus = false;

            this.radio = (this.element.is(':radio'));

            this.visualElement = jQuery('<span />')
				.addClass(this.radio ? 'ui-radio' : 'ui-checkbox');

            if (opts.replaceInput) {
                this.element
					.after(this.visualElement[0])

					.bind('usermode', function(e) {
					    (e.enabled &&
							that.destroy.call(that, true));
					});

                jQuery(this.visualElement).parent()
					.bind('click', function(e) {
					    if (that.radio) {
					        if (!(that.element.is(':checked'))) {
					            (!this.disabledStatus && that.changeCheckStatus(true, e));
					        }
					    } else {
					        (!this.disabledStatus && that.toggle.call(that, e));
					    }
					    //dropDownClose();
					    return false;
					});
            }

            this.element
				.bind('click.checkBox', jQuery.bind(this, this.reflectUI))

            if (opts.addLabel) {
                this.labels = jQuery('label[for=' + this.element.attr('id') + ']');
            }

            this.reflectUI({ type: 'initialReflect' });
        },
        _changeStateClassChain: function() {
            var stateClass = (this.checkedStatus) ? '-checked' : '',
				baseClass = 'ui-' + ((this.radio) ? 'radio' : 'checkbox') + '-state';

            stateClass += (this.disabledStatus) ? '-disabled' : '';

            if (stateClass) {
                stateClass = baseClass + stateClass;
            }

            function switchStateClass() {
                var classes = this.className.split(' '),
					found = false;
                jQuery.each(classes, function(i, classN) {
                    if (classN.indexOf(baseClass) === 0) {
                        found = true;
                        classes[i] = stateClass;
                        return false;
                    }
                });
                if (!found) {
                    classes.push(stateClass);
                }

                this.className = classes.join(' ');
            }

            this.labels.each(switchStateClass);
            this.visualElement.each(switchStateClass);
        },
        destroy: function(onlyCss) {
            this.element.removeClass('ui-helper-hidden-accessible');
            this.visualElement.addClass('ui-helper-hidden');
            if (!onlyCss) {
                var o = this.options;
                this.element.unbind('.checkBox');
                this.visualElement.remove();
                this.labels
					.unbind('.checkBox')
            }
        },

        disable: function() {
            this.element[0].disabled = true;
            this.reflectUI({ type: 'manuallyDisabled' });
        },

        enable: function() {
            this.element[0].disabled = false;
            this.reflectUI({ type: 'manuallyenabled' });
        },

        toggle: function(e) {
            this.changeCheckStatus((this.element.is(':checked')) ? false : true, e);
        },

        changeCheckStatus: function(status, e) {
            if (e && e.type == 'click' && this.element[0].disabled) {
                return false;
            }
            this.element.attr({ 'checked': status });
            this.reflectUI(e || {
                type: 'changeCheckStatus'
            });
        },

        propagate: function(n, e, _noGroupReflect) {
            if (!e || e.type != 'initialReflect') {
                if (this.radio && !_noGroupReflect) {
                    jQuery(document.getElementsByName(this.element.attr('name')))
						.checkBox('reflectUI', e, true);
                }
                return this._trigger(n, e, {
                    options: this.options,
                    checked: this.checkedStatus,
                    labels: this.labels,
                    disabled: this.disabledStatus
                });
            }
        },

        reflectUI: function(elm, e) {
            var oldChecked = this.checkedStatus,
				oldDisabledStatus = this.disabledStatus;
            e = e ||
				elm;

            this.disabledStatus = this.element.is(':disabled');
            this.checkedStatus = this.element.is(':checked');

            if (this.disabledStatus != oldDisabledStatus || this.checkedStatus !== oldChecked) {
                this._changeStateClassChain();

                (this.disabledStatus != oldDisabledStatus &&
					this.propagate('disabledChange', e));

                (this.checkedStatus !== oldChecked &&
					this.propagate('change', e));

                if (jQuery(this.element[0]).attr('propagate') != "false") {
                    jQuery('a.trigger', this.element[0].parentNode.parentNode.parentNode.parentNode.parentNode).text(jQuery(this.element[0].parentNode).text());
                }
            }
        }
    });
    jQuery.ui.checkBox.defaults = {
        replaceInput: true,
        addLabel: true
    };
})(jQuery);