tooltip = {};
tooltip.cache = {};
tooltip.mouseY = 0;
tooltip.mousePageY = 0;
tooltip.mousePageX = 0;

tooltip.interval = 0;
tooltip.current = null;
tooltip.currentAncestor = 0;
tooltip.enable = function (obj,url,time,eventObj) 
{

  if (tooltip.current !== null) 
  {
    // 
    tooltip.clearInterval(0);
    tooltip.hide();
  }
  tooltip.mousemove(eventObj);

  tooltip.current = null; // Объект тултипа
  tooltip.currentAncestor = obj; // Получатель тултипа

  tooltip.current = tooltip.show(url);
  tooltip.current.setAttribute('rel',time);
  // Устанавливаем события
  $(obj).mouseout(function () {
    tooltip.setTimer();
  });
  $(tooltip.current).mouseout(function () {
    tooltip.setTimer();
  });

  $(obj).mouseover(function () {
    tooltip.clearInterval(time);
  });
  $(tooltip.current).mouseover(function () {
    tooltip.clearInterval(time);
  });

}
tooltip.clearInterval = function (time) 
{
  
  if (tooltip.interval != 0) 
  {

    window.clearInterval(tooltip.interval);

    tooltip.interval = 0;
  }
  if (tooltip.current !== null)
  {
    tooltip.current.setAttribute('rel',time);
  }
}
tooltip.setTimer = function () 
{

  if (tooltip.interval == 0)
  {
    // Код скрытия
    tooltip.interval = window.setInterval(function () {
      if (tooltip.interval == 0) {tooltip.clearInterval();return;}
      if (tooltip.current !== null)
      {
        tooltip.current.setAttribute('rel',parseInt(tooltip.current.getAttribute('rel')) - 1);
        if (parseInt(tooltip.current.getAttribute('rel')) == 0)
        {
          tooltip.hide();
          tooltip.clearInterval();
          tooltip.current = null;
          tooltip.currentAncestor = null;
        }
      }
    },100);
    

  }
}
tooltip.show = function (url)
{

  // Получаем html
  var oDiv = null;
  var oContentObj = null;
  // Создаем каркас
  oDiv = document.createElement('div');
  oDiv.className = 'popup';
  oDiv.style.display = 'none';
  $(oDiv).html(
''+
'<div class="popupContent"><img src="large-loading.gif"/></div>'+
''
  );
  $(document.body).append(oDiv);
  $('div.popupContent',oDiv).each(function(){ oContentObj = this});

  var oArrow = document.createElement('div');
  var offset = $(tooltip.currentAncestor).offset();
  var nHeight = $(tooltip.currentAncestor).height();
  var nWidth = $(tooltip.currentAncestor).width();
  var bRecalculate = false;
  if (tooltip.mousePageY < 300 )
  {

    if (tooltip.mousePageX / 2 > tooltip.mouseClientX)
    {
      oDiv.style.left = (tooltip.mouseX - 30 )+ 'px';
      oDiv.style.top = (tooltip.mouseY + 15) + 'px';
      oArrow.className ='arrow1';
    }
    else 
    {
      oDiv.style.left = (tooltip.mouseX - 280 ) + 'px';
      oDiv.style.top = (tooltip.mouseY +15) + 'px';
      oArrow.className ='arrow2';
    }
    oDiv.insertBefore(oArrow,oContentObj);
  }
  else
  {
    if (tooltip.mousePageX / 2 > tooltip.mouseClientX)
    {
      oArrow.className ='arrow';
      oDiv.style.left = (tooltip.mouseX - 30 ) + 'px';
      oDiv.style.top = (tooltip.mouseY - 25  ) + 'px';
    }
    else 
    {
      oDiv.style.left = (tooltip.mouseX - 280 ) + 'px';
      oDiv.style.top = (tooltip.mouseY - 25 ) + 'px';
      oArrow.className ='arrow3';
    }
    bRecalculate = true;
    oDiv.appendChild(oArrow);
  }
  // left,top


  
  // Вставляем

  content = '';
  if (tooltip.cache[url] == undefined) {

    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'text',
        error: function (request,errorStr) {
          alert(errorStr)
        },
        success: function (contentLoaded) {
          content = contentLoaded;
          oContentObj.innerHTML = content;
          var nContentHeight = $(oDiv).height();
          if(!nContentHeight)
            nContentHeight = 99;
          tooltip.cache[url] = [content,nContentHeight];
          $(oDiv).css('display','block');
          if (bRecalculate)
          {
            $(oDiv).css('border-width','1px');
            oDiv.style.top = (tooltip.mouseY - nContentHeight - 35) + 'px';
            if ($.browser.msie && ($.browser.version < 7))
            {
              oDiv.style.top = (tooltip.mouseY - nContentHeight - 15) + 'px';
            }
          }
        }
        
      });
  }
  else
  {
    content = tooltip.cache[url][0];
    oContentObj.innerHTML = content;
    var nContentHeight = $(oDiv).height();
    if(!nContentHeight)
      nContentHeight = 99;
    $(oDiv).css('display','block');
    if (bRecalculate)
    {
      oDiv.style.top = (tooltip.mouseY - nContentHeight - 35) + 'px';
      if ($.browser.msie && ($.browser.version < 7))
      {
        oDiv.style.top = (tooltip.mouseY - nContentHeight - 15) + 'px';
      }
    }
  }

  // возвращаем созданный объект
  return oDiv;
}
tooltip.hide =function ()
{
  $(tooltip.current).fadeOut('slow');
  $(tooltip.current).remove();
}

//

  tooltip.mousemove = function (eventObj) {
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;

  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;

  }
    tooltip.mouseX = eventObj.pageX;
    tooltip.mouseY = eventObj.pageY;
    tooltip.mouseClientX = eventObj.clientX;
    tooltip.mousePageY = eventObj.screenY;
    tooltip.mousePageX = xScroll;
  };
