// aggancio la richiesta via ajax sui link del carrello
window.addEvent('domready', function(){
  $$('.qtabox').setStyle('display', 'inline');

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

      var request_url = this.getProperty('href')+'&'+$time();
      var id_libro = this.getProperty('id').replace('cart_', '');

        if ($('format_'+id_libro))
          {
          var format_value = $('format_'+id_libro).get('value');

          // controllo il formato: se non è stato scelto nessuno
          // mostro un avviso e interrompo l'azione
          if (format_value == "")
            {
            // evidenzio l'input
            var color_from = '#f00';
            var color_to = '#fff';
            var flash = new Fx.Tween($('format_'+id_libro), {duration: 500, link: 'chain'});
            for(x = 1; x <= 5; x++)
              flash.start('background-color',color_to,color_from).start('background-color',color_from,color_to);

            overlay.msg('<p>Attenzione!</p><p class="small"><strong>Devi scegliere il formato dell\'eBook</strong></p><a href="javascript:overlay.msg_hide();">Chiudi</a>');
            return;
            }

          request_url += '&format='+ format_value;
          }

      if ($('qta_'+id_libro))
        request_url += '&qta='+ $('qta_'+id_libro).getProperty('value');

      var xrequest = new Request.JSON({
        url : request_url,
        method : 'post',
        encoding : 'iso-8859-1',
        headers: {'X-Request': 'JSON'},
        onRequest : function(){
          overlay.show();
        },
        onComplete : function(response, responseText){
          if (!response)
            alert(responseText);
          
          $('cart-totale').set('html', response.totale);
          $('cart-articoli').set('html', response.articoli);
          $('cart-titoli').set('html', response.titoli);

          overlay.msg('<p>Articolo aggiunto nel carrello</p><p class="small"><a href="' + common_site_url() +'/carrello.php">Vai al carrello</a> | <a href="javascript:overlay.msg_hide();">Prosegui l\'acquisto</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(){
    if (this.o)
      this.o.setStyle('display', 'none');
  },

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

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

});
