/*
Class which encapsulates common logic for checkboxes list component
Used in prefectureSelector and modelSelector
*/
function CheckboxList() {
    this.rootNodeId = "";
    this.rootCheckboxId = "";
    this.blockCheckboxIdPrefix = "";
    this.selectMainCheckBoxIfOneElementInGroup = true;

    CheckboxList.prototype.initialize = function(rootNodeId, rootCheckboxId, blockCheckboxIdPrefix, selectMainCheckBoxIfOneElementInGroup) {
        this.rootNodeId = rootNodeId;
        this.rootCheckboxId = rootCheckboxId;
        this.blockCheckboxIdPrefix = blockCheckboxIdPrefix;
        this.selectMainCheckBoxIfOneElementInGroup = selectMainCheckBoxIfOneElementInGroup;
    };

    CheckboxList.prototype.checkAll = function(element) {
        $("#" + this.rootNodeId + " input[type='checkbox']").attr("checked", $(element).is(":checked"));
    };

    CheckboxList.prototype.checkBlock = function(element) {
        $("#" + this.rootNodeId + " input[type='checkbox'][id$='-" + element.id + "']").attr("checked", $(element).is(":checked"));
        this.refreshAllCheckBox();
    };

    CheckboxList.prototype.checkSingleCheckbox = function(element) {
        var block = element.id.substring(element.id.indexOf(this.blockCheckboxIdPrefix),element.id.length);
        this.refreshBlockCheckBox(block);
        this.refreshAllCheckBox();
    };

    CheckboxList.prototype.reset = function() {
        $("#" + this.rootNodeId + " input[type='checkbox']").removeAttr("checked");
    };

    CheckboxList.prototype.refreshAllCheckBox = function() {
        var allBlocks = $("#" + this.rootNodeId + " input[type='checkbox'][id^='" + this.blockCheckboxIdPrefix + "']");
        var checked = 0;

        for (var i = 0; i < allBlocks.length; i++) {
            if (allBlocks[i].checked) {
                checked++;
            }
        }

        if (checked == allBlocks.length) {
            $("#" + this.rootCheckboxId).attr("checked","checked");
        } else {
            $("#" + this.rootCheckboxId).removeAttr("checked");
        }
    };

    CheckboxList.prototype.refreshBlockCheckBox = function(blockId) {
        var elementsOfSameBlock = $("#" + this.rootNodeId + " input[type='checkbox'][id$='" + blockId + "']");
        var checked = 0;
        elementsOfSameBlock = $(elementsOfSameBlock).not("[id=" + blockId + "]")
        var checkedElementsOfSameBlock = elementsOfSameBlock.filter(":checked");
//        alert("checkedElementsOfSameBlock.length = " + checkedElementsOfSameBlock.length);
//        alert("elementsOfSameBlock .length = " + elementsOfSameBlock .length);

        /*for (var i = 0; i < elementsOfSameBlock.length; i++) {
            if (elementsOfSameBlock[i].checked && elementsOfSameBlock[i].id != blockId) {
                checked++;
            }
        }*/
        if (elementsOfSameBlock.length == checkedElementsOfSameBlock.length) {
            if (this.selectMainCheckBoxIfOneElementInGroup || elementsOfSameBlock.length > 1) {
                $("#" + blockId).attr("checked","checked");
            }

        } else {
            $("#" + blockId).removeAttr("checked");
        }
    };

    CheckboxList.prototype.setChecked = function(element) {
        if (!element.checked) {
            element.checked = "checked";
        }
    };

    CheckboxList.prototype.setUnchecked = function(element) {
        if (element.checked) {
            element.checked = "";
        }
    }
}
