﻿(function (j$) {
    var IdPrices = '757';
    var MinLowBound = 7500;
    var MaxLowBound = 60000;
    var MinHighBound = 7500;
    var MaxHighound = 60000;

    j$(document).ready(function () {
        RefreshContentZone(null);
    });

    function RefreshContentZone(targetUrl) {
        j$.ajax({ url: (targetUrl == null ? '/GlobalCatg.aspx?FamiId=1001' : targetUrl), type: 'Get', success: UpdateZone });
    }

    function UpdateZone(data) {
        j$('#comparateur-wrap').html(data);
        j$('#comparateur-wrap input').click(function () {
            var targetUrl = j$(this).val();
            RefreshContentZone(targetUrl);
        });
        j$('#comparateur-wrap select').change(function () {
            var targetUrl = j$(this).val();
            RefreshContentZone(targetUrl);
        });
        InitSubscriptionsPrices();
    }

    function InitSubscriptionsPrices() {
        GetPriceBounds();
        var low = MinLowBound;
        var high = MaxHighBound;
        if (j$('#LowValue').val() != "")
            low = Math.max(parseInt(j$('#LowValue').val()), MinLowBound);
        if (j$('#HighValue').val() != "")
            high = Math.min(parseInt(j$('#HighValue').val()), MaxHighBound);
        j$(".priceSelect").slider({
            range: true,
            min: 7500,
            max: 60000,
            step: 2500,
            values: [low, high],
            slide: PriceChanging,
            stop: PriceChanged
        });
        Log1 = parseInt(j$('#LowValue').val());
        j$('.ui-slider-handle:eq(0)').attr('id', 'handle_price-select-low');
        j$('.ui-slider-handle:eq(1)').attr('id', 'handle_price-select-high');
        j$('div.priceLow').html(low + ' &euro;');
        j$('div.priceHigh').html((high == 60000 ? ' + ' : '') + high + ' &euro;');

    }

    function PriceChanging(event, ui) {
        if (ui.values[0] >= ui.values[1] - 2500) {
            if ((ui.values[0] > Log1 + 1)) {
                if (ui.values[0] + 1 < 57500) {
                    j$('.priceSelect').slider('option', 'values', [ui.values[0], ui.values[0] + 2500]);
                }
                else {
                    j$('.priceSelect').slider('option', 'values', [57500, 60000]);
                }
            }
            else {
                if (ui.values[1] - 1 > 10000) {
                    j$('.priceSelect').slider('option', 'values', [ui.values[1] - 2500, ui.values[1]]);
                }
                else {
                    j$('.priceSelect').slider('option', 'values', [7500, 10000]);
                }
            }
        }
        Log1 = ui.values[0];
        j$('div.priceLow').html(ui.values[0] + ' &euro;');
        j$('div.priceHigh').html((ui.values[1] == 60000 ? ' + ' : '') + ui.values[1] + ' &euro;');
    }

    function PriceChanged(event, ui) {
        var filterPrice = "&K" + IdPrices + "=";
        var val0 = Math.max(Math.min(ui.values[0], MaxLowBound), MinLowBound);
        var val1 = Math.min(Math.max(ui.values[1], MinHighBound), MaxHighBound);
        if (val1 == '60000') {
            filterPrice += "~plus-de-" + val0;
        }
        else {
            if (val0 != val1) {
                filterPrice += "~de-" + val0 + '-a-' + val1;
            }
            else {
                filterPrice += "~" + val0;
            }
        }
        RefreshContentZone(j$('#NoPriceUrl').val() + filterPrice);
    }

    function GetPriceBounds() {
        MinLowBound = 60000;
        MaxLowBound = 7500;
        MinHighBound = 60000;
        MaxHighBound = 7500;
        j$('input.price_ranges').each(function (index) {
            var id = j$(this).attr("id");
            var valtype = id.split("_")[1];
            var val1 = parseInt(id.split("_")[2]);

            if (valtype == "between") {
                var val2 = parseInt(id.split("_")[3]);
                if (val1 < MinLowBound) MinLowBound = val1;
                if (val1 > MaxLowBound) MaxLowBound = val1;
                if (val2 < MinHighBound) MinHighBound = val2;
                if (val2 > MaxHighBound) MaxHighBound = val2;
            }
            else {
                if (val1 < MinLowBound) MinLowBound = val1;
                if (val1 > MaxLowBound) MaxLowBound = val1;
                if (val1 < MinHighBound) MinHighBound = val1;
                if (val1 > MaxHighBound) MaxHighBound = val1;
            }
        });
        if (MinLowBound > MaxLowBound)
            MinLowBound = MaxLowBound;
        if (MinHighBound > MaxHighBound)
            MinHighBound = MaxHighBound;
    }
})(jQuery)
