
Site.Widgets.RichCheckboxList = Class.create
(
    Site.Base, 
    {
        initialize: function($super, element, options)
        {
            $super();
            
            this.setOptions(options);
            this.element = element;
      
            this.richCheckboxes = this.element.select("input.rich-checkbox").collect
            (
                function(input)
                {
                    return new Site.Widgets.RichCheckbox(input, { onClick: this.options.richCheckboxOnClick } );
                },
                this
            );
        },
        
        getValue: function()
        {
            // gets the composite value (CSV) from this checkbox list, which would be submitted to an AJAX request
            // for this to work, each underlying checkbox needs to have the same "name", that is, they form a checkbox group
            
            return this.getCheckedValues().join(",");
        },
        
        getCheckedValues: function()
        {
            var values = new Array();
            
            this.element.select("input").each
            (
                function(input)
                {
                    if (input.checked)
                    {
                        values.push(input.value); 
                    }
                }
            );
            
            return values;
        },
        
        uncheckAll: function()
        {
            this._checkAll(false);
        },
        
        checkAll: function()
        {
            this._checkAll(true);
        },
        
        _checkAll: function(checked)
        {
            this.richCheckboxes.invoke(checked ? "check" : "uncheck");
        },
        
        destroy: function($super)
        { 
            this.richCheckboxes.invoke("destroy");
            this.richCheckboxes = null;
            
            $super();
        }      
    }
);