$(document).ready(function() {
    var winowWidth = $(window).width();
    var initData = $('#products').html();
    if (winowWidth < 991) {
        $('.filtervalues').each(function(index, element) {
            $(this).parent().find('.filtertitle i').each(function() {;
                $(this).toggle();
            });
            $(this).toggle();
        });
        $('#filtersholder').toggle();
        $('#filterstitle i').toggle();
    } else {
        var filtersHeight = $('#filters').height();
        $('#products').css('min-height', filtersHeight);
    }

    function resetFiltersForm() {
        $('.cb:checked').each(function() {
            $(this).attr('checked', false);
            $(this).parent().removeClass("checked");
        });
        $('#price').val('');
        parent.location.hash = '';
        $('#products').html(initData);
        $('#catslider').slideDown('slow');
        if ($('#categorybanners').length) {
            $('#categorybanners').show();
        }
        if (winowWidth < 991) {
            $('.filtervalues').each(function(index, element) {
                $(this).parent().find('.filtertitle i.close-icon.active').each(function() {;
                    $(this).removeClass('active');
                });
                $(this).toggle();
            });
            $('#filtersholder').toggle();
            $('#filterstitle i').toggle();
        }
        $(window).scrollTop(0);
    }
    $(document).on('click', '.resetfilters', function() {
        resetFiltersForm();
    });
    var $filterGroup = $('.filter')
    var toggleFilterGroup = function() {
        $filterGroup.each(function() {
            var $itemsWrapper = $(this).find('.filtervalues'),
                $headline = $(this).find('.filtertitle'),
                $openIcon = $headline.find('i.open-icon'),
                $closeIcon = $headline.find('i.close-icon');
            $headline.click(function() {
                if ($itemsWrapper.is(':visible')) {
                    $closeIcon.removeClass('active');
                    $openIcon.addClass('active');
                    $itemsWrapper.slideUp({
                        duration: 'fast'
                    });
                } else {
                    $closeIcon.addClass('active');
                    $openIcon.removeClass('active');
                    $itemsWrapper.slideDown({
                        duration: 'fast'
                    });
                }
            });
        });
    };
    var toggleFilters = function() {
        var $title = $('#filterstitle'),
            $holder = $('#filtersholder'),
            $openIcon = $title.find('i.open-icon'),
            $closeIcon = $title.find('i.close-icon');
        $title.click(function() {
            if ($holder.is(':visible')) {
                $closeIcon.removeClass('active');
                $openIcon.addClass('active');
                $holder.slideUp({
                    duration: 'fast'
                });
            } else {
                $closeIcon.addClass('active');
                $openIcon.removeClass('active');
                $holder.slideDown({
                    duration: 'fast'
                });
            }
        });
    };
    toggleFilterGroup();
    toggleFilters();
    $(document).on('change', ".cb", function() {
        markParent();
        makeRequest();
    });
    $(document).on('change', "#price", function() {
        makeRequest();
    });

    function markParent() {
        $(".cb").each(function() {
            if ($(this).is(':checked')) {
                $(this).parent().addClass("checked");
            } else {
                $(this).parent().removeClass("checked");
            }
        });
    }
    $(document).on("click", "div.fcheckbox", function() {
        $(this).parent().find('label').click();
    });

    function makeRequest() {
        var post = $('#filtersform').serialize();
        var post2 = decodeURIComponent(post);
        window.location.hash = post2;
        $.get('/ajax-productsfilter', post, function(data) {
            if (data == 'reset') {
                resetFiltersForm();
            } else {
                $('#products').html(data);
                $('#pbtitle').empty().text('Rezultati napredne pretrage');
                if ($('#products .pb').length) {
                    var maxTitleHight = 0;
                    var maxDescrHight = 0;
                    $('#products .pb h3').each(function(i, e) {
                        var th = $(this).height();
                        if (th > maxTitleHight) {
                            maxTitleHight = Math.round(th);
                        }
                    });
                    $('#products .pb .pd').each(function(i, e) {
                        var dh = $(this).height();
                        if (dh > maxDescrHight) {
                            maxDescrHight = Math.round(dh);
                        }
                    }); /*$('#products .pb h3').height(maxTitleHight);*/
                    $('#products .pb .pd').height(maxDescrHight);
                }
                if ($('#categorybanners').length) {
                    $('#categorybanners').hide();
                }
                if ($('#catslider:visible').length) {
                    prodPos = (parseInt($('#products').offset().top) - parseInt($('#catslider').height()) - 100);
                    $('html, body').animate({
                        scrollTop: prodPos
                    }, 500);
                    $('#catslider').slideUp();
                } else {
                    prodPos = (parseInt($('#products').offset().top) - 100);
                    $('html, body').animate({
                        scrollTop: prodPos
                    }, 500);
                }
            }
        });
    }
    if (window.location.hash) {
        var qstring = window.location.hash.substring(1);
        var pairs = qstring.split('&');
        var pairs_length = pairs.length;
        for (var i = 0; i < pairs_length; i++) {
            pair = pairs[i].split('=');
            var fid = '#f-' + pair[0].replace('f_', '').replace('[]', '') + '-' + pair[1];
            $(fid).attr('checked', true);
            $(fid).parent().toggleClass("checked");
            if ('prod[]' == pair[0]) {
                $('#b-' + pair[1]).attr('checked', true).parent().toggleClass("checked");
            } else if (pair[0] == 'price') {
                $('input[name="' + pair[0] + '"][value="' + pair[1] + '"]').attr('checked', true);
            }
        };
        $.get('/ajax-productsfilter', qstring, function(data) {
            if (data == 'reset') {
                window.location.href = window.location.href.replace(/#.*/, '');
            } else {
                if ($('#categorybanners').length) {
                    $('#categorybanners').hide();
                }
                $('#catslider').slideUp();
                $('#products').html(data);
                if ($('#products .pb').length) {
                    var maxTitleHight = 0;
                    var maxDescrHight = 0;
                    $('#products .pb h3').each(function(i, e) {
                        var th = $(this).height();
                        if (th > maxTitleHight) {
                            maxTitleHight = Math.round(th);
                        }
                    });
                    $('#products .pb .pd').each(function(i, e) {
                        var dh = $(this).height();
                        if (dh > maxDescrHight) {
                            maxDescrHight = Math.round(dh);
                        }
                    });
                    $('#products .pb h3').height(maxTitleHight);
                    $('#products .pb .pd').height(maxDescrHight);
                }
                $('#pbtitle').empty().text('Rezultati napredne pretrage');
            }
        });
    }
});