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');

  /*  fadeInterval=0;
  var objOpacity=.1;
  faderIn('thumbViewer', objOpacity);
  */
}

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;
}

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;
}

function showPreview(theImage,title,imageWidth){
  var thumbFader;
  var noTitle= "";
  if(!title)
    title=noTitle;
  if(theViewer)
    document.body.removeChild(theViewer);
  if (!contextPath)
    contextPath = '';
  createThumbDiv(thumbFader);
  var imgPreview ="";
  imgPreview += '<div id="img" align="center" style="padding:10px;padding-bottom:35px; background:white; border:1px solid #cccccc;">';
  var widthString = "";
  if(imageWidth) {
    widthString = "width='"+imageWidth+"' ";
  }

  imgPreview += '<img src="' + contextPath + theImage + '" '+widthString+' border="0" style="visibility:visible">';
  imgPreview += '<div class="imgDescription">'+title+'</div></div>';
  theViewer.innerHTML = imgPreview;

  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]) ;
  } else {
    xcoord += mouseX;
  }
  if ((getWindowDimensionH() +truebody().scrollTop) < mouseY+theViewer.offsetHeight+25){
    var diff=(mouseY+theViewer.offsetHeight+25)-(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";
  addEvent(document,"mousemove",followmouse);
}

//previewOnClick;
var clearPreview;
IMGOBJ = new Image();

function showPreviewOnClick(imgUrl, title, imageWidth, pfSerialized,centerOnWindow){
  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);
    if(!title)
      title="";
    var imgPreview = "<div id='img' align='center' style='padding:10px;padding-bottom:35px; background:white; border:1px solid #cccccc;'>";  // onClick='hidePreview();'
    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;
  }
}

/*
*   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='img' align='center' style='padding:10px;padding-bottom:35px; background:white; border:1px solid #cccccc;'>";  // onClick='hidePreview();'
  if(imageWidth)
    imgPreview = imgPreview + '<img  src="' + IMG_URL +'" width="'+imageWidth+'" border="0" style="visibility:visible">';
  else
    imgPreview = imgPreview + '<img  src="' + IMG_URL +'" border="0" style="visibility:visible">';

  imgPreview = imgPreview +'<div class="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(!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){
      theViewer.style.display="";
      fadeInterval=0;
      if ((getWindowDimensionW()+truebody().scrollLeft) - mouseX < theViewer.offsetWidth) {
        var diff=(mouseX+theViewer.offsetWidth)-(getWindowDimensionW() +truebody().scrollLeft);
        //xcoord = mouseX - diff;
        // xcoord = mouseX - ((theViewer.offsetWidth/2)+mouseDistance[0]);
        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);  
}

function hidePreviewOnClick(e){
  if (clearPreview)
    window.clearTimeout(clearPreview);
  if (getTarget(e).tagName!="A" && getTarget(e).tagName!="IMG"){
    clearPreview=window.setTimeout("hidePreview();",400);
    removeEvent(document,"mousemove",followmouse);
  }

}
