if(typeof(EcommerceController) == 'undefined')
    EcommerceController = new Object();

Object.extend(EcommerceController,
{
    init: function()
    {
        if($('panier') == null)
            return;
        this._getCartPrice(this._cartUpdated.bind(this, true));
        /*Droppables.add('panier', {greedy: true, accept: 'productPicture', onDrop: this._productDropped.bind(this)});
        $$('.productPicture').each(function(pp)
        {
            var p = pp.up();
            new Draggable(pp, {revert: true, ghosting: true, onEnd: function(){ p._dragged = true; }});
            if(p.tagName == 'A')
            {
                $(p).observe('click', function(e){ if(this._dragged){ e.stop(); this._dragged = false;} }.bindAsEventListener(p));
            }
        });*/
        var p = $('panier');
        p.absolutize();
        Event.observe(window, 'scroll', function()
        {
            var o = document.viewport.getScrollOffsets();
            if(o[1] > 21)
            {
                p.style.top = o[1]+'px';
            }
            else
            {
                p.style.top = '21px';
            }
            
        });
    },
    
    /** Cart management **/
    
    addToCart: function(productId, qty)
    {
    	if(qty){
    		this._addToCart(productId,qty, this._cartUpdated.bind(this, false));
    	}else{
    		this._addToCart(productId, this._cartUpdated.bind(this, false));
    	}
        
    },
    
    updateCart: function(productId, qty)
    {
        this._updateCart(productId, qty, this._cartUpdated.bind(this, true));
    },
    
    emptyCart: function(empty)
    {
        if(empty)
        {
            this._emptyCart(function(){ document.location = SiteStrings.homeURL; });
        }
        else
        {
            Modalbox.show(SiteStrings.confirmEmptyCart, {title: SiteStrings.cartEmptyTitle, width: 400, height: 100});
        }
    },
    
    removeFromCart: function(productId)
    {
        this._removeFromCart(productId, this._productRemoved.bind(this, productId));
    },
    
    _productRemoved: function(productId, r, j)
    {
        if(j && !j.failed)
        {
            if(j.out.qty == null || j.out.qty == 0)
                document.location = SiteStrings.homeURL;
            $('productRow-' + productId).remove();
            this._cartUpdated(true, r, j);
        }
    },
    
    _cartUpdated: function(init, r, j)
    {
        if(j && !j.failed)
        {
            if(j.out.qty == null || j.out.price == null)
                j.out.price = j.out.qty = 0;
            var p = (j.out.price / 100).toString();
            var di = p.indexOf('.');
            if(di < 0)
                p += '.00';
            else if(di > p.length - 3)
                p += '0';
            $('cartArticles').update(j.out.qty);
            $('cartPrice').update(p);
            if(!init)
                Modalbox.show(SiteStrings.productAddedToCart, {title: SiteStrings.productAddedTitle, width: 400, height: 100});
            if($('totalCartPrice'))
                $('totalCartPrice').update(p);
        }
    },
    
    _productDropped: function(dragged, dropped, e)
    {
        if(dragged.hasClassName('productPicture'))
        {
            var pid = dragged.id.gsub(/productPicture-/,'');
            this.addToCart(pid);
        }
    },
    
    /** Vouchers management **/
    
    addVoucher: function()
    {
        this._addVoucher($F('newVoucher'), this._voucherAdded.bind(this));
    },
    
    _voucherAdded: function(r, j)
    {
        if(j && !j.failed)
        {
            if(j.out)
            {
                var str = '<tr id="voucher-' + $F('newVoucher') + '"><td>' + $F('newVoucher')+'</td><td>';
                str += j.out.valueToDeduct + ((j.out.discountType == 'cashDiscount')?'€':'%');
                str += '</td><td><img src="/images/commun/delete.png" onclick="EcommerceController.removeVoucher(\'' + $F('newVoucher') + '\')"/></td></tr>';
                $('voucherList').insert(str);
                $('newVoucher').value = '';
            }
            else
                Modalbox.show(SiteStrings.invalidVoucher, {title: SiteStrings.error, width: 400, height: 100});
        }
    },
    
    removeVoucher: function(code)
    {
        this._removeVoucher(code, this._voucherRemoved.bind(this));
    },
    
    _voucherRemoved: function(r, j)
    {
        if(j && !j.failed)
        {
            $('voucher-'+j.out).remove();
            $('voucherFoot').show();
        }
    }
});