/**
 * Created by vitaliy on 07.11.16.
 */

function loadService(){

    $('.properties_block').each(function(){
        var block = $(this);
        $(this).find('input[type=checkbox]').each(function(){
            if(this.checked){
                block.removeClass('closed').addClass('opened');
                block.find('ul').css({"display":"block"})
                return true;
            }

        });

    });

    $('#finput').keyup(function() {

        var empty = false;
        $('#finput').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (empty) {
            $('.sok').attr('disabled', 'disabled').css('display','none');
        } else {
            $('.sok').removeAttr('disabled').css('display','inline-block');
        }

    });






    $('.price_tooltip_close').on('click',function(){
        $(this).parent().hide();
    });

    $('.pok').click(function(event) {
        url = $('#purl').val();
        price_min = $('#min_price').val();
        price_max = $('#max_price').val();

        document.location.href = url + "&pmin=" + price_min + "&pmax=" + price_max;
    });

    $('.sok').click(function(event) {
        if ($('#finput').val() != "Артикул" && $('#finput').val() != "") {


            document.location.href = "/search/main?word="+$('#finput').val();


        }
    });
}

function priceRequest(link){
    var tag = $(link).parents('li');
    var filter = tag.data('filter');
    var id = $("#filters").data('category');
    $.ajax({
        url: "/ajax/count",
        type: 'GET',
        data: {info:filter,category:id},
        success: function(result){
            if(result=='0'){
                $(link).addClass('disabled-link');
                tag.find('input').prop( "disabled", true );
                $(link).find("span").html('('+result+')');
            } else {
                $(link).removeClass('disabled-link');
                tag.find('input').prop( "disabled", false );
                $(link).find("span").html('('+result+')');
            }


    }});
}


function loadPrices(){
    $('.filter-link').each(function(){
        if(!$(this).parents('li').data('checked')){
            priceRequest(this);
        }


    });
}


function priceSlider(){


    if($('#price_interval').length){

        var block = $('#price_block');
        var link = block.data('url');
        var min = block.data('min');
        var max = block.data('max');
        var from = block.data('from');
        var to = block.data('to');


        $('#price_interval').ionRangeSlider({
            type: 'double',
            min: min,
            max: max,
            from: from,
            to: to,
            grid: false,
            onFinish: function(e) {
                var url = link;
                var from = e.from;
                var to = e.to;
                $.pjax({url: url.replace('{from}', from).replace('{to}', to), container: "#list-container",timeout:5000, scrollTo: false})
            }
        });
    }
}

$( document ).ready(function() {
    loadService();
    loadPrices();
    priceSlider();

    $('body').on('click', '.disabled-link', function(e){
        e.preventDefault();
    });

    $('body').on('change', '.features-option', function(){
        var url = $(this).parents('li').find('a').attr('href');
        $.pjax({url: url, container: '#list-container',timeout:5000,scrollTo:false })
    });

    $("#list-container").on("pjax:end", function() {
        loadPrices();
        loadService();
        priceSlider();

    });






});
