var SSS = {
    // handle a form submission
    // including updating the cart quantity
    updatestatus : function(json) {
        var result = json.results;
        var errors = json.errors;
        if (errors > 0) {
            for (var i = 0; i<errors.length; i++) {
                which = errors[i][0];
                err = errors[i][1];
                if (which == 'quantity') {
                    $('#label-quantity span.error').html(err);
                }
                else if (which == 'product') {
                    $('#label-product span.error').html(err);
                }
            }
        }
        else {
            var added_products = json.added_products;
            
            var cart_total = json.cart_total;
            for (var key in added_products){
                var ap = added_products[key];
            }
            var plural = '';
            if (json.cart_count > 1){
                plural='s';
            }
            var cartDivText = '<img width="128" height="128" alt="Full Basket" src="/static/i/cart/full_basket.jpg"/> <br />\
                    ('+ json.cart_count + ' Item' + plural + ' = ' + json.discount_cart_total + ') \
                                    <br/> \
                                    <a href="/one-page-checkout/" class="main-checkout-url"><img width="89" height="25" alt="Checkout" src="/static/i/cart/checkout.gif" class="sidebar_checkout"/></a>\
                                    <a rel="nofollow" href="/cart/"><img width="88" height="25" alt="View Cart" src="/static/i/cart/view_cart.gif"/></a>';
            $('#mre-cart-count-total').html(cartDivText);
        }
    },

    validate : function(formArray, jqForm, options) {
        // Animate the copy of the product to the cart
        var animateProductId = "product-animate";
        var copydiv = $("#" + animateProductId).clone();
        var copydivoffset = this.getElementPosition(animateProductId);
        var cartdivoffset = this.getElementPosition("mre-cart");
        
        var animateProductIdCopy = animateProductId + "-copy"
        copydiv.attr("id", animateProductIdCopy);
        copydiv.attr("class", "animated-div");
        
        copydiv.css({"top":copydivoffset.top, "left":copydivoffset.left, "position":"absolute"});

        // Animate the cart item showing up in the cart             

        copydiv.appendTo('#content');
        copydiv.animate({
                    "left":"-=" + (copydivoffset.left - cartdivoffset.left) + "px",
                    "top":"-=" + (copydivoffset.top - cartdivoffset.top) + "px",
                    "fontSize": "25%",
                    "opacity" : "0.4",
                    "filter" : "alpha(opacity = 40);"
                }, 1000, "linear" , function(){$(".animated-div").remove();} );
        $("#" + animateProductIdCopy + " img").animate({width:'20px', height:'20px'},1000);
        // $("#product-animate-detail-copy div").animate({width:'15%', height:'25%'},1000);
        return true;
    },

    getElementPosition : function(elemID){
        var offsetTrail = document.getElementById(elemID);
        var offsetLeft = 0;
        var offsetTop = 0;
        while (offsetTrail){
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
        }
        if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
        }
        return {left:offsetLeft,top:offsetTop};
    }

};

