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

Object.extend(EcommerceController,
{
	prodAdded : 0,
	
    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));
            }
        });*/
    },
    
    /** Cart management **/
    
    addToCart: function(productId,qty)
    {
    	this.prodAdded = productId;
        this._addToCart(productId,qty, 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 = "/"; });
        }
        else
        {	var content = "<div>Are you sure you want to empty your cart?<br/><a href='javascript:void(0);' onclick='Modalbox.hide()' style='float: left;'>Cancel</a><a href='javascript:void(0);' onclick='EcommerceController.emptyCart(true);' style='float: right;'>Confirm</a>";
            Modalbox.show(content, {title: "Confirmation required", 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 = "/";
            $('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';
            if(window.parent != window)
            {
            	window.parent.$('cartArticles').update(j.out.qty);
            	window.parent.$('cartPrice').update(p);
            }else{
            	$('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);
            if($('AddActionResult'))
            {
            	elmt = $('small-carousel-provider').getElementsByClassName('addedProd');
            	elmt.each(function(e){
            		e.innerHTML="";
            	});
            	if(j.message=="ok")
            		$('prodAdded'+this.prodAdded).innerHTML = "Product added to your cart";
            	else
            		$('prodAdded'+this.prodAdded).innerHTML = "Error";
            }
        }
    },
    
    _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 = '';
                $('voucherFoot').hide();
            }
            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();
        }
    }
});