
function activateQueue(that, queueType) {
    if (hasEmail) {
        $('.enableSubscription.' + queueType).attr('disabled', !$(that).attr('checked'));
        $('.enableSubscriptionLabel.' + queueType).toggleClass('textInactive');
    }
    if (hasEmail && !$(that).attr('checked')) {
        $('div.inputSubscription.' + queueType).hide();
        $('.enableSubscription.' + queueType).attr('checked', false);
    }
    var toAdd = $(that).attr('checked') ? 'boxActive' : 'boxInactive';
    var toRemove = $(that).attr('checked') ? 'boxInactive' : 'boxActive';
    $('.Subscription.' + queueType).addClass(toAdd);
    $('.Subscription.' + queueType).removeClass(toRemove);
    if (nrOfEnabledQueues() == 0) {
        $('#atLeastOneQueue').show( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
        $('.Button.WDForward').hide( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
    } else {
        $('#atLeastOneQueue').hide( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
        $('.Button.WDForward').show( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
    }
}

function activateSubscription(queueType) {
    if (hasEmail) {
        $('div.inputSubscription.' + queueType).toggle( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
    } else {
        $('div.inputSubscription.' + queueType).hide( 400, function () {
            F2.EventManager.publish( "PageHeightChanged" );
        });
    }
    enableMultiSelect();
}

function cancelQueue(queueType) {
    $('div.'+queueType+' .cancelQueueMsg').show( 400, function () {
        F2.EventManager.publish( "PageHeightChanged" );
    });
    $('div.'+queueType+' input:hidden:first').val('false');
    if ($('input.enableSubscription.' + queueType).attr('checked')) {
        $('input.enableSubscription.' + queueType).click();
    }
    $('input.enableSubscription.' + queueType).attr('disabled', 'disabled');
    $('.enableSubscriptionLabel.' + queueType).addClass('textInactive');
    $('.cancelQueue.' + queueType + ' .Button').hide( 400, function () {
        F2.EventManager.publish( "PageHeightChanged" );
    });
// notifyParent();
}

function toggleEdit(queueType) {
    $('#'+queueType+'>div.collapsed, #'+queueType+'>div.expanded').toggle();
    enableMultiSelect();
//   notifyParent();
}

function abort(queueType) {
    toggleEdit(queueType);
    $('.cancelQueue.'+queueType+' input:hidden').remove();
//  notifyParent();
}

function initIntresse () {
    initSubscription('#lagenhet','lgh');
    initSubscription('#senior','senior');
    initSubscription('#bilplats','fordon');
    // initSubscription('#mc','mc');
    initSubscription('#garage','garage');
    initSubscription('#forrad','forrad');

    // init select byggnad
    $('table.OmradenByggnader').each(function() {
        var $table = $(this);
        $table.find('td.Placeholder').each(function() {
            var placeholder = $(this);
            var checkbox = $table.find('input:checkbox[value=' + placeholder.data('omradeid') + ']');
            checkbox.parent().children().appendTo(placeholder);
            checkbox.click(function() {
                if ($(this).attr('checked')) {
                    $(this).parents('tr').find('span.ui-dropdownchecklist').show();
                } else {
                    $(this).parents('tr').find('select option:checked').attr('selected', false);
                    $(this).parents('tr').find('select').dropdownchecklist("refresh");
                    $(this).parents('tr').find('span.ui-dropdownchecklist').hide();
                }
            });
        });
    });

}

function initSubscription(id, queueType) {
    if ($(id + ' .enableQueue:not(:checked)').length > 0 || !hasEmail) {
        $('input.enableSubscription.' + queueType).removeAttr('checked').attr('disabled','disabled');
    } else {
        $('.enableSubscriptionLabel.' + queueType).removeClass('textInactive');
    }
}

function nrOfEnabledQueues() {
    return $('#lagenhet, #senior, #bilplats, #garage, #forrad').size() - $('.enableQueue:not(:checked)').length;
}

/**
 * Dekorerar multiselectdropdown-listorna. Måste anropas när select-listan blivit synlig, ger annars fel bredd och höjd
 */
function enableMultiSelect() {
    $("select.selectByggnad:visible").dropdownchecklist({
        width: '110',
        textFormatFunction: function(options) {
            var selectedOptions = options.filter(":selected");
            var countOfSelected = selectedOptions.size();
            var size = options.size();
            switch(countOfSelected) {
                case 0:
                    return "V&auml;lj adresser";
                case 1:
                    return "1 adress vald";
                case size:
                    return "Alla";
                default:
                    return countOfSelected + " adresser valda";
            }
        }
    });
    $("span.ui-dropdownchecklist:visible").each(function() {
        var omrChecked = $(this).parent('td').siblings('td.placeHolder').children('input').attr('checked');
        if (!omrChecked) {
            $(this).hide();
        }
    });
}
