/**
 * @author Denis Barushev <barushev@gmail.com>
 * @since 14.10.2008
 */
(function ($) {
    $.fn.combobox = function (settings) {
        settings = $.extend({
            debug: false
        }, settings);

        $('body').click(
            function () {
                $('div.combobox').removeClass('expanded');
            }
        );

        $('div.combobox').click(
            function(e) {
                e.stopPropagation();
            }
        );

        return this.each(
            function () {
                var $this = $(this);
                var comboboxDiv = $this.parent();

                $this.attr('readonly', 'readonly');

                $('div.toggle', comboboxDiv).add($this).click(
                    function () {
                        comboboxDiv.toggleClass('expanded');
                    }
                );

                comboboxDiv.find('ul[class=parent] input').click(
                    function () {
                        $this.val(comboboxDiv.find('input:checked').map(
                            function () {
                                return $(this).val();
                            }
                        ).get().join(', '));
                    }
                );

                function valueForLocation() {
                    return comboboxDiv.find('ul.location>li>label input:checked').map(
                        function () {
                            // If all sub-checkboxes are checked - return value of parent checkbox
                            if ($(this).parent().next().find('input').size() == $(this).parent().next().find('input:checked').size()) {
                                return $(this).val();
                            } else {
                                return  $(this).parent().next().find('input:checked').map(
                                    function () {
                                        return $(this).val();
                                    }
                                ).get().join(', ');
                            }
                        }
                    ).get().join(', ');
                }

                comboboxDiv.find('ul.location>li>label input').click(
                    function () {
                        if (this.checked) {
                            $(this).parent().next().find('input').attr('checked', 'checked');
                        } else {
                            $(this).parent().next().find('input').attr('checked', '')
                        }

                        $this.val(valueForLocation());
                    }
                );

                comboboxDiv.find('ul.location ul input').click(
                    function () {
                        if (this.checked) {
                            $(this).parent().parent().parent().prev().find('input').attr('checked', 'checked');
                        } else {
                            if ($(this).parent().parent().parent().find('input:checked').size() == 0) {
                                $(this).parent().parent().parent().prev().find('input').attr('checked', '');
                            }
                        }

                        $this.val(valueForLocation());
                    }
                );
            }
        );
    };
})(jQuery);
