$(document).ready(function() {
    cartRefresh();
});

function cartKey(evt, el, product_id) {
    var e = evt || window.event;
    var code;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    if (code == 13) {
        cartUpdate(product_id, el.value);
        el.blur();
    }
}

function cartUpdate(product_id, amount) {
    $.getJSON('cart', { action: 'set', id: product_id, count: amount }, function(ret) {
        if (ret.error == true) return;

        if ($('#count'+product_id).length) $('#count'+product_id).val(ret.count);
        if ($('#unit_price'+product_id).length) $('#unit_price'+product_id).html(ret.unit_price);
        if ($('#total'+product_id).length) $('#total'+product_id).html(ret.total);
        if ($('#stock'+product_id).length) $('#stock'+product_id).attr('src', ret.stock);

        cartRefresh();
    });
}

function cartAdd(product_id, amount) {
    if (!amount) amount = 1;
    $('#cart div.flash').stop(true, true).fadeOut(200);
    $.getJSON('cart', { action: 'add', id: product_id, count: amount }, function(ret) {
        if (ret.error == true) return;

        if ($('#count'+product_id).length) $('#count'+product_id).val(ret.count);
        if ($('#unit_price'+product_id).length) $('#unit_price'+product_id).html(ret.unit_price);
        if ($('#total'+product_id).length) $('#total'+product_id).html(ret.total);
        if ($('#stock'+product_id).length) $('#stock'+product_id).attr('src', ret.stock);
        
        cartRefresh();
    });
}

function cartRemove(product_id, amount) {
    $.getJSON('cart', { action: 'remove', id: product_id, count: amount }, function(ret) {
        if (ret.error == true) return;

        if ($('#count'+product_id).length) $('#count'+product_id).val(ret.count);
        if ($('#unit_price'+product_id).length) $('#unit_price'+product_id).html(ret.unit_price);
        if ($('#total'+product_id).length) $('#total'+product_id).html(ret.total);
        if ($('#stock'+product_id).length) $('#stock'+product_id).attr('src', ret.stock);

        cartRefresh();
    });
}

function cartDelete(product_id) {
    $.getJSON('cart', { action: 'set', id: product_id, count: 0 }, function(ret) {
        if (ret.error == true) return;
        document.location.reload();
    });
}

function cartClear()
{
    $.getJSON('cart', { action: 'clear' }, function(ret) {
        if (ret.error == true) return;
        document.location.reload();
    });
}

function cartRefresh() {
    $.getJSON('cart', { action: 'info' }, function(ret) {
        $('#cart span').not('span..price_vat').hide();
        if(ret.products == 1) {
            $('#cart span.one').show();
        } else if (ret.products > 1) {
            $('#cart span.many').show();
            $('#cart span.amount').html(ret.products).show();
        } else {
            $('#cart span.empty').show();
        }
        $('#cart span.price_vat').html(ret.price).show();
        $('#cart div.flash').stop(true, true).fadeIn(200);
        if (!$('#cart a').is(':visible')) {
            $('#cart a').fadeIn(200);
        }
    });
}

