/* Minification failed. Returning unminified contents.
(67,13-14): run-time warning JS1004: Expected ';': f
(69,17-18): run-time warning JS1004: Expected ';': u
(70,17-18): run-time warning JS1004: Expected ';': e
(71,17-18): run-time warning JS1004: Expected ';': e
(72,17-18): run-time warning JS1004: Expected ';': e
(96,17-18): run-time warning JS1004: Expected ';': u
(172,21-22): run-time warning JS1004: Expected ';': a
(186,29-30): run-time warning JS1004: Expected ';': a
(213,69-70): run-time warning JS1100: Expected ',': =
 */
(function ($) {
    $(function () {

        /* BIG MENU PANEL */
        $('.cd-btn').on('click', function (event) {
            event.preventDefault();
            $('.cd-panel').addClass('is-visible');
        });

        $('.cd-panel-close').on('click', function (event) {
            event.preventDefault();
            $('.cd-panel').removeClass('is-visible');
        });

        $('.cd-panel').on('click', function (event) {
            if ($(event.target).is('.cd-panel') || $(event.target).is('.cd-panel-close')) {
                $('.cd-panel').removeClass('is-visible');
                event.preventDefault();
            }
        });

        /* LOGIN MENU */
        $('.btn-login-toggle').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            $(this).parent().toggleClass('open');
        });
        $(document).on('click', function(event) {
            var parents = $(event.target).parents('.header-login-submenu');
            var headerLogin = $('.header-login-submenu');
            if(parents.length === 0 && headerLogin.hasClass('open')) {
                headerLogin.removeClass('open');
            }
        });
        $('.btn-user-toggle').parent().on('mouseover', function () {
            $(this).addClass('open');
        }).on('mouseout', function () {
            $(this).removeClass('open');
        });

        // PDF Viewer modal
        $('.docViewer').click(function(e){
          if($(window).width() > 800) {
           
            var docUrl = $(this).attr('href');
            var extension = docUrl.substr(docUrl.lastIndexOf('.') + 1);
              if (extension.toLowerCase() === 'pdf') {
                e.preventDefault();
                $("#docViewer iframe").prop('src', docUrl);
                // reset size&pozition
                var modalStyle="display:flex;flex-direction:column;height:90vh";
                $("#docViewer .modal-content").attr("style",modalStyle)
                  $("#docViewer .modal-content").resizable({
                    minHeight: 300,
                    minWidth: 300
                  });
                $("#docViewer .modal-content").draggable();
                $("#docViewer").modal('show');
                }
          }
        });


        // new filters
        var allRules = [];

        let filterOptions = function(context, type, value) {
            console.log(context, type, value);
            let userInputs = [];
            let eligibleRules = [];
            let elValue = null;
            let elKey = null;
    
            elValue = value;
            if(type !== 'product') {
                elKey = type;
            } else {
                return;
            }
    
            // hide all options
            $('#' + context +' .search-select .menu .item').addClass('hidden');
    
            allRules.forEach(function (rule, ruleIndex) {
                //console.log(rule);
              rule.forEach(function (ruleEl, ruleElindex) {
                //console.log(ruleEl, elKey, elValue);
                  if (ruleEl.Key == Number(elKey) && ruleEl.Value == Number(elValue)) {
                      //console.log("Rule complies!!!", Number(elKey), Number(elValue))
                      eligibleRules.push(ruleIndex);
                  }
              });
            });
    
            // Keep only the rules that are valid
            let updatedValidRules = [];
            eligibleRules.forEach(function (eligibleRule) {
                updatedValidRules.push(allRules.slice(eligibleRule, eligibleRule + 1)[0]);
            });
            validRules = updatedValidRules.slice();
            //console.log(validRules);
    
            /*if(typeFilterValue && productFilterValue) {
                $('.filters .search-select.filter-type .menu').children('.item').show().addClass('filtered');
                if(type !== 'product') {
                    $('.filters .dropdown.filter-product').dropdown('clear');
                }
            } else {
                
            }*/
            $('#' + context+' .search-select[data-name=flt_' + elKey + '] .menu').children('.item').removeClass('hidden').addClass('available');
            //$('.filters .search-select:not([data-name=flt_' + elKey + '])').dropdown('clear');
    
            // Show options that respect the rules that apply
            validRules.forEach(function (validRule, validRuleIndex) {
                validRule.forEach(function (ruleEl, ruleElIndex) {
                    $('#' + context+' .search-select[data-name=flt_' + ruleEl.Key + '] .menu').children('.item[data-value=' + ruleEl.Value + ']').removeClass('hidden').addClass('available');
                    if(ruleEl.Key != elKey) {
                        $('#' + context+' .search-select[data-name=flt_' + ruleEl.Key + ']').attr('data-against', elKey).attr('data-against-value', value);
                    }
                    
                });
            });

            $('#' + context+' .search-select').each(function(s) {
                console.log($(this).find('.item:visible').length);
                if($(this).find('.menu').children('.item:visible').length == 1) {
                    console.log($(this).attr('data-name') + ' has only one child');
                }
            })
            
    
        }

        var resetFilters = function() {
            //$('.filters .search-select .menu').children('.item').show();
        }

        var initFilters = function() {
            //console.log(allRules);
            $('.search-select').dropdown({
                clearable: true,
                forceSelection: false,
                fullTextSearch: "exact",
                /*onShow: function() {
                    console.log('shown!!!');
                    $(this).find('.item:not(.hidden)').addClass('filtered');
                },
                onHide: function() {
                    console.log('shown!!!');
                    $(this).find('.item:not(.hidden)').addClass('filtered');
                    if($(this).find('.item:not(.filtered)').length > 0) {
                        $(this).find('.item:not(.filtered)').addClass('hidden');
                    }
                },*/
                
                onChange: function(value, text, $selectedItem) {
                    
                    if(value) {
                        console.log($(this).parents("form").attr('id'));
                        filterOptions($(this).parents("form").attr('id'), $(this).attr('data-name').replace('flt_',''), value);
                    }else{
                        $('#' + $(this).parents("form").attr('id')+' .search-select[data-against=' + $(this).attr('data-name').replace('flt_','') + ']').attr('data-against', "").attr('data-against-value', "").dropdown('clear').find('.item').removeClass('hidden').addClass('available');
                    }
                    if(($(this).attr('data-against') && $(this).attr('data-against') != "") && ($(this).attr('data-against-value') && $(this).attr('data-against-value') != "")) {
                        filterOptions($(this).parents("form").attr('id'), $(this).attr('data-against'),  $(this).attr('data-against-value'));
                    }
                }
            });

            $('.search-select').each(function(sel) {
                let autoFill = $(this).find('.item.selected').attr('data-value');
                if(autoFill) {
                    $(this).dropdown('set selected', autoFill);
                }
            })
        }

        initFilters();
        $.get('/api/FilterObjective/',
            function (data) {
                allRules = data;
                if (data.length > 0) {
                    initFilters();
                    $('.hidden-filter').each(function (index, obj) {
                        let autoFill = $(this).find('.item.auto-filtered').attr('data-value');
                        if(autoFill) {
                            $(this).dropdown('set selected', autoFill);
                        }
                    });
                }
            }
        );

    });
})(jQuery);
;
(function ($) {
    $(function () {
        var SelectFilter = function () { };

        // Inits the script on document ready 
        SelectFilter.prototype.init = function (serverData) {
            this.data = serverData;
            this.registerEvents();
        };

        SelectFilter.prototype.validRules = [];
        SelectFilter.prototype.userInputs = [];
        SelectFilter.prototype.data = [];

        // Filters the options based on the user changes 
        SelectFilter.prototype.filterOptions = function (el, active = true) {

            var _this = this;
            var tabClass = active ? ".active" : "";
            var selectElements = el.closest('label').find('~ label select');

            // Hide all options
            selectElements.each(function () {
                var thisEl = $(this);
                if (thisEl.attr('name') !== el.attr('name')) {
                    thisEl.children().hide().removeAttr('selected');
                    thisEl.find('option:eq(0)').attr('selected', 'selected');
                }
            });
                
            // Add the current changed Key & Values to userInputs array
            var elKey = el.attr('name').split("_")[1];
            var elValue = el.val();
            var currentInput = this.userInputs.filter(function (el) {
                return el.Key == elKey;
            })[0];
            if (elValue) {
                if (!currentInput) {
                    this.userInputs.push({ Key: elKey, Value: elValue });
                } else {
                    currentInput.Value = elValue;
                }
            }
            else {
                this.userInputs.splice(this.userInputs.indexOf(currentInput), 1);
            }

            // Reset valid rules object (before filtering all rules are valid)
            this.validRules = this.data.slice();

            this.userInputs.forEach(function (selectedItem, itemIndex) {
                // Find eligible rules for this value
                var eligibleRules = [];
                _this.validRules.forEach(function (rule, ruleIndex) {
                    rule.forEach(function (ruleEl, ruleElindex) {
                        if (ruleEl.Key == elKey && ruleEl.Value == elValue) {
                            // ineligibleRules.push(ruleIndex);
                            eligibleRules.push(ruleIndex);
                        }
                    });
                });
                // Keep only the rules that are valid
                var updatedValidRules = [];
                eligibleRules.forEach(function (eligibleRule) {
                    updatedValidRules.push(_this.validRules.slice(eligibleRule, eligibleRule + 1)[0]);
                });
                _this.validRules = updatedValidRules.slice();
            });

            // Show options that respect the rules that apply
            this.validRules.forEach(function (validRule, validRuleIndex) {
                validRule.forEach(function (ruleEl, ruleElIndex) {
                    $('.tab-pane' + tabClass + ' form select[name=flt_' + ruleEl.Key + ']').children('option[value=' + ruleEl.Value + ']').show().addClass('filtered');
                });
            });

            // Show the non value items (the labels) & auto select if only one available option
            selectElements.each(function () {
                var thisEl = $(this);
                thisEl.children('.option-none').show();
                if (thisEl.children('.filtered').length === 1) {
                    thisEl.children('.filtered').attr('selected', 'selected');
                }
            });
        };

        // new melux method
        SelectFilter.prototype.selectHasOption = function () {
            // return true - we don t have any more restrictions on number of set filters to perform search
            return true;
        };

        SelectFilter.prototype.resetFilters = function () {
            // Show all options
            $('.tab-pane form select').each(function () {
                $(this).children().show();
            });
            // Remove any selected values
            $("option:selected").removeAttr("selected");
            // Reset validRules array
            this.validRules = [];
        };

        /**
         * This is where all the events are registered
         */
        SelectFilter.prototype.registerEvents = function () {
            var _this = this;
            var formEl = $('.tab-pane form');
            var selectEl = formEl.find('select');
            var clearBtn = formEl.find('#clearBtn');
            var submitBtn = formEl.find('.main-search-fields-submit');

            selectEl.on('change', function (e, param) {
                _this.filterOptions($(this), param);
            });
            clearBtn.on('click', function () {
                _this.resetFilters();
            });
            submitBtn.on('click', function (e) {
                e.preventDefault();
                if (_this.selectHasOption()) {
                    e.target.closest('form').submit();
                }
            });
        };

        function initPage(data) {
            var selectFilter = new SelectFilter();
            selectFilter.init(data);
        }
        $.get('/api/FilterObjective/',
            function (data) {
                if (data.length > 0) {
                    initPage(data);

                    $('select.hidden-filter').each(function (index, obj) {
                        var firstHiddenElement = $(obj).find('option:eq(1)').val();
                        $(obj).val(firstHiddenElement).trigger('change', [false]);
                    });
                }
            });
    });
})(jQuery);
;
(function ($) {
    $(function () {

        $('.btn-yesno').button();
        $('.btn-yesno').on('click', function (e) {
            var radio = $(e.target).find('[type="radio"]');
            var value = $(radio).val();
            var embededUrlSection = $('#embeded-url-section').html();
            embededUrlSection = embededUrlSection.replace('includeSearch=false', 'includeSearch=' + value);
            embededUrlSection = embededUrlSection.replace('includeSearch=true', 'includeSearch=' + value);
            $('#embeded-url-section').html(embededUrlSection);
        });

        $('.btn-opts').button();
        $('.btn-opts').on('click', function (e) {
            var radio = $(e.target).find('[type="radio"]');
            var value = ($(radio).val() == "true");
            $("#embed-search-only").toggle(!value);
            $("#embed-page-only").toggle(value);
        });

        // TODO put this somewhere else
        $('.row-block').on('click', function (e) {
            var block = $(this);
            if (block.hasClass('opened')) {
                block.removeClass('opened');
            } else {
                block.addClass('opened');
            }
        });

        $('#headerResume').on('click', function (e) {
            if ($(this).hasClass('opened')) {
                $(this).removeClass('opened');
                $('.row-block').removeClass('opened');
                $('.block-step-container').collapse('hide')
            } else {
                $(this).addClass('opened');
                $('.row-block').addClass('opened');
                $('.block-step-container').collapse('show')
            }
        });

        //bind show/hide cr
        $("#checkCR").on("change", function (e) {
            var url = '/Base/ToggleConsistency/';
            //ajax
            $.get(url,
                function (data) {
                    if (data === true) {
                        $(".reviews").show();
                        $(".crItem").show();
                        $(".crMenuItem").show();
                        $("img.online").addClass("lefted");
                    } else {
                        $(".reviews").hide();
                        $(".crItem").hide();
                        $(".crMenuItem").hide();
                        $("img.online").removeClass("lefted");
                    }
                })
                .fail(
                    function (jqXHR, textStatus, err) {
                        $("#errorsPane").html("<span class='error'>" + err + "</span>").show();
                    });
        });

        var bigMenuContainer = $("#big-menu-container");
        if (!_.isUndefined(bigMenuContainer)) {
            bigMenuContainer.find('.submenu.level2').slideToggle();
            bigMenuContainer.find('.side-menu-submenu').click(function () {
                var $this = $(this);
                if (!$this.parent().parent().hasClass('level2')) {
                    $this.next('.submenu').slideToggle();
                    $this.parent().parent().toggleClass('active');
                }
            });
        }
    });
})(jQuery);
;
