var Soleo = {
    Cart: {
        OnLoad: function() {
            for (var i = 1; i < Infinity; i++) {
                var span = $('cart-span' + i);
                if (span) {
                    Event.observe('cart-span' + i, 'change', Soleo.Cart.ChangeSafeEvent /*function() {
                        Soleo.Cart.ChangeSafe($('cart-product' + (i - 1)).value, i - 1);
                    }*/);
                }
                else {
                    break;
                }
            }
        },
        // Adds an item to the cart and moves to the page on failure.
        AddSafe: function(id) {
            try {
                this.Add(id);
            }
            catch (ex) {
                window.location = '?Produits/Ajouter/' + id;
            }
            return false;
        },
        // Increments an item to the cart and moves to the page on failure.
        IncrementSafe: function(id, number) {
            try {
                this.Increment(id, number);
            }
            catch (ex) {
                window.location = '?Panier/Augmenter/' + id;
            }
            return false;
        },
        // Decrements an item to the cart and moves to the page on failure.
        DecrementSafe: function(id, number) {
            try {
                this.Decrement(id, number);
            }
            catch (ex) {
                window.location = '?Panier/Reduire/' + id;
            }
            return false;
        },
        ChangeSafeEvent: function(event) {
            var number = parseInt(event.target.id.substr(9));
            var id = $('cart-product' + number).value;
            Soleo.Cart.ChangeSafe(id, number);
        },
        // Decrements an item to the cart and moves to the page on failure.
        ChangeSafe: function(id, number) {
            try {
                this.Change(id, number);
            }
            catch (ex) {
                // Do nothing, waiting for form submission.
            }
            return false;
        },
        // Adds an item to the cart.
        Add: function(id) {
            document.body.style.cursor = 'wait';
            new Ajax.Request('?raw=1&path=Produits/Ajouter/' + id, {
                method: 'get',
                onSuccess: function(transport) {
                    document.body.style.cursor = 'default';
                    var response = transport.responseText || "no response text";
                    var cartInfo = transport.responseXML.getElementsByTagName('cart-info')[0];
                    var newPrice = 0;
                    var newProductsCount = 0;
                    for (var i = 0; i < cartInfo.childNodes.length; i++) {
                        child = cartInfo.childNodes[i];

                        if (child && child.nodeType == 1) {
                            if (child.nodeName == 'price') {
                                if (child.childNodes[0].nodeValue) {
                                    newPrice = parseFloat(child.childNodes[0].nodeValue);
                                }
                                else {
                                    throw 'Browser not supported';
                                }
                            }
                            if (child.nodeName == 'count-products') {
                                if (child.childNodes[0].nodeValue) {
                                    newProductsCount = parseInt(child.childNodes[0].nodeValue);
                                }
                                else {
                                    throw 'Browser not supported';
                                }
                            }
                        }
                    }

                    var cartPrice = $('cartPrice');
                    var cartCount = $('cartCount');
                    if (cartPrice) {
                        if (cartPrice.textContent) {
                            cartPrice.textContent = newPrice.toFixed(2);
                        }
                        else if (cartPrice.innerText) {
                            cartPrice.innerText = newPrice.toFixed(2);
                        }
                        else {
                            throw 'Browser not supported';
                        }
                    }
                    if (cartCount) {
                        if (cartCount.textContent) {
                            cartCount.textContent = newProductsCount;
                        }
                        else if (cartPrice.innerText) {
                            cartCount.innerText = newProductsCount;
                        }
                        else {
                            throw 'Browser not supported';
                        }
                    }

                    Soleo.Cart.ShowMessage();
                },
                onFailure: function() {
                    document.body.style.cursor = 'default';
                    throw 'Ajax failed';
                }
            });
        },
        // Increments an item in the cart.
        Increment: function(id, number) {
            document.body.style.cursor = 'wait';
            new Ajax.Request('?raw=1&path=Panier/Augmenter/' + id, {
                method: 'get',
                onSuccess: Soleo.Cart.IncrementDecrementSuccess(number, 1),
                onFailure: function() {
                    document.body.style.cursor = 'default';
                    throw 'Ajax failed';
                }
            });
        },
        // Decrements an item in the cart.
        Decrement: function(id, number) {
            // Handles a case when the quantity is 1.
            var productCount = $('cart-span' + number);
            if (productCount) {
                if (productCount.value) {
                    if (parseInt(productCount.value) <= 1) throw "Quantity is less or equal to one. Refresh need.";
                }
                else {
                    throw 'Browser not supported';
                }
            }

            document.body.style.cursor = 'wait';
            new Ajax.Request('?raw=1&path=Panier/Reduire/' + id, {
                method: 'get',
                onSuccess: Soleo.Cart.IncrementDecrementSuccess(number, -1),
                onFailure: function() {
                    document.body.style.cursor = 'default';
                    throw 'Ajax failed';
                }
            });
        },
        // Change quantity of an item in the cart.
        Change: function(id, number) {
            // Handles a case when the quantity is 1.
            var productCount = $('cart-span' + number);
            if (productCount) {
                if (productCount.value) {
                    productCount.style.color = (parseInt(productCount.value) < 0 || parseInt(productCount.value) > 9999) ? '#c20' : '#000';
                    if (parseInt(productCount.value) < 1) throw "Quantity is zero. Refresh need.";
                }
                else {
                    throw 'Browser not supported';
                }
            }

            document.body.style.cursor = 'wait';
            new Ajax.Request('?raw=1&path=Panier/Varier/' + id, {
                method: 'post',
                parameters: { 'nq': parseInt(productCount.value) },
                onSuccess: Soleo.Cart.IncrementDecrementSuccess(number, 0),
                onFailure: function() {
                    document.body.style.cursor = 'default';
                    throw 'Ajax failed';
                }
            });
        },
        IncrementDecrementSuccess: function(number, way) {
            return function(transport) {
                document.body.style.cursor = 'default';
                var response = transport.responseText || "no response text";
                var cartInfo = transport.responseXML.getElementsByTagName('cart-info')[0];
                var newPrice = 0;
                var newTaxedPrice = 0;
                var newProductsCount = 0;
                for (var i = 0; i < cartInfo.childNodes.length; i++) {
                    child = cartInfo.childNodes[i];

                    if (child && child.nodeType == 1) {
                        if (child.nodeName == 'price') {
                            if (child.childNodes[0].nodeValue) {
                                newPrice = parseFloat(child.childNodes[0].nodeValue);
                            }
                            else {
                                throw 'Browser not supported';
                            }
                        }
                        else if (child.nodeName == 'taxed-price') {
                            if (child.childNodes[0].nodeValue) {
                                newTaxedPrice = parseFloat(child.childNodes[0].nodeValue);
                            }
                            else {
                                throw 'Browser not supported';
                            }
                        }
                        else if (child.nodeName == 'count-products') {
                            if (child.childNodes[0].nodeValue) {
                                newProductsCount = parseInt(child.childNodes[0].nodeValue);
                            }
                            else {
                                throw 'Browser not supported';
                            }
                        }
                    }
                }

                var cartPrice = $('cartPrice');
                var cartCount = $('cartCount');
                var inCartPrice = $('inCartPrice');
                var inCartTax = $('inCartTax');
                var inCartTaxedPrice = $('inCartTaxedPrice');
                if (cartPrice) {
                    if (cartPrice.textContent) {
                        cartPrice.textContent = newPrice.toFixed(2);
                    }
                    else if (cartPrice.innerText) {
                        cartPrice.innerText = newPrice.toFixed(2);
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }
                if (cartCount) {
                    if (cartCount.textContent) {
                        cartCount.textContent = newProductsCount;
                    }
                    else if (cartPrice.innerText) {
                        cartCount.innerText = newProductsCount;
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }
                if (inCartPrice) {
                    if (inCartPrice.textContent) {
                        inCartPrice.textContent = newPrice.toFixed(2);
                    }
                    else if (inCartPrice.innerText) {
                        inCartPrice.innerText = newPrice.toFixed(2);
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }
                if (inCartTaxedPrice) {
                    if (inCartTaxedPrice.textContent) {
                        inCartTaxedPrice.textContent = newTaxedPrice.toFixed(2);
                    }
                    else if (inCartTaxedPrice.innerText) {
                        inCartTaxedPrice.innerText = newTaxedPrice.toFixed(2);
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }
                if (inCartTax) {
                    if (inCartTax.textContent) {
                        inCartTax.textContent = (newTaxedPrice - newPrice).toFixed(2);
                    }
                    else if (inCartTax.innerText) {
                        inCartTax.innerText = (newTaxedPrice - newPrice).toFixed(2);
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }

                var productCount = $('cart-span' + number);
                if (productCount) {
                    if (productCount.value) {
                        productCount.value = parseInt(productCount.value) + 1 * way;
                    }
                    else {
                        throw 'Browser not supported';
                    }
                }
            }
        },

        FadeEffect: null,

        ShowMessage: function() {
            if (this.FadeEffect) this.FadeEffect.cancel();
            $('cartAddMessage').style.display = 'block';
            Element.setOpacity($('cartAddMessage'), 1.0);
            this.FadeEffect = new Effect.Fade('cartAddMessage', {
                duration: 1.0,
                delay: 3.0
            });
        }
    }
}

Event.observe(window, 'load', Soleo.Cart.OnLoad);
