jQuery.fn.extend({
   findPos : function() {
       obj = $(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
   }
});

$(function () {

  $('.bubbleInfo').each(function () {
    // options
    var distance =-10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;
		
		

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
		var nb_relicat_produit = $('.nb_relicat_produit', this);
		
		var popup = $('#popup').css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0)]).mouseover(function () {
 
			pos =  trigger.findPos();	
			my_val = parseFloat(trigger.get(0).id);

			if(my_val > 0) {
				if(my_val > 1) { pluriel = "s"; } else { pluriel = ""; }
				my_texte = "Vous avez actuellement " + my_val + " article" + pluriel + " en reliquat."; 
			} else {
				my_texte = "Vous n'avez aucun reliquat pour cet article.";
			}
			$('#popup_texte').get(0).innerHTML = my_texte;
			// reset position of popup box
			popup.css({
				top:  pos.y - 55,
				left:  pos.x - 180,
				display: 'block',
				opacity:1
			})
			.animate({
				top: '-=' + distance + 'px',
				opacity: 1
			}, time, 'swing')
			;

    }).mouseout(function () {
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
          popup.css('display', 'none');
        });
    });
  });

