document.observe ('dom:loaded', intercept_buying_process);

function intercept_buying_process (e)
{
    $('column_container').select ('a[href*=nl/toevoegen]').invoke ('observe', 'click', ajax_buy);
}

function ajax_buy (e)
{    
    // get the link
    if (e.element().nodeName == 'img' || e.element().nodeName == 'IMG')
    {
        var link = e.element().up('a');
    } else if (e.element().nodeName == 'a' || e.element().nodeName == 'A')  {
        var link = e.element();
    }
    e.stop();
    
    if (link)
    {
        // do not go to requested page
        e.stop();
        
        // instead of opening the link, we do the call with ajax
        new Ajax.Request(link.href, {
            method: 'get',
            onSuccess: buySuccess,
            asynchronous: false
          });
    }
    
    // if we haven 't found a link tag, then just do the buying manually

}

// call this when request was successfull
function buySuccess (response)
{
    
    // als $('cart_confirm') bestaat, vervangen en tonen
    if (!$('cart_confirm'))
    {
        
        // build the cart_confirm div
        var tpl = new Template ('<div class="mid_table_noborder" id="cart_confirm"></div>');
        var cart_confirm = tpl.evaluate('');
        $('column_mid').insert({ top: cart_confirm });
    }
    
    // insert the content
    $('cart_confirm').update (response.responseText);
    $('cart_confirm').show();
    
    // update the contents of the cart block on the right
    new Ajax.Updater('cart_replacable_block', '/nl/cart/update', {});

    // fade the div after 6 seconds
    Effect.Fade('cart_confirm', { delay: 6, duration: 0.5 });
    
    

}