$(document).ready(function(){

    var result_block = $('.basket_result');
    var one_item_block = $('.busket_block');

    function countItems(){
        var length = $('.busket_modal_01').find('.order_list_li').length;
        if(length >= 1){
            $('.head_basket_count').html(length);
            $('.all_count').html(length);
        } else {
            $('.head_basket_count').html('');
            $('.all_count').html('');
        }
    }



    function changeAjaxPrice(id, num){
        $.post( "/orders/buy-items",  {id: id, num:num}, function( data ) {
        });
    }

    function countPrise(block){
        var totalBlock = block.parents('.order_list');
        var total_price = 0;
        totalBlock.find('.price_val').each(function(){
            total_price += +$(this).html();
        });
        $('.all_price_span').html(total_price);
    }


    $('.item').on('click', '.basket_add_but', function(e){
        var id = $(this).data('id');
        $.post( "/orders/buy-items",  {id: id, num:1}, function( data ) {
            $('.basket_result').each(function(){
                $(this).html(data);
                countItems();
            });

        });

    });

    $('.main_cont_wrap').on('click', '.cart_btn', function(e){
        var id = $(this).data('id');
        var num = one_item_block.find('.buy_one_item').val();
        $.post( "/orders/buy-items",  {id: id, num:num}, function( data ) {
            $('.basket_result').each(function(){
                $(this).html(data)
            });
        });

    });

    result_block.on('click', '.delete_item_btn', function(){
        var block  = $(this).parents('.order_list_li');


        var id = block.data('id');

        $.post( "/orders/delete",  {id: id}, function( data ) {
        });
        var forCount = block.parents('ul');
        $('.order_list_li[data-id='+id+']').each(function(){
            var block = $(this);
            block.remove();
        });
        countPrise(forCount);
        countItems();



    });

    result_block.on('click',  '.button_minus', function(){
        var block  = $(this).parents('.order_list_li');
        var price_block = block.find('.price_val');
        var input = block.find('input');
        var number = input.val();
        var id = block.data('id');

        if(number > 1){
            number--;
            input.val(number);
            var price = price_block.data('price');
            var new_price = number * +price;
            price_block.html(new_price);
            changeAjaxPrice(id, number);
            synchronizationPriceData(id, number);
        }

        countPrise(block);
    });


    result_block.on('click',  '.button_plus', function(){
        var block  = $(this).parents('.order_list_li');
        var price_block = block.find('.price_val');
        var input = block.find('input');
        var number = input.val();
        var id = block.data('id');

        number++;
        input.val(number);
        var price = price_block.data('price');
        var new_price = number * +price;
        price_block.html(new_price);

        changeAjaxPrice(id, number);
        synchronizationPriceData(id, number);
        countPrise(block);
    });

    result_block.on('change', '.buy_one_item', function(){
        var block  = $(this).parents('.order_list_li');
        var num = $(this).val();
        var price_block = block.find('.price_val');
        var price = price_block.data('price');
        var id = block.data('id');

        var new_price = num * +price;
        price_block.html(new_price);
        changeAjaxPrice(id, num);
        synchronizationPriceData(id, num);
        countPrise(block);
    });

    function synchronizationPriceData(id, number){
        $('.order_list_li[data-id='+id+']').each(function(){
            var block = $(this);
            block.find('input').val(number);
            var price_block = block.find('.price_val');
            var price = price_block.data('price');
            var new_price = number * +price;
            price_block.html(new_price);
        });
    }



    one_item_block.on('click',  '.button_minus', function(){
        var input = one_item_block.find('.buy_one_item');
        var number = input.val();
        if(number > 1){
            number--;
            input.val(number);
        }
    });


    one_item_block.on('click',  '.button_plus', function(){
        var input = one_item_block.find('.buy_one_item');
        var number = input.val();
        number++;
        input.val(number);
    });

    /****************************compare and bookmarks********************************************/

    function addItemToCompare(id){
        $.post( "/orders/compare",  {id: id}, function( data ) {
        });
    }

    $('#add_to_compare').click(function (event) {
        event.preventDefault();
        var id = $('#one_item_block').data('id');
        addItemToCompare(id);
    });

    $('#add_to_bookmarks').click(function(event){
        event.preventDefault();
        var id = $('#one_item_block').data('id');
        $.post( "/orders/bookmarks",  {id: id}, function( data ) {
        });
    });


});