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);
}

function showPreview(theImage,title,imageWidth){
  var thumbFader;
  var noTitle= "";

  if(!title)title=noTitle;
  if (imageWidth) thumbWidth = imageWidth;
  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;">'; 
  imgPreview += '<img src="' + contextPath + theImage + '" 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);
}

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="/applications/webwork/parts/flv/flvplayer.swf?autostart='+autostart+'&file='+theMovie+'" width="'+w+'" height="'+h+'" wmode="transparent">';
  videoContent +='<param name="movie" value="/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;
}
//previewOnClick;
var clearPreview;

function showPreviewOnClick(imgUrl){
  if (clearPreview)  window.clearTimeout(clearPreview);
  if(theViewer) document.body.removeChild(theViewer);
  createThumbDiv();
  IMG_URL=imgUrl;
  IMGOBJ = new Image();
  IMGOBJ.onload = startShowPreview;
  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>';
}
function startShowPreview(){
  theViewer.style.visibility="hidden";
  imgPreview ="";
  imgPreview += '<div id "imgPreview" align="center" style="padding:10px; background:white; border:1px solid #cccccc;" onClick="hidePreview()">';  //width:'+thumbWidth+'px;
  imgPreview += '<img src="' +contextPath+ IMG_URL +'" border="0" >';
  // if(title)imgPreview += '<br><br>'+title;
  imgPreview +='</div>';
  theViewer.innerHTML = imgPreview;
  setTimeout("setPosPreviewOnClick(theViewer)",100);
  //addEvent(document,"mousemove",hidePreviewOnClick);
}
function failed(){
  alert("faild 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";

  }
  //    ycoord=0;
  //   xcoord=0;
}
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);
  }
}

function hidePreview(){
  if (theViewer) document.body.removeChild(theViewer);
  theViewer=false;
}
