// aggancio la richiesta via ajax sui link del carrello
window.addEvent('domready', function(){
  // carrello ajax solo se abbiamo il carrello visuale in colonna
  if (!$('cart'))
    return;

  $$('.compra').each(function(el){
    el.addEvent('submit', function(e){
      e = new Event(e);
      e.preventDefault();

      this.set('send', {
        url : this.getProperty('action'),
        encoding : 'utf8',
        headers: {'X-Request': 'Ajax'},
        onRequest : function(){ overlay.show(); },
        onComplete : function(response){
          // se esiste il blocco carrello lo sostituisco
          var cart_wrapper = $('cart');
          var col_wrapper = $('s-dx');

          if (cart_wrapper){
            cart_wrapper.destroy();
            old_content = col_wrapper.get('html');
            col_wrapper.set('html', response, old_content);
          }

          overlay.msg('<p>Articolo aggiunto</p><a href="javascript:overlay.msg_hide();">Scegli altri prodotti</a> | <a href="' + window.site_url +'cart.php">Completa l\'ordine</a></p>');
        }
      }).send();
    });
  });
});

var overlay = new Hash({
  show : function(){
    if (!this.o)
      this.o = new Element('div', {'id': 'overlay'}).injectInside(document.body);

    this.o.setStyles({'display': 'block', 'opacity': 0.9, 'top': (window.getScrollTop() + (window.getHeight() / 3))});
  },

  hide : function(){
    this.o.setStyle('display', 'none');
  },

  msg : function(message){
    if (!this.m)
      this.m = new Element('div', {'id': 'overlay-msg'}).injectInside(document.body);

    this.hide();
    this.m.setStyles({'opacity': 0.9, 'top': (window.getScrollTop() + (window.getHeight() / 3))});
    this.m.set('html', '<br />' + message);
    (function(){ this.msg_hide() }).bind(this).delay(5000)
  },

  msg_hide : function(){
    this.m.fade('out');
  }

});
