


// ------------------------------

(function($){ $.fn.agiturToolTip = function(){
 return this.each(function() {
  var text = $(this).attr("title");
  var fadeInSpeed = 400;
  var fadeOutSpeed = 200;
    
  $(this).attr("title", "");
  if(text != undefined) {
   
   $(this).hover(function(e){
    
    var text2 = text;
    var loadAjax = false;
    
    if (text.substr(0,7) == 'http://' || text.substr(0,8)=='https://') {
     var myUrl = text;
     var datakey = 'ajax_tooltip_'+myUrl;
      
     if ($('body').data(datakey)) {
      text2 = $('body').data(datakey)+'***from cache';
     } else {
      text2 = "<img src=\"http://www.agitur.it/pulsanti/working_16.gif\">";
      loadAjax = true;    
     }
    }
    
    
    var tipX = e.pageX + 12;
    var tipY = e.pageY + 12;
    $(this).attr("title", ""); 
    $("body").append("<div id='agiturToolTip' style='position: absolute; z-index: 100; display: none;'>" + text2 + "</div>");
    $("#agiturToolTip").css("left", tipX).css("top", tipY).fadeIn(fadeInSpeed);
    
    if (loadAjax) {
     
     $.ajax({
       url: myUrl,
       success: function(data) {
         $('#agiturToolTip').html(data+'<br>***NO cache');
         $('body').data(datakey, data);
       }
     }); 
     
    }
     
   }, function(){
    $("#agiturToolTip").remove();
    $(this).attr("title", text);
   });
   $(this).mousemove(function(e){
    var tipX = e.pageX + 12;
    var tipY = e.pageY + 12;
    var tipWidth = $("#agiturToolTip").outerWidth(true);
    var tipHeight = $("#agiturToolTip").outerHeight(true);
    if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
    if (tipX<0) {
    	tipX = 0;
	}
    if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
    $("#agiturToolTip").css("left", tipX).css("top", tipY).fadeIn(fadeInSpeed);
   });
  }
 });
}})(jQuery);


$(document).ready(function(){
 $(".with-tooltip").agiturToolTip();
});
 


