var theViewer=false;
var mouseDistance=[10,10];
var fadeInterval=0;
var thumbFader;
function createThumbDiv (centerOnWindow) {
  clearTimeout(thumbFader);
  var thumbDiv = createEl("DIV");
  thumbDiv.style.position = "absolute";
  thumbDiv.style.display = "";
  thumbDiv.className = "thumbViewer";
  thumbDiv.id = "thumbViewer";
  thumbDiv.name = "thumbViewer";
  thumbDiv.style.zIndex = 2000;
  if (!isExplorer)
    thumbDiv.style.opacity = .0;
  else
    thumbDiv.style.filter="alpha(opacity:"+0+")";
  if(!centerOnWindow){
    thumbDiv.style.top=mouseY+mouseDistance[0]+"px";
    thumbDiv.style.left=mouseX+mouseDistance[1]+"px";
  }else{
    centerObject(thumbDiv.id)
  }
  document.body.appendChild(thumbDiv);
  theViewer=obj('thumbViewer');
}

var fader=.1;
function faderIn(objToFade, objOpacity){
  if (fadeInterval<=10){
    fader=(objOpacity/10)*fadeInterval;
    fadeInterval++;
    thumbFader=setTimeout("faderIn('"+objToFade+"', "+objOpacity+")",0);
  }else
    fadeInterval=0;
  if (obj(objToFade)) {
    if (!isExplorer)
      obj(objToFade).style.opacity=fader;
    else
      obj(objToFade).style.filter="alpha(opacity:"+fader*100+")";
  }
}

function hidePreview(){
  removeEvent(document,"mousemove",followmouse);
  if (theViewer)
    document.body.removeChild(theViewer);
  theViewer=false;
  removeEvent(document,"mousedown",hidePreview);
}

function followmouse(e){
  var distanceFromMouse=30;
  if(theViewer){
    var xcoord=mouseDistance[0];
    var ycoord=mouseDistance[1];
    if ((getWindowDimensionW()+truebody().scrollLeft) - mouseX < theViewer.offsetWidth+25) {
      var diff=(mouseX+theViewer.offsetWidth+25)-(getWindowDimensionW() +truebody().scrollLeft);
      xcoord = mouseX - (theViewer.offsetWidth+mouseDistance[0])-distanceFromMouse ;
    } else {
      xcoord += mouseX+distanceFromMouse;
    }
    if ((getWindowDimensionH() +truebody().scrollTop) < mouseY+theViewer.offsetHeight+25){
      var diff=(mouseY+theViewer.offsetHeight+25)-(getWindowDimensionH() +truebody().scrollTop);
      ycoord = mouseY - diff+distanceFromMouse;
    } else {
      ycoord +=  mouseY+distanceFromMouse;
    }
    if(ycoord < 0) { ycoord = ycoord*-1; }

    theViewer.style.left=xcoord+"px";
    theViewer.style.top=ycoord+"px";
    if (getTarget(e).tagName!="IMG" && getTarget(e).tagName!="SPAN") {
      hidePreview();
    }
  }
}

function closeList(){
  theListViewer.parentNode.removeChild(theListViewer);
}

function createListDiv (){
  var thumbDiv = createEl("DIV");
  thumbDiv.style.position = "absolute";
  thumbDiv.style.display = "";
  thumbDiv.className = "listViewer";
  thumbDiv.id = "listViewer";
  thumbDiv.name = "listViewer";
  thumbDiv.style.zindex = 2000;
  thumbDiv.style.backgroundColor = "white";
  thumbDiv.style.top=mouseY+mouseDistance[0]+"px";
  thumbDiv.style.left=mouseX+mouseDistance[1]+"px";
  document.body.appendChild(thumbDiv);
  theListViewer=obj('listViewer');

  addEvent(document,"click",closeList);
  fadeInterval=0;
}

//previewOnClick;


