﻿function PopupWithCheckboxes(anchor_id, popup_id, noneSelectedText, allSelectedText) {
    this.anchor = jQuery('#' + anchor_id);
    this.popup = jQuery('#' + popup_id);
    this.checkboxes = {};
    this.allCheckBox = null;
    this.noneCheckBox = null;
    this.noneSelectedText = noneSelectedText ? noneSelectedText : 'Select';
    this.allSelectedText = allSelectedText ? allSelectedText : null;
    var th = this;

    var allcb = this.popup.find('.select-holder-all input:checkbox');
    if (allcb.length > 0) {
        this.allCheckBox = allcb[0];
        jQuery(allcb).change(function() {
            if (this.checked)
                for (var i in th.checkboxes)
                th.checkboxes[i].cb.checked = true;
            th.renewAnchor();
        });
    }

    var nonecb = this.popup.find('.select-holder-none input:checkbox');
    if (nonecb.length > 0) {
        this.noneCheckBox = nonecb[0];
        jQuery(nonecb).change(function() {
            if (this.checked)
                for (var i in th.checkboxes)
                th.checkboxes[i].cb.checked = false;
            th.renewAnchor();
        });
    }

    this.popup.find('input:checkbox').each(function(idx, el) {
    if ((!th.allCheckBox || (this.id != th.allCheckBox.id)) 
        && (!th.noneCheckBox || (this.id != th.noneCheckBox.id))) {
            var label = jQuery("label[for='" + this.id + "']").html();
            th.checkboxes[idx] = { cb: this, text: label };
            jQuery(this).change(function() { th.renewAnchor() });
        }
    });
    
    this.popup.find('a').click(function() { th.close() });
    this.anchor.click(function() {
        if (th.anchor.hasClass('open'))
            th.close();
        else
            th.open()
    });
    this.renewAnchor();
}

PopupWithCheckboxes.prototype.closeAll = function () {
    jQuery('.select-holder').addClass('closed');
    jQuery('.value-select').removeClass('open');
    jQuery('.value-select').addClass('closed');
}

PopupWithCheckboxes.prototype.close = function () {
    this.popup.addClass('closed');
    this.anchor.removeClass('open');
    this.anchor.addClass('closed');
}

PopupWithCheckboxes.prototype.open = function () {
    this.closeAll();
    this.popup.removeClass('closed');
    this.anchor.removeClass('closed');
    this.anchor.addClass('open');
}

PopupWithCheckboxes.prototype.renewAnchor = function() {
    var s = '';
    var allChecked = true;
    var noneChecked = true;
    for (var i in this.checkboxes) {
        var item = this.checkboxes[i];
        if (item.cb.checked) {
            s += ', ' + item.text;
            noneChecked = false;
        }
        else allChecked = false;
    }
    if (this.allCheckBox)
        this.allCheckBox.checked = allChecked;

    if (this.noneCheckBox)
        this.noneCheckBox.checked = noneChecked;

    if (s.length > 0) {
        if (allChecked && this.allSelectedText)
            s = this.allSelectedText;
        else
            s = s.substring(2);
    }
    else
        s = this.noneSelectedText;
    this.anchor.html(s);
}
