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)
    {
		var script = document.createElement('script');
        script.id = 'cartScript';
        script.src = this._scriptUrl + '/cart/add?ItemId=' + id + '&Token=' + token;
        document.body.appendChild(script);
    },
    
    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);
    },
    
    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 responseText = response.result;
        
        this.refreshCartBlock(responseText); 
        new Effect.Highlight('cartItem_' + itemId, { duration: 5.0, endcolor: '#f4faea' });
    },
    
    refreshCartAfterRemoval: function(response)
    {
        var responseText = response.result;
        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));
});