var allowCart = true;
function displayCart(shoppingBag)
{
    if (allowCart == true){
        allowCart = false;
        if (shoppingBag.visible()){
            new Effect.BlindUp(shoppingBag, { duration:0.2, afterFinish: function(){
                allowCart = true;
            }})
        }else{
            new Effect.BlindDown(shoppingBag, { duration:0.2, afterFinish: function(){
                allowCart = true;
            }})
        }
    }
}

document.observe('dom:loaded', function(){

    // clients fader
    setInterval(function(){
        var active = $$('#clients > li.active')[0]
        var next = active.next('li')
        if (!next) next = $('clients').firstDescendant()
        new Effect.Fade(active,{ duration:1, afterFinish: function(){
            active.removeClassName('active')
        }})
        new Effect.Appear(next,{ duration:1, afterFinish: function(){
            next.addClassName('active')
        }})
    }, 6000)

    // shopping bag dropdown
    $('shoppingBagDropdown').observe('click', function(e){
        displayCart($('shoppingBag'))
        e.stop()
    })

    // submit search on ENTER
    var search = $('search');
    if (search){
        search.observe('keypress', function(event){
            if (this.value.length > 0){
                var key = event.which || event.keyCode;
                if (key == 13){
                    $('search_mini_form').submit();
                }
            }
        })
    }

})

Event.observe(window, 'load', function(){
    
    // OGONE cancel order
    var ogone = $$('body.ogone-api-paypage')[0];
    if (ogone){
        var tableRow = $$('#ncol_ref tbody tr')[0];
        if (tableRow){
            var tableCell = tableRow.childElements()[1];
            if (tableCell){
                var orderIdBox = tableCell.down('b')
                if (orderIdBox){
                    var orderId = orderIdBox.innerHTML;
                    if (orderId && isNaN(orderId) == false){
                        var cancelUrl = 'https://www.mijnsmoking.nl/ogone/api/cancelOrder/?orderID=' + orderId;
                        $$('a[href]').each(function(link){
                            var newUrl = cancelUrl + '&redirectURL=' + encodeURIComponent(link.href);
                            link.href = newUrl;
                        })
                    }
                }
            }
        }
    }
    
})
