var DownloadTips = new Object();

DownloadTips.currentTip = null;
DownloadTips.showTimer = null;
DownloadTips.hideTimer = null;

DownloadTips.setup = function () {
   $('.dlt_icon_info').hover(DownloadTips.displayTip, DownloadTips.hideTips);

   $('.rd_tooltip').hover(function () {
         clearTimeout(DownloadTips.hideTimer);
      },
      DownloadTips.hideTips
   );
}

DownloadTips.displayTip = function () {
   var icon = this;

   // About OFFSET_CORRECTION: I hate it when DOM measurements sometimes don't pan out pixel-perfect as they should,
   // but I hate tracking the cause across browsers even more, hence a small magic number constant. -WJ
   var tipBottom = 0, OFFSET_CORRECTION = 5;

   $('.rd_tooltip').hide();
   clearTimeout(DownloadTips.hideTimer);

   DownloadTips.currentTip = $(this).parent().next();

   DownloadTips.showTimer = setTimeout(function () {
      DownloadTips.currentTip.load(
         '../templates/snippets/ajax_download.asp?id='+ icon.id +'&size='+ icon.getAttribute('filesize') +'&language='+ Globals.language,
         function () {
            //1. show
            DownloadTips.currentTip.show();

            //2. apply bottom-overflow protection.
            tipBottom = getPosition(DownloadTips.currentTip.get(0)).top - measure('scroll').y + DownloadTips.currentTip.get(0).offsetHeight;
            if (tipBottom > measure('window').y) {
               DownloadTips.currentTip.css({top: -(tipBottom - measure('window').y + OFFSET_CORRECTION) });
            }
         }
      );
   }, 500);
}
DownloadTips.hideTips = function () {
   clearTimeout(DownloadTips.showTimer);

   DownloadTips.hideTimer = setTimeout(function () {
      $('.rd_tooltip').hide();
   }, 500)
}

$(document).ready(DownloadTips.setup);
