﻿// Tooltips!
//
// Init tooltips on document ready by calling either:
//	
//  setTips(jQuery(selector)); - for normal '?' icons on 'a' tags with tooltips
//	setTips(jQuery(selector),"inline-content"); - for images etc.

// jQuery.noConflict();  jacksons is prototype

function setTips(helperEl,type){       
  
   jQuery(helperEl).each(function(index) 
   {     
        // find the next free tooltip ID in the form of id="tip[number]"
        var counter = 1;
        var flag = 0;
        var selector = '';
        
        // loop through each number starting at 1, checking to see if an element
        // with id="tip[number]" exists, exit when the element doesn't exist
        // we use this empty selector as our tooltips id
        // selector is also set to the string selector for our new element
        while (flag != 1)
        {
            selector = '#tip' + counter;
            if( $(selector).length == 0 )
            {
                flag = 1;
            }
            else
            {
                counter ++;
            }
        }

        // create tooltip div with the id number that we worked out above
        // fill with the tooltip contents defined by the user
        if(type == "inline-content")
        {
            var tipContents = '<div class="toolTip" id="tip' + counter + '"><div class="ttInner">' + jQuery(this).find('.tooltip-content').html() + '</div></div>';  
        }
        else
        {  
            var tipContents = '<div class="toolTip" id="tip' + counter + '"><div class="ttInner">' + jQuery(this).html() + '</div></div>'; 
        }                   
        
        // append our new div element to the bottom of the document
        jQuery('body').append(tipContents);
           
        // use the selector we set earlier to bind hover events to the tooltip anchor to show the tooltip
        var tip = jQuery(selector);
        jQuery(this).bind({
            mousemove: function(e) 
            {
                tip.css({top: (e.pageY + 13) + "px",left: (e.pageX + 13) + "px"})
                tip.show()
            },
            mouseleave: function() 
            {
                tip.hide()
            }
        });       
   });         
}
