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 = "";
  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 videoContent ="";
function showMovie(theMovie,title,autostart, page, w, h){
  //url, nome, w, h, scroll, resiz
  if (!autostart)
    autostart=false;
  if (!w)
    w=480;
  if (!h)
    h=360;
  videoContent ="";
  videoContent +='<object type="application/x-shockwave-flash" data="'+contextPath+'/applications/webwork/parts/flv/flvplayer.swf?autostart='+autostart+'&file='+theMovie+'" width="'+w+'" height="'+h+'" wmode="transparent">';
  videoContent +='<param name="movie" value="'+contextPath+'/applications/webwork/parts/flv/flvplayer.swf?autostart='+autostart+'&file='+theMovie+'" />';
  videoContent +='<param name="wmode" value="transparent" />';
  videoContent +='</object>';
  videoContent += '<div style="color: white; padding:10px">'+title+'</div>';
  centerPopup(page,"moviePlayer", '500', '500', 'no', 'no' );
}

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;
var clearPreview;
IMGOBJ = new Image();

function showPreviewOnClick(imgUrl, title, imageWidth){
  if(theViewer)
    document.body.removeChild(theViewer);
  if (!contextPath)
    contextPath = '';

  createThumbDiv();
  IMG_URL=imgUrl;
//  IMGOBJ.onload = function (){startShowPreview(imageWidth)}
//  IMGOBJ.onerror = failed;
//  IMGOBJ.src =contextPath + IMG_URL;
//  theViewer.innerHTML= '<div style="background-color:white"><img src="'+contextPath+'/applications/webwork/parts/images/loading.gif"></div>';
  startShowPreview(imageWidth, title);
}

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();'
  var slash = "";
  if (contextPath && contextPath.length>0)
    slash = "/";
  if(imageWidth)
    imgPreview = imgPreview + '<img  src="' +contextPath+slash+ IMG_URL +'" width="'+imageWidth+'" border="0" style="visibility:visible">';
  else
    imgPreview = imgPreview + '<img  src="' +contextPath+slash+ 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);
}

function setPosPreviewOnClick(theViewer){
  var xcoord=mouseDistance[0];
  var ycoord=mouseDistance[1];
  if (theViewer){
    if ((getWindowDimensionW()+truebody().scrollLeft) - mouseX < theViewer.offsetWidth+25){
      var diff=(mouseX+theViewer.offsetWidth+25)-(getWindowDimensionW() +truebody().scrollLeft);
      //xcoord = mouseX - diff;
      xcoord = mouseX - ((theViewer.offsetWidth/2)+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,"mousedown",hidePreview);
    theViewer.style.visibility="visible";
  }
}

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);
  }

}
