/**
 * jquery.acktip.js
 *
 * @version 1.0
 * @author Alessandro Alessio
 * @copyright 2010
 * @site www.acktel.com
 *       www.a2area.it
 * @credits http://www.sohtanaka.com
 */


(function($){  
   $.fn.acktip = function(options) {

      var defaults = {
         effects           : '',
         moveX             : 20,
         moveY             : 20,
         fadeSpeed         : 500
      };
      
      var options = $.extend(defaults, options);
      var tip_id = '';
      var tip = '';
      
      return this.each(function() {
         //definizione dell'id toolitp
         //tip_id = $(this).attr('rel');
         //alert(tip_id);
         //tip = $(tip_id);
         //attivazione al mouseover
         $(this).hover(function(){
            
            //definizione dell'id toolitp
            tip_id = $(this).attr('rel');
            //alert(tip_id);
            tip = $(tip_id);
            
            //alert(this);
            if (options.effects=='fade') {
               tip.fadeIn(options.fadeSpeed);
            } else {
               tip.show();
            }
         }, function() {
            tip.hide();
         }).mousemove(function(e) {
            //recupero le coordinate e applico lo spostamento (options.moveX/options.moveY)
            var mouseX = e.pageX + options.moveX;
            var mouseY = e.pageY + options.moveY;
            var tipWidth = tip.width(); //Find width of tooltip
            var tipHeight = tip.height(); //Find height of tooltip
            
            //distanzia l'elemento dal bordo di destra
            var tipVisX = $(window).width() - (mouseX + tipWidth);
            //distanzia l'elemento dal bordo in basso
            var tipVisY = $(window).height() - (mouseY + tipHeight);
            
            if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
               mouseX = e.pageX - tipWidth - 20;
            } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
               mouseY = e.pageY - tipHeight - 20;
            }
            //Absolute position the tooltip according to mouse position
            tip.css({  top: mouseY, left: mouseX });
         });
      });
      
   };  
})(jQuery);
