/*
 Tooltip script
 powered by jQuery (http://www.jquery.com)

 written by Alen Grakalic (http://cssglobe.com)
 for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery

 ddrivetip conversion & tooltip modification by RG, aka Mottie (http://wowmotty.blogspot.com/)
*/

 // 'tooltipObjFlag' can be defined outside the script. It makes the script load tooltip content
 // from object id contained in the title, e.g. title='###test1'... will load content from #test1
 if (typeof(tooltipObjFlag)=='undefined') { var tooltipObjFlag = '###' }

 // 'tooltipSpeed' sets the display speed of the tooltip
 if (typeof(tooltipSpeed)=='undefined') { var tooltipSpeed = 300 }

 var xOffset = 20; // Don't use negative values here
 var yOffset = 20;

 var tooltipCSS = 'position:absolute;z-index:1000;';

this.tooltip = function(){
// Tooltips
 $('.tooltip').unbind().hover(function(e) {
  this.t = (this.title == '') ? this.t : this.title;
  var ttt = this.t;
  this.title = '';
  // Load tooltip content from an object
  var rx = new RegExp('^' + tooltipObjFlag);
  if (rx.test(ttt)) {
   tte = ttt.replace(rx,'#').split(' ')[0];
   if (tte.length < 1 || tte.length > 20) {
    ttt = this.t;
   } else {
    ttt = $(tte).html();
   }
  }
  // retrieves width and color information from the rel attribute
  // rel='250,#000000;color:#ffffff;' => tooltip width = 250, background color = #000000, text = #ffffff
  var tmp = (typeof($(this).attr('rel'))=='undefined') ? '' : $(this).attr('rel').split(',');
  this.w = (tmp[0] == '') ? $('#tooltip').width() : tmp[0];
  this.c = (typeof(tmp[1])=='undefined') ? '' : 'background-color:' + tmp[1];
  $(this).before("<div id='tooltip' style='" + tooltipCSS + "width:" + this.w + "px;" + this.c + "'>" + ttt + "</div>");
  ttrelocate(e,'#tooltip');
  $('#tooltip').fadeIn(tooltipSpeed);
 },
  function(){tthide(this,'#tooltip')});
 $('.tooltip').mousemove(function(e) {
  ttrelocate(e,'#tooltip');
 });
// Image & URL screenshot preview
 $('a.preview,a.screenshot').unbind().hover(function(e){
  this.t = this.title;
  this.title = '';
  var c = (this.t != '') ? '<br/>' + this.t : '';
  var tmp = ($(this).hasClass('preview')) ? this.href + "' alt='Image preview' />" : this.rel + "' alt='url preview' />";
  $(this).before("<div id='preview' style='" + tooltipCSS + "'><img src='"+ tmp + c +"</div>");
  ttrelocate(e,'#preview');
  $('#preview').fadeIn(tooltipSpeed);
 },
  function(){tthide(this,'#preview')}); 
 $('a.preview,a.screenshot').mousemove(function(e){
  ttrelocate(e,'#preview');
 });
}
function tthide(x,ttid){
 x.title = x.t;
 $(ttid).remove();
}
function ttrelocate(e,ttid){
 var curX = (document.all) ? event.clientX : e.pageX;
 var curY = (document.all) ? event.clientY : e.pageY;
 var ttw = ($(ttid).width());
 var tth = $(ttid).height();
 var wscrY = $(window).scrollTop();
 var wscrX = $(window).scrollLeft();
 var ttleft = ((curX + xOffset*2 + ttw) > $(window).width() - xOffset) ? curX - ttw - xOffset : curX + xOffset;
 if (ttleft < wscrX + xOffset) ttleft = wscrX - xOffset;
 var tttop = ((curY + yOffset + tth) > $(window).height() - yOffset) ? curY - tth - yOffset : curY + yOffset;
 if (tttop < wscrY + yOffset) tttop = wscrY + yOffset;
 $(ttid).css('top', tttop + 'px').css('left', ttleft + 'px');
}

// Convert ddrivetip functions (http://www.dynamicdrive.com/dynamicindex5/dhtmltooltip.htm)
// to work with this tooltip
function ddrivetip(ttt,ttc,ttw){
 var enabletooltip = true;
 var ttc = (ttc == '') ? '' : 'background-color:' + ttc;
 $('body').append("<div id='tooltip2' style='" + tooltipCSS + "width:" + ttw + "px;" + ttc + "'>" + ttt + "</div>");
 $('#tooltip2').fadeIn(tooltipSpeed);
}
function hideddrivetip(){
 $('#tooltip2').remove();
}
function positiontip(evt){
 if ($('#tooltip2').html() != null ) ttrelocate(evt,'#tooltip2');
}
document.onmousemove = positiontip;

$(document).ready(function(){
 tooltip();
});
