$(document).ready(function(){
    var loading = {};

     $('.product-cart').live("contextmenu", function(e) {
         return false;
     });

    $('.checkout').live('mouseup', function(e){
        location = 'shopping-cart';
    });

    $('.product-cart, .cart-remove').live('mouseup', function(e){
        e.preventDefault();
        var product_id = $(this).attr('product_id');
        if (loading[product_id]){
            loading[product_id]++;
        } else {
            loading[product_id] = 1;
        }
        var data = '';
        if ($(this).hasClass('cart-remove') || e.which == 3){
            data = 'remove=' + product_id;
        } else {
            var quantity = $('#cart-input-quantity-'+product_id).val() == NaN || $('#cart-input-quantity-'+product_id).val() == undefined  ? 1 : $('#cart-input-quantity-'+product_id).val();
            $('#cart-input-quantity-'+product_id).val(1);
            data = 'product_id=' + product_id + '&quantity=' + Math.max(quantity,  1);
        }

        $('#cart-loading-' + product_id).show('fade', 'fast');
        $('#cart-header').addClass('loading-marker');

        $.ajax({
            url: 'index.php?route=checkout/cart/update',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function(json) {
                if (json['redirect']) {
                    location = json['redirect'];
                }

                loading[json['product_id']]--;
                if (loading[json['product_id']] <= 0){
                    loading[json['product_id']] = 0;
                    $('#cart-loading-' + product_id).hide('fade', 'fast');
                    $('#quantity-' + json['product_id']).html(json['quantity'] ? json['quantity'] : '');

                    if (json['quantity']){
                        $('#quantity-container-' + json['product_id']).show('fade', 'fast');
                        $('#cart-input-block-' + json['product_id']).hide();
                        $('#cart-quantity-block-' + json['product_id']).show();
                        $('#checkout-' + product_id).show('fade', 'fast');
                    } else {
                        $('#cart-quantity-block-' + json['product_id']).hide();
                        $('#cart-input-block-' + json['product_id']).show();
                        $('#quantity-container-' + json['product_id']).hide('fade', 'fast');
                        $('#checkout-' + product_id).hide('fade', 'fast');
                    }
                }

                $('.success, .warning, .attention, .information, .error').remove();

                if (json['error']) {
                    if (json['error']['warning']) {
                        $('#error .content').html(json['error']['warning']);
                        var error = $('#error');
                        error.css("margin-top", -(error.outerHeight() / 2) + "px");
                        error.css("margin-left", -(error.outerWidth() / 2) + "px");
                        $('#error').show('fade', 'fast');
                        latest_popup = undefined;
                        $('.popup').hide('fade', 'fast');
                    }
                }

                if (json['products_total']){
                    $('.cart_total').addClass('active');
                } else {
                    $('.cart_total').removeClass('active');
                }

                $('#cart_total').html(json['total']);
                if (json['output']) {
                    $('#cart .content').html(json['output']);
                }
            },
            complete: function(){
                $('#cart-header').removeClass('loading-marker');

                $('a[title],img[title],div[title]').tooltip({
                    delay: 100,
                    fade: false,
                    showURL: false
                });
            }
        });
    });

    $('.product-wishlist, .wishlist-remove').live('mouseup', function(e){
        e.preventDefault();
        var product_id = $(this).attr('product_id');
        if (loading[product_id]){
            loading[product_id]++;
        } else {
            loading[product_id] = 1;
        }
        var data = '';
        if ($(this).hasClass('wishlist-remove') || e.which == 3){
            data = 'remove=' + product_id;
        } else {
            data = 'product_id=' + product_id;
        }

        $('#cart-loading-' + product_id).show('fade', 'fast');
        $('#wishlist-header').addClass('loading-marker');

        $.ajax({
            url: 'index.php?route=checkout/wishlist/update',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function(json) {
                if (json['redirect']) {
                    location = json['redirect'];
                }

                loading[json['product_id']]--;
                if (loading[json['product_id']] <= 0) {
                    loading[json['product_id']] = 0;
                    $('#cart-loading-' + product_id).hide('fade', 'fast');

                    if (json['present']) {
                        $('#wishlist-' + json['product_id']).removeClass('active');
                    } else {
                        $('#wishlist-' + json['product_id']).addClass('active');
                    }
                    $('#wishlist-' + json['product_id'])
                            .attr('title', json['title'])
                            .tooltip({
                                delay: 100,
                                fade: false,
                                showURL: false
                            });
                }

                $('.success, .warning, .attention, .information, .error').remove();

                if (json['error']) {
                    if (json['error']['warning']) {
//                    $('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                    }
                }

                if (json['success']) {
//                $('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
//                $('.attention').fadeIn('slow');
                }

                if (json['products_total']) {
                    $('.wishlist_total').addClass('active');
                } else {
                    $('.wishlist_total').removeClass('active');
                }

                $('#wishlist_total').html(json['total']);
                if (json['output']) {
                    $('#wishlist .content').html(json['output']);
                }
            },
            complete: function(){
                $('#wishlist-header').removeClass('loading-marker');

                $('a[title],img[title],div[title]').tooltip({
                    delay: 100,
                    fade: false,
                    showURL: false
                });
            }
        });
    });

    $('#cart').bind('popup', function(e) {
        $('#cart-header').addClass('loading-marker');
        $.ajax({
            url: 'index.php?route=checkout/cart/update',
            dataType: 'json',
            success: function(json) {
                if (json['output']) {
                    $('#cart .content').html(json['output']);
                }
            },
            complete: function(){
                $('#cart-header').removeClass('loading-marker');
            }
        });
    });

    $('.product-wishlist').bind("contextmenu", function(e) {
        return false;
    });

    $('#wishlist').bind('popup', function(e) {
        $('#wishlist-header').addClass('loading-marker');
        $.ajax({
            url: 'index.php?route=checkout/wishlist/update',
            dataType: 'json',
            success: function(json) {
                if (json['output']) {
                    $('#wishlist .content').html(json['output']);
                }
            },
            complete: function(){
                $('#wishlist-header').removeClass('loading-marker');
            }
        });
    });
});

