$(function () { //init productSelectorService.loadProducts(function () { //the filter button is hidden by default, when we get the product data, we show the button $("#filter-button").show(); //console.log(productSelectorService.products); //handle wrapper height var filterHeight = $("#filter-form").height(); $(".results-container").css({ minHeight: filterHeight }); var startWithType = $("#shown-products").data("start-with-type"); var startWithRange = $("#shown-products").data("start-with-range"); if (startWithType || startWithRange) { productSelectorService.showProductsByTypeAndRange(startWithType, startWithRange); } else { productSelectorService.showAllProducts(); } }); $(".filter__field--range").change(function () { productSelectorService.filter(); //handle current hull length number separator if ($(".hull-max").text().indexOf(",") !== -1) { var $hullLengthCurrent = $(".hull-length-current"); var newHullValue = $hullLengthCurrent.text().toString().replace(".", ","); $hullLengthCurrent.text(newHullValue); } if ($(".beam-overall-max").text().indexOf(",") !== -1) { var $beamOverallCurrent = $(".beam-overall-current"); var newBeamOverallValue = $beamOverallCurrent.text().toString().replace(".", ","); $beamOverallCurrent.text(newBeamOverallValue); } }); $(".filter input[type=checkbox]").change(function () { productSelectorService.filter(); }); function handleTrailerClick($clicked) { if ($clicked.attr('checked')) { if ($clicked.prop('id') == 'input-checkbox__trailerable') { $("#input-checkbox__nottrailerable").attr('checked', false); } else { $("#input-checkbox__trailerable").attr('checked', false); } } } $("#input-checkbox__trailerable").click(function () { handleTrailerClick($(this)); }); $("#input-checkbox__nottrailerable").click(function () { handleTrailerClick($(this)); }); function resetFilter(e) { e.preventDefault(); //uncheck all boxes $(".filter input[type=checkbox]").attr('checked', false); $(".filter input[type=radio]").attr('checked', false); //return single sliders to origin positions $(".filter__field--range").each(function () { var $slider = $(this); $slider.val($slider.data("default")); }); /* //return JUI sliders to default var $priceRange = $("#price-range"); $priceRange.slider('values', 0, $priceRange.data('default-min')); $priceRange.slider('values', 1, $priceRange.data('default-max')); */ $('.filter__field--range').each(function () { $(this).closest("fieldset").find('.range-value').html($(this).val().toString().replace(".", $(this).data("separator"))); }); //show all products productSelectorService.showAllProducts(); $(".custom-selector-type").hide(); $(".custom-selector-type-generic").show(); } $("#reset-button").click(function (e) { resetFilter(e); }); $("#product-selector-panel").on("click", "#filter-options-reset", function (e) { resetFilter(e); }); $("#product-sort-select").change(function () { productSelectorService.sortProducts(); }); function getTopPos(t) { if (t.length > 0) { return t.position().top; } return 0; } function getBottomPos(t) { if (t.length > 0) { return t.position().top + t.outerHeight(true); } return 0; } function handleCompareButton() { var $compareButton = $('.compare-button'); var posFilter = getTopPos($('.filter')); var posFilterBottom = getBottomPos($('.filter-results')); var $allComparedCheckboxesChecked = $(".product-compare-checkbox:checked"); if ($allComparedCheckboxesChecked.length > 1 && $(window).scrollTop() > posFilter && $(window).scrollTop() < posFilterBottom) { $compareButton.addClass('compare-button--active'); $compareButton.show(); } else { $compareButton.removeClass('compare-button--active'); $compareButton.hide(); } //update the text on the button $compareButton.find("span").text($allComparedCheckboxesChecked.length); } $(window).scroll(handleCompareButton); $(".product-compare-checkbox").click(function() { var $allComparedCheckboxesChecked = $(".product-compare-checkbox:checked"); var $allComparedCheckboxesUnchecked = $(".product-compare-checkbox:not(:checked)"); if ($allComparedCheckboxesChecked.length === 3) { $allComparedCheckboxesUnchecked.prop('disabled', true); } else { $allComparedCheckboxesUnchecked.prop('disabled', false); } handleCompareButton(); }); function setEqualHeight() { var $allElements = $('*').find('[data-same-height]'); var groups = {}, group; $allElements.each(function (i, el) { group = $(el).data('same-height'); if (groups.hasOwnProperty(group)) { groups[group] += 1; } else { groups[group] = 1; } }); for (var key in groups) { var $group = $('[data-same-height="' + key + '"]'); // Get an array of all element heights var elementHeights = $group.map(function () { return $(this).height(); }).get(); // Math.max takes a variable number of arguments // `apply` is equivalent to passing each height as an argument var maxHeight = Math.max.apply(null, elementHeights); // Set each height to the max height $group.height(maxHeight); } } $(".compare-button").click(function() { var $comparePanel = $("#product-selector-compare-panel"); $comparePanel.show(); var $selectorPanel = $("#product-selector-panel"); $selectorPanel.hide(); var $allComparedCheckboxes = $(".product-compare-checkbox:checked"); var productIds = []; $allComparedCheckboxes.each(function() { productIds.push($(this).data("product-id")); }); productSelectorService.scrollTo("product-selector-compare-panel"); productSelectorService.getComparedItems(productIds, $selectorPanel.data("country-code"), $selectorPanel.data("language-code"), function (data) { var $itemsContainer = $comparePanel.find("#product-selector-compare-items"); $itemsContainer.html(data); setEqualHeight(); }); }); $("#compare-return-button").click(function() { $("#product-selector-compare-panel").hide(); $("#product-selector-panel").show(); }); });