Ajaxify.handle('form#product_addtocart_form',function(oRef){
   if(productAddToCartForm){
       productAddToCartForm.submit=function() {
           if(this.validator.validate()) {
               Ajaxify.Request.send(this.form.action,function(data){},this.form.serialize());
               return false;
           }
       }
   }
});

Ajaxify.handle('form.product_addtocart_form', function(oRef){
    oRef.submit=function() {
        var form = new VarienForm(oRef.id);
        if(form.validator.validate()) {
            Ajaxify.Request.send(oRef.action,function(data){},oRef.serialize());
            return false;
        } else {
            
        }
    }
});

Ajaxify.Event.register('ajax_request_complete',function(response){
    var oResponse = response.responseText.evalJSON();
    
    if (oResponse.ajaxRequestId == 'dressingroom')
        if(oResponse.error) {
            $('drag-and-drop-error').update('<span style="display: inline;color: red; font-weight: bold">'+oResponse.error+'</span>');
            $('wishlist-product-' + oResponse.productId).down('td.buttons').removeClassName('buttons-clicked');
            return false;
        }
        else {
            var oldItemId = 'wishlist-item-' + oResponse.productId;
            var itemId = 'cart-item-' + oResponse.productId + '-' + oResponse.inCartId;
            var imgItem = $(oldItemId); 
            imgItem.id= itemId;
            imgItem.previous('span').update('<span class="in-cart"></span>');
            imgItem.next('form').remove();
            $$('img[data-id=' + oldItemId + ']')[0].writeAttribute({'data-id': itemId});
            $('wishlist-product-' + oResponse.productId).writeAttribute({'id': 'cart-product-' + oResponse.productId, 'data-id': itemId, 'class': 'cart-product'}).down('td.buttons').removeClassName('buttons-clicked').update('<span class="in-cart"></span>');
        }
    
    if (oResponse.error && oResponse.ajaxRequestId == 'intermission') {
        $('messages_product_view').update('<span style="color: red; font-weight: bold">'+oResponse.error+'</span>');
        return false;
    }

    // update minicart content
    $$('.header-shopping-bag')[0].remove();
    $$('.header-top-container')[0].insert({bottom: oResponse.minicart});
    pngFix();
    if (!oResponse.ajaxRequestId) {
        return false;
    }
    toggleCartOnclick();
    if (oResponse.ajaxRequestId == 'quicklook') {
        if ($('messages_product_view')) {
            $('messages_product_view').update('');
        }
        toggleCart();
        if (BaobazQuicklook) {
            BaobazQuicklook.hideQL();
        }
        return false;
    }
    if (oResponse.ajaxRequestId == 'intermission') {
        if (oResponse.productType == 'configurable') {
            $$('.product-size dd').invoke('hide');
            $('attr-'+oResponse.attrId+'-size-'+oResponse.attrValue).up('dd').show();
        } else if (oResponse.productType == 'bundle') {
            var bundlePrice = 0;
            $$('.product-data .product-selections').invoke('hide');
            $$('.product-options').invoke('hide');
            for (var option_id in oResponse.params['bundle_option_qty']) {
                if (oResponse.params['bundle_option_qty'][option_id] && oResponse.params['bundle_option_qty'][option_id] != 0) {
                    var optionPrice = new Number(oResponse.optionPrices[option_id]).toFixed(2);
                    bundlePrice += parseFloat(optionPrice);
                    $('option-'+option_id+'-sel-'+oResponse.params['bundle_option'][option_id]).show();
                    $('option-'+option_id).show();
                    $('product-selected-options-'+option_id).show();
                    $('option-'+option_id).innerHTML = oResponse.optionNames[option_id];
                    if (oResponse.optionColors[option_id]) {
                        $('option-'+option_id).next('.product-selected-options').down('.product-color span').setStyle({backgroundColor: oResponse.optionColors[option_id]});
                    } else {
                        $('option-'+option_id).next('.product-selected-options').down('.product-color').hide();
                    }
                }
            }
            bundlePrice = new Number(bundlePrice);
            bundlePrice = bundlePrice.toFixed(2).replace('.', ',');
            $$('.product-data .price-box .price')[0].innerHTML = bundlePrice+' €';
        }  
        
        if ($('messages_product_view')) {
            $('messages_product_view').update('');
        }
        Modalbox.show($('cart-intermission'), {title: '', width: 548}); 
             
        return false;
    }
});