function showPreviewOnClick(imgUrl, title, imageWidth, pfSerialized,centerOnWindow){
  var IMGOBJ = new Image();
  if(theViewer)
    document.body.removeChild(theViewer);
  if (!contextPath)
    contextPath = '';
  if(!centerOnWindow) centerOnWindow=false;
  createThumbDiv(centerOnWindow);

  if(pfSerialized!=null) {
    //IMG_URL=getContent(contextPath + '/commons/layout/ajaxImgPreview.jsp?FILE_TO_SHOW='+encodeURIComponent(pfSerialized)+'&TITLE='+encodeURIComponent(title)+'&IMGW='+imageWidth);
    IMG_URL=getContent(contextPath + '/commons/layout/ajaxImgPreview.jsp', 'FILE_TO_SHOW='+encodeURIComponent(pfSerialized)+'&TITLE='+encodeURIComponent(title)+'&IMGW='+imageWidth);
    if(!title)
      title="";
    var imgPreview = "<div id='img' align='center' style='padding:10px;padding-bottom:35px; background:white; border:1px solid #cccccc;'>";
    imgPreview = imgPreview + IMG_URL;
    imgPreview = imgPreview +'<div class="imgDescription">'+title+'</div></div>';
    theViewer.innerHTML = imgPreview;
    setTimeout("setPosPreviewOnClick(theViewer,"+centerOnWindow+")",800);

  } else {
    var slash = "";
    if (contextPath && contextPath.length>0)
      slash = "/";

    IMG_URL=contextPath+slash+imgUrl;
    IMGOBJ.onload = function () {
      startShowPreview(imageWidth, title, centerOnWindow);
    };
    IMGOBJ.onerror = failed;
    IMGOBJ.src =IMG_URL;
  }
}

function showPreview(imgUrl, title, imageWidth, pfSerialized,centerOnWindow){
      showPreviewOnClick(imgUrl, title, imageWidth, pfSerialized,centerOnWindow);
}
  
/*
*   10-7-7  Matteo Bicocchi  -- Modificata la gestione della visualizzazione per renderla più fluida e tiene in considerazione il caricamento dell'immagine
*
*/
function startShowPreview(imageWidth, title, centerOnWindow){
  //theViewer.style.visibility="hidden";
  if(!title) title="";
  var imgPreview ="";
  imgPreview =imgPreview + "<div id='imgDisplay' align='center' >";
  if(imageWidth && imageWidth > 0){
    imgPreview = imgPreview + '<img id="imgtoshow" src="' + IMG_URL +'" width="'+imageWidth+'" border="0" style="visibility:visible">';
  }else{
    imgPreview = imgPreview + '<img  id="imgtoshow" src="' + IMG_URL +'" border="0" style="visibility:visible" >';
  }
  var imgClose = "";
  imgClose = "<img class='closeGallery' src='" + contextPath +"/applications/webwork/site_" + installationId + "/media/closeGallery.png'>";
  imgPreview = imgPreview + imgClose +'<div class="imgDescription" id="imgDescription">'+title+'</div></div>';
  theViewer.innerHTML = imgPreview;
  setTimeout("setPosPreviewOnClick(theViewer, "+centerOnWindow+")",100);
}

function failed(){
  alert("failed to load image: "+IMG_URL);
}

function setPosPreviewOnClick(theViewer, centerOnWindow){
  var objOpacity=1;
  if (obj("imgDescription")) obj("imgDescription").style.width= obj("imgtoshow").offsetWidth;
  
 if(!centerOnWindow) {
    if (truebody().scrollHeight<=getWindowDimensionH()) document.body.style.overflow="hidden";
    var add=truebody().scrollHeight<=getWindowDimensionH()?0:20;
    var xcoord=mouseDistance[0];
    var ycoord=mouseDistance[1];
    if (theViewer){
      fadeInterval=0;
      if ((getWindowDimensionW()+truebody().scrollLeft) - mouseX < theViewer.offsetWidth) {
        var diff=(mouseX+theViewer.offsetWidth)-(getWindowDimensionW() +truebody().scrollLeft);
        xcoord = getWindowDimensionW()-theViewer.offsetWidth-add;
      } else {
        xcoord += mouseX;
      }

      if ((getWindowDimensionH() +truebody().scrollTop) < mouseY+theViewer.offsetHeight){
        var diff=(mouseY+theViewer.offsetHeight)-(getWindowDimensionH() +truebody().scrollTop);
        ycoord = mouseY - diff;
      } else {
        ycoord +=  mouseY;
      }

      if(ycoord < 0)
        ycoord = ycoord*-1;

      theViewer.style.left=xcoord+"px";
      theViewer.style.top=ycoord+"px";
    }
    document.body.style.overflow="auto";
  }else{
    centerObject(theViewer.id)
  }
  faderIn(theViewer.id, objOpacity);

  addEvent(document,"mousedown",hidePreview);
}


