// Alexander Feldman, 2002

// How long to wait after mouseover before show word translation.
var mouseOverDelay   = 300;

var commentWin1Top   = 100;
var commentWinWidth  = 100;
var commentWinHeight = 100;
var commentWinColor  = "lightblue";
var translatePosY    = 100;
var wordHasMouseOver = false;
var wordTimerHandler = false;
var wordTimerYPos    = '';
var wordTimerWord    = '';


//
// Private functions

// evnt, inside, winLayer are required parameters, others - are optional.
function commentWinOpenIt(evnt, inside, winLayer, nestedLevel, insideIsAnUrl)
{
  if (insideIsAnUrl)
    inside = commentWinGetByUrl(inside);

  var shift = 60*nestedLevel;
  var left  = 10 + shift;
//var top   = event.toElement.offsetTop + event.toElement.offsetHeight +
//            (nestedLevel > 0 ? commentWin1Top : 0);
//
// WORK:  var top = document.body.scrollTop  + window.event.clientY + 16;
//  var top = document.body.scrollTop  + 100 + 16;
  var y = (eval('typeof(' + 'evnt.clientY' + ')') == 'undefined') ?
    evnt : evnt.clientY;
  var top = document.body.scrollTop  + y + 16;

  if (nestedLevel == 0)
    commentWin1Top = top + 3 + 3; // 3 - table border height.
  commentWinWidth  = 0.75*window.screen.width; // - 2*shift;
  commentWinHeight = 0.05*window.screen.height;
  commentWinColor  = "lightblue";

  winLayer.style.pixelLeft = document.body.scrollLeft + left; // window.event.clientX
  winLayer.style.pixelTop  = top;
  winLayer.style.visibility = "visible";

  showData(inside, winLayer);
}


// Return converted "inside" from URL to html with text of link.
function commentWinGetByUrl(inside)
{
  return
    "<iframe frameborder=\"0\" name=\"raampje\" width=\"" +
    (commentWinWidth - 10) + "\" height=\"" +
    (commentWinHeight - 30) + "\" src=\"" +
    inside + "\" scrolling=\"no\"></iframe>";
}


function showData(inside, winLayer)
{
  var output =
    "<table cellpadding=\"0\" cellspacing=\"0\" width=\"" +
    commentWinWidth + "\" height=\"" +
    commentWinHeight + "\" border=\"3\">" +
      "<tr>" +
        "<td width=\"100%\">" +
          "<table width=\"100%\" height=\"100%\" bgcolor=\"" +
          commentWinColor + "\">" +
            "<tr>" +
              "<td height=\"100%\" width=\"100%\">" +
                inside +
              "</td>" +
            "</tr>" +
          "</table>" +
        "</td>" +
      "</tr>" +
    "</table>";

  winLayer.innerHTML  = output;
  winLayer.visibility = "visible";
}


function commentWinCloseIt(winLayer)
{
  winLayer.style.visibility = "hidden";
}


//
// Public functions

function commentWinOpen(evnt, inside)
{
  commentWinCloseIt(commentWin2);
  commentWinOpenIt(evnt, inside, commentWin, 0);
}


function commentWin2Open(evnt, inside)
{
  commentWinOpenIt(evnt, inside, commentWin2, 1);
}


function commentWinClose()
{
  commentWinCloseIt(commentWin2);
  commentWinCloseIt(commentWin);
}


function commentWin2Close()
{
  commentWinCloseIt(commentWin2);
}


function translateWinOpen(y, word)
{
  translatePosY = y;
  document.all.translationCall.src = "get.php?word=" + word;
}


function translateWinClose()
{
  commentWinClose();
}


/**
 * These 4 functions are used to make a delay after mouseover
 * before showing word translation - this makes possible to
 * move mouse over document to wanted word without needed to
 * click <Ctrl> to prevent translation of other words we
 * mouseover randomly while going to our word.
 *
 */
function translateMouseOver(evnt, word)
{
  resetWordTimer();
  wordHasMouseOver = true;
  wordTimerYPos    = evnt.clientY
  wordTimerWord    = word;
  if (! evnt.altKey && ! evnt.ctrlKey)
  {
    wordTimerHandler = setTimeout("callbackWordTrans()", mouseOverDelay);
  }
}


function translateMouseOut(evnt)
{
  resetWordTimer();
  wordHasMouseOver = false;
  if (! evnt.altKey && ! evnt.ctrlKey)
  {
    translateWinClose();
  }
}


function resetWordTimer()
{
  if (wordTimerHandler != false)
  {
    clearInterval(wordTimerHandler);
    wordTimerHandler = false;
  }
}


function callbackWordTrans()
{
  translateWinOpen(wordTimerYPos, wordTimerWord);
}

