/**
 * jquery.acktip.js
 *
 * @version 1.1
 * @update 8/6/2011
 * @author Alessandro Alessio
 * @copyright 2010
 * @site www.acktel.com
 *       www.a2area.it
 * @credits http://www.sohtanaka.com
 *
 * @history
 *    - Aggiunta la possibilità di non effettuare il controllo sui limiti della pagina
 *    - Aggiunta la possibilità di posizionare il popup a top/bottom e left/rigth tramite il parametro position
 */


(function($){  
   $.fn.acktip = function(options) {

      var defaults = {
         effects           : '',
         moveX             : 20,
         moveY             : 20,
         fadeSpeed         : 500,
         position          : 'bottom right',
         checkPageLimit    : true
      };
      
      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)
            p = options.position.split(' ');
            if (p[1]=='right'){
               var mouseX = e.pageX + options.moveX;   
            } else if (p[1]=='left') {
               var mouseX = e.pageX - $(tip).width() - options.moveX;
            }
            if (p[0]=='bottom'){
               var mouseY = e.pageY + options.moveY;
            } else if (p[0]=='top') {
               var mouseY = e.pageY - $(tip).height() - options.moveY;
            }
            var tipWidth = $(tip).width(); //Find width of tooltip
            var tipHeight = $(tip).height(); //Find height of tooltip
            
            if (options.checkPageLimit==true){
               //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);
