function shoppingbasket(product_pk, amount, change) {
    if (amount == 0) {
        if (!confirm('Tento produkt není momentálně skladem. V případě zájmu objednejte a bude Vás kontaktovat náš prodejce ohledně dostupnosti. Opravdu chcete vložit zboží do košíku?')) {
            return false;
        }
    }
    response_html = $.ajax({
        url: '/nakupni-kosik/'+product_pk+'/'+change+'/',
        async: false
    }).responseText;
    $('#id_shoppingbasket').html(response_html);
    $('#id_shoppingbasket').effect('bounce', 500);
    return true;
};

function shoppingbasket_json(product_pk, amount) {
    $.getJSON('/nakupni-kosik/'+product_pk+'/'+amount+'/json/', function(data) {
        for (product in data.items) {
            $('#id_item_price_'+product).html(data.items[product].price_CZK+' ('+data.items[product].price_EUR+')');
        }
        $('#id_price_summary').html(data.price_CZK+' ('+data.price_EUR+')');
        $('input:radio[name="delivery"]').each(function() {
            var rel = $(this).attr('rel');
            var rel_splitted = rel.split(':', 6);
            var rel_price_limit = rel_splitted[1];
            var rel_price = rel_splitted[2];
            var rel_price_CZK = rel_splitted[3];
            var rel_price_EUR = rel_splitted[4];          
            var rel_name = rel_splitted[5];

            if (rel_price>0 && (rel_price_limit==0 || (data.price < rel_price_limit))) {
                $(this).next('label').html(rel_name+' '+'('+rel_price_CZK+' - '+rel_price_EUR+')');
            } else {
                $(this).next('label').html(rel_name);
            };
        });
        
        response_html = $.ajax({
            url: '/nakupni-kosik/ajax/',
            async: false
        }).responseText;
        $('#id_shoppingbasket').html(response_html);
        $('#id_shoppingbasket').effect('bounce', 500);
    });
    
}

function shoppingbasket_change_payments(obj) {
    var rel = $(obj).attr('rel');
    var rel_splitted = rel.split(':', 2);
    var rel_payments = rel_splitted[0];
    var payments_pks = rel_payments.split(',');


    $('input:radio[name="payment"]').hide().next().hide();
    $('input:radio[name="payment"][value="'+payments_pks.join('"],[value="')+'"]').show().next().show();
    $('input:radio[name="payment"][value="'+payments_pks[0]+'"]').attr('checked', true);
    
}

$(document).ready(function() {
    $('input:radio[name="delivery"]').change(function(e) {shoppingbasket_change_payments(this)});
    var delivery_first = $('input:radio[name="delivery"]').first();
    if (delivery_first.length) {
        delivery_first.attr('checked', true);
        shoppingbasket_change_payments(delivery_first);
    }
});
