
var hid_bub = false;
var t_bub;

function hideBubble(bblElm, bblArrw, bblState) {
    window.clearInterval(t_bub);
    $(bblArrw).css("display", "none");          
    $(bblElm).fadeOut('fast', function() { 
        hid_bub = true;
    });
    
    bblState.value = "invisible";
}

function hideBubbleButKeepVisibilityState(bblElm, bblArrw) {
    $(bblArrw).css("display", "none");          
    $(bblElm).fadeOut('fast', function() { });
}

function mouseoffBubble(bblElm, bblArrw, bblState) {
   
   t_bub = window.setTimeout(function() { 

      if(hid_bub) {  
        $(bblArrw).css("display", "none");          
        $(bblElm).fadeOut('fast', function() { 
            hid_bub = true;
            bblState.value = "invisible";   
            hid_bub = false; 
        });
      }
          
   }, 360); 
 }

 function showBubble(hoverIco, theBub, theBubArrw, bblState, X, Y, TailOrientation) {
         
        window.clearInterval(t_bub);
        var left = 0;
        var top = 0;

        // Manual overrides for absolute positioning!
        if(X != 0 && X != null && Y != 0 && Y != null) {
            var $elementRelativeTo = $(".main-menu");
            
            left = X + $elementRelativeTo.position().left;
            top = Y;
                                                
            switch(TailOrientation) {
                case "left":
                    $(theBubArrw).css("top", top + ($(theBub).outerHeight()/2) - 20 + "px");
                    $(theBubArrw).css("left", left + "px");
                    $(theBubArrw).css("display", "block");                   
                break;
                
                case "top":
                    $(theBubArrw).css("top", top + "px");
                    $(theBubArrw).css("left", left + ($(theBub).outerWidth()/2)-20 + "px");
                    $(theBubArrw).css("display", "block");                     
                
                break;
                
                case "bottom":
                    $(theBubArrw).css("top", top + $(theBub).outerHeight() - 33 + "px");
                    $(theBubArrw).css("left", left + ($(theBub).outerWidth()/2)-20 + "px");
                    $(theBubArrw).css("display", "block");                     
                
                break;
                
                case "right":
                    $(theBubArrw).css("top", top + ($(theBub).outerHeight()/2) - 20 + "px");
                    $(theBubArrw).css("left", left-37 + $(theBub).outerWidth() + "px");
                    $(theBubArrw).css("display", "block");                   
                break;                                                
            }
            
            
        } else {
        
            left = $(hoverIco).position().left;
            top = $(hoverIco).position().top;    
            
            if(top < ($(window).scrollTop() + 10))  top = $(window).scrollTop() + 35;if(top < 35) top = 35;    
        
            $(theBubArrw).css("top", top + "px");
            $(theBubArrw).css("left", left-33 + "px");
            $(theBubArrw).css("display", "block");
                    
            left = left - $(theBub).outerWidth();
            top = top - ($(theBub).outerHeight()/2) + 10;

            if(top < ($(window).scrollTop() + 10))  top = $(window).scrollTop() + 10;if(top < 10) top = 10;
         
        }

        $(theBub).css("left", left + "px");
        $(theBub).css("top", top + "px");
        $(theBub).css("display", "block");

 }
 
 function makeBubbleVis(hoverIco, theBub, theBubArrw, bblState, X, Y, TailOrientation) {
   t_bub = window.setTimeout(function() { 
         showBubble(hoverIco, theBub, theBubArrw, bblState, X, Y, TailOrientation);
   }, 500);      
     
 }

 function makeBubble(hoverIco, theBub, theBubArrw, bblState, bblModal, X, Y, TailOrientation) {
        
    $(hoverIco).mouseover(function() {
        
        if($(theBubArrw).css("display") == "block") return;
        
        $(".bubbletip").css("display", "none");
        $(".bubblearrow").css("display", "none");        
        
        
       t_bub = window.setTimeout(function() { 
            if(!hid_bub)showBubble(hoverIco, theBub, theBubArrw, bblState, X, Y, TailOrientation);   
       }, 360);         
                
    });
    //alert("bblModal:" + bblModal.value + " bblState" + bblState.value);


     $(hoverIco).mouseout(function() {
         hid_bub = true;
         mouseoffBubble(theBub, theBubArrw, bblState);
     });  
     
     
     $(theBub).mouseover(function() { hid_bub = false; });

     $(theBub).mouseout(function() {

        if (bblModal.value != "modal") {
           hid_bub = true;
           mouseoffBubble(theBub, theBubArrw, bblState);
        }
     });   


    
  
 }
