//camera synch
$(document).ready(function() {
    var filternumber = {
        1: 'U',
        2: 'B',
        3: 'V',
        4: 'R',
        5: 'I',
        6: 'Halpha',
        7: 'Free'
    };

    var binningconversion = {
        "1,1": 1,
        "2,2": 2,
        "3,3": 3,
        "4,4": 4
    };

    function getFilterFromNumber(number) {
        return filternumber[number] || '';
    }

    function getBinningFromNumber(number) {
        return binningconversion[number] || '';
    }

    function selectOptionByValue(selectElement, value) {
        selectElement.val(value);
    }
    var CameraSection = $('#Camera');
    var cameraFilter = $('#camera-filter var');
    var cameraBinning = $('#camera-binning var');
    var filterSelect = $('#filter');
    var binningSelect = $('#binning');

    function mutationCallback(mutations) {
        mutations.forEach(function(mutation) {

            CameraSection.removeClass('disabled-section');

            if (mutation.target.nodeType === Node.ELEMENT_NODE) {
                var newValue = $(mutation.target).text();
                if (mutation.target.parentElement.id === 'camera-filter') {
                    selectOptionByValue(filterSelect, getFilterFromNumber(newValue));
                    observerFilter.disconnect();
                } else if (mutation.target.parentElement.id === 'camera-binning') {
                    selectOptionByValue(binningSelect, getBinningFromNumber(newValue));
                    observerBinning.disconnect();
                }
            }

        });
    }

    var observerFilter = new MutationObserver(mutationCallback);
    var observerBinning = new MutationObserver(mutationCallback);
    var config = { childList: true, subtree: true };
    observerFilter.observe(cameraFilter[0], config);
    observerBinning.observe(cameraBinning[0], config);

});
