$(function () {

    //$.facebox.settings.closeImage = '/static/images/closelabel.png';
    //$.facebox.settings.loadingImage = '/static/images/loading.gif';

    $('.add_to_cart').click(function() {
        var id_str = $(this).attr('id');
        var id = id_str.substr(id_str.indexOf('_') + 1);

        $.ajax({
                    type: 'POST',
                    url: '/checkout/order/add_to_cart/',
                    data: {product_id: id},
                    success: function(data) {
                        jQuery.facebox(data);
                        refresh_order(data);
                    }
                });
        return false;
    });

    $('.remove_from_cart').click(function() {
        var id_str = $(this).attr('id');
        var id = id_str.substr(id_str.indexOf('_') + 1);

        $.ajax({
                    type: 'POST',
                    url: '/checkout/order/remove_from_cart/',
                    data: {product_id: id},
                    success: function(data) {
                        $('#cart_' + id).slideUp('slow', function() {
                            $('.j-cart-position:not(:hidden)').each(function(idx, elm) {
                                $(this).html(idx + 1);
                            });
                        });
                    }
                });
        return false;
    });


    $('.remove_from_checkout_cart').click(function() {
        var id_str = $(this).attr('id');
        var id = id_str.substr(id_str.indexOf('_') + 1);

        $.ajax({
                    type: 'POST',
                    url: '/checkout/order/remove_from_cart/',
                    data: {product_id: id},
                    success: function(data) {
                        $('#cart_' + id).slideUp('slow', refresh_order());
                    }
                });
        return false;
    });

    $('.shipping_type').change(function () {
        var value = $(this).val();
        if (value == 'another') {
            $('#shipping_another_address').slideDown('slow');
        }
        else {
            $('#shipping_another_address').slideUp('slow');
        }
    });

    $('#gift').change(function () {
        if ($(this).attr('checked')) {
            $('.b-gratulation').slideDown('slow');
        }
        else {
            $('.b-gratulation').slideUp('slow');
        }
    });

    $('.j-cart-item-quantity').focusout(function() {
        var id_str = $(this).attr('id');
        var id = id_str.substr(id_str.indexOf('_') + 1);

        var quantity = $(this).val();
        var quantity_available = $('#j-quantity-available-' + id);

        if( isNaN(parseInt(quantity, 10))){
            quantity = 1;
            $(this).val(quantity);
        }
        
        if (quantity <= 0)
        {
            quantity = 1;
            $(this).val(quantity);
        }

        if(quantity > parseInt(quantity_available.text(), 10))
        {
            quantity = 1;
            $(this).val(quantity);
            quantity_available.parent().css({'color': 'red', 'font-weight': 'bold'});
        }
        else
        {
            quantity_available.parent().css({'color': '#000', 'font-weight': 'normal'});
        }

        $.ajax({
                    type: 'POST',
                    url: '/checkout/order/set_product_quantity/',
                    data: {product_id: id, quantity: quantity},
                    success: function(data) {
                        refresh_order();
                    }
                });
    });

    function refresh_order() {
        $.ajax({
            type: 'POST',
            url: '/checkout/order/update_checkout_cart/',
            success: function(data) {
                //$('#j-order-sum').text(data['sum'] + ' EUR');
                //$('#j-order-saving').text(data['saving']);
                //$('#j-tax-7').text(data['tax-7']);
            }
        });
    }

});

