
(function ($) {
    $(document).ready(function () {
        var MAPPING = {
            '' : ['',
            ],
            'Research': [
                '',
                'Working Papers',
                'Reports',
                'Journal Articles',
                'Surveys',
                'Statistics',
                'Studies',
                'Monographs',
                'Books',
                'Edited Books',
                'Dissertations',
                'Studies'
            ],
            'Policy': [
                '',
                'Action Plan',
                'Legislation & Jurisprudence',
                'Regulations',
                'Labour Administration Audits',
                'Guides',
                'Good Practices',
                'Guidelines',
                'Working Papers',
                'Collective Agreements',
                'Reports',
                'Recommendations',
                'ILO Conventions & Recommendations',
                'Books',
                'Edited Books'
            ],
            'Training Material': [
                '',
                'Videos',
                'Manuals',
                'Presentations',
                'Curricula',
                'Training of Trainers'
            ],
            'Project and Programme': [
                '',
                'Meeting Reports', 
                'Project Documents', 
                'Workplans',
                'Evaluations',
                'Lessons Learned'
            ],
            'Information Material': [
                '',
                'Country Profiles', 
                'Flyers', 
                'Fact Sheets',
                'Statistics',
                'Reports',
                'Newsletters',
                'Brochures'
            ],
            'CoP Product': [
                '',
                'Consolidated Replies', 
                'Discussion Papers',
                'Promotional Material',
                'Community Updates',
                'Podcast',
            ]
        };
        var refresh = function () {
            var self = $(this);
            var subtype = $('#archetypes-fieldname-resource_subtype #resource_subtype');
            var options = MAPPING[self.val()];
            if (options == undefined) {
                return;
            };
            $('option', subtype).remove();
            $.each(options, function (idx, opt) {
                var node = $('<option value="' + opt + '">' + opt + '</option>');
                subtype.append(node);
            });
        }

        // set to only values provided above
        $('#archetypes-fieldname-resource_type option').remove();
        $.each(MAPPING, function (opt, value) {
            var node = $('<option value="' + opt + '">' + opt + '</option>');
            $('#archetypes-fieldname-resource_type #resource_type').append(node);
        });
        $('#archetypes-fieldname-resource_type #resource_type').change(refresh);
        $('#archetypes-fieldname-resource_type #resource_type').each(refresh);
    });
})(jQuery);

