/**
 * @author Abdellah
 */
function abCart(trigger, container){
    $('showConfirm').set('style', 'display:none;');
    action = $(container).action;
    $(trigger).addEvent('click', function(e){
        e.stop();
        new Request.HTML({
            url: action,
            data: $(container).toQueryString() + '&comingFrom=ajax',
            onSuccess: function(html){
                $('confirmCart').set('text', '');
                $('confirmCart').adopt(html);
                box.open($('showConfirm'));
            },
            onRequest: function(){
            
            }
        }).send();
    })
    
    if ($$('ul.examples')) {
        $$('ul.examples a').each(function(link){
            link.addEvent('click', function(e){
                e.stop();
                window.parent.location.href = link.href;
                box.close();
            })
        })
    }
}

function abCart2(triggers, containers){
    triggers = $$(triggers);
    containers = $$(containers);
    
    $('showConfirm').set('style', 'display:none;');
    $$(triggers).each(function(trigger, i){
        action = containers[i].action;
        trigger.addEvent('click', function(e){
            e.stop();
            new Request.HTML({
                url: action,
                data: containers[i].toQueryString() + '&comingFrom=ajax',
                onSuccess: function(html){
                    $('confirmCart').set('text', '');
                    $('confirmCart').adopt(html);
                    box.open($('showConfirm'));
                },
                onRequest: function(){
                
                }
            }).send();
        })
    })
    
    if ($$('ul.examples')) {
        $$('ul.examples a').each(function(link){
            link.addEvent('click', function(e){
                e.stop();
                window.parent.location.href = link.href;
                box.close();
            })
        })
    }
}

function abCart3(){
    triggersUp = $$('.cartUp', '.cartDown', '.cartRemove');
    qtyHolders = $$('.quantity');
    priceHolders = $$('.cartPrice');
    totalPriceHolder = $('cartTotal');
    totalPriceShipHolder = $('cartPriceShip');
    totalPriceAllHolder = $('cartPriceAll');
    
    triggersUp.each(function(trigger, i){
        trigger.addEvent('click', function(e){
            e.stop();
            //remise a zero des select
            if ($('choix-type')) $('choix-type').value=0;
            if ($('choix-pays')) $('choix-pays').value=0;
            cartHolder = $('cartRow-' + trigger.rel);
            priceHolder = $('cartPrice-' + trigger.rel);
            priceHtHolder = $('cartPriceHt-' + trigger.rel);
            tvaHolder = $('cartTva-' + trigger.rel);
            qtyHolder = $('cartQty-' + trigger.rel);
            var cartFx = new Fx.Tween(cartHolder, {
                duration: 1000
            });
            cartHide = function(){
                cartHolder.destroy();
                var cartRows = $$('.cartRow');
                //si plus aucun produit ds panier, afficher la notification
                if (cartRows.length <= 0) {
                    $('cartContent').fade(1, 0);
                    $('cartContent').destroy();
                    $('emptyCartMsg').removeClass('hidden').fade(0, 1);
                }
            };
            new Request.JSON({
                url: trigger.href,
                data: 'updateQty[' + trigger.rel + ']=' + trigger.rev + '&comingFrom=ajax',
                onSuccess: function(response){
            		selectPaysChange();
            		if (response.prodQty > 0) {
                        qtyHolder.set('text', response.prodQty);
                        priceHolder.set('text', response.prodPrice);
                        priceHtHolder.set('text', response.prodPriceHt);
                        tvaHolder.set('text', response.prodPriceTva);
                        totalPriceHolder.set('text', response.totalPrice);
                        totalPriceShipHolder.set('text', response.totalPriceShip);
                        totalPriceAllHolder.set('text', (parseFloat(response.totalPrice) + parseFloat(response.totalPriceShip)).toFixed(2));
                    }
                    else {
                        totalPriceHolder.set('text', response.totalPrice);
                        totalPriceShipHolder.set('text', response.totalPriceShip);
                        totalPriceAllHolder.set('text', (parseFloat(response.totalPrice ) + parseFloat(response.totalPriceShip)).toFixed(2));
                        cartFx.start('opacity', 1, 0);
                        cartHide.delay(1000);
                    }
                    if(response.totalProducts > 0) {
						$('myCart').getElement('a').set('html', 'Mon panier : <em> ' + response.totalProducts + ' article(s)</em>');
					}
					else {
						$('myCart').getElement('a').set('html', 'Mon panier : <em> 0 articles</em>');
					}
                	trigger.removeClass('working');
                },
                onRequest: function(){
                	trigger.addClass('working');
                }
            }).send();
        })
    })
}

function abFreeShipping(){
	if($type($('SlideRight')) == 'element'){
		Element.Events.ctrlShiftClick = {
			base: 'click',
			condition: function(e){
				return (e.control && e.shift);
			}
		}; 		
		new Element('a', {
			id: 'abFreeShipping',
			style: 'position:absolute;display:block;width:50px;height:50px;right:0;bottom:0;cursor:default;',
			href: '?stopfraisport=1'
		}).addEvent('ctrlShiftClick', function(e){}).inject($('SlideRight'));
	}
}
