jQuery.fn.toggleAllCheckbox = function(options) {

    var checkAllBox = $(this);
    var checkboxes = $(options.checkboxes).not(checkAllBox);


    checkboxes.click(function() {
        var checkedCheckboxes = checkboxes.filter(":checked");
        if (checkboxes) {
            if (!checkedCheckboxes || checkedCheckboxes.length < checkboxes.length) {
                checkAllBox.removeAttr("checked");
            } else if (checkedCheckboxes.length == checkboxes.length) {
                checkAllBox.attr("checked", "true");
            }
        }
        refreshCheckboxLabelsState();
    });


    checkAllBox.click(function() {
        if (checkAllBox.is(":checked")) {
            checkboxes.attr("checked", "true");
        } else {
            checkboxes.removeAttr("checked");
        }

        refreshCheckboxLabelsState();

    });


    function refreshCheckboxLabelsState() {
       checkboxes.add(checkAllBox).each(function() {
            if (this.checked) {
                $('label[for="' + $(this).attr("id") + '"]').addClass('selected');
            }
            else {
                $('label[for="' + $(this).attr("id") + '"]').removeClass('selected');
            }
        });
    }


}

