var tt;
var ifr;
 
function initTT() {
  tt = document.createElement('div');
  tt.style.position = 'absolute';
  tt.style.zIndex   = 1022;
  tt.className      = 'tt_info';
  tt.style.display  = 'none';

  document.body.appendChild(tt);

  if(true || IE6_TODO) {
    ifr = document.createElement('iframe');
    ifr.src = 'empty.html';
    ifr.style.position = 'absolute';
    ifr.style.border = 'none';
    ifr.style.padding = 0 + 'xp';
    ifr.style.margin  = 0 + 'xp';
    ifr.style.zIndex   = 1021;
    ifr.style.display  = 'none';

    document.body.appendChild(ifr);
  }
}

function scanTT(elem) {
  initTT();
  var itjes = new Array();
  if(itjes = elem.getElementsByTagName('div'))
  {
    for(var i in itjes) {
      if(itjes && itjes[i] && itjes[i].getAttribute && itjes[i].getAttribute('tt'))
        addTT(itjes[i]);
    }
  }
}

function addTT(elem) {
  addEvent(elem, 'mouseover', showTT);
  addEvent(elem, 'mouseout',  hideTT);
}

function showTT(elem) {

  if(elem.srcElement)
    target = elem.srcElement
  else
    target = this;

  var msg = target.getAttribute('tt');
  if(!msg) return;

  var cls = target.getAttribute('tt_class');
  if(!cls) cls = 'tt_info';

  var pos = findPos(target);
  var top = pos[0] + 28;
  var left = pos[1] + 7;

  var startScroll     =  document.documentElement.scrollWidth;
  tt.style.visibility = 'hidden';
  tt.className        = cls;
  tt.style.right      = 'auto';
  tt.innerHTML        = '<p>' + msg + '</p>';
  tt.style.top        = top  + 'px';
  tt.style.left       = left + 'px';
  tt.style.display    = 'block';
  var tipWidth        = tt.offsetWidth;

  // check if we have to show the tt from rigth to left (only if in lightbox !!)
  if(hasParentWithId(target, "lightbox"))
  {
    var pos = findPos(lightboxContent);
    var lleft = pos[1];
    var lightBoxRight = lightboxContent.offsetWidth + lleft;
    var spaceLeft = lightBoxRight - left;
    if((tipWidth/2) > spaceLeft)
    {
      left = document.body.offsetWidth - lightBoxRight + 25;
      tt.style.left = 'auto';
      tt.style.right = left + 'px';

      // in ie6 the div is placed correctly when the page is scrolled
      var scrollDiff = document.documentElement.scrollWidth - startScroll;
      if(scrollDiff > 0)
      {
        var scrollCorr = scrollDiff + 30 ;
        left = left + scrollCorr;
        tt.style.right = left + 'px';
      }
    }
  }

  if(tt.offsetWidth > 370)
    tt.style.width = '370px';

  tt.style.visibility  = 'visible';

  if(ifr) {

    if((tipWidth/2) > spaceLeft)
    {
      ifr.style.right = left + 'px';
      ifr.style.left = 'auto';
    }
    else
    {
      ifr.style.left = left + 'px';
      ifr.style.right = 'auto';
    }
    ifr.style.top  = top  + 'px';
    ifr.style.display  = 'block';
    ifr.style.width = tt.offsetWidth + 'px';
    ifr.style.height = tt.offsetHeight + 'px';
  }
}

function hideTT() {
  tt.style.width = 'auto';
  tt.style.display = 'none';
  ifr.style.display = 'none';
}