var select_all = 'Select All ',
    unselect_all = 'Unselect All ',
    total_pages = 0,
    page_count_dict = {};

function toogle_checkboxes (group) {
    $('input[type=checkbox]', '#'+group).each(function (index, item) {
        item.checked = !item.checked;
    });
}

function switch_checkboxes (identifier) {
    var value = false;
    
    if ($('#' + identifier + ' .select').html() == select_all) {
        value = true;
    }

    $('.' + identifier + ' input[type=checkbox]').each(function (index, item) {
        item.checked = value;
    });

    count_checkboxes(identifier);

}

function count_checkboxes(identifier) {

    var page_count = 0, 
        select = $('#' + identifier + ' .select');

    total_pages -= (page_count_dict[identifier] || 0);

    $('.' + identifier + ' input[type=checkbox]').each(function (index, item) {
        if (item.checked) {
            page_count += pdf_info[item.id];
        }
    });
    if (page_count > 0) {
        $('#' + identifier + ' .counter').text(
            page_count + (page_count > 1 ? ' pages': ' page'));
        $(select).text('Unselect All ');

        $('#abstract_' + identifier + ' input[type=checkbox]').attr('checked', true);
        page_count_dict[identifier] = page_count;
    } else {
        $('#' + identifier + ' .counter').text('');
        $(select).text(select_all);
        $('#abstract_' + identifier + ' input[type=checkbox]').attr('checked', false);
        page_count_dict[identifier] = 0;
    }

    total_pages += (page_count_dict[identifier] || 0);
    var text = '';
    if (total_pages) {
        text = total_pages + (total_pages > 1 ? ' pages': ' page') + ' selected';
    } 
    
    $('#page_count').text(' - ' + text);
    $('#page_count_1').text(text);
}


$(document).ready(function(){

    $('.toggle').each(function (index, el) {
        var identifier = $(this).parent().attr('id'),
            select = $('#' + identifier + ' .select');
        
        if ($(el).hasClass('expanded')) {
            $(el).toggle(
                function () {
                    $('.' + $(this).parent().attr('id')).slideUp();
                    $('.bullet', $(this).parent()).text('+');
                    $(select).hide();
                },
                function () {
                    $('.' + $(this).parent().attr('id')).slideDown();
                    $('.bullet', $(this).parent()).text('-');
                    $(select).show();
                }
            );
        }
        else {
            $(el).toggle(
                function () {
                    $('.' + $(this).parent().attr('id')).slideDown();
                    $('.bullet', $(this).parent()).text('-');
                    $(select).show();
                },
                function () {
                    $('.' + $(this).parent().attr('id')).slideUp();
                    $('.bullet', $(this).parent()).text('+');
                    $(select).hide();
                }
            );
        }
        
        $('.' + $(this).parent().attr('id') + ' input[type]=checkbox').each(function (index, el) {
            $(el).change(function (event) {
                count_checkboxes(identifier);
            });
        });
        count_checkboxes(identifier);
       

        $(select).click(function() {switch_checkboxes(identifier);});
    });

    $('.mandatory').each(function (index, el) {
        count_checkboxes($(this).parent().attr('id'));
    });

});
