var theViewer=false;
var mouseDistance=[10,10];
var fadeInterval=0;

function createThumbDiv (thumbFader){
  clearTimeout(thumbFader);
  var thumbDiv = createEl("DIV");
  thumbDiv.style.position = "absolute";
  thumbDiv.style.display = "none";
  thumbDiv.className = "thumbViewer";
  thumbDiv.id = "thumbViewer";
  thumbDiv.name = "thumbViewer";
  thumbDiv.style.zIndex = 2000;
  if (!isExplorer)
    thumbDiv.style.opacity = .1;
  else
    thumbDiv.style.filter="alpha(opacity:"+0+")";
  thumbDiv.style.top=mouseY+mouseDistance[0]+"px";
  thumbDiv.style.left=mouseX+mouseDistance[1]+"px";
  document.body.appendChild(thumbDiv);
  theViewer=obj('thumbViewer');
//  fadeInterval=0;
//  var objOpacity=.96;
//  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 (!imageWidth)
  //    imageWidth = 300;
  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;
/*
*   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){
  //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)",100);
}

function failed(){
  alert("failed to load image: "+IMG_URL);
}

var clearPreview;
IMGOBJ = new Image();

function showPreviewOnClick(imgUrl, title, imageWidth, pfSerialized){
  if(theViewer)
    document.body.removeChild(theViewer);
  if (!contextPath)
    contextPath = '';

  createThumbDiv();
  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>';
    //alert(imgPreview)
    theViewer.innerHTML = imgPreview;
    IMGOBJ.onload = function (){
       setTimeout("setPosPreviewOnClick(theViewer)",800);
    }
    IMGOBJ.onerror = failed;
    IMGOBJ.src =contextPath + IMG_URL;


  } else {
    var slash = "";
    if (contextPath && contextPath.length>0)
      slash = "/";

    IMG_URL=contextPath+slash+imgUrl;
    IMGOBJ.onload = function (){
      startShowPreview(imageWidth, title);
    }
    IMGOBJ.onerror = failed;
    IMGOBJ.src =IMG_URL;
    //startShowPreview(imageWidth, title);
  }
}


function setPosPreviewOnClick(theViewer){
 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;
      var objOpacity=.96;
     faderIn(theViewer.id, objOpacity);

    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";
    addEvent(document,"mousedown",hidePreview);
  }
  document.body.style.overflow="auto";

}

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);
  }

}
