// aggancio la richiesta via  AJAX sui link del carrello
window.addEvent('domready', function(){
  $$('.compra').each(function(el){
    el.addEvent('click', function(e){
      e = new Event(e);
      e.preventDefault();

     var xrequest = new Request.JSON({
        url: this.getProperty('href')+'&'+$time(),
        encoding : 'utf8',
          headers: {'X-Request': 'JSON'},
        onRequest : function(){ overlay.show(); },
        onComplete : function(response){
         // aggiorna il riepilogo del carrello visibile in alto nel sito
          $('cart-totale').set('html', response.totale);
          $('cart-articoli').set('html', response.articoli);

          overlay.msg('<p>Articolo aggiunto</p><a href="javascript:overlay.msg_hide();">Scegli altri prodotti</a> | <a href="' +window.site_url+'/carrello.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(3000)
  },

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

});