$(document).ready( function() {
    var page_url = window.location.protocol + '//' + window.location.hostname + '/';
    
    formatBasket();
    
    $('img.addtocart').click( function() {
        if ($('tr.current_item').length) {
            var newproduct = $('tr.current_item');
            newproduct.addClass('product').removeClass('current_item').show();
            newproduct.find('input[name=quantity]').attr('value', 1);
        }
        if ($('tr.product').length > 3)
            $('tr.moreproducts').show();

        formatBasket();
    });
    
    $('div#totalBox,div.basketWrapper').mouseover( function() {
        if ($('table.basketTable').find('tr.product').length)
            $('div.basketWrapper').show();
    });
    
    $('div#totalBox,div.basketWrapper').mouseout( function() {
        $('div.basketWrapper').hide();
    });
    
    $('a.cart-update').click( function() {
        var prod = $(this).parent('td').find('input[name=quantity]');
        
        if ($(this).hasClass('minus'))
            prod_qty = prod.val() - 1;
        else if ($(this).hasClass('plus'))
            prod_qty = 1 + parseInt(prod.val());
        else
            prod_qty = parseInt(prod.val());
        
        if (!prod_qty)
            deleteProduct(prod);
        else
            updateProduct(prod, prod_qty);

        return false;
    });

    $('input.cart-quantity').blur( function() {
        var prod = $(this);
        var prod_qty = parseInt(prod.val());
        
        updateProduct(prod, prod_qty);
        
        return false;
    });
    
    function deleteProduct(prod) {
        var prod_id = prod.parent('td').find('input[name=prod_id]').val();
        
        $.post(
            page_url + 'checkout/cart/delete',
            {
                id: prod_id
            },
            function(data) {
                var result = $.parseJSON(data);
                
                if (result.error != '')
                    alert(result.error);
                else {
                    var product_id = 0;
                    if ($('input[name=prodid]').length)
                        product_id = $('input[name=prodid]').val();
                    
                    if ($('a[rel=item' + prod_id + ']').length)
                        $('a[rel=item' + prod_id + ']').removeClass('compare-incart');

                    if (prod_id == product_id)
                        prod.parents('tr.product').addClass('current_item').removeClass('product').hide();
                    else
                        prod.parents('tr.product').addClass('removed').removeClass('product').hide();
                    updateCart();
                    formatBasket();
                }
            }
        );
    }
    
    function updateProduct(prod, prod_qty) {
        var prod_id = prod.parent('td').find('input[name=prod_id]').val();
        var prod_opt = prod.parent('td').find('input[name=prod_opt]').val();
        var prod_price = prod.parent('td').prev('td').find('span').html();
            
        $.post(
            page_url + 'checkout/cart/update',
            {
                id: prod_id,
                qty: prod_qty,
                opt: prod_opt,
                price: prod_price
            },
            function(data) {
                var result = $.parseJSON(data);
                
                if (result.error != '')
                    alert('Error: ' + result.error);
                else {
                    updateCart();
                    formatBasket();
                    prod.val(result.qty);
                }
            }
        );
    }
       
    function updateCart() {
        $.post(
            page_url + 'checkout/cart/cart',
            function(data) {
                var result = $.parseJSON(data);
                
                $('span.cartvalues').html(result.totals.count + ' item(s) - &pound;' + result.totals.total);
                $('span.cart-total').html(result.totals.total);
                $('p.cart-total').html(result.totals.total);
                $('p.cart-vat').html(result.totals.vat);
            }
        )
    }
    
    function formatBasket() {
        var products = $('table.basketTable').find('tr.product');
        var basket_amount = products.length;
        
        if (basket_amount <= 4)
            $('tr.moreproducts').hide();
         
        products.hide();
        products.filter(':lt(4)').show();
        
        $('th.itemcount').html(basket_amount);
        $('td.itemcount').html(basket_amount);
        $('tr.moreproducts span.product_more').html(basket_amount - 4);
        $('tr.moreproducts span.plural').html((basket_amount - 4) > 1 ? 's' : '');
        
        if (basket_amount < 1) {
            $('span.cartvalues').html('0 item(s) - &pound;0.00');
            $('span.cart-total').html(0);
            $('div.basketWrapper').hide();
        }
    }
    
});

