var Cart = Class.create(
{
    initialize: function()
    {
        // add the delete handler here for those delete links because those delete links are regenerated every time an
        // item is added/removed
        /*$$('.del').each(function(link)
        {
            link.observe(
                'click',
                function() {
                    var itemId = link.id.replace('removeFromCart_', '');
                    //alert(itemId);
                    this.remove(itemId);
                }.bind(this)
            );
            link.onclick = function() { return false; }
        }.bind(this));
        */
        // same deal with the currency links
        $$('.CartCurrency').each(function(link)
        {
            link.observe(
                'click',
                function() {
                    var country = link.id.replace('currency_', '');
                    this.changeCurrency(country);
                }.bind(this)
            );
            link.onclick = function() { return false; }
        }.bind(this));


        var protocol = (("https:" == document.location.protocol) ? "https:" : "http:");
        this._scriptUrl = protocol + '//' + window.location.hostname;
    },

    add: function(id, token, ns)
    {

        buybutton = 'addToCartImageButton_' + id;
        buymessage = 'addToCartMessage_' + id;

        $(buymessage).setStyle('color: #F00');
        $(buymessage).update('Item added to your cart!');

        new Effect.Morph(buymessage, {
          style: 'color: #AAA;', // CSS Properties
          duration: 0.5, // Core Effect properties
          delay: 3
        });

        ns = (typeof ns == 'undefined') ? 'add' : ns;
        var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/add?ItemId=' + id + '&Token=' + token + '&Ns=' + ns;
        //alert(document.body);
        document.body.appendChild(script);
    },

    addFromHomePage: function(id, token, drug)
    {
        var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/addfromhome?ItemId=' + id + '&Token=' + token + '&Drug=' + drug;
        document.body.appendChild(script);
    },
    
    loadPageAfterHomeCartAdd: function(message)
    {
        if(message.drug != '')
        {
            window.location = this._scriptUrl + '/' + message.drug;
        }
        else
        {
            window.location = this._scriptUrl + '/cart';
        }
    },

    addFromComparisonGrid: function(id, token)
    {
        var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/add?ItemId=' + id + '&Token=' + token;
        document.body.appendChild(script);
        //window.location.reload();
    },

    remove: function(id, token)
    {
        var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/remove?ItemId=' + id + '&Token=' + token;
        document.body.appendChild(script);
    },

    refreshCartHeader: function(numberOfItems)
    {
        // update the cart header
        var numberOfItemsText = numberOfItems == 1 ? 'Your cart contains 1 product.' : 'Your cart contains ' + numberOfItems + ' products.';
        $('shoppingCartHeader').update(numberOfItemsText);

    },

    changeCurrency: function(country)
    {
        var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/changecurrency?Country=' + country;
        document.body.appendChild(script);
    },

    refreshCartBlock: function(responseText)
    {
        // update the cart block and remove that cart script
        $('quoteList').update(responseText);
        $('cartScript').remove();

    },

    refreshCartAfterAdd: function(response)
    {
        var itemId = response.id;
        var numberOfItems = response.numberOfItems;
        var responseText = response.result;

        this.refreshCartHeader(numberOfItems);
        this.refreshCartBlock(responseText);
        new Effect.Highlight('cartItem_' + itemId, { duration: 5.0, endcolor: '#f4faea' });
    },

    refreshCartAfterRemoval: function(response)
    {
        var numberOfItems = response.numberOfItems;
        var responseText = response.result;

        this.refreshCartHeader(numberOfItems);
        this.refreshCartBlock(responseText);
    },

    refreshCartAfterCurrencyChange: function(response)
    {
        var responseText = response.result;
        this.refreshCartBlock(responseText);
    }
});

var cart = new Cart();

document.observe('dom:loaded', function()
{
    //cartUtil = new Cart();

    /*$$('.AddToCart').each(function(link)
    {
        link.observe(
            'click',
            function()
            {
                var itemId = this.id.replace('addToCart_', '');
                cartUtil.add(itemId);
            }
        );

        link.onclick = function() { return false; }
    }.bind(this));*/
    $$('.CartCurrency').each(function(link)
    {
        link.observe(
            'click',
            function() {
                var country = link.id.replace('currency_', '');
                cart.changeCurrency(country);
            }.bind(this)
        );
        link.onclick = function() { return false; }
    }.bind(this));
});
