var smartfilter_class = function ()
{
    var self = this,
        controller_set = '/smartfilter/ajax/';

    self.events = function ()
    {
        self.AddToSet();
        self.ToggleFilter();
        self.InitCartSetItem();
        self.InitSmartSearchFilter();
    }
    /***************************************************************/
    
    self.ToggleFilter = function () {
        
        var growth = false;            
                
        
        $('#toggle-footer,#toggle-header').click(function() {
            
            $('#content-filter').slideToggle(1000);
            
                if ( $(window).width() < 1050 && $('#content-filter').is(':visible') || $('#content-filter').is(':hidden')) 
                {
                    $('body').toggleClass('sf');
                }
            
            if (growth) 
            {
                
                $('#icon-footer').toggleClass('fa-angle-double-up fa-angle-double-down');
            
                growth = false
            } 
            else 
            {
                $('#icon-footer').toggleClass('fa-angle-double-down fa-angle-double-up');
                
                growth = true
            }
            
        });
        
    }

    self.AddToSet = function () {

        $( 'button.action-set-btn' ).on('click', function() {

            var action  = $(this).attr('data-action'),
                single  = $(this).data('single');

            if( single )
            {
                var num = parseInt($('#product_qty_num').val(), 10);
            }
            else
            {
                var input = $(this).parents('div.input-group').find('input.product_qty_num');

                var num = parseInt(input.val(), 10);
            }


            if( action == 'minus' )
            {
                if( num > 1 )
                {
                    num = num - 1;
                }
            }
            else
            {
                num = num + 1;
            }


            if( single )
            {
                $('#product_qty_num').val(num);

                $('form#productPageFormOrder input[name=qty]').val(num);
            }
            else
            {
                input.val(num);
            }
        });

        $('.add-to-set').click(function () {

            var product_id = $(this).attr('id'),
                qty        = parseInt($('#product_qty_'+product_id).val(), 10);

            if( $.isNumeric(product_id) && $.isNumeric(qty) && qty > 0 )
            {
                updateCartSet(product_id, qty);
            }

        });
    }

    self.InitCartSetItem = function () {

        var timeout = null;

        $( '.cartset-count' ).bind('change keyup', function() {

            var product_id = parseInt($(this).attr('id'), 10),
                qty        = parseInt($(this).val(), 10);

            clearTimeout(timeout);

            timeout = setTimeout(function() {

                if( $.isNumeric(product_id) && product_id > 0 && $.isNumeric(qty) && qty > 0 )
                {
                    updateCartSet(product_id, qty);
                }

            }, 500)
        });

        $( '.cartset-remove' ).click(function () {

            var product_id = parseInt($(this).attr('id'), 10);

            if( $.isNumeric(product_id) )
            {
                updateCartSet(product_id, 0);
            }
        });

        $( '.add-cartset-to-cart' ).off('click').on('click',function () {

            if( confirm('Вы действительно хотите добавить этот набор в корзину?') )
            {
                $.ajax({
                    url: controller_set + 'add_to_cart',
                    data: {'pointer':1},
                    cache: false,
                    type:  'POST',
                    beforeSend: function () {

                        //$('#set-icon-block_'+product_id+' > i').removeClass().addClass('fa fa-circle-o-notch fa-spin fa-fw');
                    },
                    complete: function () {

                        //$('#set-icon-block_'+product_id+' > i').removeClass().addClass('fa fa-calculator fa-fw');
                    },
                    success: function (obj)
                    {
                        if (obj.status === 'success')
                        {
                            window.location.href = '/cart';
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError)
                    {
                        var error = String(xhr.status + ' ' + thrownError);

                        console.log(error);
                    }
                });
            }
        });
    }

    self.InitSmartSearchFilter = function() {

        if( $('#smartSearchFilterForm').length > 0 ) {

            $('#smartSearchFilterForm').change(function () {

                var filterType = $(this).val(),
                    pointer    = false;

                if( filterType !== 'none' ) {

                    $('.smart_filter__groupchk').removeClass('active');

                    $('#smartFilterForm input[type=checkbox]').prop('checked',false);

                    $('#smartFilterID_'+filterType).addClass('active');
                }
                else
                {
                    $('.smart_filter__groupchk').removeClass('active');
                }
            });
        }
    }

    updateCartSet = function ( product_id, qty ) {

        if( $.isNumeric(product_id) && $.isNumeric(qty) )
        {
            $.ajax({
                url: controller_set + 'update_set',
                data: {'product_id':product_id, 'qty':qty},
                cache: false,
                type:  'POST',
                beforeSend: function () {

                    $('#set-icon-block_'+product_id+' > i').removeClass().addClass('fa fa-circle-o-notch fa-spin fa-fw');
                },
                complete: function () {

                    $('#set-icon-block_'+product_id+' > i').removeClass().addClass('fa fa-calculator fa-fw');
                },
                success: function (obj)
                {
                    if (obj.status === 'success')
                    {
                        $('#filter-cartset').html(obj.view.cartset_list);
                        $('#total-sum-carset').html(obj.view.sum);

                        self.InitCartSetItem();
                    }
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    var error = String(xhr.status + ' ' + thrownError);

                    $('#set-icon-block_'+product_id+' > i').removeClass().addClass('fa fa-calculator fa-fw');

                    console.log(error);
                }
            });
        }
    }
}

var smartfilter = new smartfilter_class();

$(document).ready(function(){smartfilter.events();});