var upController = true;
var downController = true;
var downWaitTime = 200;
var upWaitTime = 200;
var fadeInController = false;
var currentOpacity = 0;
var currentBoxScroll = 0;


if (window.addEventListener)  // MOZILLA FireForx
  window.addEventListener('DOMMouseScroll', mouseWheel, false);

function mouseWheel(event)
{
  //alert(event.detail);
  var target;
  target = event.target;
  
  while (target!=null) {
    if (target.id=='box') {
      target = null;
      //alert ('SONO DENTRO IL BOX!');
      scrollBox(event.detail*20)
      return false;
    }
    else
      target = target.parentNode;
  }
  return true;
}


function scrollBox(offset)
{
  var box = document.getElementById('box');

  currentBoxScroll = box.scrollTop+offset
  box.scrollTop=currentBoxScroll;
}

function startScroll(dir)
{
  if (dir=='up') {
    upController = true;
    downController = false;
    upWaitTime = 200;
    scrollUp();
  } else {
    downController = true;
    upController = false;
    downWaitTime = 200;
    scrollDown();
  }
}

function scrollUp()
{
  if(upController) {
    scrollBox(-40);
    setTimeout('scrollUp()', upWaitTime);
    //upWaitTime = upWaitTime / 2
  }
}

function scrollDown()
{
  if(downController) {
    scrollBox(40);
    setTimeout('scrollDown()', downWaitTime);
    //downWaitTime = downWaitTime / 2;
  }
}

function stopScroll(dir)
{
  if (dir=='up') {
    upController = false;
  } else {
    downController = false;
  }
}

function setScroll()
{
  var box = document.getElementById('box');
  var box2 = document.getElementById('box2');
  var td1 = document.getElementById('td1');
  var td2 = document.getElementById('td2');
  var arrowUp = document.getElementById('arrowUp');
  var arrowDown = document.getElementById('arrowDown');
  var arrowsHeight = 0;
  var boxHeight = 0;

  /* per FireFox - imposta l'altezza del riquadro centrale al 40% della pagina */
  boxHeight = document.body.clientHeight*4/10;
  if (boxHeight<220)
    boxHeight = 220;
  box.style.maxHeight = boxHeight;
  box.style.minHeight = boxHeight;
  box2.style.maxHeight = boxHeight;
  box2.style.minHeight = boxHeight;
  /* FireFox - imposta l'altezza delle 2 righe della pagina dove&quando */
  if (td1!=null)
    td1.height = boxHeight/100*75 - 5;
  if (td2!=null)
  td2.height = boxHeight/100*25 - 5;

  /*if (arrowUp.style.visibility=='')
    arrowsHeight = 6;
  if (arrowDown.style.visibility=='')
    arrowsHeight = arrowsHeight + 6;*/

  if ( box.scrollHeight>(box.offsetHeight+arrowsHeight) ) {
    arrowUp.style.visibility = '';
    arrowDown.style.visibility = '';
  } else {
    arrowUp.style.visibility  = 'hidden';
    arrowDown.style.visibility = 'hidden';
  }
}

function showLargePhoto(photo,w2h)
{
  var box = document.getElementById('box');
  var contentText = document.getElementById('contentText');
  var bigPhoto = document.getElementById(photo);
  var bigPhotoHeight = 0;
  var bigPhotoWidth = 0;

  //setOpacity(box,25)

  bigPhotoHeight = box.offsetHeight
  bigPhotoWidth = (bigPhotoHeight*w2h);
  bigPhoto.style.width = bigPhotoWidth+'px';
  bigPhoto.style.height = bigPhotoHeight+'px';
  //bigPhoto.src = photo;

  // imposto il bordo sx/dx
  //bigPhoto.style.left = ((document.body.clientWidth-box.offsetWidth)/2) + (box.offsetWidth-bigPhotoWidth)/2;

  
  document.getElementById('bigPhoto1').style.display = 'none';
  document.getElementById('bigPhoto2').style.display = 'none';
  document.getElementById('bigPhoto3').style.display = 'none';

  setOpacity(bigPhoto,0);
  bigPhoto.style.display = '';
  contentText.style.display = 'none';

  box.scrollTop = 0;


  fadeInController = true;
  fadeIn(photo,20);
}

function hideLargePhoto(photo)
{
  //var contentText = document.getElementById('contentText');
  //var bigPhoto = document.getElementById(photo);

  //setOpacity(box,100)
  //box.style.visibility = '';

  fadeInController = false;
  fadeOut(photo,currentOpacity);
}


function setOpacity(obj,opacity){
  opacity=(opacity==100) ? 99.999 : opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
} 

function fadeIn(objId,opacity) {

  if (fadeInController) {
    if(!document.getElementById) return;
    obj = document.getElementById(objId);
  
    if (opacity <= 100) {
      setOpacity(obj,opacity);
      currentOpacity = opacity;
      opacity += 20;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 25);
    }
  }
}

function fadeOut(objId,opacity) {

  if (!fadeInController) {
    if(!document.getElementById) return;
    obj = document.getElementById(objId);
  
    if (opacity >= 0) {
      setOpacity(obj,opacity);
      opacity -= 20;
    }
    if (opacity > 0)
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 25);
    else {
      var box = document.getElementById('box');
      var contentText = document.getElementById('contentText');

      obj.style.display = 'none';
      contentText.style.display = '';
      box.scrollTop=currentBoxScroll;
      box.scrollTop=currentBoxScroll;
    }
  }
} 

