var LastRef = '';

function moveMouse(e) {
      e = (e ? e : event);
      EvtTarget = e.target ? e.target : e.srcElement;

      var iShow = 0;
      var popWindow = document.getElementById('PopWindow');

      /* If title or rel is the tooltip, we're OK */
      var TempTarget = EvtTarget;
      var i = 10;
      while (iShow == 0 && TempTarget && i > 0) {
        if (TempTarget.id == 'PopWindow') {
          iShow = 1;
        } else if (!TempTarget.parentNode)  {
          i = 0;
        } else {
          TempTarget = TempTarget.parentNode;
          i = i - 1;
        }
      }

      if (iShow == 0) {
        /* If the rel is the achivment graphic, we're OK */
        var ref = EvtTarget.rel;
        if (ref == null || ref == '') {
          ref = EvtTarget.id;
        }
        if ((ref == null || ref == '')&& EvtTarget.parentNode) {
          ref = EvtTarget.parentNode.rel;
          if (ref == null || ref == '') {
            ref = EvtTarget.id;
          }
        }
        for(i=0; i<aTips.length; i++) {
          if (aTips[i][0] == ref) {
            iShow = 2;
            if (i != iTip) {
              PopAnchorLeft = 1;
              PopAnchorTop = 1;
              iTip = i;

              if (popWindow == null) {
                popWindow = document.createElement('div');
                popWindow.setAttribute('id', 'PopWindow');
                document.body.appendChild(popWindow);
              }
              popWindow.setAttribute('class', aTips[i][1]);
              popWindow.innerHTML = '';
              if (aTips[i][4]) {
                  popWindow.style.width = aTips[i][4] + 'px';
              }

              var aEls = aTips[i][2].split('|');
              var aSubDivs = new Array();
              var w, l, aSubs, aSSubs;
              for (j=0; j<aEls.length; j++) {
                w = aEls[j].substring(0,1);
                if (w == 't') {
                  l = aEls[j].substring(1);
                  o = '';
                  while (l > 0) {
                    j = j + 1;
                    o = o + aEls[j];
                    l = l - aEls[j].length;
                    if ( l>0 ) {
                      l=l-1;
                      o = o + '|';
                    }
                  }
                  le = aSubDivs.length;
                  if (le > 0) {
                    aSubDivs[le-1].innerHTML = aSubDivs[le-1].innerHTML + o;
                  } else {
                    popWindow.innerHTML = popWindow.innerHTML + o;
                  }


                } else if (w == 'i') {
                  // Adding an image to the item!
                  var newImage = document.createElement('img');
                  aSubs = aEls[j].split('#');
                  for (k=1; k<aSubs.length; k++) {
                    aSSubs = aSubs[k].split('@');
                    newImage.setAttribute(aSSubs[0], aSSubs[1]);
                  }
                  le = aSubDivs.length;
                  if (le > 0) {
                    aSubDivs[le-1].appendChild(newImage);
                  } else {
                    popWindow.appendChild(newImage);
                  }

                } else if (w == 'd') {
                  // Creating a sub-div
                  le = aSubDivs.length;
                  aSubDivs[le] = document.createElement('div');
                  aSubs = aEls[j].split('#');
                  for (k=1; k<aSubs.length; k++) {
                    aSSubs = aSubs[k].split('@');
                    aSubDivs[le].setAttribute(aSSubs[0], aSSubs[1]);
                  }

                } else if (w == 'e') {
                  // Add the sub-div
                  le = aSubDivs.length;
                  if (le > 1) {
                    aSubDivs[le-2].appendChild(aSubDivs[le-1]);
                  } else if (le>0) {
                    popWindow.appendChild(aSubDivs[le-1]);
                  }
                  aSubDivs.pop();
                }
              }

              popWindow.style.display = 'block';
            }
          }
        }
      }
      if (iShow == false && iTipStatus>0) {
        popWindow.style.left = '-600px';
        iTipStatus = 0;
      }
      if (iShow == 2) {

         // Now check that the popWindow is visible
         var aCursor = getCursorXY(e);
         var aSize = GetInnerSize();
         var aScroll = GetScrollPos();
         var visTop = aScroll[1] + 10;
         var visLeft = aScroll[0] + 10;
         var visBottom = aScroll[1] + aSize[1] - 10;
         var visRight = aScroll[0] + aSize[0] - 10;
         var w=parseInt(popWindow.offsetWidth, 10);
         var h=parseInt(popWindow.offsetHeight, 10);
         var l=aCursor[0]-5;
         var t=aCursor[1]-5;
         if (PopAnchorLeft == 1 && l+w > visRight) {
          PopAnchorLeft = 2;
         }
         if (PopAnchorLeft == 2) {
           if (l - w < visLeft) {
             PopAnchorLeft = 1;
             l = l - 5;
           } else {
             l = l + 5 - w;
           }
         }
         if (PopAnchorTop == 1 && t+h > visBottom) {
          PopAnchorTop = 2;
         }
         if (PopAnchorTop == 2) {
           if (t - h < visTop) {
             PopAnchorTop = 1;
             t = t - 5;
           } else {
             t = t + 5 - h;
           }
         }

         if (l + w > visRight) {
            l = visRight - w;
         }
         if (t + h > visBottom) {
            t = visBottom - h;
         }
         if (l < visLeft) {
            l = visLeft + 1;
         }
         if (t < visTop) {
            t = visTop + 1;
         }

         popWindow.style.left = l + 'px';
         popWindow.style.top = t + 'px';

         iTipStatus = 1;
      }
    }

function debug(txt) {
  var t = document.getElementById('debug');
  if (t) {
    var t2 = t.innerHTML;
    t2 = t.innerHTML + " | " + txt;
    t.innerHTML = t2;
  }
}


function getCursorXY(e) {
   var x = (window.Event && e.pageX) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
   var y = (window.Event && e.pageY) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
   return [x,y];
}

var aTips = new Array();
var iTip;
var iTipStatus = 0;

iTip = -1;

if (typeof document.attachEvent!='undefined') {
   window.attachEvent('onload',LoadTips);
   document.attachEvent('onmousemove',moveMouse);
} else {
   window.addEventListener('load',LoadTips,false);
   document.addEventListener('mousemove',moveMouse,false);
}

function LoadTips() {
  /* Ideally, link the links to the achievements  */
}
