
function apriImg(id){
  $('oscura').setOpacity(0.5);
  $('oscura').show();
  loadImg(id);
}

function loadImg(id, aggiorna){
  new Ajax.Request('getData.php', {
    method: 'post',
    parameters: "id="+id,
    onSuccess: function(transport) {
      //alert(transport.responseText);
      obj = transport.responseJSON;
      $('visore_nome').innerHTML = charaReplace(obj.nome);
      $('visore_testo').innerHTML = charaReplace(obj.testo);
      email = Builder.node('a', { 'href': 'mailto:info@lafatadiavalon.it?subject=Richiesta di informazioni per '+obj.nome }, 'per richiedere informazioni clicca qui');
      $('visore_info').appendChild(email);
      lista = $$('.llista');
      for(i=0; i < lista.length; i++){
        if(lista[i].rel == id){
          img_cur = i+1;
          if(i > 0) img_pre = lista[i-1].rel;
          else img_pre = false;
          if(i < lista.length-1) img_pro = lista[i+1].rel;
          else img_pro = false;
        }
      }
      nav = Builder.node('span', 'Prodotto '+img_cur+' di '+lista.length+' ');
      if (img_pre) {
        pre = Builder.node('a', {
          'href': 'javascript: void(0)',
          'onclick': 'aggiornaImg(' + img_pre + ')'
        }, '< precedente');
        nav.appendChild(pre);
      }
      spazio = Builder.node('span', ' ');
      nav.appendChild(spazio);
      if (img_pro) {
        pro = Builder.node('a', {
          'href': 'javascript: void(0)',
          'onclick': 'aggiornaImg(' + img_pro + ')'
        }, 'successivo >');
        nav.appendChild(pro);
      }
      $('visore_nav').appendChild(nav);
      ima = new Image();
      ima.onload = function(){
        $('visore_immagine').src = ima.src;
        dim = $('oscura').getDimensions();
        dimv = $('visore_prodotti').getDimensions();
        x = (dim.width - 900) / 2;
        y = (dim.height - dimv.height) / 2;
        if(y < 10) y = 10;
        if (!aggiorna) {
          $('visore_prodotti').setStyle({
            top: y + 'px',
            //left: x + 'px'
            left: '-10px'
          });
          $('visore_prodotti').appear();
        }else{
          a = $('visore_prodotti').offsetTop;
          if (a != y) {
            newy = y - a;
            //if(y < 10) y = 10;
            new Effect.Move('visore_prodotti', {
              y: newy,
              mode: 'relative'
            });
          }
          $('visore_immagine').setStyle("visibility: visible");
        }
      }
      ima.src = 'img.php?i=galleria/'+obj.immg+'.jpg&d=wh&w=600&h=600';
    }
  })
}

function aggiornaImg(id){
  $('visore_immagine').setStyle("visibility: hidden");
  $('visore_info').innerHTML = "";
  $('visore_nav').innerHTML = "";
  $('visore_nome').innerHTML = "";
  $('visore_testo').innerHTML = "";
  loadImg(id, true);
}

function chiudiImg(){
  $('oscura').hide();
  $('visore_prodotti').hide();
  $('visore_info').innerHTML = "";
  $('visore_nav').innerHTML = "";
}

function sendForm(){
  chkd = true;
  msg = '';
  $$('.obbl').each(function(o){
    if (!o.present()) {
      chkd = false;
      msg = (msg == '') ? 'il campo ' + o.id + ' è obbligatorio' : msg;
    }
  })
  if(!chkd){
    alert(msg);
    return false;
  }
  $('invia').hide();
  $('iscrizione').action = 'invia.php';
  $('iscrizione').submit();
}

function charaReplace(str){
  nl = new RegExp("\n", "g");
  rxp = [nl, /\[\[g\]\]/g, /\[\[\-g\]\]/g, /\[\[i\]\]/g, /\[\[\-i\]\]/g, /\[\[t\]\]/g, /\[\[\-t\]\]/g, /\[\[t2\]\]/g, /\[\[\-t2\]\]/g, /\[\[t3\]\]/g, /\[\[\-t3\]\]/g, /\[\[c-rosso\]\]/g, /\[\[c-grigio\]\]/g, /\[\[\-c\]\]/g, /\[\[l-/g, /\|l\]\]/g, /\[\[m-/g, /\|m\]\]/g, /\[\[\-l\]\]/g, /\[\[\-m\]\]/g, /\[\[nd\]\]/g, /\[\[qm\]\]/g, /\[\[eq\]\]/g, /\[\[vd\]\]/g, /\[\[sh\]\]/g, /\[\[vs\]\]/g, /\[\[pc\]\]/g];
	rep = ["<br />", "<strong>", "</strong>", "<em>", "</em>", "<h1>", "</h1>", "<h2>", "</h2>", "<h3>", "</h3>", "<span style='color: #a00'>", "<span style='color: #888'>", "</span>", "<a href='http://", "' target='_blank'>", "<a href='mailto:", "'>", "</a>", "</a>", "&", "?", "=", "&#34;", "#", "&#39;", "%"];
	for(i = 0; i < rxp.length; i++){
		str = str.replace(rxp[i], rep[i]);
	}
	return str;
}

