var basket = {
   
    addProduct: function(product, quantity) {
        $j('#loadingBasket').show();
        $j.getJSON('/order/addproduct/',{
            format: 'json',
            productId : product.id,
            quantity: quantity
        }, function(ret){
            product = ret.product;
            var msg = "", i;
        
            if (undefined !== ret.status.ok) {
                msg += product.name + "<br />";
                msg += "<img src='" + product.thumbImage + "' />";
            }

            for (i in ret.status) {
                msg == "" ? msg = ret.status[i] : msg += "<br />" + ret.status[i];
            }
        
            if (msg !="") {
                $j.jGrowl(msg);
            }
            if (undefined !== ret.status.ok) {
                basket.reload(true);
            }
            $j('#loadingBasket').hide();
        });
    },
    
    reload: function(hideBasketAfterReload){
        $j.get('/widget/basket-quick-view',function(html){
            var box = $j(".basket-box");
            if (box.size() == 0) {
                $j("#li-basket").append(html);
            } else {
                $j(".basket-box").replaceWith(html);
            }
    
            var count = $j(".basket-box").attr("count"),
            total = $j(".basket-box").attr("total"),
            connector = 'item';
            if (count > 1) {
                connector = 'items';
            }
            $j("#basket-items-count").html(count + ' ' + connector + ' ' + "&pound;" + total);
            $j("ul.basket-box").show();
            if (true === hideBasketAfterReload) {
                setTimeout(function(){
                    $j("ul.basket-box").fadeOut(1000);
                },2000);
            }
        });
    },

    removeProduct: function(orderProductId) {
        window.location = '/order/removeproduct/orderProductId/'+orderProductId;
    }

};

